-
Notifications
You must be signed in to change notification settings - Fork 6
103 lines (86 loc) · 3.39 KB
/
Copy pathclone-stats.yml
File metadata and controls
103 lines (86 loc) · 3.39 KB
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
name: Update Clone Stats
on:
schedule:
- cron: "0 */12 * * *" # Runs every 12 hours
workflow_dispatch: # Allows manual trigger
permissions:
contents: write
jobs:
update-clone-stats:
runs-on: ubuntu-latest
env:
# Prefer a personal access token for traffic APIs.
TRAFFIC_TOKEN: ${{ secrets.TRAFFIC_TOKEN }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Switch to stats branch
run: git checkout -B stats
- name: Restore existing stats history
run: |
if git show origin/stats:stats/clone_history.json >/tmp/clone_history.json 2>/dev/null; then
mkdir -p stats
cp /tmp/clone_history.json stats/clone_history.json
fi
- name: Fetch clone stats from GitHub API
run: |
API_URL="https://api.github.com/repos/${{ github.repository }}/traffic/clones"
fetch_traffic() {
local token="$1"
local code
code=$(curl -sS -L \
-H "Authorization: Bearer ${token}" \
-H "Accept: application/vnd.github+json" \
-o traffic.json \
-w "%{http_code}" \
"${API_URL}")
echo "${code}"
}
if [ -n "${TRAFFIC_TOKEN}" ]; then
STATUS=$(fetch_traffic "${TRAFFIC_TOKEN}")
if [ "${STATUS}" = "401" ] || [ "${STATUS}" = "403" ]; then
echo "TRAFFIC_TOKEN was rejected (${STATUS}); retrying with github.token fallback."
STATUS=$(fetch_traffic "${{ github.token }}")
fi
else
STATUS=$(fetch_traffic "${{ github.token }}")
fi
if [ "${STATUS}" -lt 200 ] || [ "${STATUS}" -ge 300 ]; then
echo "Failed to fetch clone traffic (HTTP ${STATUS}). Response:"
cat traffic.json
exit 1
fi
# Fail early with a clear message if GitHub API did not return usable traffic data.
if [ "$(jq -r 'has("count") and has("uniques")' traffic.json)" != "true" ]; then
echo "Failed to fetch clone traffic. Response:"
cat traffic.json
exit 1
fi
- name: Extract total clone stats
run: |
COUNT=$(jq '.count' traffic.json)
UNIQUES=$(jq '.uniques' traffic.json)
if [ -f stats/clone_history.json ]; then
jq -s '
(.[0] + .[1])
| sort_by(.timestamp)
| group_by(.timestamp)
| map(max_by(.count))
' stats/clone_history.json <(jq '.clones' traffic.json) > stats/clone_history_merged.json
else
jq '.clones' traffic.json > stats/clone_history_merged.json
fi
mv stats/clone_history_merged.json stats/clone_history.json
TOTAL_COUNT=$(jq '[.[].count] | add // 0' stats/clone_history.json)
mkdir -p stats
echo "{ \"count\": $COUNT, \"uniques\": $UNIQUES, \"total_count\": $TOTAL_COUNT }" > stats/clones.json
- name: Commit and push to stats branch
run: |
git config --global user.name "github-actions"
git config --global user.email "actions@github.com"
git add stats/clone_history.json
git add stats/clones.json
git commit -m "Update clone stats" || exit 0
git push origin stats --force