Skip to content

Update Clone Stats in README #1

Update Clone Stats in README

Update Clone Stats in README #1

name: Update Clone Stats in README
on:
schedule:
- cron: '0 0 * * 1,5' # Execute every Monday at 00:00 UTC
workflow_dispatch: # Allow manual trigger
jobs:
update-readme:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Fetch GitHub clone stats and update README # trigger rescan
env:
MY_PAT: ${{ secrets.MY_PAT }}
REPO: ${{ github.repository }}
run: |
echo "🔑 Using PAT to fetch clone data"
CLONE_DATA=$(curl -s -H "Authorization: token $MY_PAT" \
https://api.github.com/repos/$REPO/traffic/clones)
echo "Debug: CLONE_DATA = $CLONE_DATA"
COUNT=$(echo "$CLONE_DATA" | jq '.count')
UNIQUES=$(echo "$CLONE_DATA" | jq '.uniques')
echo "🔁 Clones: $COUNT, 👤 Unique: $UNIQUES"
BADGE="![Clones](https://img.shields.io/badge/clones-${COUNT}_total_${UNIQUES}_unique-blue)"
echo "Generated badge: $BADGE"
# Replace the old badge with the new one, regardless of its position in README.md
sed -i 's|!\[Clones\](https://img.shields.io/badge/clones.*)|'"$BADGE"'|' README.md
echo "🔑 Using PAT to fetch visitor data"
VIEW_DATA=$(curl -s -H "Authorization: token $MY_PAT" \
https://api.github.com/repos/$REPO/traffic/views)
echo "Debug: VIEW_DATA = $VIEW_DATA"
COUNT=$(echo "$VIEW_DATA" | jq '.count')
UNIQUES=$(echo "$VIEW_DATA" | jq '.uniques')
echo "👁️ Views: $COUNT, 👤 Unique Visitors: $UNIQUES"
# BADGE="![Visitors](https://img.shields.io/badge/visitors-${COUNT}_total_${UNIQUES}_unique-brightgreen)"
BADGE="![Visitors](https://img.shields.io/badge/visitors-${COUNT}_total-brightgreen)"
echo "Generated badge: $BADGE"
# Replace the old badge with the new one in README.md (optional: use comment tags)
sed -i 's|!\[Visitors\](https://img.shields.io/badge/visitors.*)|'"$BADGE"'|' README.md
- name: Commit and push if README was updated
env:
MY_PAT: ${{ secrets.MY_PAT }}
run: |
git config --global user.name 'deadislove'
git config --global user.email 'taweilin7689@gmail.com'
git add README.md
git commit -m "Update clone stats" || echo "No changes to commit"
# Use PAT to push changes
REPO_URL="https://x-access-token:${MY_PAT}@github.com/${GITHUB_REPOSITORY}.git"
git push "$REPO_URL" HEAD:main