-
Notifications
You must be signed in to change notification settings - Fork 2
143 lines (128 loc) · 5.94 KB
/
clone.yml
File metadata and controls
143 lines (128 loc) · 5.94 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
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
name: GitHub Clone Count Update Everyday
on:
schedule:
- cron: "0 0 * * *"
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v6
- name: gh login
run: echo "${{ secrets.SECRET_TOKEN }}" | gh auth login --with-token
- name: parse latest clone count
run: |
set -euo pipefail
if ! curl -f --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/traffic/clones" \
> clone.json; then
echo "Error: Failed to fetch clone statistics"
exit 1
fi
- name: create gist and download previous count
id: set_id
run: |
set -euo pipefail
if gh secret list | grep -q "GIST_ID"
then
echo "GIST_ID found"
echo "GIST=${{ secrets.GIST_ID }}" >> $GITHUB_OUTPUT
curl -f "https://gist.githubusercontent.com/${{ github.actor }}/${{ secrets.GIST_ID }}/raw/clone.json" > clone_before.json || true
if grep -q '404: Not Found' clone_before.json 2>/dev/null || [ ! -s clone_before.json ]; then
echo "GIST_ID not valid anymore. Creating another gist..."
gist_id=$(gh gist create clone.json | awk -F / '{print $NF}')
if [ -z "$gist_id" ]; then
echo "Error: Failed to create gist"
exit 1
fi
echo "$gist_id" | gh secret set GIST_ID
echo "GIST=$gist_id" >> $GITHUB_OUTPUT
cp clone.json clone_before.json
git rm --ignore-unmatch CLONE.md || true
fi
else
echo "GIST_ID not found. Creating a gist..."
gist_id=$(gh gist create clone.json | awk -F / '{print $NF}')
if [ -z "$gist_id" ]; then
echo "Error: Failed to create gist"
exit 1
fi
echo "$gist_id" | gh secret set GIST_ID
echo "GIST=$gist_id" >> $GITHUB_OUTPUT
cp clone.json clone_before.json
fi
- name: update clone.json
run: |
set -euo pipefail
python3 << 'EOF'
import json
# Read current clone data from GitHub API
with open('clone.json', 'r') as fh:
now = json.load(fh)
# Read previous accumulated data
with open('clone_before.json', 'r') as fh:
before = json.load(fh)
# Create timestamp index for efficient lookup
timestamps = {before['clones'][i]['timestamp']: i for i in range(len(before['clones']))}
# Start with previous data and merge in new data
latest = dict(before)
for i in range(len(now['clones'])):
timestamp = now['clones'][i]['timestamp']
if timestamp in timestamps:
# Update existing timestamp with new data
latest['clones'][timestamps[timestamp]] = now['clones'][i]
else:
# Add new timestamp
latest['clones'].append(now['clones'][i])
# Recalculate totals from all clones
latest['count'] = sum(map(lambda x: int(x['count']), latest['clones']))
latest['uniques'] = sum(map(lambda x: int(x['uniques']), latest['clones']))
# Aggregate old data by month if we have more than 100 timestamps
if len(timestamps) > 100:
remove_this = []
clones = latest['clones']
for i in range(len(timestamps) - 35):
clones[i]['timestamp'] = clones[i]['timestamp'][:7] # Truncate to YYYY-MM
if clones[i]['timestamp'] == clones[i+1]['timestamp'][:7]:
clones[i+1]['count'] += clones[i]['count']
clones[i+1]['uniques'] += clones[i]['uniques']
remove_this.append(clones[i])
for item in remove_this:
clones.remove(item)
# Write updated data back
with open('clone.json', 'w', encoding='utf-8') as fh:
json.dump(latest, fh, ensure_ascii=False, indent=4)
EOF
- name: Update gist with latest count
run: |
set -euo pipefail
content=$(sed -e 's/\\/\\\\/g' -e 's/\t/\\t/g' -e 's/\"/\\"/g' -e 's/\r//g' "clone.json" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g')
echo '{"description": "${{ github.repository }} clone statistics", "files": {"clone.json": {"content": "'"$content"'"}}}' > post_clone.json
if ! curl -f -s -X PATCH \
--user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
-H "Content-Type: application/json" \
-d @post_clone.json "https://api.github.com/gists/${{ steps.set_id.outputs.GIST }}" > /dev/null; then
echo "Error: Failed to update gist"
exit 1
fi
if [ ! -f CLONE.md ]; then
shields="https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count&url="
url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set_id.outputs.GIST }}/raw/clone.json"
repo="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set_id.outputs.GIST }}/raw/clone.json"
echo '' > CLONE.md
echo '**Markdown**' >> CLONE.md
echo '```markdown' >> CLONE.md
echo "[](${repo})" >> CLONE.md
echo '```' >> CLONE.md
git add CLONE.md
git config --global user.name "GitHub Action"
git config --global user.email "action@github.com"
git commit -m "create clone count badge"
fi
- name: Push
uses: ad-m/github-push-action@v0.8.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}