diff --git a/.github/workflows/heart-health.yml b/.github/workflows/heart-health.yml index ed71884b..874534d6 100644 --- a/.github/workflows/heart-health.yml +++ b/.github/workflows/heart-health.yml @@ -48,7 +48,8 @@ on: # actions: read → list and download workflow artifacts (smoke_timings, # unit_timings). The artifact endpoints need this scope even # for the repo's own artifacts, and the default token grant -# does not include it. +# does not include it. (Cross-repo reads use the App +# token minted in the job, not this grant.) permissions: contents: write issues: write @@ -94,20 +95,30 @@ jobs: - name: Install PyYAML run: pip install --quiet pyyaml + - name: Mint App token (read-only, every polled repo) + # The checks below read OTHER repos: CI runs and main commits + # (ci_status, ci_timing, no_run_census), open PRs (open_prs) and the + # artifact /zip downloads smoke_timings and unit_timings make against + # the workspaces and libraries. A cross-repo artifact download needs + # actions:read ON THAT REPO, which the repo-scoped GITHUB_TOKEN never + # has (the endpoint answers 403). The org App token is owner-wide (no + # `repositories:`) and read-only. No fallback: a failed mint fails the + # job rather than rendering a board from a token that cannot see. + id: app_token + uses: actions/create-github-app-token@v3 + with: + client-id: ${{ vars.APP_CLIENT_ID }} + private-key: ${{ secrets.APP_PRIVATE_KEY }} + owner: PyAutoLabs + permission-actions: read + permission-contents: read + permission-pull-requests: read + - name: Run cloud-safe checks (CI, timing, smoke and release evidence) env: - # The repo-scoped GITHUB_TOKEN is enough for every check here except - # the artifact /zip downloads smoke_timings and unit_timings make - # against OTHER repos (workspaces and libraries respectively): - # actions/download-artifact documents a cross-repo download as needing - # a token with actions:read ON THAT REPO, and the default token has no - # standing there — the endpoint answers 403. The check records that - # per repo as an honest error rather than a quiet-looking empty - # result, and the remedy is a secret, never a code change: a - # fine-grained PAT (Actions: read on the workspace AND library repos) - # stored as HEART_TIMINGS_TOKEN. Absent that secret this falls back to - # the auto-provisioned token and Heart's own artifacts still ingest. - GH_TOKEN: ${{ secrets.HEART_TIMINGS_TOKEN || secrets.GITHUB_TOKEN }} + # Owner-wide read-only App token (step above). A per-repo refusal is + # still recorded by each check as that repo's honest error. + GH_TOKEN: ${{ steps.app_token.outputs.token }} run: | # API-only checks: CI conclusions and open PRs (health), then CI # wall-clock, the NO_RUN census, the per-script smoke timings and the diff --git a/heart/checks/smoke_timings.sh b/heart/checks/smoke_timings.sh index 40f99b0b..7840d4c0 100755 --- a/heart/checks/smoke_timings.sh +++ b/heart/checks/smoke_timings.sh @@ -75,10 +75,9 @@ ingest_one_repo() { while read -r id artifact_name; do [[ -z "$id" ]] && continue err="$(mktemp)" - # KNOWN RISK: the repo-scoped GITHUB_TOKEN may be refused (403) by the /zip - # endpoint for ANOTHER repo's artifacts. The remedy is a secret, not a code - # change — HEART_TIMINGS_TOKEN, a fine-grained PAT with Actions: read on the - # workspace repos (see .github/workflows/heart-health.yml). Either way one + # The /zip endpoint refuses (403) another repo's artifacts to a token + # without actions:read there; heart-health.yml supplies an owner-wide + # read-only GitHub App token for exactly this. Either way one # leg's failure is recorded as that leg's error and must not lose the repo's # other leg, hence the `.error` marker and the `continue`. if gh api "repos/$owner_name/actions/artifacts/$id/zip" >"$downloads/$id.zip" 2>"$err" \ diff --git a/heart/checks/unit_timings.sh b/heart/checks/unit_timings.sh index bd087cc2..799a877f 100755 --- a/heart/checks/unit_timings.sh +++ b/heart/checks/unit_timings.sh @@ -78,10 +78,9 @@ ingest_one_repo() { while read -r id artifact_name; do [[ -z "$id" ]] && continue err="$(mktemp)" - # KNOWN RISK: the repo-scoped GITHUB_TOKEN may be refused (403) by the /zip - # endpoint for ANOTHER repo's artifacts. The remedy is a secret, not a code - # change — HEART_TIMINGS_TOKEN, a fine-grained PAT with Actions: read on the - # library repos (see .github/workflows/heart-health.yml). Either way one + # The /zip endpoint refuses (403) another repo's artifacts to a token + # without actions:read there; heart-health.yml supplies an owner-wide + # read-only GitHub App token for exactly this. Either way one # leg's failure is recorded as that leg's error and must not lose the repo's # other leg, hence the `.error` marker and the `continue`. if gh api "repos/$owner_name/actions/artifacts/$id/zip" >"$downloads/$id.zip" 2>"$err" \ diff --git a/tests/test_heart_health_wiring.py b/tests/test_heart_health_wiring.py index 190c39c1..7194b882 100644 --- a/tests/test_heart_health_wiring.py +++ b/tests/test_heart_health_wiring.py @@ -47,15 +47,18 @@ def test_the_job_can_read_actions_artifacts(): assert data["permissions"]["actions"] == "read" -def test_the_cloud_step_prefers_the_timings_token_and_falls_back(): +def test_the_cloud_step_uses_the_minted_app_token_without_fallback(): """Cross-repo artifact downloads need a token with actions:read on THAT - repo; the remedy is a secret, not a code change. Absent it the step still - runs on the auto-provisioned token.""" + repo. The job mints an owner-wide read-only App token for it; there is no + fallback to the repo-scoped token — a missing credential fails the job.""" _, job = _job() + mint = next(s for s in job["steps"] if s.get("id") == "app_token") + assert mint["uses"].startswith("actions/create-github-app-token@") + assert mint["with"]["permission-actions"] == "read" + assert "repositories" not in mint["with"] token = _cloud_step(job)["env"]["GH_TOKEN"] - assert "secrets.HEART_TIMINGS_TOKEN" in token - assert "secrets.GITHUB_TOKEN" in token - assert "||" in token + assert token == "${{ steps.app_token.outputs.token }}" + assert "||" not in token def test_the_snapshot_folds_in_the_smoke_timings_rollup():