-
Notifications
You must be signed in to change notification settings - Fork 15
/
get-github-stats.sh
executable file
·152 lines (140 loc) · 5.63 KB
/
get-github-stats.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
# collect stats about PRs and issues in the given time frame
# by default, look for "external contributors" not role maintainers
set -euo pipefail
# "cache" of user lookups
# assumes that if someone is a collaborator on any system roles repo,
# that person is a collaborator on all system roles repos
# this is generally the case for PRs
declare -A USERS
# how many PRs were created for the given role
declare -A PRS_CREATED
# how many PRs were merged
declare -A PRS_MERGED
# how many PRs are open
declare -A PRS_OPEN
# how many PRs were created by non-maintainers
declare -A PRS_CREATED_NON_MAINT
# how many PRs were merged from non-maintainers
declare -A PRS_MERGED_NON_MAINT
# how many PRs are open from non-maintainers
declare -A PRS_OPEN_NON_MAINT
# how many issues were created for the given role
declare -A ISSUES_CREATED
# how many issues were closed
declare -A ISSUES_CLOSED
# how many issues were created by non-maintainers
declare -A ISSUES_CREATED_NON_MAINT
# how many issues were closed from non-maintainers
declare -A ISSUES_CLOSED_NON_MAINT
# silence shellcheck about unset vars
upstream_org="${upstream_org:?upstream_org is unset}"
repo="${repo:?repo is unset}"
if [ -z "${DATE_RANGE:-}" ]; then
echo ERROR: Please specify DATE_RANGE like this
echo DATE_RANGE=2024-01-01..2024-06-30
exit 1
fi
# is the given user a role repo maintainer
user_is_maintainer() {
local username
username="$1"
if [ -z "${USERS[$username]:-}" ]; then
if gh api --silent \
"/repos/$upstream_org/$repo/collaborators/$username" 2> /dev/null; then
USERS["$username"]=0
else
USERS["$username"]=1
fi
fi
return "${USERS[$username]}"
}
# get PRs
get_prs() {
gh pr list -R "$upstream_org/$repo" \
-S "created:$DATE_RANGE state:open state:closed" \
--json number,author,state,title \
--jq '.[] | "\(.number) \(.author.login) \(.state) \(.title)"'
}
get_issues() {
gh issue list -R "$upstream_org/$repo" \
-S "created:$DATE_RANGE state:open state:closed" \
--json number,author,state \
--jq '.[] | "\(.number) \(.author.login) \(.state)"'
}
get_prs > prs.txt
while read -r number author state title; do
# exclude changelog and ci related prs
if [[ "$title" =~ ^ci: ]]; then
continue
fi
if [[ "$title" =~ ^docs\(changelog\) ]]; then
continue
fi
PRS_CREATED["$repo"]=$(("${PRS_CREATED[$repo]:-0}" + 1))
# see if author is a maintainer
if ! user_is_maintainer "$author"; then
PRS_CREATED_NON_MAINT["$repo"]=$(("${PRS_CREATED_NON_MAINT[$repo]:-0}" + 1))
fi
case "$state" in
MERGED) PRS_MERGED["$repo"]=$(("${PRS_MERGED[$repo]:-0}" + 1))
if ! user_is_maintainer "$author"; then
PRS_MERGED_NON_MAINT["$repo"]=$(("${PRS_MERGED_NON_MAINT[$repo]:-0}" + 1))
fi ;;
CLOSED) echo closed without merging ;;
OPEN) PRS_OPEN["$repo"]=$(("${PRS_OPEN[$repo]:-0}" + 1))
if ! user_is_maintainer "$author"; then
PRS_OPEN_NON_MAINT["$repo"]=$(("${PRS_OPEN_NON_MAINT[$repo]:-0}" + 1))
fi ;;
*) echo unknown state "$state" ;;
esac
done < prs.txt
rm -f prs.txt
get_issues > issues.txt
# shellcheck disable=SC2034
while read -r number author state; do
ISSUES_CREATED["$repo"]=$(("${ISSUES_CREATED[$repo]:-0}" + 1))
# see if author is a maintainer
if ! user_is_maintainer "$author"; then
ISSUES_CREATED_NON_MAINT["$repo"]=$(("${ISSUES_CREATED_NON_MAINT[$repo]:-0}" + 1))
fi
if [ "$state" = CLOSED ]; then
ISSUES_CLOSED["$repo"]=$(("${ISSUES_CLOSED[$repo]:-0}" + 1))
if ! user_is_maintainer "$author"; then
ISSUES_CLOSED_NON_MAINT["$repo"]=$(("${ISSUES_CLOSED_NON_MAINT[$repo]:-0}" + 1))
fi
fi
done < issues.txt
rm -f issues.txt
if [ -n "${PRS_CSVFILE:-}" ]; then
if [ ! -s "${PRS_CSVFILE}" ]; then
echo Role,PRs Created,PRs Merged,PRs open,Created non-maint,Merged non-maint,Open non-maint > "$PRS_CSVFILE"
fi
echo "$repo,${PRS_CREATED[$repo]:-0},${PRS_MERGED[$repo]:-0},${PRS_OPEN[$repo]:-0},${PRS_CREATED_NON_MAINT[$repo]:-0},${PRS_MERGED_NON_MAINT[$repo]:-0},${PRS_OPEN_NON_MAINT[$repo]:-0}" >> "$PRS_CSVFILE"
else
echo In the range "$DATE_RANGE" in "$upstream_org/$repo":
echo PRs created: "${PRS_CREATED[$repo]:-0}"
echo PRs merged: "${PRS_MERGED[$repo]:-0}"
echo PRs open: "${PRS_OPEN[$repo]:-0}"
echo PRs created by non-maintainers: "${PRS_CREATED_NON_MAINT[$repo]:-0}"
echo PRs merged from non-maintainers: "${PRS_MERGED_NON_MAINT[$repo]:-0}"
echo PRs open from non-maintainers: "${PRS_OPEN_NON_MAINT[$repo]:-0}"
fi
if [ -n "${ISSUES_CSVFILE:-}" ]; then
if [ ! -s "${ISSUES_CSVFILE}" ]; then
echo Role,Issues Created,Issues Closed,Created non-maint,Closed non-maint > "$ISSUES_CSVFILE"
fi
echo "$repo,${ISSUES_CREATED[$repo]:-0},${ISSUES_CLOSED[$repo]:-0},${ISSUES_CREATED_NON_MAINT[$repo]:-0},${ISSUES_CLOSED_NON_MAINT[$repo]:-0}" >> "$ISSUES_CSVFILE"
else
echo In the range "$DATE_RANGE" in "$upstream_org/$repo":
echo Issues created: "${ISSUES_CREATED[$repo]:-0}"
echo Issues closed: "${ISSUES_CLOSED[$repo]:-0}"
echo Issues created by non-maintainers: "${ISSUES_CREATED_NON_MAINT[$repo]:-0}"
echo Issues closed from non-maintainers: "${ISSUES_CLOSED_NON_MAINT[$repo]:-0}"
fi
cat <<EOF
curl -H 'accept: application/json' -H "Authorization: Token \$galaxy_token" -L \
-s https://galaxy.ansible.com/api/v1/roles\?namespace=linux-system-roles\&page_size=50 | \
jq -r '.results[] | "\(.name),\(.download_count)"' | sort -r -k2 -n -t, > galaxy.csv
get https://galaxy.ansible.com/ui/standalone/roles/willshersystems/sshd
EOF