-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-dump.sh
executable file
·52 lines (41 loc) · 1.58 KB
/
git-dump.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
################################
#
# Author: Sambhav Saxena
# Version: v1
#
# This script will help users to communicate and retrieve information from Github.
# Usage: Please provide your github token and REST parameters to the script as input.
#
# REST expression?
# Visit https://docs.github.com/en/rest and look for URL body like so: https://api.github.com/repos/OWNER/REPO.
# Replicate this schema for the REST expression, starting after the domain.
#
# Example: ./file-name.sh gh_xxxxxxxxxxxxxxxxxxxxxxx /repos/sambhavsaxena/dopeshit
#
################################
if [ ${#@} -lt 2 ]; then
echo "usage: $0 [your github token] [REST expression]"
exit 1;
fi
GITHUB_TOKEN=$1
GITHUB_API_REST=$2
GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.v3+json"
temp=`basename $0`
TMPFILE=`mktemp /tmp/${temp}.XXXXXX` || exit 1
function rest_call {
curl -s $1 -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" >> $TMPFILE
}
# single page result-s (no pagination), have no Link: section, the grep result is empty
last_page=`curl -s -I "https://api.github.com${GITHUB_API_REST}" -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" | grep '^Link:' | sed -e 's/^Link:.*page=//g' -e 's/>.*$//g'`
# does this result use pagination?
if [ -z "$last_page" ]; then
# no - this result has only one page
rest_call "https://api.github.com${GITHUB_API_REST}"
else
# yes - this result is on multiple pages
for p in `seq 1 $last_page`; do
rest_call "https://api.github.com${GITHUB_API_REST}?page=$p"
done
fi
cat $TMPFILE