|
| 1 | +name: Collect GitHub Metrics |
| 2 | + |
| 3 | +on: |
| 4 | + # Run weekly on Mondays at 00:00 UTC |
| 5 | + schedule: |
| 6 | + - cron: '0 0 * * 1' |
| 7 | + # Allow manual trigger |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + collect-metrics: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout repository |
| 18 | + uses: actions/checkout@v6 |
| 19 | + with: |
| 20 | + # Use default fetch for faster checkout |
| 21 | + fetch-depth: 1 |
| 22 | + # Ensure we're on the latest main branch |
| 23 | + ref: main |
| 24 | + |
| 25 | + - name: Setup GitHub CLI |
| 26 | + run: | |
| 27 | + # gh is pre-installed on GitHub Actions runners |
| 28 | + # Use METRICS_GITHUB_TOKEN if available (for traffic data), fallback to GITHUB_TOKEN |
| 29 | + TOKEN="${{ secrets.METRICS_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}" |
| 30 | + echo "$TOKEN" | gh auth login --with-token |
| 31 | +
|
| 32 | + - name: Install uv |
| 33 | + uses: astral-sh/setup-uv@v7 |
| 34 | + with: |
| 35 | + version: "0.9.11" |
| 36 | + enable-cache: true |
| 37 | + |
| 38 | + - name: Set up Python |
| 39 | + uses: actions/setup-python@v6 |
| 40 | + with: |
| 41 | + python-version: '3.12' |
| 42 | + |
| 43 | + - name: Install Python dependencies |
| 44 | + run: uv pip install --system pyyaml |
| 45 | + |
| 46 | + - name: Configure git |
| 47 | + run: | |
| 48 | + git config user.name "github-actions[bot]" |
| 49 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 50 | +
|
| 51 | + - name: Ensure data directory exists |
| 52 | + run: mkdir -p catalog/public/data |
| 53 | + |
| 54 | + - name: Collect GitHub metrics |
| 55 | + id: collect |
| 56 | + run: | |
| 57 | + python scripts/collect_github_metrics.py |
| 58 | + if [ $? -ne 0 ]; then |
| 59 | + echo "error=true" >> $GITHUB_OUTPUT |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | + echo "error=false" >> $GITHUB_OUTPUT |
| 63 | + env: |
| 64 | + GH_TOKEN: ${{ secrets.METRICS_GITHUB_TOKEN || secrets.GITHUB_TOKEN }} |
| 65 | + |
| 66 | + - name: Validate JSON files |
| 67 | + run: | |
| 68 | + echo "Validating JSON files..." |
| 69 | + python -c "import json; json.load(open('catalog/public/data/github_metrics.json'))" |
| 70 | + python -c "import json; json.load(open('catalog/public/data/github_metrics_history.json'))" |
| 71 | + echo "✓ JSON files are valid" |
| 72 | +
|
| 73 | + - name: Check for changes |
| 74 | + id: check_changes |
| 75 | + run: | |
| 76 | + git add catalog/public/data/github_metrics.json |
| 77 | + git add catalog/public/data/github_metrics_history.json |
| 78 | + if git diff --cached --quiet; then |
| 79 | + echo "changed=false" >> $GITHUB_OUTPUT |
| 80 | + echo "No changes to commit" |
| 81 | + else |
| 82 | + echo "changed=true" >> $GITHUB_OUTPUT |
| 83 | + echo "Changes detected, will commit" |
| 84 | + fi |
| 85 | +
|
| 86 | + - name: Commit and push metrics data |
| 87 | + if: steps.check_changes.outputs.changed == 'true' |
| 88 | + run: | |
| 89 | + git commit -m "chore: update GitHub metrics data |
| 90 | +
|
| 91 | + 🤖 Automated weekly metrics collection |
| 92 | +
|
| 93 | + - Updated current metrics snapshot |
| 94 | + - Added to historical metrics database |
| 95 | +
|
| 96 | + Generated by: ${{ github.workflow }} |
| 97 | + Run ID: ${{ github.run_id }}" |
| 98 | +
|
| 99 | + # Push with retry logic |
| 100 | + for i in {1..3}; do |
| 101 | + if git push origin main; then |
| 102 | + echo "✓ Successfully pushed changes" |
| 103 | + break |
| 104 | + else |
| 105 | + if [ $i -lt 3 ]; then |
| 106 | + echo "Push failed, retrying in 5 seconds... (attempt $i/3)" |
| 107 | + sleep 5 |
| 108 | + git pull --rebase origin main |
| 109 | + else |
| 110 | + echo "Failed to push after 3 attempts" |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + fi |
| 114 | + done |
| 115 | +
|
| 116 | + - name: Create summary |
| 117 | + if: always() |
| 118 | + run: | |
| 119 | + echo "## GitHub Metrics Collection Results" >> $GITHUB_STEP_SUMMARY |
| 120 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 121 | + echo "**Workflow Run:** \`${{ github.workflow }}\`" >> $GITHUB_STEP_SUMMARY |
| 122 | + echo "**Trigger:** \`${{ github.event_name }}\`" >> $GITHUB_STEP_SUMMARY |
| 123 | + echo "**Date:** \`$(date -u)\`" >> $GITHUB_STEP_SUMMARY |
| 124 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 125 | +
|
| 126 | + if [ "${{ steps.collect.outputs.error }}" == "true" ]; then |
| 127 | + echo "**Status:** ❌ Collection failed" >> $GITHUB_STEP_SUMMARY |
| 128 | + exit 0 |
| 129 | + fi |
| 130 | +
|
| 131 | + if [ -f catalog/public/data/github_metrics.json ]; then |
| 132 | + REPO_COUNT=$(python -c "import json; data=json.load(open('catalog/public/data/github_metrics.json')); print(len(data.get('repos', {})))") |
| 133 | + echo "**Repositories Tracked:** $REPO_COUNT" >> $GITHUB_STEP_SUMMARY |
| 134 | +
|
| 135 | + LAST_UPDATED=$(python -c "import json; data=json.load(open('catalog/public/data/github_metrics.json')); print(data.get('last_updated', 'Unknown'))") |
| 136 | + echo "**Last Updated:** \`$LAST_UPDATED\`" >> $GITHUB_STEP_SUMMARY |
| 137 | + fi |
| 138 | +
|
| 139 | + if [ "${{ steps.check_changes.outputs.changed }}" == "true" ]; then |
| 140 | + echo "**Status:** ✅ Metrics updated and committed" >> $GITHUB_STEP_SUMMARY |
| 141 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 142 | + echo "The deployment workflow will automatically trigger to publish the updated analytics." >> $GITHUB_STEP_SUMMARY |
| 143 | + else |
| 144 | + echo "**Status:** ℹ️ No changes detected" >> $GITHUB_STEP_SUMMARY |
| 145 | + fi |
0 commit comments