Skip to content

Commit 01dc576

Browse files
authored
Add GitHub workflow for daily clone count update
1 parent ea6016b commit 01dc576

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

.github/workflows/clone.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: GitHub Clone Count Update Everyday
2+
3+
on:
4+
schedule:
5+
- cron: "0 */24 * * *"
6+
workflow_dispatch:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
- name: gh login
16+
run: echo "${{ secrets.SECRET_TOKEN }}" | gh auth login --with-token
17+
18+
- name: parse latest clone count
19+
run: |
20+
curl --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
21+
-H "Accept: application/vnd.github.v3+json" \
22+
https://api.github.com/repos/${{ github.repository }}/traffic/clones \
23+
> clone.json
24+
- name: create gist and download previous count
25+
id: set_id
26+
run: |
27+
if gh secret list | grep -q "GIST_ID"
28+
then
29+
echo "GIST_ID found"
30+
echo "GIST=${{ secrets.GIST_ID }}" >> $GITHUB_OUTPUT
31+
curl https://gist.githubusercontent.com/${{ github.actor }}/${{ secrets.GIST_ID }}/raw/clone.json > clone_before.json
32+
if cat clone_before.json | grep '404: Not Found'; then
33+
echo "GIST_ID not valid anymore. Creating another gist..."
34+
gist_id=$(gh gist create clone.json | awk -F / '{print $NF}')
35+
echo $gist_id | gh secret set GIST_ID
36+
echo "GIST=$gist_id" >> $GITHUB_OUTPUT
37+
cp clone.json clone_before.json
38+
git rm --ignore-unmatch CLONE.md
39+
fi
40+
else
41+
echo "GIST_ID not found. Creating a gist..."
42+
gist_id=$(gh gist create clone.json | awk -F / '{print $NF}')
43+
echo $gist_id | gh secret set GIST_ID
44+
echo "GIST=$gist_id" >> $GITHUB_OUTPUT
45+
cp clone.json clone_before.json
46+
fi
47+
- name: update clone.json
48+
run: |
49+
curl https://raw.githubusercontent.com/MShawon/github-clone-count-badge/master/main.py > main.py
50+
python3 main.py
51+
- name: Update gist with latest count
52+
run: |
53+
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')
54+
echo '{"description": "${{ github.repository }} clone statistics", "files": {"clone.json": {"content": "'"$content"'"}}}' > post_clone.json
55+
curl -s -X PATCH \
56+
--user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
57+
-H "Content-Type: application/json" \
58+
-d @post_clone.json https://api.github.com/gists/${{ steps.set_id.outputs.GIST }} > /dev/null 2>&1
59+
if [ ! -f CLONE.md ]; then
60+
shields="https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count&url="
61+
url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set_id.outputs.GIST }}/raw/clone.json"
62+
repo="https://github.com/MShawon/github-clone-count-badge"
63+
echo ''> CLONE.md
64+
echo '
65+
**Markdown**
66+
```markdown' >> CLONE.md
67+
echo "[![GitHub Clones]($shields$url&logo=github)]($repo)" >> CLONE.md
68+
echo '
69+
```
70+
**HTML**
71+
```html' >> CLONE.md
72+
echo "<a href='$repo'><img alt='GitHub Clones' src='$shields$url&logo=github'></a>" >> CLONE.md
73+
echo '```' >> CLONE.md
74+
75+
git add CLONE.md
76+
git config --global user.name "GitHub Action"
77+
git config --global user.email "action@github.com"
78+
git commit -m "create clone count badge"
79+
fi
80+
- name: Push
81+
uses: ad-m/github-push-action@master
82+
with:
83+
github_token: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)