Track Clone Count #102
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Track Clone Count | |
| on: | |
| push: | |
| branches: [main] | |
| schedule: | |
| - cron: "0 0 * * *" # Daily at midnight UTC | |
| workflow_dispatch: | |
| env: | |
| FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true | |
| jobs: | |
| clone-count: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v6 | |
| - name: Fetch clone data from GitHub API | |
| env: | |
| GH_ACTOR: ${{ github.actor }} | |
| GH_REPO: ${{ github.repository }} | |
| SECRET_TOKEN: ${{ secrets.SECRET_TOKEN }} | |
| run: | | |
| curl --user "$GH_ACTOR:$SECRET_TOKEN" \ | |
| -H "Accept: application/vnd.github.v3+json" \ | |
| "https://api.github.com/repos/$GH_REPO/traffic/clones" \ | |
| > clone.json | |
| - name: Create or retrieve gist for persistent storage | |
| id: set_id | |
| env: | |
| GH_TOKEN: ${{ secrets.SECRET_TOKEN }} | |
| GIST_SECRET: ${{ secrets.GIST_ID }} | |
| GH_ACTOR: ${{ github.actor }} | |
| run: | | |
| if [ -n "$GIST_SECRET" ]; then | |
| echo "GIST_ID found" | |
| echo "GIST=$GIST_SECRET" >> "$GITHUB_OUTPUT" | |
| curl "https://gist.githubusercontent.com/$GH_ACTOR/$GIST_SECRET/raw/clone.json" > clone_before.json | |
| if grep -q '404: Not Found' clone_before.json; then | |
| echo "GIST_ID not valid anymore. Creating another gist..." | |
| gist_id=$(gh gist create clone.json | awk -F / '{print $NF}') | |
| 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 | |
| fi | |
| else | |
| echo "GIST_ID not found. Creating a gist..." | |
| gist_id=$(gh gist create clone.json | awk -F / '{print $NF}') | |
| echo "$gist_id" | gh secret set GIST_ID | |
| echo "GIST=$gist_id" >> "$GITHUB_OUTPUT" | |
| cp clone.json clone_before.json | |
| fi | |
| - name: Accumulate clone statistics | |
| run: | | |
| curl https://raw.githubusercontent.com/MShawon/github-clone-count-badge/master/main.py > main.py | |
| python3 main.py | |
| - name: Update gist with accumulated data | |
| env: | |
| SECRET_TOKEN: ${{ secrets.SECRET_TOKEN }} | |
| GH_ACTOR: ${{ github.actor }} | |
| GH_REPO: ${{ github.repository }} | |
| GIST: ${{ steps.set_id.outputs.GIST }} | |
| run: | | |
| 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\": \"$GH_REPO clone statistics\", \"files\": {\"clone.json\": {\"content\": \"$content\"}}}" > post_clone.json | |
| curl -s -X PATCH \ | |
| --user "$GH_ACTOR:$SECRET_TOKEN" \ | |
| -H "Content-Type: application/json" \ | |
| -d @post_clone.json "https://api.github.com/gists/$GIST" > /dev/null 2>&1 | |
| 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/$GH_ACTOR/$GIST/raw/clone.json" | |
| repo="https://github.com/$GH_REPO" | |
| { | |
| echo '' | |
| echo '**Badge Markdown**' | |
| echo '' | |
| echo '```markdown' | |
| echo "[]($repo)" | |
| 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 changes | |
| env: | |
| SECRET_TOKEN: ${{ secrets.SECRET_TOKEN }} | |
| GH_REPO: ${{ github.repository }} | |
| run: | | |
| git remote set-url origin "https://x-access-token:$SECRET_TOKEN@github.com/$GH_REPO.git" | |
| git push origin HEAD:main || echo "Nothing to push" |