Skip to content

Refactor update-clones-badge workflow for token usage #3

Refactor update-clones-badge workflow for token usage

Refactor update-clones-badge workflow for token usage #3

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

Check failure on line 33 in .github/workflows/update-clones-badge.yml

View workflow run for this annotation

GitHub Actions / .github/workflows/update-clones-badge.yml

Invalid workflow file

You have an error in your yaml syntax on line 33
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