forked from openziti/ziti-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-stats.sh
executable file
·374 lines (332 loc) · 11.5 KB
/
gh-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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
export GH_PAGE_SIZE=100
export ORG=openziti
export REPO=ziti
export TODAY=$(date '+%Y%m%d')
export SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
export STATS_DIR="/tmp/stats"
export GH_TOKEN="${GITHUB_TOKEN}"
function outputFileExists {
if [[ -f $1 ]]; then
return 0
else
return 1
fi
}
function getRateLimit {
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/rate_limit
}
function getReleases {
makeOutputDir
local api=releases
local outputFile="${STATS_DIR}/${TODAY}/${REPO}.${api}.json"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already fetched ${api} for today: ${outputFile}"; fi
else
if [[ ! -z $GH_ECHO ]]; then echo "fetching ${api} to ${outputFile}"; fi
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${ORG}/${REPO}/${api}" \
-o "${outputFile}"
fi
}
function getStargazers {
makeOutputDir
local api=stargazers
local page=${1-1}
local outputFile="${STATS_DIR}/${TODAY}/${REPO}.${api}.${page}.json"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already fetched ${api} for today: ${outputFile}"; fi
else
if [[ ! -z $GH_ECHO ]]; then echo "fetching ${api} to ${outputFile}"; fi
curl -s -L \
-H "Accept: application/vnd.github.star+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${ORG}/${REPO}/${api}?per_page=${GH_PAGE_SIZE}&page=$page" \
-o "${outputFile}"
fi
}
function getRepo {
makeOutputDir
local api=repo
local page=${1-1}
local outputFile="${STATS_DIR}/${TODAY}/${REPO}.${api}.json"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already fetched ${api} for today: ${outputFile}"; fi
else
if [[ ! -z $GH_ECHO ]]; then echo "fetching ${api} to ${outputFile}"; fi
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${ORG}/${REPO}" \
-o "${outputFile}"
fi
}
function getAllRepos {
makeOutputDir
local api=repos
local page=${1-1}
local outputFile="${STATS_DIR}/${TODAY}/${ORG}.${api}.json"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already fetched ${api} for today: ${outputFile}"; fi
else
if [[ ! -z $GH_ECHO ]]; then echo "fetching ${api} to ${outputFile}"; fi
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/orgs/${ORG}/${api}?per_page=${GH_PAGE_SIZE}" \
-o "${outputFile}"
fi
}
function getDownloadsByPage {
makeOutputDir
local api=releases
local page=${1-1}
local outputFile="${STATS_DIR}/${TODAY}/${REPO}.${api}.${page}.json"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already fetched ${api} for today: ${outputFile}"; fi
else
if [[ ! -z $GH_ECHO ]]; then echo "fetching ${api} to ${outputFile}"; fi
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${ORG}/${REPO}/${api}?per_page=${GH_PAGE_SIZE}&page=$page" \
-o "${outputFile}"
fi
}
function fetchDownloads {
makeOutputDir
local outputFile="${STATS_DIR}/${TODAY}/${REPO}.releases.all.json"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already condensed releases for ${REPO}: ${outputFile}"; fi
else
if [[ ! -z $GH_ECHO ]]; then echo "condensing releases for ${REPO} to: ${outputFile}"; fi
local to=$(getReleasePageCount)
if [[ "${to}x" != "x" ]]; then
for (( i=1; i<=$to; i++)); do getDownloadsByPage $i; done
jq -s .[] "${STATS_DIR}/${TODAY}/${REPO}.releases."*json > "${STATS_DIR}/${TODAY}/${REPO}.releases.all.json"
else
echo "[]" > "${outputFile}"
fi
fi
}
function fetchAllDownloads {
makeOutputDir
for repo in $(listRepoNames); do
export REPO=$repo
echo "Fetching Downloads for: ${REPO}"
fetchDownloads
done
}
function getReleasePageCount {
makeOutputDir
local result=$(curl -I -s \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${ORG}/${REPO}/releases?page=1" \
| grep -i link \
| sed -n 's/.*page=\([0-9]*\).*rel="last".*/\1/p')
if [[ "${result}x" == "x" ]]; then
echo "1"
else
echo "${result}"
fi
}
function makeOutputDir {
mkdir -p ${STATS_DIR}/${TODAY}
}
function getStarCount {
getRepo
jq .stargazers_count ${STATS_DIR}/${TODAY}/${REPO}.repo.json
}
function listRepoNames {
local targetFile="${STATS_DIR}/${TODAY}/${ORG}.repos.json"
if outputFileExists "${targetFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already fetched ${api} for today: ${outputFile}"; fi
else
getAllRepos
fi
jq -r .[].name ${targetFile} | sort
}
function getStarCountPages {
echo $(($(getStarCount) / $GH_PAGE_SIZE + 1))
}
function fetchStargazers {
local to=$(getStarCountPages)
for (( i=1; i<=$to; i++)); do getStargazers $i; done
}
function fetchAllStargazers {
for repo in $(listRepoNames); do
export REPO=$repo
echo "Fetching Stargazers for: ${REPO}"
fetchStargazers
done
}
function condenseStargazers {
fetchAllStargazers
for repo in $(listRepoNames); do
export REPO=$repo
local outputFile="${STATS_DIR}/${TODAY}/${REPO}.stargazers.all.json"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already condensed stargazers for ${REPO}: ${outputFile}"; fi
else
if [[ ! -z $GH_ECHO ]]; then echo "condensing stargazers for ${REPO} to: ${outputFile}"; fi
jq -s .[] "${STATS_DIR}/${TODAY}/${REPO}.stargazers."*json > "${STATS_DIR}/${TODAY}/${REPO}.stargazers.all.json"
fi
done
}
function condenseAllStargazers {
local outputFile="${STATS_DIR}/${TODAY}/all.stargazers.json"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already condensed all stars for ${TODAY}: ${outputFile}"; fi
else
echo "condensing all stargazers to: ${outputFile}"
condenseStargazers
jq -s 'flatten' "${STATS_DIR}/${TODAY}/"*stargazers.all.json > "${outputFile}"
fi
}
function stargazersToCsv {
condenseAllStargazers
local outputFile="${STATS_DIR}/${TODAY}/all.stargazers.csv"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already created stargazers csv for ${TODAY}: ${outputFile}"; fi
else
for repo in $(listRepoNames); do
export REPO=$repo
echo "Converting stargazers for ${REPO} to csv"
jq -r '.[] | { starred_at: .starred_at, login: .user.login } | [.starred_at, .login, "'"${REPO}"'"] | @csv' \
"${STATS_DIR}/${TODAY}/${REPO}.stargazers.all.json" >> "${outputFile}"
done
fi
echo sorting "${outputFile}" to "${STATS_DIR}/${TODAY}/all.stargazers.by.repo.csv"
sort -k3 -k1 -t "," "${outputFile}" > "${STATS_DIR}/${TODAY}/all.stargazers.by.repo.csv"
}
function stargazersPerRepo {
condenseStargazers
for repo in $(listRepoNames); do
export REPO=$repo
local outputFile="${STATS_DIR}/${TODAY}/${REPO}.stargazers.csv"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already created stargazers list for ${REPO}: ${outputFile}"; fi
else
if [[ ! -z $GH_ECHO ]]; then echo "creating stargazers list for ${REPO} to: ${outputFile}"; fi
jq -r '.[] | { starred_at: .starred_at, login: .user.login } | [.starred_at, .login, "'"${REPO}"'"] | @csv' \
"${STATS_DIR}/${TODAY}/${REPO}.stargazers.all.json" | sort > "${outputFile}"
fi
done
}
function getClones {
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${ORG}/${REPO}/traffic/clones?per=week"
}
function getTopRefferrals {
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${ORG}/${REPO}/traffic/popular/paths"
}
function getTopRefferralSources {
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${ORG}/${REPO}/traffic/popular/referrers"
}
function getForks {
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${ORG}/${REPO}/forks"
}
function getPageViews {
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${ORG}/${REPO}/traffic/views?per=week"
}
function getForkCount {
getRepo
jq .forks_count ${STATS_DIR}/${TODAY}/${REPO}.repo.json
}
function getForksPageCount {
echo $(($(getForkCount) / $GH_PAGE_SIZE + 1))
}
function fetchForks {
for repo in $(listRepoNames); do
local totalForks=$(getForkCount)
local pages=$(getForksPageCount)
export REPO=$repo
echo "$repo has $pages of forks totalling $totalForks"
done
}
function reduceAllDownloads {
fetchAllDownloads
local outputFile="${STATS_DIR}/${TODAY}/all.releases.csv"
if outputFileExists "${outputFile}"; then
if [[ ! -z $GH_ECHO ]]; then echo "already reduced downloads to a csv for ${TODAY}: ${outputFile}"; fi
else
for repo in $(listRepoNames); do
export REPO=$repo
echo "Converting stargazers for ${REPO} to csv"
echo "Reducing Downloads for: ${REPO}"
jq -r '.[] | {tag_name, created_at, total_downloads: (reduce .assets[] as $asset (0; if ($asset.browser_download_url | endswith("sha256")) then . else . + $asset.download_count end))} | [.tag_name, .created_at, .total_downloads, "'"${REPO}"'"] | @csv' \
"${STATS_DIR}/${TODAY}/${REPO}.releases.all.json" >> \
"${outputFile}"
done
echo "generated csv: ${outputFile}"
fi
}
function removeTempStargazerFiles {
find . -name *stargazers.[0123456789].json | xargs rm
}
function findTeamId {
local team=$1
curl -s -H "Authorization: Token $GH_ADMIN_TOKEN" \
"https://api.github.com/orgs/${ORG}/teams" \
| jq --arg team "$team" '.[] | select(.name==$team) | .id'
}
function addTeamToRepo {
local team=$1
local repo=$2
local teamId=$3
echo "Adding $team with teamId:$teamId to $repo"
curl -H "Authorization: Token $GH_ADMIN_TOKEN" \
-X PUT -d '{"permission":"push"}' "https://api.github.com/teams/${teamId}/repos/${ORG}/${repo}"
}
function addTeamToAllRepos {
local team=$1
local repo=$2
local teamId=$(findTeamId $team)
for repo in $(listRepoNames)
do
echo "Adding team:$team to repo:$repo with teamId:$teamId"
addTeamToRepo $team $repo $teamId
done
}
function toStargazerData {
local inputFile="${STATS_DIR}/${TODAY}/all.stargazers.csv"
grep -v '"ziti"' ${inputFile} | grep -v '"zrok"' | sort | sed '1s/^/date,user,repo\n/'| csvtojson > "${STATS_DIR}/${TODAY}/all.other.stargazers.json"
grep '"ziti"' ${inputFile} | sort | sed '1s/^/date,user,repo\n/'| csvtojson > "${STATS_DIR}/${TODAY}/all.ziti.stargazers.json"
grep '"zrok"' ${inputFile} | sort | sed '1s/^/date,user,repo\n/'| csvtojson > "${STATS_DIR}/${TODAY}/all.zrok.stargazers.json"
echo "stargazer json written to ${STATS_DIR}/${TODAY}/all*.stargazers.json"
ls -l ${STATS_DIR}/${TODAY}/all*.stargazers.json
}
stargazersToCsv
toStargazerData
cp ${STATS_DIR}/${TODAY}/all*.stargazers.json ./docusaurus/src/pages/stargazers/