expand tilde-slash arguments in the completion scirpt
[hband-tools.git] / crawler-bin / github
blobfcd6a383741db1ffece6e2f97dd6323dc002fbbe
1 #!/bin/bash
3 cached=''
4 unset ghUser
5 unset ghRepo
6 unset cachekey
7 unset apicall
8 unset jq
9 usagetext="Options:
10 -C use cached data, no download
11 Subcommands:
12 repos [USERNAME]
13 issues
14 stars
15 stargazers"
17 errorlevel()
19 return $1
22 while getopts Ch opt
24 case $opt in
25 h) echo "$usagetext"
26 exit 1;;
27 C) cached=1;;
28 *) exit 1;;
29 esac
30 done
32 shift $(($OPTIND - 1))
34 case "$1" in
35 repos)
37 echo "name is-a-fork created pushed updated description"
38 next_url="https://api.github.com/users/${2?USERNAME is missing.}/repos"
39 while [ -n "$next_url" ]
41 resp=`curl -sSi "$next_url"`
43 next_url=`echo "$resp" |\
44 sed -ne '/^Link:/ s/.*\?<\(.\+\)>\s*;\s*rel=.next.*/\1/ p; /^\r*$/q' |\
45 head -n 1`
47 echo "$resp" |\
48 sed -e '1,/^\r*$/d' |\
49 jq -r '.[] | [.name, (.fork|tostring), .created_at, .pushed_at, .updated_at, ((.description//"")|gsub("\n";"\\n"))] | join("\t")'
50 done
51 )|\
52 if [ -t 1 ]
53 then
54 column -s $'\t' -n -t
55 else
56 cat
58 exit ${PIPESTATUS[0]}
60 issues)
61 apicall="issues?state=all"
62 cachekey=issues
63 jq='map([
64 .number,
65 "\n==========================",
66 ([(.number|tostring)+":",.title]|join(" ")),
67 ([" "+.state,(.labels|.[].name)]|join(", ")),
68 ([" comments",.comments|tostring]|join(": ")),
69 " --------------------------",
70 (.body | split("\n") | .[] | " "+.)
71 ]) |
72 sort | .[] | .[1:] | join("\n")'
74 stars)
75 apicall="stargazers"
76 cachekey=stargazers
77 jq='. | length'
79 stargazers)
80 apicall="stargazers"
81 cachekey=stargazers
82 jq='[.[].login] | sort | join("\n")'
85 echo "$usagetext" >&2
86 exit 1
88 esac
91 set -e
92 set -u
95 masterremote=`git config branch.master.remote`
96 oIFS=$IFS
97 IFS=$'\n'
98 for info in `git remote -v | perl -ne 'm{^(\S+)\s+\S+github.com[/:](.+?)/([^/.\s]+)} and print "$1 $2 $3\n"'`
100 IFS=$oIFS read remote ghUser ghRepo <<<"$info"
101 if [ "$remote" = "$masterremote" ]; then break; fi
102 done
103 IFS=$oIFS
104 gitdir=`git rev-parse --git-dir`
107 static_fetch_api_and_save_cache()
109 apiurl=https://api.github.com/repos/$ghUser/$ghRepo/$apicall
110 if [ ! "$cached" ] && curl -sS "$apiurl" | tee "$gitdir/$cachekey.json~"
111 then
112 if jq -r "$jq" < "$gitdir/$cachekey.json~" >/dev/null 2>&1
113 then
114 mv "$gitdir/$cachekey.json~" "$gitdir/$cachekey.json"
115 else
116 echo "Can not parse Githup API <$apiurl> response, using cache." 1>&2
117 cat "$gitdir/$cachekey.json"
118 errorlevel 2
120 else
121 cat "$gitdir/$cachekey.json"
122 if [ ! "$cached" ]
123 then
124 errorlevel 3
129 static_parse_api_response()
131 jq -r "$jq"
134 set +e
135 set -o pipefail
137 static_fetch_api_and_save_cache | static_parse_api_response
140 declare -A error=([fetch]=${PIPESTATUS[0]} [display]=${PIPESTATUS[1]})
142 if [ ${error[fetch]} = 0 ]
143 then
144 exit ${error[display]}
145 else
146 exit ${error[fetch]}