-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitapi.sh
44 lines (36 loc) · 1.25 KB
/
gitapi.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
#!/bin/sh
git_token=`cat github.token`
GIT_JSONDIR="json-gh"
GIT_RATE_LEFT=
function git_rateleft {
GIT_RATE_LEFT=`curl --silent -H "Authorization: token $git_token" -X GET https://api.github.com/rate_limit | jq -r '.resources.core.remaining'`
}
function git_ratelimit {
current_epoch=`date +%s`
target_epoch=`curl --silent -H "Authorization: token $git_token" -X GET https://api.github.com/rate_limit | jq -r '.resources.core.reset'`
sleep_seconds=$(( $target_epoch - $current_epoch + 5))
echo "[`date`] sleeping $sleep_seconds seconds until `date -r $target_epoch`..." >>/dev/stderr
sleep $sleep_seconds
git_rateleft
}
function git_api {
if [[ $GIT_RATE_LEFT < 1 ]]; then
echo "GitHub API rate limit reached" >>/dev/stderr
git_ratelimit
fi
GIT_RATE_LEFT=$((GIT_RATE_LEFT - 1))
}
function git_api_commit {
git_api
curl --fail --location --silent -H "Authorization: token $git_token" https://api.github.com/repos/$1/commits/$2
}
function git_api_repo {
git_api
curl --fail --location --silent -H "Authorization: token $git_token" https://api.github.com/repos/$1
}
function git_json_path {
first=`echo $1 | cut -c1-1`
second=`echo $1 | cut -c2-2`
echo "$first/$second/$1.json"
}
git_rateleft