| 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@v4 |
| 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 | |
| 25 | - name: create gist and download previous count |
| 26 | id: set_id |
| 27 | run: | |
| 28 | if gh secret list | grep -q "GIST_ID" |
| 29 | then |
| 30 | echo "GIST_ID found" |
| 31 | echo "GIST=${{ secrets.GIST_ID }}" >> $GITHUB_OUTPUT |
| 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 |
| 37 | echo "GIST=${gist_id}" >> $GITHUB_OUTPUT |
| 38 | cp clone.json clone_before.json |
| 39 | git rm --ignore-unmatch CLONE.md |
| 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 |
| 45 | echo "GIST=${gist_id}" >> $GITHUB_OUTPUT |
| 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 | |
| 54 | - name: update gist with latest count |
| 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" \ |
| 61 | -d @post_clone.json https://api.github.com/gists/${{ steps.set_id.outputs.GIST }} > /dev/null 2>&1 |
| 62 | |
| 63 | if [ ! -f CLONE.md ]; then |
| 64 | shields="https://img.shields.io/badge/dynamic/json?color=success&label=Clone&query=count&url=" |
| 65 | url="https://gist.githubusercontent.com/${{ github.actor }}/${{ steps.set_id.outputs.GIST }}/raw/clone.json" |
| 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 |
| 80 | |
| 81 | git add CLONE.md |
| 82 | git config --global user.name "GitHub Action" |
| 83 | git config --global user.email "action@github.com" |
| 84 | git commit -m "create clone count badge" |
| 85 | fi |
| 86 | |
| 87 | - name: Push |
| 88 | uses: CasperWA/push-protected@v2 |
| 89 | with: |
| 90 | token: ${{ secrets.SECRET_TOKEN }} |