-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.sh
executable file
·79 lines (64 loc) · 1.42 KB
/
github.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# https://gist.github.com/mbohun/b161521b2440b9f08b59
usage() {
local self
self="$(basename "$0")"
echo "Usage: ${self} [-t GITHUB_TOKEN] REST_EXPRESSION"
echo "Examples:"
echo "$self XXX /user"
echo "$self XXX /users/pschmitt/starred"
}
gh_curl() {
curl -fsSL \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${GITHUB_TOKEN}" \
"$@"
}
get_page_count() {
local url="$1"
gh_curl -I "$url" | \
grep '^Link:' | \
sed -e 's/^Link:.*page=//g' -e 's/>.*$//g'
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
then
set -e
case "$1" in
--token|-t)
GITHUB_TOKEN="$2"
shift 2
;;
esac
REST_EXPRESSION="${1#/}" # remove leading '/'
if [[ -z "$GITHUB_TOKEN" ]]
then
echo "Missing GitHub token." >&2
usage
exit 2
fi
if [[ -z "$REST_EXPRESSION" ]]
then
echo "Missing REST endpoint." >&2
usage
exit 2
fi
URL="https://api.github.com/${REST_EXPRESSION}"
PAGES="$(get_page_count "$URL")"
# does this result use pagination?
if [[ -z "$PAGES" ]]
then
# Single page result
gh_curl "$URL" | jq
else
echo "There are $PAGES pages of results" >&2
# Pagination
res='[]' # empty JSON array
for page in $(seq 1 "$PAGES")
do
res_page="$(gh_curl "${URL}?page=$page")"
res="$(jq -s '.[0] + .[1]' <<< "${res} ${res_page}")"
done
jq <<< "$res"
fi
fi
# vim set ft=bash et ts=2 sw=2 :