Commit | Line | Data |
---|---|---|
241e529d | 1 | name: GitHub Clone Count Update |
3848c429 JB |
2 | |
3 | on: | |
4 | schedule: | |
c41ef1e0 | 5 | - cron: '0 */24 * * *' |
3848c429 JB |
6 | workflow_dispatch: |
7 | ||
8 | jobs: | |
9d9a7a0d | 9 | clone-count: |
3848c429 JB |
10 | runs-on: ubuntu-latest |
11 | ||
12 | steps: | |
49722c24 | 13 | - uses: actions/checkout@v4 |
c41ef1e0 | 14 | |
3848c429 JB |
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 | ||
25 | - name: create gist and download previous count | |
9d9a7a0d | 26 | id: set-gist |
3848c429 JB |
27 | run: | |
28 | if gh secret list | grep -q "GIST_ID" | |
29 | then | |
30 | echo "GIST_ID found" | |
35920236 | 31 | echo "GIST=${{ secrets.GIST_ID }}" >> $GITHUB_OUTPUT |
3848c429 JB |
32 | curl https://gist.githubusercontent.com/${{ github.actor }}/${{ secrets.GIST_ID }}/raw/clone.json > clone_before.json |
33 | if cat clone_before.json | grep '404: Not Found'; then | |
34 | echo "GIST_ID not valid anymore. Creating another gist..." | |
35 | gist_id=$(gh gist create clone.json | awk -F / '{print $NF}') | |
36 | echo $gist_id | gh secret set GIST_ID | |
35920236 | 37 | echo "GIST=${gist_id}" >> $GITHUB_OUTPUT |
3848c429 | 38 | cp clone.json clone_before.json |
940943f4 | 39 | git rm --ignore-unmatch CLONE.md |
3848c429 JB |
40 | fi |
41 | else | |
42 | echo "GIST_ID not found. Creating a gist..." | |
43 | gist_id=$(gh gist create clone.json | awk -F / '{print $NF}') | |
44 | echo $gist_id | gh secret set GIST_ID | |
35920236 | 45 | echo "GIST=${gist_id}" >> $GITHUB_OUTPUT |
3848c429 JB |
46 | cp clone.json clone_before.json |
47 | fi | |
48 | ||
49 | - name: update clone.json | |
50 | run: | | |
51 | curl https://raw.githubusercontent.com/MShawon/github-clone-count-badge/master/main.py > main.py | |
52 | python3 main.py | |
53 | ||
c41ef1e0 | 54 | - name: update gist with latest count |
3848c429 JB |
55 | run: | |
56 | 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') | |
57 | echo '{"description": "${{ github.repository }} clone statistics", "files": {"clone.json": {"content": "'"$content"'"}}}' > post_clone.json | |
58 | curl -s -X PATCH \ | |
59 | --user "${{ github.actor }}:${{ secrets.SECRET_TOKEN }}" \ | |
60 | -H "Content-Type: application/json" \ | |
9d9a7a0d | 61 | -d @post_clone.json https://api.github.com/gists/${{ steps.set-gist.outputs.GIST }} > /dev/null 2>&1 |
3848c429 JB |
62 | |
63 | if [ ! -f CLONE.md ]; then | |
64 | shields="https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count&url=" | |
9d9a7a0d | 65 | url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set-gist.outputs.GIST }}/raw/clone.json" |
3848c429 JB |
66 | repo="https://github.com/MShawon/github-clone-count-badge" |
67 | echo ''> CLONE.md | |
68 | echo ' | |
69 | **Markdown** | |
70 | ||
71 | ```markdown' >> CLONE.md | |
72 | echo "[![GitHub Clones]($shields$url&logo=github)]($repo)" >> CLONE.md | |
73 | echo ' | |
74 | ``` | |
75 | ||
76 | **HTML** | |
77 | ```html' >> CLONE.md | |
78 | echo "<a href='$repo'><img alt='GitHub Clones' src='$shields$url&logo=github'></a>" >> CLONE.md | |
79 | echo '```' >> CLONE.md | |
c41ef1e0 | 80 | |
169e5973 JB |
81 | git config --local user.name "GitHub Action" |
82 | git config --local user.email "action@github.com" | |
9869fa53 JB |
83 | git add CLONE.md |
84 | git commit -m "docs: create clone count badge" | |
3848c429 JB |
85 | fi |
86 | ||
9d9a7a0d | 87 | - name: push |
a43b0b47 | 88 | uses: CasperWA/push-protected@v2 |
3848c429 | 89 | with: |
a43b0b47 | 90 | token: ${{ secrets.SECRET_TOKEN }} |