Skip to content

Commit 47a76e8

Browse files
committed
new github workflow for clone count
1 parent ef6be5d commit 47a76e8

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

.github/workflows/clone.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Clone Count Update
2+
3+
on:
4+
schedule:
5+
- cron: "10 0 * * SUN"
6+
workflow_dispatch:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v3
14+
with:
15+
ref: clone_count
16+
17+
- name: gh login
18+
run: echo "${{ secrets.SECRET_TOKEN }}" | gh auth login --with-token
19+
20+
- name: parse latest clone count
21+
run: |
22+
curl --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
23+
-H "Accept: application/vnd.github.v3+json" \
24+
https://api.github.com/repos/${{ github.repository }}/traffic/clones \
25+
> clone.json
26+
27+
- name: create gist and download previous count
28+
id: set_id
29+
run: |
30+
if gh secret list | grep -q "GIST_ID"
31+
then
32+
echo "GIST_ID found"
33+
echo "GIST=${{ secrets.GIST_ID }}" >> $GITHUB_OUTPUT
34+
curl https://gist.githubusercontent.com/${{ github.actor }}/${{ secrets.GIST_ID }}/raw/clone.json > clone_before.json
35+
if cat clone_before.json | grep '404: Not Found'; then
36+
echo "GIST_ID not valid anymore. Creating another gist..."
37+
gist_id=$(gh gist create clone.json | awk -F / '{print $NF}')
38+
echo $gist_id | gh secret set GIST_ID
39+
echo "GIST=$gist_id" >> $GITHUB_OUTPUT
40+
cp clone.json clone_before.json
41+
git rm --ignore-unmatch CLONE.md
42+
fi
43+
else
44+
echo "GIST_ID not found. Creating a gist..."
45+
gist_id=$(gh gist create clone.json | awk -F / '{print $NF}')
46+
echo $gist_id | gh secret set GIST_ID
47+
echo "GIST=$gist_id" >> $GITHUB_OUTPUT
48+
cp clone.json clone_before.json
49+
fi
50+
51+
- name: update clone.json
52+
run: |
53+
curl https://raw.githubusercontent.com/MShawon/github-clone-count-badge/master/main.py > main.py
54+
python3 main.py
55+
56+
- name: Update gist with latest count
57+
run: |
58+
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')
59+
echo '{"description": "${{ github.repository }} clone statistics", "files": {"clone.json": {"content": "'"$content"'"}}}' > post_clone.json
60+
curl -s -X PATCH \
61+
--user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \
62+
-H "Content-Type: application/json" \
63+
-d @post_clone.json https://api.github.com/gists/${{ steps.set_id.outputs.GIST }} > /dev/null 2>&1

0 commit comments

Comments
 (0)