| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- # Redraws the repository growth chart every three hours and publishes it to an orphan
- # branch named `metrics` (issue #294). The README and the wiki show
- # https://raw.githubusercontent.com/ChiefGyk3D/Clouds-Against-Humanity/metrics/repo-growth.png,
- # so the stars line never waits for a person to refresh a snapshot by hand.
- #
- # Two jobs, split by what they may do:
- # render contents: read. Runs scripts/gen_repo_metrics.py --refresh with the
- # default GITHUB_TOKEN. That token cannot read stargazer time stamps, so the
- # run never asks for them: it reads the current star count and appends it to a
- # ledger of one dated count per day, carried from the last published snapshot
- # (the star history before the baseline date is committed in main). It never
- # reads a user name (the script validates that on every run). It draws
- # the picture with hash-pinned matplotlib, re-validates the two files and
- # uploads them as a one-day artifact. It cannot push.
- # publish contents: write, no other secrets. Downloads the artifact, validates it
- # again (it crossed an artifact boundary; it must descend from the committed
- # baseline and keep every earlier ledger point) and pushes a child commit of
- # the exact metrics tip the render job read, with an exact lease
- # (--force-with-lease=refs/heads/metrics:<that tip>), so it can only be a pure
- # fast-forward from it. A moved branch fails the run. The first run creates the branch
- # as an orphan root; history is kept, at most one small commit per run (eight a
- # day). The push is skipped only when the bundle is byte-identical to the branch
- # (as-of.txt carries the fetch time, so in practice every run pushes); the ledger still keeps one
- # point per UTC day, and a rerun on the same day replaces that day's point. main is never written, so its branch
- # protection is untouched.
- #
- # Residual risk: GITHUB_TOKEN cannot be scoped to one branch, so the publish job's
- # token is repository-wide contents: write. What limits it: the job runs only
- # checkout, the artifact download and scripts/publish_metrics_branch.sh from main,
- # holds no other secret, hands the token to the three git commands that talk to the
- # remote (scoped to the remote URL, never written to disk, never seen by the
- # validator), and the script refuses any ref but refs/heads/metrics. The real
- # control is a repository ruleset; see docs/wiki/Repository-Growth.md.
- #
- # The committed docs/wiki/images/repo-growth.png and .metrics-snapshot.json stay in
- # main as the offline, hash-checked fallback that CI's --check compares; the scheduled
- # picture does not replace them. Only runs on main in this repository.
- name: Growth chart
- on:
- schedule:
- - cron: "23 */3 * * *" # every three hours, off the hour (UTC)
- workflow_dispatch:
- permissions: {}
- concurrency:
- group: growth-chart
- cancel-in-progress: false
- jobs:
- render:
- if: github.repository == 'ChiefGyk3D/Clouds-Against-Humanity' && github.ref == 'refs/heads/main'
- runs-on: ubuntu-24.04
- timeout-minutes: 15
- permissions:
- contents: read
- outputs:
- tip: ${{ steps.carry.outputs.tip }}
- steps:
- - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- with:
- persist-credentials: false
- - name: Install the plotting libraries (every wheel pinned by hash)
- run: |
- python3 -m venv "$RUNNER_TEMP/venv"
- "$RUNNER_TEMP/venv/bin/pip" install --require-hashes --no-deps -r scripts/growth-chart-requirements.txt
- # The star ledger lives in the previously published snapshot, and the metrics
- # branch is mutable, so nothing from it is trusted. The script carries one file
- # out (size read without writing it, regular file only, fail-closed on any read
- # error; only "no such branch" starts a ledger from nothing) and prints the commit
- # it read. That commit becomes a job output: the publish job may only push a child
- # of exactly that tip. gen_repo_metrics.py then checks the file against the
- # committed baseline and keeps every earlier ledger point.
- - name: Carry the star ledger from the metrics branch
- id: carry
- run: |
- bash scripts/carry_metrics_snapshot.sh "https://github.com/$GITHUB_REPOSITORY.git" carried >> "$GITHUB_OUTPUT"
- # --no-star-times: the default token cannot read stargazer time stamps (REST and
- # GraphQL are both refused), so the run reads only the current star count. The star
- # history comes from the committed baseline snapshot; the ledger of daily counts
- # comes from the carried snapshot. Forks, pull requests and issues are read fresh.
- - name: Fetch counts and draw the chart
- env:
- GH_TOKEN: ${{ github.token }}
- run: |
- mkdir bundle
- "$RUNNER_TEMP/venv/bin/python" -I scripts/gen_repo_metrics.py --refresh --no-page --no-star-times \
- --repo "$GITHUB_REPOSITORY" \
- --baseline docs/wiki/.metrics-snapshot.json --ledger-from carried/metrics-snapshot.json \
- --snapshot bundle/metrics-snapshot.json --out bundle/repo-growth.png
- - name: Validate the bundle
- run: |
- python3 -I scripts/gen_repo_metrics.py --verify-bundle bundle \
- --expect-repo "$GITHUB_REPOSITORY" --max-age-hours 1
- - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
- with:
- name: growth-chart-bundle
- path: bundle
- if-no-files-found: error
- retention-days: 1
- publish:
- needs: render
- if: github.repository == 'ChiefGyk3D/Clouds-Against-Humanity' && github.ref == 'refs/heads/main'
- runs-on: ubuntu-24.04
- timeout-minutes: 10
- permissions:
- contents: write # the metrics branch only; the script never writes main
- steps:
- - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- with:
- persist-credentials: false
- - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
- with:
- name: growth-chart-bundle
- path: bundle
- - name: Validate again and push to the metrics branch
- env:
- GH_TOKEN: ${{ github.token }}
- METRICS_MAX_AGE_HOURS: "3"
- METRICS_EXPECTED_TIP: ${{ needs.render.outputs.tip }}
- run: bash scripts/publish_metrics_branch.sh bundle
|