Track Repository Stats #1612
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 Repository Stats | |
| on: | |
| schedule: | |
| - cron: '0 * * * *' | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| jobs: | |
| update-stats: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Fetch Stats, Update File, and Commit | |
| run: | | |
| # 1. Setup folder and static file path | |
| FOLDER="repo-stats" | |
| mkdir -p $FOLDER | |
| FILE="$FOLDER/stats.md" | |
| LAST_UPDATED=$(date -u +'%Y-%m-%d %H:%M:%S UTC') | |
| # 2. Fetch Repo Info (Stars, Forks, Watchers) | |
| REPO_DATA=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" https://api.github.com/repos/${{ github.repository }}) | |
| STARS=$(echo "$REPO_DATA" | jq -r '.stargazers_count // 0') | |
| FORKS=$(echo "$REPO_DATA" | jq -r '.forks_count // 0') | |
| WATCHERS=$(echo "$REPO_DATA" | jq -r '.subscribers_count // 0') | |
| # 3. Fetch Traffic Views (Last 14 days) | |
| TRAFFIC_DATA=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" https://api.github.com/repos/${{ github.repository }}/traffic/views) | |
| VIEWS=$(echo "$TRAFFIC_DATA" | jq -r '.count // 0') | |
| UNIQUE_VIEWS=$(echo "$TRAFFIC_DATA" | jq -r '.uniques // 0') | |
| # 4. Generate/Overwrite the Markdown File | |
| echo "# Repository Stats for ${{ github.repository }}" > "$FILE" | |
| echo "**⏱️ Last Updated:** $LAST_UPDATED" >> "$FILE" | |
| echo "" >> "$FILE" | |
| echo "- **⭐ Stars:** $STARS" >> "$FILE" | |
| echo "- **🍴 Forks:** $FORKS" >> "$FILE" | |
| echo "- **👀 Watchers:** $WATCHERS" >> "$FILE" | |
| echo "- **📈 Total Views (Last 14 days):** $VIEWS" >> "$FILE" | |
| echo "- **👤 Unique Visitors (Last 14 days):** $UNIQUE_VIEWS" >> "$FILE" | |
| # 5. Commit and Push Changes directly in the same step | |
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
| git config --local user.name "github-actions[bot]" | |
| git add "$FILE" | |
| git commit -m "docs: update repo stats - $LAST_UPDATED" || echo "No changes to commit" | |
| git push |