Track Metrics #7
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 Metrics | |
| on: | |
| schedule: | |
| - cron: '0 6 * * *' | |
| workflow_dispatch: {} | |
| permissions: | |
| contents: write | |
| jobs: | |
| track: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Fetch and record metrics | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| REPO="${{ github.repository }}" | |
| DATE=$(date -u +%Y-%m-%d) | |
| FILE=".github/metrics/daily.jsonl" | |
| mkdir -p .github/metrics | |
| # Skip if already recorded today | |
| if grep -q "\"$DATE\"" "$FILE" 2>/dev/null; then | |
| echo "Metrics for $DATE already recorded" | |
| exit 0 | |
| fi | |
| # Fetch each metric separately with safe fallbacks | |
| CLONE_TOTAL=0; CLONE_UNIQUE=0 | |
| if clones_raw=$(gh api "repos/$REPO/traffic/clones" 2>/dev/null); then | |
| CLONE_TOTAL=$(echo "$clones_raw" | jq -r '.count // 0') | |
| CLONE_UNIQUE=$(echo "$clones_raw" | jq -r '.uniques // 0') | |
| fi | |
| VIEW_TOTAL=0; VIEW_UNIQUE=0 | |
| if views_raw=$(gh api "repos/$REPO/traffic/views" 2>/dev/null); then | |
| VIEW_TOTAL=$(echo "$views_raw" | jq -r '.count // 0') | |
| VIEW_UNIQUE=$(echo "$views_raw" | jq -r '.uniques // 0') | |
| fi | |
| DOWNLOADS=0 | |
| if dl_raw=$(gh api "repos/$REPO/releases" 2>/dev/null); then | |
| DOWNLOADS=$(echo "$dl_raw" | jq '[.[].assets[]?.download_count // 0] | add // 0') | |
| fi | |
| STARS=0; FORKS=0 | |
| if repo_raw=$(gh api "repos/$REPO" 2>/dev/null); then | |
| STARS=$(echo "$repo_raw" | jq -r '.stargazers_count // 0') | |
| FORKS=$(echo "$repo_raw" | jq -r '.forks_count // 0') | |
| fi | |
| REFERRERS="[]" | |
| if ref_raw=$(gh api "repos/$REPO/traffic/popular/referrers" 2>/dev/null); then | |
| REFERRERS=$(echo "$ref_raw" | jq -c '[.[:5][] | {referrer, count, uniques}]' 2>/dev/null || echo "[]") | |
| fi | |
| # Build JSONL entry | |
| ENTRY=$(jq -n -c \ | |
| --arg d "$DATE" \ | |
| --argjson ct "$CLONE_TOTAL" --argjson cu "$CLONE_UNIQUE" \ | |
| --argjson vt "$VIEW_TOTAL" --argjson vu "$VIEW_UNIQUE" \ | |
| --argjson dl "$DOWNLOADS" \ | |
| --argjson s "$STARS" --argjson f "$FORKS" \ | |
| --argjson r "$REFERRERS" \ | |
| '{date:$d, clones:{total:$ct,unique:$cu}, views:{total:$vt,unique:$vu}, downloads:$dl, stars:$s, forks:$f, referrers:$r}') | |
| echo "$ENTRY" >> "$FILE" | |
| echo "Recorded: $ENTRY" | |
| - name: Commit if changed | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add .github/metrics/daily.jsonl | |
| if git diff --cached --quiet; then | |
| echo "No changes to commit" | |
| else | |
| git commit -m "metrics: $(date -u +%Y-%m-%d) daily snapshot" | |
| git push | |
| fi |