Collect All Repo Traffic Metrics #203
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: Collect All Repo Traffic Metrics | |
| on: | |
| schedule: | |
| - cron: "0 1 * * *" # Every day at 01:00 UTC | |
| workflow_dispatch: | |
| jobs: | |
| collect: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write # Needed for pushing changes | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.GH_TRAFFIC_TOKEN }} | |
| fetch-depth: 0 | |
| - name: Fetch & Aggregate GitHub Traffic Metrics | |
| env: | |
| TOKEN: ${{ secrets.GH_TRAFFIC_TOKEN }} | |
| run: | | |
| TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") | |
| mkdir -p metrics | |
| TRAFFIC_FILE=metrics/traffic.csv | |
| REFERRERS_FILE=metrics/referrers.csv | |
| # Initialize CSVs if not exist | |
| [ ! -f "$TRAFFIC_FILE" ] && echo "repo,timestamp,clones,unique_cloners,views,unique_visitors,github_stars,github_forks" > "$TRAFFIC_FILE" | |
| [ ! -f "$REFERRERS_FILE" ] && echo "repo,timestamp,referrer,views" > "$REFERRERS_FILE" | |
| REPOS=("SasanLabs/VulnerableApp" "SasanLabs/VulnerableApp-facade" "SasanLabs/LLMForge" "SasanLabs/SAFE") | |
| for REPO in "${REPOS[@]}"; do | |
| echo "Fetching traffic for $REPO" | |
| # GitHub metrics | |
| CLONES_JSON=$(curl -s -H "Authorization: Bearer $TOKEN" https://api.github.com/repos/$REPO/traffic/clones) | |
| VIEWS_JSON=$(curl -s -H "Authorization: Bearer $TOKEN" https://api.github.com/repos/$REPO/traffic/views) | |
| REPO_JSON=$(curl -s -H "Authorization: Bearer $TOKEN" https://api.github.com/repos/$REPO) | |
| echo "------ $REPO CLONES RESPONSE ------" | |
| echo "$CLONES_JSON" | jq | |
| # Per-day clones/views for yesterday | |
| TOTAL_CLONES=$(echo "$CLONES_JSON" | jq -r '.clones[-1].count // 0') | |
| UNIQUE_CLONERS=$(echo "$CLONES_JSON" | jq -r '.clones[-1].uniques // 0') | |
| TOTAL_VIEWS=$(echo "$VIEWS_JSON" | jq -r '.views[-1].count // 0') | |
| UNIQUE_VISITORS=$(echo "$VIEWS_JSON" | jq -r '.views[-1].uniques // 0') | |
| GITHUB_STARS=$(echo "$REPO_JSON" | jq -r '.stargazers_count // 0') | |
| GITHUB_FORKS=$(echo "$REPO_JSON" | jq -r '.forks_count // 0') | |
| # Append to traffic.csv | |
| echo "$REPO,$TIMESTAMP,$TOTAL_CLONES,$UNIQUE_CLONERS,$TOTAL_VIEWS,$UNIQUE_VISITORS,$GITHUB_STARS,$GITHUB_FORKS" >> "$TRAFFIC_FILE" | |
| # Referrers | |
| curl -s -H "Authorization: Bearer $TOKEN" https://api.github.com/repos/$REPO/traffic/popular/referrers \ | |
| | jq -r --arg REPO "$REPO" --arg TS "$TIMESTAMP" '.[] | "\($REPO),\($TS),\(.referrer),\(.count)"' >> "$REFERRERS_FILE" | |
| done | |
| - name: Fetch & Aggregate Docker Hub Metrics | |
| run: | | |
| TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") | |
| DOCKER_FILE=metrics/docker.csv | |
| [ ! -f "$DOCKER_FILE" ] && echo "repo,timestamp,docker_image,pull_count,last_updated,stars,forks,official,automated" > "$DOCKER_FILE" | |
| DOCKER_IMAGES=("sasanlabs/owasp-vulnerableapp" "sasanlabs/owasp-vulnerableapp-facade" "sasanlabs/llmforge") | |
| for IMAGE in "${DOCKER_IMAGES[@]}"; do | |
| echo "Fetching Docker metrics for $IMAGE" | |
| REPO=$(echo $IMAGE | cut -d/ -f2) | |
| DOCKER_JSON=$(curl -s "https://hub.docker.com/v2/repositories/$IMAGE/") | |
| PULL_COUNT=$(echo "$DOCKER_JSON" | jq -r '.pull_count // 0') | |
| LAST_UPDATED=$(echo "$DOCKER_JSON" | jq -r '.last_updated // "N/A"') | |
| STARS=$(echo "$DOCKER_JSON" | jq -r '.star_count // 0') | |
| FORKS=$(echo "$DOCKER_JSON" | jq -r '.fork_count // 0') | |
| OFFICIAL=$(echo "$DOCKER_JSON" | jq -r '.is_official // false') | |
| AUTOMATED=$(echo "$DOCKER_JSON" | jq -r '.is_automated // false') | |
| echo "$REPO,$TIMESTAMP,$IMAGE,$PULL_COUNT,$LAST_UPDATED,$STARS,$FORKS,$OFFICIAL,$AUTOMATED" >> "$DOCKER_FILE" | |
| done | |
| - name: Commit & Push changes | |
| env: | |
| TOKEN: ${{ secrets.GH_TRAFFIC_TOKEN }} | |
| run: | | |
| TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") | |
| git config user.name "preetkaran20" | |
| git config user.email "preetkaran20@gmail.com" | |
| git add metrics/traffic.csv metrics/referrers.csv metrics/docker.csv | |
| git commit -m "Update multi-repo traffic metrics ($TIMESTAMP)" || echo "No changes" | |
| git push "https://preetkaran20:${TOKEN}@github.com/SasanLabs/VulnerableApp.git" HEAD:master |