diff --git a/.github/scripts/test-native-queue-workflows.py b/.github/scripts/test-native-queue-workflows.py index e598728df9..30890e6ad2 100644 --- a/.github/scripts/test-native-queue-workflows.py +++ b/.github/scripts/test-native-queue-workflows.py @@ -97,6 +97,10 @@ def test_dependabot_runs_the_same_trusted_pr_verifier(self): verifier = workflow("pr-verifier.yml") events = verifier.get("on", verifier.get(True)) self.assertIn("opened", events["pull_request_target"]["types"]) + # A github.token push fires no pull_request_target event; App/PAT label + # flips must re-run the required context on the current head (#17036). + for action in ("synchronize", "ready_for_review", "labeled", "unlabeled"): + self.assertIn(action, events["pull_request_target"]["types"]) job = verifier["jobs"]["verify"] self.assertNotIn("if", job) self.assertEqual(job["name"], "verify PR contents") @@ -104,6 +108,42 @@ def test_dependabot_runs_the_same_trusted_pr_verifier(self): if step.get("name") == "Verifier action") self.assertIn("docker run --rm", verify_step["run"]) + def test_autofix_push_deterministically_redispatches_the_verifier(self): + # #16567: the autofix push landed after the last prow label and no + # label event followed for three days, so labeled/unlabeled alone has + # no producer. workflow_dispatch is one of the two events a + # github.token may trigger; the guard dispatches pr-verifier on the + # PR branch so the check lands on the pushed head (#17036). + verifier = workflow("pr-verifier.yml") + events = verifier.get("on", verifier.get(True)) + self.assertIn("pr_number", events["workflow_dispatch"]["inputs"]) + self.assertTrue(events["workflow_dispatch"]["inputs"]["pr_number"]["required"]) + self.assertIn("inputs.pr_number", verifier["concurrency"]["group"]) + steps = verifier["jobs"]["verify"]["steps"] + hydrate = next(s for s in steps if s.get("id") == "dispatch_event") + self.assertEqual(hydrate["if"], "github.event_name == 'workflow_dispatch'") + # Never verify a fork's PR or a commit that is no longer the PR head. + self.assertIn("only same-repo PRs may be dispatched", hydrate["run"]) + self.assertIn('echo "stale=true"', hydrate["run"]) + verify_step = next(s for s in steps if s.get("id") == "verifier") + self.assertIn("steps.dispatch_event.outputs.stale != 'true'", verify_step["if"]) + self.assertIn("steps.dispatch_event.outputs.event_path", verify_step["env"]["VERIFIER_EVENT_PATH"]) + self.assertIn('-v "$VERIFIER_EVENT_PATH:/github/event.json:ro"', verify_step["run"]) + + guard = workflow("changelog-fragment-guard.yml") + self.assertEqual(guard["permissions"]["actions"], "write") + gsteps = guard["jobs"]["changelog-fragment-guard"]["steps"] + autofix = next(s for s in gsteps if s.get("id") == "autofix") + dispatch = next(s for s in gsteps if "Dispatch pr-verifier" in s.get("name", "")) + self.assertLess(gsteps.index(autofix), gsteps.index(dispatch)) + self.assertIn("steps.autofix.outputs.pushed == 'true'", dispatch["if"]) + # A PAT push already fires pull_request_target: dispatch only on the + # github.token path, and dispatch the PR branch so the check lands on + # the pushed head. + self.assertEqual(dispatch["env"]["HAS_PUSH_TOKEN"], "${{ secrets.TOPUP_PUSH_TOKEN != '' }}") + self.assertIn('[ "$HAS_PUSH_TOKEN" = "true" ]', dispatch["run"]) + self.assertIn('gh workflow run pr-verifier.yml --repo "$GITHUB_REPOSITORY" --ref "$HEAD_REF" -f "pr_number=${PR_NUMBER}"', dispatch["run"]) + def test_gosec_concurrency_fits_the_runner_cpu_limit(self): # gosec defaults -concurrency to runtime.NumCPU(), which inside a # runner pod is the NODE's vCPU count, not the pod's cgroup limit. diff --git a/.github/workflows/changelog-fragment-guard.yml b/.github/workflows/changelog-fragment-guard.yml index 2bb7522185..3b5319af7e 100644 --- a/.github/workflows/changelog-fragment-guard.yml +++ b/.github/workflows/changelog-fragment-guard.yml @@ -58,8 +58,12 @@ permissions: # same-repo PR branches; pull-requests: write posts the sticky guidance # comment. Both are read-only on fork PRs and those steps are skipped there. # No PR code is executed in this workflow — only git, grep and echo run. + # actions: write lets the autofix dispatch pr-verifier.yml on the head it + # pushed with github.token, the one event shape that token may fire + # (#17036). contents: write pull-requests: write + actions: write # Only a new push may cancel a running guard: the superseded run is on the # old head SHA, where nothing merges anyway. Label events must NOT cancel — @@ -117,11 +121,16 @@ jobs: # Full history so the merge base with the target branch exists # locally and the three-dot diff below is computable. fetch-depth: 0 - # The autofix push uses TOPUP_PUSH_TOKEN when set (a push made with - # that PAT re-runs the PR checks, so the guard goes green on its - # own); with GITHUB_TOKEN the push lands but does not trigger new - # runs — the next push or a maintainer re-run picks it up. Same - # optional-PAT convention as cli-pin-bump.yml / v5-topup.yml. + # The autofix push uses TOPUP_PUSH_TOKEN when set: a push made with + # that PAT fires `pull_request` AND `pull_request_target` runs, so + # every check re-reports on the new head. With github.token the + # push lands and fires the `pull_request` runs, but NO + # `pull_request_target` run (observed on #16567's b9cd4bac: eleven + # pull_request runs with actor=github-actions[bot], no PR Verifier). + # Dependabot-triggered runs see no Actions secrets, so Dependabot PRs + # (#16567) always take the github.token path; the dispatch step + # after the push re-runs pr-verifier for them. Same optional-PAT + # convention as cli-pin-bump.yml / v5-topup.yml / notice-autofix.yml. token: ${{ secrets.TOPUP_PUSH_TOKEN != '' && secrets.TOPUP_PUSH_TOKEN || github.token }} - name: Code changes must carry a changelog.d fragment (or no-changelog) @@ -240,6 +249,9 @@ jobs: Signed-off-by: ${PR_AUTHOR_LOGIN} <${author_email}>" # --force-with-lease against the SHA this run evaluated: never # clobber a push the author made while this job ran. + # When this push was made with github.token it fires no + # `pull_request_target` run, so pr-verifier (a required context) + # would stay "expected"; the step below re-triggers it (#17036). if git push --force-with-lease="refs/heads/${HEAD_REF}:${HEAD_SHA}" origin "HEAD:refs/heads/${HEAD_REF}"; then echo "pushed=true" >> "$GITHUB_OUTPUT" echo "fragment=$f" >> "$GITHUB_OUTPUT" @@ -247,6 +259,38 @@ jobs: echo "::warning::could not push the generated fragment (branch moved or token lacks push); leaving guidance comment instead" fi + # Deterministically re-run pr-verifier on the head just pushed (#17036). + # On #16567 the autofix push landed 47s after the last prow label and + # nothing relabelled the PR for three days, so pr-verifier's new + # labeled/unlabeled types alone had no producer. When the push used + # TOPUP_PUSH_TOKEN it already fired `pull_request_target` and nothing + # more is needed. When it used github.token, the only events that token + # may trigger are workflow_dispatch and repository_dispatch, so dispatch + # pr-verifier.yml on the PR branch: the run's check lands on the branch + # tip, which is the head just pushed. The autofix only pushes to + # same-repo branches, so this never dispatches a fork's workflow file. + - name: Dispatch pr-verifier on the autofix head (github.token push) + if: ${{ steps.autofix.outputs.pushed == 'true' }} + env: + GH_TOKEN: ${{ github.token }} + HAS_PUSH_TOKEN: ${{ secrets.TOPUP_PUSH_TOKEN != '' }} + PR_NUMBER: ${{ github.event.pull_request.number }} + HEAD_REF: ${{ github.event.pull_request.head.ref }} + run: | + set -euo pipefail + if [ "$HAS_PUSH_TOKEN" = "true" ]; then + echo "Pushed with TOPUP_PUSH_TOKEN — the push itself fired pull_request_target; nothing to dispatch." + exit 0 + fi + if gh workflow run pr-verifier.yml --repo "$GITHUB_REPOSITORY" --ref "$HEAD_REF" -f "pr_number=${PR_NUMBER}"; then + echo "dispatched pr-verifier.yml on ${HEAD_REF} for #${PR_NUMBER}" + else + # Most likely the branch's pr-verifier.yml predates the + # workflow_dispatch trigger (HTTP 422): a rebase onto the base + # branch fixes it, and the rebase push fires everything anyway. + echo "::warning::could not dispatch pr-verifier.yml on ${HEAD_REF}; the required 'verify PR contents' context stays expected on the new head until a push, a non-github.token label flip, or a close/reopen (#17036). Rebasing onto the base branch resolves it." + fi + # Whatever the autofix did, tell the author once (one sticky comment, # updated in place) and remove the comment when the check passes. - name: Post or update guidance comment diff --git a/.github/workflows/pr-verifier.yml b/.github/workflows/pr-verifier.yml index 4bc1fc4c2a..893dcb6979 100644 --- a/.github/workflows/pr-verifier.yml +++ b/.github/workflows/pr-verifier.yml @@ -1,11 +1,37 @@ name: PR Verifier +# REQUIRED CONTEXT on v5: `verify PR contents` must report on the final head +# of every PR. On #16567 changelog-fragment-guard's autofix pushed b9cd4bac +# with github.token (a Dependabot-triggered `pull_request` run sees no Actions +# secrets, so TOPUP_PUSH_TOKEN was empty; every run on that head reports +# actor=github-actions[bot]). That push fired the `pull_request` workflows +# but NOT this `pull_request_target` one, no label event followed for three +# days, and the context stayed "expected" until a human closed and reopened +# the PR — BLOCKED the whole time (#16567, #17036). A PAT or App-token push +# fires both event kinds, so the gap is specific to github.token pushes. +# Two things close it: +# - `labeled`/`unlabeled` (and `ready_for_review`) are listed, mirroring +# native-queue-policy.yml, so any App- or PAT-made label flip re-runs the +# verifier on the current head (github.token label flips fire nothing). +# - `workflow_dispatch`: changelog-fragment-guard dispatches this workflow +# on the PR branch right after a github.token autofix push. +# workflow_dispatch is one of the two events a github.token may trigger, +# and the run's check lands on the branch tip, i.e. the pushed head. Only +# same-repo branches are ever dispatched (the autofix never pushes to +# forks); such a run uses the GitHub-hosted ARM runner like fork PRs do. +# The concurrency group below bounds the cost. on: pull_request_target: - types: [opened, edited, synchronize, reopened] + types: [opened, edited, synchronize, reopened, ready_for_review, labeled, unlabeled] + workflow_dispatch: + inputs: + pr_number: + description: Number of the same-repo PR whose branch tip this run verifies (#17036) + required: true + type: string concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + group: ${{ github.workflow }}-${{ github.event.pull_request.number || inputs.pr_number || github.ref }} cancel-in-progress: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }} permissions: @@ -23,6 +49,39 @@ jobs: env: PR_VERIFIER_IMAGE: ghcr.io/hivecommons/pr-verifier@sha256:8c2b63817c4bfa7de510ab699b154637f27a42f87826c98083903daf2a539943 steps: + # A dispatched run has no pull_request payload. Rebuild one from the API + # for the verifier image, and bail out (green, nothing verified, said so) + # when the PR head is no longer this ref's tip: whoever moved it fired + # the normal events, and a verdict on a non-head commit is noise. + - name: Reconstruct the pull_request payload for a dispatched run + if: github.event_name == 'workflow_dispatch' + id: dispatch_event + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: ${{ inputs.pr_number }} + run: | + set -euo pipefail + if ! [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then + echo "::error::pr_number must be a PR number, got '$PR_NUMBER'" + exit 1 + fi + pr="$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}")" + head_repo="$(jq -r '.head.repo.full_name' <<<"$pr")" + head_sha="$(jq -r '.head.sha' <<<"$pr")" + if [ "$head_repo" != "$GITHUB_REPOSITORY" ]; then + echo "::error::#${PR_NUMBER} comes from ${head_repo}; only same-repo PRs may be dispatched" + exit 1 + fi + if [ "$head_sha" != "$GITHUB_SHA" ]; then + echo "::notice::#${PR_NUMBER} head is ${head_sha}, not this run's ${GITHUB_SHA}; the push that moved it fired pr-verifier itself — nothing verified here (#17036)." + echo "stale=true" >> "$GITHUB_OUTPUT" + exit 0 + fi + repo="$(gh api "repos/${GITHUB_REPOSITORY}")" + jq -n --argjson pr "$pr" --argjson repo "$repo" \ + '{action: "synchronize", number: $pr.number, pull_request: $pr, repository: $repo}' \ + > "${RUNNER_TEMP}/pr-verifier-event.json" + echo "event_path=${RUNNER_TEMP}/pr-verifier-event.json" >> "$GITHUB_OUTPUT" # Login and pull are advisory for the same reason the verification below # is: this check exists to report on PR CONTENTS, and a registry-side # failure (org rename, package visibility change, GHCR outage — #6085 @@ -59,21 +118,26 @@ jobs: run: | echo "::warning::PR Verifier image $PR_VERIFIER_IMAGE was unavailable or incompatible — verification was SKIPPED, not passed. Inspect the pull/platform error; this is a runner/registry/image problem, not a PR problem (#6085)." - name: Verifier action - if: steps.pull.outcome == 'success' + if: steps.pull.outcome == 'success' && steps.dispatch_event.outputs.stale != 'true' id: verifier continue-on-error: true + env: + # A dispatched run presents itself to the image as the + # pull_request_target run it stands in for (see dispatch_event). + VERIFIER_EVENT_NAME: ${{ github.event_name == 'workflow_dispatch' && 'pull_request_target' || github.event_name }} + VERIFIER_EVENT_PATH: ${{ steps.dispatch_event.outputs.event_path || github.event_path }} run: | docker run --rm \ -e INPUT_GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}" \ -e GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}" \ -e GITHUB_ACTIONS="true" \ - -e GITHUB_EVENT_NAME="$GITHUB_EVENT_NAME" \ + -e GITHUB_EVENT_NAME="$VERIFIER_EVENT_NAME" \ -e GITHUB_EVENT_PATH="/github/event.json" \ -e GITHUB_REPOSITORY="$GITHUB_REPOSITORY" \ -e GITHUB_SHA="$GITHUB_SHA" \ -e GITHUB_REF="$GITHUB_REF" \ -e GITHUB_WORKSPACE="/github/workspace" \ - -v "$GITHUB_EVENT_PATH:/github/event.json:ro" \ + -v "$VERIFIER_EVENT_PATH:/github/event.json:ro" \ -v "$GITHUB_WORKSPACE:/github/workspace:ro" \ "$PR_VERIFIER_IMAGE" - name: Report verifier failure