-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_num_stars
executable file
·100 lines (91 loc) · 2.63 KB
/
get_num_stars
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#! /bin/bash
#
# get_num_stars: get the number of contributors in a remote repo.
# currently only supports github and gitlab
#
#
# Check the command line args
#
if [ $# -ne 2 ]; then
echo "usage: get_num_stars: <host> <project>"
echo " supported hosts: github, gitlab, bitbucket"
echo " shortcuts: gh, gl, bb"
echo " example: get_num_stars github openssl/openssl"
echo " example: get_num_stars gitlab tortoisegit/tortoisegit"
echo " example: get_num_stars bitbucket swsc/lookup"
exit 1
fi
host=$1
proj=$2
#
# Use jq-linux64 if available, otherwise use jq
#
if which jq-linux64 >/dev/null; then
JQ=jq-linux64
elif which jq >/dev/null; then
JQ=jq
else
echo "The jq command needed for this script is unavailable."
fi
#
# get the github API auth key(s) from ~/github-auth
#
if [ ! -f ~/github-auth ]; then
echo Error: missing ~/github-auth file
echo Error: missing ~/github-auth file >&2
echo ~/github-auth must exists and contain github API auth key or keys >&2
echo 'Create keys at github->Settings->Developer Settings->personal access tokens' >&2
exit 1
fi
mapfile -t auth_array < ~/github-auth
len="${#auth_array[@]}"
i="$(($RANDOM % $len))"
auth=${auth_array[$i]}
#
# get the gitlab API auth key(s) from ~/gitlab-auth
#
if [ ! -f ~/gitlab-auth ]; then
echo Error: missing ~/gitlab-auth file
echo Error: missing ~/gitlab-auth file >&2
echo ~/gitlab-auth must exists and contain gitlab API auth key or keys >&2
echo 'Create keys at gitlab->User Settings>Access Tokens' >&2
exit 1
fi
mapfile -t auth_array < ~/gitlab-auth
len="${#auth_array[@]}"
i="$(($RANDOM % $len))"
glauth=${auth_array[$i]}
#
# Main body of script
#
case $host in
github|gh)
response=$(curl -s -H "Authorization: token $auth" "https://api.github.com/repos/$proj" | $JQ ".stargazers_count")
echo $response
;;
gitlab|gl)
proj=$(echo $proj | sed -e "s@/@%2F@")
page=0
total=0
while true; do
page=$((page + 1))
url="https://gitlab.com/api/v4/projects/$proj/starrers?per_page=100&page=$page"
response=$(curl -s -H "PRIVATE-TOKEN: $glauth" "$url")
if [[ "$response" = "[]" ]]; then
break
else
starrers=$(echo "$response" | $JQ length)
total=$((total + starrers))
fi
done
echo $total
;;
bitbucket|bb)
echo "unknown (bitbucket)"
;;
*)
echo "unknown ($host)"
# echo "Error: $host not supported, must be one of github, gitlab, or bitbucket"
exit 1
esac
exit 0