From 9d219c05f5d1065ce2654dc8644df194106bfc8b Mon Sep 17 00:00:00 2001 From: Dave Chen Date: Mon, 29 Jun 2026 14:44:52 -0400 Subject: [PATCH] Add GitHub Action to track repository clone traffic --- .github/workflows/track-clones.yml | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 .github/workflows/track-clones.yml diff --git a/.github/workflows/track-clones.yml b/.github/workflows/track-clones.yml new file mode 100644 index 0000000..c1c3493 --- /dev/null +++ b/.github/workflows/track-clones.yml @@ -0,0 +1,97 @@ +name: Track Repository Clones + +on: + schedule: + - cron: '0 0 * * *' # Daily at midnight UTC + workflow_dispatch: # Allow manual triggering + +permissions: + contents: write + +jobs: + track-clones: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Fetch and store clone statistics + env: + GH_TOKEN: ${{ secrets.TRAFFIC_TOKEN }} + REPO: ${{ github.repository }} + run: | + python3 << 'EOF' + import json + import os + import urllib.request + import urllib.error + from datetime import datetime, timezone + + token = os.environ["GH_TOKEN"] + repo = os.environ["REPO"] + tracking_file = ".github/clone-traffic.json" + + # Fetch from GitHub Traffic API + url = f"https://api.github.com/repos/{repo}/traffic/clones" + req = urllib.request.Request(url, headers={ + "Authorization": f"token {token}", + "Accept": "application/vnd.github.v3+json", + }) + + try: + with urllib.request.urlopen(req) as response: + api_data = json.loads(response.read()) + except urllib.error.HTTPError as e: + print(f"HTTP Error {e.code}: {e.reason}") + print(e.read().decode()) + raise SystemExit(1) + + # Load existing tracking data + os.makedirs(".github", exist_ok=True) + if os.path.exists(tracking_file): + with open(tracking_file, "r") as f: + data = json.load(f) + else: + data = {"records": []} + + # Merge new daily records, avoiding duplicates by timestamp + existing_timestamps = {r["timestamp"] for r in data["records"]} + added = 0 + for clone in api_data.get("clones", []): + if clone["timestamp"] not in existing_timestamps: + data["records"].append({ + "timestamp": clone["timestamp"], + "count": clone["count"], + "uniques": clone["uniques"], + }) + existing_timestamps.add(clone["timestamp"]) + added += 1 + + # Keep records sorted chronologically + data["records"].sort(key=lambda x: x["timestamp"]) + + # Update summary fields + data["total_clones"] = sum(r["count"] for r in data["records"]) + data["total_unique_cloners"] = sum(r["uniques"] for r in data["records"]) + data["last_updated"] = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + + with open(tracking_file, "w") as f: + json.dump(data, f, indent=2) + f.write("\n") + + print(f"Added {added} new record(s). Total records: {len(data['records'])}") + print(f"Cumulative clones: {data['total_clones']}") + EOF + + - name: Commit updated statistics + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add .github/clone-traffic.json + if git diff --staged --quiet; then + echo "No new data to commit." + else + git commit -m "chore: update clone traffic statistics [skip ci]" + git push + fi