Refactor update-clones-badge workflow for token usage #3
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: Update clone stats | ||
| on: | ||
| schedule: | ||
| - cron: "0 */12 * * *" | ||
| workflow_dispatch: | ||
| permissions: | ||
| contents: write | ||
| jobs: | ||
| update-clones: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout repo | ||
| uses: actions/checkout@v4 | ||
| - name: Get clone stats | ||
| env: | ||
| GH_TOKEN: ${{ secrets.CLONE_STATS_TOKEN }} | ||
| run: | | ||
| OWNER=${GITHUB_REPOSITORY%/*} | ||
| REPO=${GITHUB_REPOSITORY#*/} | ||
| RESPONSE=$(curl -s \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| -H "Authorization: Bearer $GH_TOKEN" \ | ||
| "https://api.github.com/repos/$OWNER/$REPO/traffic/clones") | ||
| export RESPONSE | ||
| COUNT=$(python - << 'PY' | ||
| import json, os | ||
| data = json.loads(os.environ["RESPONSE"]) | ||
| print(data.get("count", 0)) | ||
| PY | ||
| ) | ||
| cat > clones.json <<EOF | ||
| {"schemaVersion": 1, "label": "clones", "message": "$COUNT"} | ||
| EOF | ||
| - name: Commit changes | ||
| run: | | ||
| if [ -z "$(git status --porcelain)" ]; then | ||
| echo "No changes." | ||
| exit 0 | ||
| fi | ||
| git config user.name "github-actions[bot]" | ||
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | ||
| git add clones.json | ||
| git commit -m "Update clone stats" | ||
| git push | ||