From 5363a3f49757b4cfc60c86c8970bcc6bffeb5731 Mon Sep 17 00:00:00 2001 From: Nikolai Emil Damm Date: Sat, 14 Nov 2026 19:52:06 +0100 Subject: [PATCH] refactor(ci): replace recheck-open-prs with the merge queue The merge queue now runs the required checks on the exact result each PR would land as, which is what recheck-open-prs approximated by re-running every open PR after main moved. Removes the workflow, its script, three tests, the recovery guide and the proposal workflow's dispatch of it. Closes #704 Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yaml | 28 - .../propose-marketplace-release.yaml | 18 - .github/workflows/recheck-open-prs.yaml | 126 ---- README.md | 4 +- docs/marketplace-releases.md | 4 +- docs/pr-recheck-recovery.md | 14 - docs/scripts.md | 1 - docs/tool-ownership.md | 1 - docs/validation.md | 60 +- .../ci-script-test-sharding-workflow.test.sh | 4 - scripts/marketplace-proposal-workflow.test.sh | 36 +- scripts/recheck-open-prs-base-refresh.test.sh | 234 ------- scripts/recheck-open-prs-observations.test.sh | 207 ------ scripts/recheck-open-prs.sh | 658 ------------------ scripts/recheck-open-prs.test.sh | 594 ---------------- 15 files changed, 23 insertions(+), 1966 deletions(-) delete mode 100644 .github/workflows/recheck-open-prs.yaml delete mode 100644 docs/pr-recheck-recovery.md delete mode 100644 scripts/recheck-open-prs-base-refresh.test.sh delete mode 100644 scripts/recheck-open-prs-observations.test.sh delete mode 100755 scripts/recheck-open-prs.sh delete mode 100755 scripts/recheck-open-prs.test.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 89d3cd56..9c3d26da 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -150,34 +150,6 @@ jobs: - name: ๐Ÿงช Guard complete script-test sharding run: bash scripts/ci-script-test-sharding-workflow.test.sh - - name: ๐Ÿงช Self-test the open-PR recheck - # Proves recheck-open-prs.sh re-triggers each open PR in the one order that is safe - # (close BEFORE reopen), never leaves a PR closed when a step fails mid-sequence, - # restores an armed auto-merge without arming one that was not, and fails closed on - # an unparseable listing rather than reading it as "no open PRs". Hermetic: `gh` is - # stubbed on PATH, so nothing reaches the network or touches a real pull request. - run: ./scripts/recheck-open-prs.test.sh - - - name: ๐Ÿงช Verify PR recheck observations and recovery - env: - RECHECK_OBSERVATION_ARTIFACT_DIR: ${{ runner.temp }}/recheck-observation-recovery - run: bash scripts/recheck-open-prs-observations.test.sh - - - name: ๐Ÿ“ฆ Verify hosted recovery artifact collection - if: ${{ always() }} - # Synthetic PR settings from the hermetic test exercise the same collection as the caller. - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: recheck-recovery-test-${{ github.run_id }}-${{ github.run_attempt }} - path: | - ${{ runner.temp }}/recheck-observation-recovery/**/rearm/** - ${{ runner.temp }}/recheck-observation-recovery/**/closed/** - if-no-files-found: error - retention-days: 14 - - - name: ๐Ÿงช Self-test head-checked PR base refreshes - run: bash scripts/recheck-open-prs-base-refresh.test.sh - - name: ๐Ÿงช Self-test changelog generation and publication checks run: bash scripts/plugin-changelog.test.sh diff --git a/.github/workflows/propose-marketplace-release.yaml b/.github/workflows/propose-marketplace-release.yaml index a1af2edf..805b94f1 100644 --- a/.github/workflows/propose-marketplace-release.yaml +++ b/.github/workflows/propose-marketplace-release.yaml @@ -101,21 +101,3 @@ jobs: jq . "$RUNNER_TEMP/proposal-created.json" printf 'status=%s\n' "$(jq -er .status "$RUNNER_TEMP/proposal-created.json")" >> "$GITHUB_OUTPUT" cat "$RUNNER_TEMP/proposal-created.json" >> "$GITHUB_STEP_SUMMARY" - - recheck: - name: Start normal required PR checks - needs: propose - if: >- - github.ref == 'refs/heads/main' && needs.propose.outputs.status == 'CREATED' && - ((github.event_name == 'workflow_dispatch' && inputs.propose == true) || - (github.event_name == 'schedule' && vars.MARKETPLACE_AUTOPROPOSE == 'true')) - runs-on: ubuntu-latest - # Dispatch the existing rechecker; this job cannot read or change repository contents or PRs. - permissions: - actions: write - steps: - - name: Dispatch the existing current-main CI recheck - env: - GH_TOKEN: ${{ github.token }} - GH_REPO: ${{ github.repository }} - run: gh workflow run recheck-open-prs.yaml --repo "$GH_REPO" --ref main -F dry-run=false diff --git a/.github/workflows/recheck-open-prs.yaml b/.github/workflows/recheck-open-prs.yaml deleted file mode 100644 index 2200bc9f..00000000 --- a/.github/workflows/recheck-open-prs.yaml +++ /dev/null @@ -1,126 +0,0 @@ -name: ๐Ÿ” Recheck open PRs - -# A pull-request workflow runs only on that PR's own events, so every PR already open when a new -# required gate lands on `main` keeps the green `CI - Required Checks` it earned BEFORE the gate -# existed โ€” and the branch rule keyed on that check name is satisfied by the stale run. Such a PR -# can merge without the new gate ever running against it, which is exactly what each gate was -# added to stop. -# -# GitHub's own mechanism for this is `strict_required_status_checks_policy` ("require branches to -# be up to date before merging"), but it is declared org-wide and Observe-only in -# devantler-tech/.github, so it is not this repository's to flip. This workflow is the -# repository-scoped equivalent: when `main` moves, ask every open PR to run again. See -# scripts/recheck-open-prs.sh for the head-checked base update and non-Dependabot reopen routes, -# their observed-event checks, and why they need an App token. Dependabot PRs are never closed. -# -# WHY EVERY PUSH, RATHER THAN A NARROWER TRIGGER -# Deciding "did a gate change?" was tried and cannot be made correct here. -# - A `paths:` filter is capped at 300 files, so a large sync can change `ci.yaml` without the -# filter seeing it โ€” the failure this workflow exists to prevent, hidden behind its trigger. -# - Diffing the pushed range instead loses a push that the concurrency group coalesced away: a -# gate change queued behind a running sweep is replaced by a later unrelated push, whose own -# range shows no gate change, and nothing ever sweeps for it. -# - Testing `ci.yaml` alone misses a gate STRENGTHENED in its implementation. `ci.yaml` runs -# `scripts/validate-manifests.sh` and friends; adding a rejection there changes what the -# required check accepts while `ci.yaml` itself is untouched. Enumerating every file that -# implements a gate is a list that goes stale silently. -# Every push it is. The cost is bounded and visible โ€” each merge re-runs the open PRs' checks โ€” -# and on this repository a merge already invalidates every open PR, since a plugin's version is -# its cache key and lives in files every plugin change touches. - -on: - push: - branches: [main] - workflow_dispatch: - inputs: - dry-run: - description: List the pull requests that would be re-triggered, without touching them - type: boolean - default: false - -concurrency: - # One recheck at a time: overlapping branch updates or fork reopen operations could race. - # `cancel-in-progress: false` protects the pass that is already running; a - # PENDING pass discarded by a third push loses nothing, because every pass sweeps every open - # PR unconditionally, so the newest one does strictly more than the one it displaced. - # - # A dry run gets its OWN group. Actions holds one running plus one pending per group, so a dry - # run sharing this key would displace a pending sweep โ€” and since it only lists, the pull - # requests that sweep would have re-triggered never receive a fresh event at all. A run that - # mutates nothing must not be able to cancel one that does. - group: recheck-open-prs${{ inputs.dry-run && '-dry-run' || '' }} - cancel-in-progress: false - -permissions: {} - -jobs: - recheck: - name: Re-trigger every open PR's required checks - runs-on: ubuntu-latest - permissions: - contents: read - steps: - - name: ๐Ÿ“„ Checkout - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - with: - persist-credentials: false - - # Two token steps rather than one with conditional scopes, because the two modes need - # different SETS of permissions and not merely different levels of the same ones. A dry run - # never reaches the workflow-runs endpoint, so granting it that scope would hand an - # advertised read-only mode a permission it cannot use; and the action takes a fixed list, - # so a scope cannot be dropped by an expression the way a level can be lowered by one. - # - # Both mint an App token rather than using GITHUB_TOKEN: events produced with that token do - # not start new workflow runs, so a reopen performed with it would be silent โ€” the same - # reason update-agent-skills.yaml mints one to open its PR. - - name: ๐Ÿ”‘ Generate GitHub App token (sweep) - id: app-token - if: ${{ !inputs.dry-run }} - # `contents: write` is required for same-repository base updates and for restoring a - # reopened PR's previously armed auto-merge request. The script never arms an unarmed request. - # - # `actions: read` is likewise load-bearing. Before restoring an armed auto-merge the script - # waits for a `pull_request` run from the reopen, and that read is what makes the wait safe - # โ€” without it the baseline request 403s, the pull request is left untouched with its stale - # green, and the gate this workflow applies is bypassed on exactly the pull requests that - # were about to merge themselves. - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.APP_CLIENT_ID }} - private-key: ${{ secrets.APP_PRIVATE_KEY }} - permission-contents: write - permission-pull-requests: write - permission-actions: read - - - name: ๐Ÿ”‘ Generate GitHub App token (dry run) - id: app-token-dry - if: ${{ inputs.dry-run }} - # Listing pull requests is all a dry run does, so those are the only scopes it gets. - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.APP_CLIENT_ID }} - private-key: ${{ secrets.APP_PRIVATE_KEY }} - permission-contents: read - permission-pull-requests: read - - - name: ๐Ÿ” Re-trigger open pull requests - env: - GH_TOKEN: ${{ steps.app-token.outputs.token || steps.app-token-dry.outputs.token }} - DRY_RUN: ${{ inputs.dry-run && '--dry-run' || '' }} - RECHECK_RECOVERY_ROOT: ${{ runner.temp }}/recheck-recovery - run: | - # shellcheck disable=SC2086 # DRY_RUN is a single optional flag or empty - ./scripts/recheck-open-prs.sh --repo "${GITHUB_REPOSITORY}" --base main $DRY_RUN - - - name: ๐Ÿ“ฆ Preserve unresolved PR recovery settings - if: ${{ always() }} - # Only saved PR settings are collected; never upload tokens, environment or inventories. - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: recheck-recovery-${{ github.run_id }}-${{ github.run_attempt }} - path: | - ${{ runner.temp }}/recheck-recovery/**/rearm/** - ${{ runner.temp }}/recheck-recovery/**/closed/** - if-no-files-found: ignore - retention-days: 14 diff --git a/README.md b/README.md index ae1c2c15..5cc207fa 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,7 @@ records which gaps to fill. Maintained first-party skills are authored in their plugins here; curated external skills retain their original authors and upstream update paths. To suggest a bundle or report a problem, [open an issue](https://github.com/devantler-tech/agent-plugins/issues). For a safe first repository change, start with [Contributing](CONTRIBUTING.md). -Maintainers can [prepare and publish a marketplace release](docs/marketplace-releases.md) with a reviewed version proposal, matching manifests, and verified release notes. Actions can explicitly create a signed draft containing only the generated marketplace versions and start normal PR CI. Review and merge remain separate gates. Pull-request CI checks that a version update exactly matches its generated proposal; publication separately requires exact successful main CI. Scheduled proposal creation and publication are independently opt-in. - -If a pull-request recheck fails, follow the [recovery guide](docs/pr-recheck-recovery.md) to retrieve its saved merge settings and verify the current PR before restoring them. +Maintainers can [prepare and publish a marketplace release](docs/marketplace-releases.md) with a reviewed version proposal, matching manifests, and verified release notes. Actions can explicitly create a signed draft containing only the generated marketplace versions; closing and reopening the draft starts its PR CI. Review and merge remain separate gates. Pull-request CI checks that a version update exactly matches its generated proposal; publication separately requires exact successful main CI. Scheduled proposal creation and publication are independently opt-in. The marketplace is under the [Apache 2.0 license](LICENSE). Bundled skills keep their own licenses, listed in the [plugin catalogue](docs/plugins.md#skill-licenses); third-party skills ship their copyright diff --git a/docs/marketplace-releases.md b/docs/marketplace-releases.md index e3c1d1a9..2a78e696 100644 --- a/docs/marketplace-releases.md +++ b/docs/marketplace-releases.md @@ -322,8 +322,8 @@ signature readback, a fresh fetch and exact release-tree verification precede dr Final readback binds the bot author, branch, source, commit, draft, title and plain-language body. Only `CREATED` with `proposal: DRAFT_READBACK_VERIFIED` reports success. -The final job dispatches the existing `recheck-open-prs.yaml` on main so the normal App-backed reopen -starts fresh required PR checks. That dispatch reports a request, not successful CI. The draft still +The draft is created with the workflow token, which starts no PR workflows, so close and reopen it +to start its required checks. The draft still requires current-head checks, substantive review, zero unresolved findings and normal promotion and merge. It is never automatically promoted or merged, and proposal creation never reserves a release tag or publishes a release. diff --git a/docs/pr-recheck-recovery.md b/docs/pr-recheck-recovery.md deleted file mode 100644 index 28d375af..00000000 --- a/docs/pr-recheck-recovery.md +++ /dev/null @@ -1,14 +0,0 @@ -# Recovering PR recheck settings - -The **Recheck open PRs** workflow refreshes required checks while preserving each pull request's merge settings. When it cannot verify recovery, it fails and uploads the saved settings as `recheck-recovery--`. The artifact remains available for 14 days and requires signed-in repository read access. It contains saved PR identity and merge settings, including the chosen commit message, rather than credentials, environment variables or the full PR inventory. - -Open the failed workflow run and download its recovery artifact, or use: - -```bash -gh run download RUN_ID --repo devantler-tech/agent-plugins \ - --name recheck-recovery-RUN_ID-ATTEMPT --dir recovery -``` - -Each `closed/` record identifies a PR whose reopen could not be verified. Each `rearm//before` record contains the original PR identity and auto-merge settings; the sibling files preserve the strategy and commit message exactly. Inspect the records as data, and compare their PR number, author, head and base with the current PR before changing it. Restore auto-merge only after the required checks have refreshed successfully at that current head. If the identity has moved, review the current state instead of applying the older settings. - -The local CLI keeps unresolved records in its temporary directory. Set `RECHECK_RECOVERY_ROOT` to an absolute directory when a caller needs to collect them after the process exits. Successful, verified recovery removes its records. The hosted workflow collects unresolved records even after a failed recheck step; a failed artifact upload remains an explicit workflow failure. diff --git a/docs/scripts.md b/docs/scripts.md index 00fb909f..dfebf8b0 100644 --- a/docs/scripts.md +++ b/docs/scripts.md @@ -12,7 +12,6 @@ complete inventory; read a script's header for its arguments and safeguards. | Synchronize one curated plugin | Dispatch [Update Agent Skills](../.github/workflows/update-agent-skills.yaml) with `agentic-engineering`; [skill-update-scope.sh](../scripts/skill-update-scope.sh) resolves only declared selections before the normal per-skill updater runs. Default and scheduled `all` updates still cover the full catalogue. | | Prepare or inspect a marketplace release | [Release guide](marketplace-releases.md) and [prepare-marketplace-release.sh](../scripts/prepare-marketplace-release.sh) | | Propose or publish a marketplace release | [Release guide](marketplace-releases.md), which explains the separate opt-in steps and recovery | -| Recheck open pull requests after changing CI | [recheck-open-prs.sh](../scripts/recheck-open-prs.sh) | The directory also contains checks for package discovery, skill provenance, licensing and authoring rules, version changes, changelogs, generated writes, release observations and instruction size. diff --git a/docs/tool-ownership.md b/docs/tool-ownership.md index 6d6418ff..5b361ee3 100644 --- a/docs/tool-ownership.md +++ b/docs/tool-ownership.md @@ -45,7 +45,6 @@ Their documented interfaces are supported here; none establishes an independent | [`guard-bundled-skill-edits.sh`](../scripts/guard-bundled-skill-edits.sh) | PR CI checks changed paths against base provenance and the authorized sync writer | Retain the source-ownership and writer rules here. Skill installation transport cannot decide this repository's editing exemption. | | [`guard-gh-json-fields.sh`](../scripts/guard-gh-json-fields.sh) | CI checks agent-readable guidance for unsupported GitHub CLI fields | Keep the guidance policy and content-bound exemptions here; delegate Go syntax decoding to the adjacent parser. | | [`gh-json-go/main.go`](../scripts/gh-json-go/main.go) | The guidance guard builds this installed decoder and passes retained Go source; CI also runs its tests | Retain Go for syntax/literal interpretation, using the standard library. It decodes source as data; it never builds the inspected package. | -| [`recheck-open-prs.sh`](../scripts/recheck-open-prs.sh) | The recheck workflow renews checks or refreshes a base under its exact-head and author rules | Keep forge orchestration with the owning workflow and GitHub CLI. Its branch-writing policy does not fit a cluster-management command; complexity alone does not establish a new CLI audience. | | [`plugin-changelog.sh`](../scripts/plugin-changelog.sh) | Skill update writes notes; CI checks notes for changed plugin versions | Keep version/changelog policy here, reuse the locked CommonMark decoder for heading syntax rather than duplicating that grammar. | | [`refresh-desired-state-digests.sh`](../scripts/refresh-desired-state-digests.sh) | Skill update recomputes declared resource digests; maintainers can check without writing | The writer must share hashing rules with the manifest validator; keep both under one repository owner. | | [`sha256.lib.sh`](../scripts/sha256.lib.sh) | The validator and digest writer source the same hashing functions | Keep local sharing. Two callers within the marketplace are one product need, not evidence for a separate hashing library. | diff --git a/docs/validation.md b/docs/validation.md index af3e9e8c..34b4e2f0 100644 --- a/docs/validation.md +++ b/docs/validation.md @@ -156,57 +156,9 @@ checks, the bundled-skill edit guard, and skill-spec validation. The exact depen [`ci.yaml`](../.github/workflows/ci.yaml); `actionlint` above is a local-only convenience, not a CI gate. Never weaken a check to pass โ€” fix the root cause. -**Adding a gate does not retroactively apply it to open PRs โ€” the recheck workflow is what does.** -A pull-request workflow runs only on that PR's own `pull_request` events, so every PR already open -when a new job joins `CI - Required Checks` keeps the green it earned *before* that job existed, and -the branch rule keyed on the check's name is satisfied by the stale run. Such a PR can merge without -the new gate ever running against it โ€” which is how a stale plugin version or a hand-edited synced -skill would reach consumers past the very checks added to stop them. -[`recheck-open-prs.yaml`](../.github/workflows/recheck-open-prs.yaml) closes that window: **every push -to `main`** re-triggers every open PR's checks, and it can also be dispatched by hand with a -`dry-run` input to see what a sweep would touch. So **when you add or alter a required job, the -recheck is the mechanism that makes it apply to work already in flight** โ€” there is nothing extra to -remember, but there is something to notice if it ever stops running. - -It sweeps unconditionally because deciding *whether* a gate changed cannot be made correct here, and -three narrower designs were tried and rejected: a `paths:` filter is capped at 300 files, so a large -sync can change `ci.yaml` without the filter seeing it; diffing the pushed range loses a push the -concurrency group coalesced away; and testing `ci.yaml` alone misses a gate strengthened in its -*implementation*, since that file runs `validate-manifests.sh` and friends and a new rejection there -changes what the required check accepts while `ci.yaml` is untouched. Each blind spot is silent, -which is worse than no trigger. **If you narrow this trigger, you are re-opening one of those three.** - -[`recheck-open-prs.sh`](../scripts/recheck-open-prs.sh) refreshes **same-repository branches** through -GitHub's update-branch API with the observed head as its concurrency guard. It verifies that the -result contains both the old head and current base, preserves auto-merge settings, and observes a -fresh PR workflow event. An updated head requires a new current-head review before merge; -existing commits are retained. Current non-Dependabot branches use the reopen route to ensure -App-created drafts receive their required PR CI event. A branch that conflicts with the base is -reported and skipped, not counted as a failure: GitHub will not merge it, and the push that -resolves the conflict starts a fresh PR run against the current gates. Only GitHub's settled -verdict (`mergeable: false`, `dirty`) for the head the update tried counts; an unknown verdict or a -moved head is still a failure. - -**Forks** use close and immediate reopen, which preserves their head and current-head review. -Every reopen requires a newer PR event and a matching OPEN readback of head, base, author and -merge state, including when auto-merge was unarmed. Unknown merge strategies refuse mutation; -restoring a known strategy also pins the observed head. The restored request is read back before -completion is counted. -**Dependabot PRs are never closed or recreated:** closing can suppress a wanted update, so a -Dependabot fork is reported as a failure and left untouched. Unknown author or repository-boundary -data, failed updates and incomplete readback likewise cannot report a successful refresh. -For a current Dependabot branch, the helper observes a PR workflow run at that head and verifies -the head includes the current named base. Missing event evidence is a failure, not a reason to close it. - -Re-running an old workflow replays its original merge revision; it does not apply a new gate. -These refresh routes use an App token because `GITHUB_TOKEN` events start no new workflow runs. -For the fork route, a previously armed auto-merge is restored with its original strategy and message -only after a fresh PR event is observable. An unarmed request is never armed, and missing event -evidence prevents restoration. The hermetic tests cover both routes, real commit ancestry, -Dependabot protection, recovery and incomplete observations. - -GitHub's own mechanism for this is `strict_required_status_checks_policy` โ€” "require branches to be up -to date before merging" โ€” which would block a stale PR outright rather than re-running it. It is -declared **org-wide and `Observe`-only** in `devantler-tech/.github`, so turning it on is a maintainer -decision affecting every repository, not this one's to make; the workflow above is the -repository-scoped equivalent. +**A new gate applies to open PRs through the merge queue.** `main` requires a merge queue (declared in +devantler-tech/.github `deploy/repository-rulesets/require-merge-queue-on-agent-plugins.yaml`). The queue +runs `CI - Required Checks` on the exact commit each queued PR would land as, so a PR opened before a +gate reached `main` still has to pass that gate before it merges. CI runs on `merge_group` for this; +PR-only steps such as the bundled-skill edit guard have already passed on the PR before it can join the +queue. Merge with `gh pr merge --match-head-commit ` (no `--squash`: the queue squashes). diff --git a/scripts/ci-script-test-sharding-workflow.test.sh b/scripts/ci-script-test-sharding-workflow.test.sh index 5b7713c2..a9dfcd6e 100755 --- a/scripts/ci-script-test-sharding-workflow.test.sh +++ b/scripts/ci-script-test-sharding-workflow.test.sh @@ -89,10 +89,6 @@ check_workflow() { 'bash scripts/propose-marketplace-release.test.sh' 'bash scripts/marketplace-proposal-workflow.test.sh' 'jq -e '\''.data.viewer.login == "github-actions[bot]"'\''' - './scripts/recheck-open-prs.test.sh' - 'bash scripts/recheck-open-prs-observations.test.sh' - 'uses: actions/upload-artifact@' - 'bash scripts/recheck-open-prs-base-refresh.test.sh' 'bash scripts/plugin-changelog.test.sh' 'bash scripts/plugin-changelog-boundaries.test.sh' 'bash scripts/plugin-changelog-release-boundaries.test.sh' diff --git a/scripts/marketplace-proposal-workflow.test.sh b/scripts/marketplace-proposal-workflow.test.sh index 6fb81ed2..56e75652 100644 --- a/scripts/marketplace-proposal-workflow.test.sh +++ b/scripts/marketplace-proposal-workflow.test.sh @@ -11,49 +11,45 @@ fail() { printf 'FAIL %s\n' "$*" >&2; exit 1; } guard() { awk -v wanted="$1" '/^ [a-z]+:/ {job=$1;sub(/:$/,"",job)} job==wanted && /^ if: >-$/ {reading=1;next} reading && /^ / {sub(/^ /,"");printf "%s ",$0;next} reading {exit}' "$workflow" } -assess=$(guard assess) propose=$(guard propose) recheck=$(guard recheck) -if [ -z "$assess" ] || [ -z "$propose" ] || [ -z "$recheck" ]; then fail 'missing explicit job guards'; fi +assess=$(guard assess) propose=$(guard propose) +if [ -z "$assess" ] || [ -z "$propose" ]; then fail 'missing explicit job guards'; fi # Evaluate the workflow's boolean guard against one synthetic event context. evaluate() { local expression expression=$(printf '%s' "$1" | sed -E "s/'/\"/g;s/&&/and/g;s/\|\|/or/g;s/(github|inputs|vars|needs)\./\$context.\1./g") jq -nr --argjson context "$2" "$expression" } -# Assert the three job admission results for one event, flag and status combination. +# Assert the two job admission results for one event, flag and status combination. check() { local event=$1 ref=$2 armed=$3 flag=$4 assessment=$5 creation=$6 expected=$7 context actual context=$(jq -nc --arg event "$event" --arg ref "$ref" --argjson armed "$armed" --arg flag "$flag" --arg assessment "$assessment" --arg creation "$creation" '{github:{event_name:$event,ref:$ref},inputs:{propose:$armed},vars:{MARKETPLACE_AUTOPROPOSE:$flag},needs:{assess:{outputs:{status:$assessment}},propose:{outputs:{status:$creation}}}}') - actual="$(evaluate "$assess" "$context") $(evaluate "$propose" "$context") $(evaluate "$recheck" "$context")" + actual="$(evaluate "$assess" "$context") $(evaluate "$propose" "$context")" [ "$actual" = "$expected" ] || fail "$event/$ref/$armed/$flag/$assessment/$creation: $actual" passed=$((passed+1)) } -check workflow_dispatch refs/heads/main false '' PREPARED '' 'true false false' -check workflow_dispatch refs/heads/main false true PREPARED '' 'true false false' -check workflow_dispatch refs/heads/main true '' PREPARED CREATED 'true true true' +check workflow_dispatch refs/heads/main false '' PREPARED '' 'true false' +check workflow_dispatch refs/heads/main false true PREPARED '' 'true false' +check workflow_dispatch refs/heads/main true '' PREPARED CREATED 'true true' for status in '' NO_CHANGE REFUSED prepared; do - check workflow_dispatch refs/heads/main true '' "$status" '' 'true false false' + check workflow_dispatch refs/heads/main true '' "$status" '' 'true false' done for flag in '' false TRUE 1 enabled; do - check schedule refs/heads/main false "$flag" PREPARED '' 'false false false' + check schedule refs/heads/main false "$flag" PREPARED '' 'false false' done -check schedule refs/heads/main false true PREPARED CREATED 'true true true' +check schedule refs/heads/main false true PREPARED CREATED 'true true' for status in '' NO_CHANGE REFUSED prepared; do - check schedule refs/heads/main false true "$status" '' 'true false false' -done -for status in '' PREPARED NO_CHANGE REFUSED created; do - check workflow_dispatch refs/heads/main true '' PREPARED "$status" 'true true false' + check schedule refs/heads/main false true "$status" '' 'true false' done for ref in refs/heads/feature refs/tags/v1.0.0; do - check workflow_dispatch "$ref" true true PREPARED CREATED 'false false false' + check workflow_dispatch "$ref" true true PREPARED CREATED 'false false' done for event in push pull_request pull_request_target workflow_run; do - check "$event" refs/heads/main true true PREPARED CREATED 'false false false' + check "$event" refs/heads/main true true PREPARED CREATED 'false false' done awk ' /^permissions: \{\}$/ {root++} /^ assess:/ {job="assess"} /^ propose:/ {job="propose"} - /^ recheck:/ {job="recheck"} /^ permissions:$/ {permissions=job;next} permissions && /^ (contents|actions|pull-requests):/ {p[permissions,$1]=$2;next} permissions && !/^ / {permissions=""} @@ -62,11 +58,9 @@ awk ' /^ persist-credentials: false$/ {credentials++} /^ default: false$/ {off++} /^ needs: assess$/ {create_needs++} - /^ needs: propose$/ {recheck_needs++} END {exit !(root==1 && p["assess","contents:"]=="read" && p["assess","actions:"]=="read" && p["assess","pull-requests:"]=="read" && p["propose","contents:"]=="write" && p["propose","pull-requests:"]=="write" && p["propose","actions:"]=="read" && - p["recheck","actions:"]=="write" && !p["recheck","contents:"] && !p["recheck","pull-requests:"] && - main==2 && history==2 && credentials==2 && off==1 && create_needs==1 && recheck_needs==1)} + main==2 && history==2 && credentials==2 && off==1 && create_needs==1)} ' "$workflow" || fail 'permission, dependency, checkout or default-off boundary' passed=$((passed+1)) concurrency=$(sed -n 's/^ group: marketplace-proposal-${{ \(.*\) }}$/\1/p' "$workflow") @@ -87,8 +81,6 @@ check_concurrency workflow_dispatch false true assessment check_concurrency workflow_dispatch false '' assessment check_concurrency schedule false true write for flag in '' false TRUE; do check_concurrency schedule false "$flag" assessment; done -# shellcheck disable=SC2016 # Match the literal variable reference used by the actual workflow. -grep -Fq 'gh workflow run recheck-open-prs.yaml --repo "$GH_REPO" --ref main -F dry-run=false' "$workflow" || fail 'CI must use the existing canonical recheck' ! grep -Eq 'workflow_run:|pull_request_target:|download-artifact|APP_PRIVATE_KEY|gh pr (merge|ready)' "$workflow" || fail 'unexpected authority or artifact consumer' passed=$((passed+1)) printf 'PASS marketplace proposal workflow (%s cases)\n' "$passed" diff --git a/scripts/recheck-open-prs-base-refresh.test.sh b/scripts/recheck-open-prs-base-refresh.test.sh deleted file mode 100644 index 11fb3cff..00000000 --- a/scripts/recheck-open-prs-base-refresh.test.sh +++ /dev/null @@ -1,234 +0,0 @@ -#!/usr/bin/env bash -# Exercise the production sweep against real local commit ancestry and a hermetic forge. -set -euo pipefail -unset GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_NAMESPACE -here=$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P) -work=$(mktemp -d) -trap 'rm -rf "$work"' EXIT -mkdir -p "$work/temp" -export TMPDIR="$work/temp" -export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=commit.gpgsign GIT_CONFIG_VALUE_0=false -git init -q "$work/repo" -git -C "$work/repo" config user.name Fixture -git -C "$work/repo" config user.email fixture@example.invalid -git -C "$work/repo" commit -qm initial --allow-empty -original=$(git -C "$work/repo" rev-parse HEAD) -git -C "$work/repo" checkout -qb base -printf 'gate\n' > "$work/repo/gate" -git -C "$work/repo" add gate -git -C "$work/repo" commit -qm gate -base=$(git -C "$work/repo" rev-parse HEAD) -git -C "$work/repo" checkout -qb candidate "$original" -printf 'adaptation\n' > "$work/repo/adaptation" -git -C "$work/repo" add adaptation -git -C "$work/repo" commit -qm adaptation -head=$(git -C "$work/repo" rev-parse HEAD) -git -C "$work/repo" merge -qm refresh base -updated=$(git -C "$work/repo" rev-parse HEAD) -git -C "$work/repo" checkout -qb later base -printf 'later gate\n' > "$work/repo/gate" -git -C "$work/repo" add gate -git -C "$work/repo" commit -qm later -later=$(git -C "$work/repo" rev-parse HEAD) -mkdir "$work/bin" -cat > "$work/bin/gh" <<'EOF' -#!/usr/bin/env bash -set -euo pipefail -printf '%s\n' "$*" >> "$FIXTURE/calls" -if [[ "$*" == *'/actions/'*'/runs'* ]]; then - if [[ "$MODE" == late-movement ]]; then printf '%s' "$LATER" > "$FIXTURE/current"; fi - if [[ "$MODE" == late-base ]]; then : > "$FIXTURE/later-base"; fi - sha="" - for arg in "$@"; do case "$arg" in head_sha=*) sha=${arg#head_sha=} ;; esac; done - if [ -f "$FIXTURE/reopened" ]; then id=92; else id=91; fi - [ "$MODE" != no-check ] || id=0 - jq -nc --arg sha "$sha" --argjson id "$id" \ - '[{total_count:(if $id==0 then 0 else 1 end),workflow_runs:(if $id==0 then [] else [{id:$id,event:"pull_request",head_sha:$sha,path:".github/workflows/ci.yaml",repository:{full_name:"owner/name"}}] end)}]' - exit -fi -if [[ "$*" == *'/branches/'* ]]; then - [[ "$MODE" != branch-read-fails ]] || exit 1 - if [[ "$MODE" == dropped-base && -f "$FIXTURE/current" ]] || [ -f "$FIXTURE/later-base" ]; then - printf '%s\n' "$LATER" - elif [[ "$MODE" == invalid-base ]]; then printf 'unknown\n' - else printf '%s\n' "$BASE"; fi - exit -fi -if [[ "$*" == *'/compare/'* ]]; then - [[ "$MODE" != compare-fails ]] || exit 1 - pair=${2##*/}; left=${pair%...*}; right=${pair#*...} - git -C "$FIXTURE/repo" cat-file -e "$left^{commit}" - git -C "$FIXTURE/repo" cat-file -e "$right^{commit}" - if [ "$left" = "$right" ]; then printf 'identical\n' - elif git -C "$FIXTURE/repo" merge-base --is-ancestor "$left" "$right"; then printf 'ahead\n' - elif git -C "$FIXTURE/repo" merge-base --is-ancestor "$right" "$left"; then printf 'behind\n' - else printf 'diverged\n'; fi - exit -fi -if [[ "$*" == *'/update-branch'* ]]; then - [[ "$*" == *'--method PUT'* && "$*" == *"expected_head_sha=$HEAD"* ]] || exit 3 - [[ "$MODE" != update-fails && "$MODE" != conflict* ]] || { [[ "$MODE" != conflict* ]] || echo "gh: merge conflict between base and head (HTTP 422)" >&2; exit 1; } - [[ "$MODE" != unchanged ]] || exit - printf '%s' "$UPDATED" > "$FIXTURE/current" - exit -fi -if [[ "$*" == *'repos/owner/name/pulls/11 '* ]]; then - case "$MODE" in - conflict) printf '%s\tfalse\tdirty\n' "$HEAD" ;; - conflict-computing) printf '%s\tnull\tunknown\n' "$HEAD" ;; - conflict-moved) printf '%s\tfalse\tdirty\n' "$LATER" ;; - conflict-read-fails) exit 1 ;; - *) printf '%s\ttrue\tbehind\n' "$HEAD" ;; - esac - exit -fi -if [[ "$*" == *'api --paginate'* ]]; then - jq -nc '[[{number:11,title:"wanted update",state:"open",base:{ref:"main",repo:{full_name:"owner/name"}}}]]' - exit -fi -if [[ "$*" == *'pr view'* ]]; then - [[ "$MODE" != unreadable-after-update || ! -f "$FIXTURE/current" ]] || exit 1 - current=$(cat "$FIXTURE/current" 2>/dev/null || printf '%s' "$HEAD") - [[ "$MODE" != missing-author ]] && author='app/dependabot' || author='' - if [[ "$MODE" == human || "$MODE" == current-human ]]; then author=devantler; fi - [[ "$MODE" != missing-boundary ]] && fork=false || fork=null - if [[ "$MODE" == dropped-adaptation && -f "$FIXTURE/current" ]]; then current=$BASE; fi - current_base=$BASE - if [[ "$MODE" == dropped-base && -f "$FIXTURE/current" ]]; then current_base=$LATER; fi - jq -n --arg head "$current" --arg base "$BASE" --arg author "$author" --argjson fork "$fork" \ - '{number:11,baseRefName:"main",state:"OPEN",headRefOid:$head,baseRefOid:$base,author:{login:$author},isCrossRepository:$fork, - autoMergeRequest:{mergeMethod:"SQUASH",commitHeadline:"original title",commitBody:"original body"}}' | - jq --arg mode "$MODE" --arg base "$current_base" --arg original "$ORIGINAL" --argjson after "$(test -f "$FIXTURE/current" && echo true || echo false)" \ - --argjson reopened "$(test -f "$FIXTURE/reopened" && echo true || echo false)" --argjson rearmed "$(test -f "$FIXTURE/rearmed" && echo true || echo false)" \ - 'if $mode=="missing-merge-state" then del(.autoMergeRequest) - elif $mode=="unarmed" then .autoMergeRequest=null - elif $mode=="missing-merge-after" then if $after then del(.autoMergeRequest) else .autoMergeRequest=null end - elif $mode=="changed-merge-state" and $after then .autoMergeRequest=null - elif $mode=="changed-boundary" and $after then .isCrossRepository=true - elif $mode=="changed-author" and $after then .author.login="another-author" - elif $mode=="wrong-pr" then .number=12 - elif $mode=="retargeted" then .baseRefName="different-base" - else . end | .baseRefOid=(if $mode=="stale-pr-base" then $original else $base end) - | if $reopened and ($rearmed | not) then .autoMergeRequest=null else . end' - exit -fi -case "$1 $2" in - 'pr close') : > "$FIXTURE/closed"; exit ;; - 'pr reopen') rm -f "$FIXTURE/closed"; : > "$FIXTURE/reopened"; exit ;; - 'pr merge') : > "$FIXTURE/rearmed"; exit ;; -esac -printf 'unexpected mutation\n' >&2 -exit 3 -EOF -chmod +x "$work/bin/gh" -export FIXTURE="$work" BASE="$base" HEAD="$head" UPDATED="$updated" LATER="$later" ORIGINAL="$original" -# Exercise branch refresh with real Git ancestry and verify the resulting PR state. -run_case() { - local mode=$1 expected=$2 rc=0 - export MODE="$mode" - rm -f "$work/current" "$work/later-base" - : > "$work/calls" - PATH="$work/bin:$PATH" RECHECK_CHECK_WAIT_SECONDS=1 RECHECK_CHECK_POLL_SECONDS=1 \ - bash "$here/recheck-open-prs.sh" --repo owner/name > "$work/output" 2>&1 || rc=$? - [ "$rc" -eq "$expected" ] || { cat "$work/output"; echo "FAIL $mode: exit $rc"; exit 1; } - ! grep -Eq '^pr (close|reopen|merge)' "$work/calls" || { echo "FAIL $mode: changed open/merge state"; exit 1; } - if [ "$expected" -eq 0 ]; then - grep -q '/update-branch' "$work/calls" - grep -q '/actions/workflows/ci.yaml/runs' "$work/calls" - test "$(git -C "$work/repo" show "$UPDATED:adaptation")" = adaptation - else - if grep -q '1 of 1 current-base refreshes completed' "$work/output"; then - echo "FAIL $mode: falsely reported completion"; exit 1 - fi - fi - echo "PASS same-repository $mode" -} -run_case normal 0 -run_case human 0 -run_case unarmed 0 -run_case stale-pr-base 0 -run_case update-fails 1 -run_case unchanged 1 -run_case unreadable-after-update 1 -run_case missing-merge-after 1 -run_case dropped-adaptation 1 -run_case dropped-base 1 -run_case no-check 1 -run_case compare-fails 1 -run_case branch-read-fails 1 -run_case invalid-base 1 -run_case missing-author 1 -run_case missing-boundary 1 -run_case missing-merge-state 1 -run_case changed-merge-state 1 -run_case changed-boundary 1 -run_case changed-author 1 -run_case wrong-pr 1 -run_case retargeted 1 -run_case late-movement 1 -run_case late-base 1 -# A conflicting branch cannot merge, and the push that resolves it re-runs every check, so only -# GitHub's settled conflict verdict for the head this run tried is reported rather than failed. -for mode in conflict conflict-computing conflict-moved conflict-read-fails; do - export MODE="$mode" - rm -f "$work/current" "$work/later-base" - : > "$work/calls" - rc=0 - PATH="$work/bin:$PATH" RECHECK_CHECK_WAIT_SECONDS=1 RECHECK_CHECK_POLL_SECONDS=1 \ - bash "$here/recheck-open-prs.sh" --repo owner/name > "$work/output" 2>&1 || rc=$? - expected=1 - [ "$mode" != conflict ] || expected=0 - if [ "$rc" -ne "$expected" ] || grep -Eq '^pr (close|reopen|merge)' "$work/calls" \ - || grep -q '/actions/workflows/ci.yaml/runs' "$work/calls"; then - cat "$work/output"; echo "FAIL $mode: exit $rc"; exit 1 - fi - if [ "$mode" = conflict ]; then - grep -q '^ skipped #11 โ€” it conflicts with main' "$work/output" - grep -q '::notice::1 pull request(s) conflict with main' "$work/output" - ! grep -q '::error::' "$work/output" || { cat "$work/output"; echo 'FAIL conflict: reported an error'; exit 1; } - else - grep -q '::error::#11 head-checked base update failed' "$work/output" - ! grep -q 'skipped #11' "$work/output" || { echo "FAIL $mode: skipped without a settled conflict"; exit 1; } - fi - echo "PASS same-repository $mode" -done -# A current Dependabot branch stays open, but its observed run certifies only that exact head/base. -export HEAD="$updated" -for mode in normal no-check late-movement late-base; do - export MODE="$mode" - rm -f "$work/current" "$work/later-base" - : > "$work/calls" - rc=0 - PATH="$work/bin:$PATH" RECHECK_CHECK_WAIT_SECONDS=1 RECHECK_CHECK_POLL_SECONDS=1 \ - bash "$here/recheck-open-prs.sh" --repo owner/name > "$work/output" 2>&1 || rc=$? - expected=1 - [ "$mode" != normal ] || expected=0 - if [ "$rc" -ne "$expected" ] || grep -Eq '/update-branch|^pr (close|reopen|merge)' "$work/calls"; then - cat "$work/output"; echo "FAIL current Dependabot $mode: exit $rc"; exit 1 - fi - echo "PASS current Dependabot $mode" -done -# An existing run cannot certify a gate added later: the same head no longer contains the base. -export MODE=unchanged BASE="$later" -rm -f "$work/current" "$work/later-base" -: > "$work/calls" -rc=0 -PATH="$work/bin:$PATH" RECHECK_CHECK_WAIT_SECONDS=1 RECHECK_CHECK_POLL_SECONDS=1 \ - bash "$here/recheck-open-prs.sh" --repo owner/name > "$work/output" 2>&1 || rc=$? -test "$rc" -eq 1 -grep -q '/update-branch' "$work/calls" -if grep -Eq '^pr (close|reopen|merge)' "$work/calls"; then - echo 'FAIL advanced gate: closed a wanted update'; exit 1 -fi -echo 'PASS existing run cannot certify an advanced base at the unchanged head' -export BASE="$base" -# Native proposal drafts are created with GITHUB_TOKEN and need the App's reopened event. -export MODE=current-human -rm -f "$work/reopened" "$work/rearmed" "$work/current" "$work/later-base" -: > "$work/calls" -PATH="$work/bin:$PATH" RECHECK_CHECK_WAIT_SECONDS=1 RECHECK_CHECK_POLL_SECONDS=1 \ - bash "$here/recheck-open-prs.sh" --repo owner/name > "$work/output" 2>&1 -test -f "$work/reopened" -test -f "$work/rearmed" -if grep -q '/update-branch' "$work/calls"; then echo 'FAIL current draft: unnecessary head movement'; exit 1; fi -echo 'PASS current non-Dependabot draft still receives a fresh reopen event' diff --git a/scripts/recheck-open-prs-observations.test.sh b/scripts/recheck-open-prs-observations.test.sh deleted file mode 100644 index 509cc6b1..00000000 --- a/scripts/recheck-open-prs-observations.test.sh +++ /dev/null @@ -1,207 +0,0 @@ -#!/usr/bin/env bash -# Exercise real recheck control flow; the only forge is an offline stateful gh. -set -uo pipefail -HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) -SCRIPT=${RECHECK_SCRIPT:-$HERE/recheck-open-prs.sh} -WORK=$(mktemp -d) -trap 'rm -rf "$WORK"' EXIT -fail=0 -# Create an isolated, stateful forge fixture for one observation or recovery case. -make_case() { - local dir=$1 mode=$2 - mkdir -p "$dir/bin" "$dir/db" "$dir/temp" - printf '%s' "$mode" > "$dir/mode" - jq -nc '[{number:11,title:"first",state:"open",base:{ref:"main",repo:{full_name:"owner/name"}}}]' > "$dir/list.json" - jq -nc '{number:11,state:"OPEN",baseRefName:"main",headRefOid:("1"*40),baseRefOid:("2"*40),author:{login:"devantler"},isCrossRepository:true,autoMergeRequest:{mergeMethod:"SQUASH",commitHeadline:"Chosen subject",commitBody:"Chosen body"}}' > "$dir/db/pr.json" - cat > "$dir/bin/gh" <<'STUB' -#!/usr/bin/env bash -set -euo pipefail -dir=${RECHECK_FIXTURE:?} -mode=$(cat "$dir/mode") -printf '%s\n' "${GH_HOST:-unset} $*" >> "$dir/calls" -filter=""; previous=""; slurp=false; paginate=false -for arg in "$@"; do - [ "$previous" != --jq ] || filter=$arg - [ "$arg" != --slurp ] || slurp=true - [ "$arg" != --paginate ] || paginate=true - previous=$arg -done -if "$slurp" && ! "$paginate"; then exit 2; fi -# Return the same raw or projected response shape requested from the fixture. -emit() { - if [ -n "$filter" ]; then jq -r "$filter"; elif "$slurp"; then jq -s .; else cat; fi -} -if [ "$1" = api ]; then - case "$*" in - *actions/*runs*) - if [ "$mode" = paginated-runs ]; then - total=101; [ ! -f "$dir/db/reopened" ] || total=102 - jq -nc --argjson total "$total" \ - '[range(1;$total+1)|{id:.,event:"pull_request",head_sha:("1"*40),path:".github/workflows/ci.yaml",repository:{full_name:"owner/name"}}] as $runs | - range(0;($total/100|ceil)) as $page | {total_count:$total,workflow_runs:$runs[$page*100:($page+1)*100]}' | emit - exit 0 - fi - fresh=false; [ ! -f "$dir/db/reopened" ] || fresh=true - count=0; id=10; path=.github/workflows/ci.yaml; head=$(printf '%040d' 0 | tr 0 1) - if "$fresh"; then count=1; id=20; fi - if "$fresh" && [ "$mode" = wrong-workflow ]; then path=.github/workflows/other.yaml; fi - if "$fresh" && [ "$mode" = workflow-ref ]; then path=.github/workflows/ci.yaml@refs/pull/11/merge; fi - if "$fresh" && [ "$mode" = wrong-workflow-ref ]; then path=.github/workflows/other.yaml@main; fi - if "$fresh" && [ "$mode" = empty-workflow-ref ]; then path=.github/workflows/ci.yaml@; fi - if "$fresh" && [ "$mode" = newline-workflow ]; then path=$'.github/workflows/ci.yaml\n'; fi - if "$fresh" && [ "$mode" = newline-workflow-ref ]; then path=$'.github/workflows/ci.yaml@main\n'; fi - if "$fresh" && [ "$mode" = wrong-head ]; then head=$(printf '%040d' 0 | tr 0 3); fi - if "$fresh" && [ "$mode" = incomplete-runs ]; then count=2; fi - if "$fresh" && [ "$mode" = fractional-count ]; then count=1.000000000000000001; fi - if "$fresh"; then - jq -nc --arg path "$path" --arg head "$head" --argjson id "$id" --argjson count "$count" \ - '{total_count:$count,workflow_runs:[{id:$id,event:"pull_request",head_sha:$head,path:$path,repository:{full_name:"owner/name"}}]}' | emit - else jq -nc '{total_count:0,workflow_runs:[]}' | emit; fi - exit 0 ;; - *branches/*) jq -nc '{commit:{sha:("2"*40)}}' | emit; exit 0 ;; - *pulls*) cat "$dir/list.json" | emit; exit 0 ;; - esac -fi -if [ "$1" = pr ]; then - case "$2" in - view) cat "$dir/db/pr.json" | emit ;; - close) - printf '%s\n' close >> "$dir/mutations" - jq '.state="CLOSED" | .autoMergeRequest=null' "$dir/db/pr.json" > "$dir/db/next" - mv "$dir/db/next" "$dir/db/pr.json" ;; - reopen) - printf '%s\n' reopen >> "$dir/mutations" - if [ "$mode" = recovery-noop ]; then - if [ ! -f "$dir/db/reopen-attempted" ]; then : > "$dir/db/reopen-attempted"; exit 1; fi - exit 0 - fi - jq '.state="OPEN"' "$dir/db/pr.json" > "$dir/db/next" - mv "$dir/db/next" "$dir/db/pr.json" - : > "$dir/db/reopened" ;; - merge) - printf '%s\n' merge >> "$dir/mutations" - subject=""; body=""; method=SQUASH; prev="" - for arg in "$@"; do - case "$prev" in --subject) subject=$arg ;; --body) body=$arg ;; --body-file) body=$(cat "$arg"; printf x); body=${body%x} ;; esac - case "$arg" in --merge) method=MERGE ;; --rebase) method=REBASE ;; esac - prev=$arg - done - jq --arg subject "$subject" --arg body "$body" --arg method "$method" \ - '.autoMergeRequest={mergeMethod:$method,commitHeadline:$subject,commitBody:$body}' "$dir/db/pr.json" > "$dir/db/next" - mv "$dir/db/next" "$dir/db/pr.json" ;; - *) exit 2 ;; - esac - exit 0 -fi -exit 2 -STUB - chmod +x "$dir/bin/gh" -} -# Run the production entrypoint with fixture-only dependencies and capture its status. -run_case() { - local dir=$1 - shift - rc=0 - out=$(env PATH="$dir/bin:$PATH" TMPDIR="$dir/temp" RECHECK_FIXTURE="$dir" GH_HOST=enterprise.invalid \ - RECHECK_REAL_MKDIR="$(command -v mkdir)" RECHECK_CHECK_WAIT_SECONDS=1 RECHECK_CHECK_POLL_SECONDS=1 \ - bash "$SCRIPT" --repo owner/name "$@" 2>&1) || rc=$? -} -# Record each behavioral assertion without stopping later independent cases. -check() { - local name=$1 - shift - if "$@"; then printf 'PASS %s\n' "$name"; else printf 'FAIL %s (exit=%s)\n%s\n' "$name" "$rc" "$out"; fail=$((fail+1)); fi -} -# Prove an invalid observation caused no forge mutations. -no_mutations() { [ ! -s "$1/mutations" ]; } -# Prove every request used this automation's designated forge. -github_host_only() { ! grep -v '^github.com ' "$1/calls"; } -# Prove an uncertain run observation refused to restore auto-merge. -held_without_rearm() { [ "$rc" -ne 0 ] && ! jq -e '.autoMergeRequest!=null' "$1/db/pr.json" >/dev/null; } - -d="$WORK/imprecise-jq"; make_case "$d" good -cat > "$d/bin/jq" <<'STUB' -#!/usr/bin/env bash -# Model a valid jq executable whose numeric parser rounds fractional counts. -case "$*" in *1.000000000000000001*) printf 'false\n'; exit 1 ;; esac -exec "$RECHECK_REAL_JQ" "$@" -STUB -chmod +x "$d/bin/jq" -RECHECK_REAL_JQ=$(command -v jq); export RECHECK_REAL_JQ -run_case "$d" --dry-run -check 'imprecise jq is rejected before any forge request' test "$rc" -eq 2 -check 'imprecise jq cannot read or mutate any PR' test ! -e "$d/calls" - -d="$WORK/host"; make_case "$d" good; run_case "$d" --dry-run -check 'github.com is the explicit forge for every request' github_host_only "$d" - -d="$WORK/inventory"; make_case "$d" good -jq '. + [{number:"invalid",title:"later",state:"open",base:{ref:"main",repo:{full_name:"owner/name"}}}]' "$d/list.json" > "$d/next" -mv "$d/next" "$d/list.json"; run_case "$d" -check 'malformed later PR prevents all earlier PR mutations' no_mutations "$d" - -d="$WORK/duplicate"; make_case "$d" good -jq '. + .' "$d/list.json" > "$d/next"; mv "$d/next" "$d/list.json"; run_case "$d" -check 'duplicate PR identities prevent all mutations' no_mutations "$d" - -for mode in wrong-repository wrong-base nonpositive-number; do - d="$WORK/$mode"; make_case "$d" good - case "$mode" in - wrong-repository) filter='.[0].base.repo.full_name="other/name"' ;; - wrong-base) filter='.[0].base.ref="other"' ;; - *) filter='.[0].number=0' ;; - esac - jq "$filter" "$d/list.json" > "$d/next"; mv "$d/next" "$d/list.json"; run_case "$d" - check "$mode inventory prevents all PR mutations" no_mutations "$d" -done - -for mode in wrong-workflow wrong-workflow-ref empty-workflow-ref newline-workflow newline-workflow-ref wrong-head incomplete-runs fractional-count; do - d="$WORK/$mode"; make_case "$d" "$mode"; run_case "$d" - check "$mode cannot authorize auto-merge" held_without_rearm "$d" -done - -d="$WORK/workflow-ref"; make_case "$d" workflow-ref; run_case "$d" -check 'documented workflow path with a ref suffix can certify a fresh CI event' test "$rc" -eq 0 - -d="$WORK/pagination"; make_case "$d" paginated-runs; run_case "$d" -check 'fresh CI on a complete multi-page run inventory succeeds' test "$rc" -eq 0 -check 'fresh-run requests select the native CI workflow' grep -q '/actions/workflows/ci.yaml/runs' "$d/calls" - -d="$WORK/metadata"; make_case "$d" good -jq '.autoMergeRequest.commitHeadline="Chosen subject\n\n" | .autoMergeRequest.commitBody="Chosen body\n\n"' "$d/db/pr.json" > "$d/next" -mv "$d/next" "$d/db/pr.json"; run_case "$d" -check 'commit metadata retains every trailing newline' jq -e '.autoMergeRequest.commitHeadline=="Chosen subject\n\n" and .autoMergeRequest.commitBody=="Chosen body\n\n"' "$d/db/pr.json" - -d="$WORK/journal"; make_case "$d" good -cat > "$d/bin/mkdir" <<'STUB' -#!/usr/bin/env bash -for arg in "$@"; do case "$arg" in */rearm/11) exit 1 ;; esac; done -exec "$RECHECK_REAL_MKDIR" "$@" -STUB -chmod +x "$d/bin/mkdir"; run_case "$d" -check 'failed recovery journal prevents closing the PR' no_mutations "$d" - -d="$WORK/recovery"; make_case "$d" recovery-noop; run_case "$d" -check 'acknowledged recovery reopen is read back before reporting recovery' awk \ - '/pr reopen/ {seen=1; verified=0} seen && /pr view .*--json state/ {verified=1} END {exit !verified}' "$d/calls" -# Verify unresolved recovery keeps the operator's original settings on disk. -retained_recovery() { - local before - before=$(find "${2:-$1/temp}" -name before -type f) || return 1 - [ -n "$before" ] && jq -e '.autoMergeRequest.commitBody=="Chosen body"' "$before" >/dev/null -} -check 'unresolved recovery retains the original merge settings for the operator' retained_recovery "$d" -check 'incomplete CI observations retain the original merge settings for the operator' retained_recovery "$WORK/incomplete-runs" - -d="$WORK/collection"; make_case "$d" incomplete-runs -collection=${RECHECK_OBSERVATION_ARTIFACT_DIR:-$WORK/collected-records} -RECHECK_RECOVERY_ROOT="$collection" run_case "$d" -check 'held settings survive the process in the caller-designated artifact directory' retained_recovery "$d" "$collection" - -d="$WORK/collection-failure"; make_case "$d" good -: > "$d/not-a-directory" -RECHECK_RECOVERY_ROOT="$d/not-a-directory" run_case "$d" -check 'unwritable recovery collection rejects the sweep before any forge request' test ! -e "$d/calls" - -printf 'recheck observations: %s failure(s)\n' "$fail" -[ "$fail" -eq 0 ] diff --git a/scripts/recheck-open-prs.sh b/scripts/recheck-open-prs.sh deleted file mode 100755 index 148e0295..00000000 --- a/scripts/recheck-open-prs.sh +++ /dev/null @@ -1,658 +0,0 @@ -#!/usr/bin/env bash -# Re-trigger the required checks on every open pull request targeting the default branch. -# -# WHY THIS EXISTS -# A pull-request workflow runs only on that PR's own `pull_request` events. Every PR already -# open when a new required gate lands on the default branch therefore keeps the green -# `CI - Required Checks` result it earned BEFORE the gate existed, and the branch rule keyed on -# that check name is satisfied by the stale run. Such a PR can merge without the new gate ever -# having run against it โ€” exactly the class of change (a version left unbumped, a synced skill -# hand-edited) each gate was added to stop. -# -# The repository's ruleset does not set `strict_required_status_checks_policy`, which is -# GitHub's own mechanism for this ("require branches to be up to date before merging"), and it -# is declared org-wide and Observe-only, so it is not this repository's to flip. This script is -# the repository-scoped equivalent: after the gate lands, ask every open PR to run again. -# -# WHY A FRESH BASE, AND NOT A RE-RUN -# Re-running a workflow run reuses the ORIGINAL event's `GITHUB_SHA` and `GITHUB_REF`. For a -# `pull_request` run that ref is `refs/pull/N/merge`, so a re-run replays the merge commit as it -# stood before the gate landed โ€” with the old workflow file. Only a NEW `pull_request` event -# resolves the merge ref again and picks the new gate up. Same-repository branches are updated -# through the head-checked update-branch API, preserving their commits and open state. This -# moves the head and requires review at that new head. Current non-Dependabot branches and -# forks use `reopened`, which preserves their head and starts CI for App-created drafts. -# Current Dependabot branches require an observed PR run without closing; Dependabot forks -# are refused because closing a Dependabot PR can suppress a wanted update. -# -# WHY AN APP TOKEN IS REQUIRED -# Events produced with the repository's `GITHUB_TOKEN` do not start new workflow runs, so a -# branch update or reopen performed with it would be silent. The caller must pass a token from the repository's -# GitHub App โ€” the same reason `update-agent-skills.yaml` mints one to open its PR. -# -# THE TWO THINGS THIS MUST NEVER LEAVE BEHIND -# A pull request closed, and an auto-merge that was armed before the run and is not after it. -# Both are tracked in a state directory from BEFORE the mutation that could cause them, and the -# exit trap settles both from the pull request's ACTUAL state rather than from an assumption -# about whether a failed call took effect โ€” a request can be applied and still report failure. -# -# Usage: -# ./scripts/recheck-open-prs.sh --repo OWNER/NAME [--base BRANCH] [--dry-run] -# -# Reads `gh` from PATH and expects it already authenticated with an App token. -# Exit 0 when every selected PR was re-triggered (or none was selected), 1 when any PR could not -# be, 2 on a usage or environment error. -set -uo pipefail - -# Explain the supported invocation and report a usage error. -usage() { - cat >&2 <<'EOF' -usage: recheck-open-prs.sh --repo OWNER/NAME [--base BRANCH] [--dry-run] -EOF - exit 2 -} - -repo="" -base="main" -dry_run=0 - -while [ "$#" -gt 0 ]; do - case "$1" in - --repo) - [ "$#" -ge 2 ] || usage - repo=$2 - shift 2 - ;; - --base) - [ "$#" -ge 2 ] || usage - base=$2 - shift 2 - ;; - --dry-run) - dry_run=1 - shift - ;; - *) usage ;; - esac -done - -[ -n "$repo" ] || usage -# A malformed slug would silently address a different repository, so it is validated rather -# than passed through. -case "$repo" in - */*/*) usage ;; - */*) ;; - *) usage ;; -esac - -command -v gh > /dev/null 2>&1 || { - echo "recheck-open-prs: gh is required" >&2 - exit 2 -} -# Used to take every field of a pull request's auto-merge request out of ONE response, so a -# transient failure cannot be mistaken for "no custom metadata". -command -v jq > /dev/null 2>&1 || { - echo "recheck-open-prs: jq is required" >&2 - exit 2 -} -# Probe the needed behavior rather than a version: Apple jq 1.7 preserves decimals -# but does not expose have_decnum. Rounded input cannot safely validate run counts. -if ! jq -ner '1.000000000000000001|tojson=="1.000000000000000001"' > /dev/null 2>&1; then - echo "recheck-open-prs: jq must preserve decimal-number spelling" >&2 - exit 2 -fi -# This repository's automation always addresses github.com, including recovery. -export GH_HOST=github.com - -# A hosted caller supplies a collection root to upload unresolved settings after this process. -# Snapshot fields are PR metadata only; credentials and environment values are never journaled. -umask 077 -if [ -n "${RECHECK_RECOVERY_ROOT:-}" ]; then - case "$RECHECK_RECOVERY_ROOT" in /*) ;; *) echo "recheck-open-prs: recovery root must be absolute" >&2; exit 2 ;; esac - if [ -L "$RECHECK_RECOVERY_ROOT" ] || ! mkdir -p "$RECHECK_RECOVERY_ROOT"; then - echo "recheck-open-prs: recovery root could not be prepared" >&2 - exit 2 - fi - state=$(mktemp -d "$RECHECK_RECOVERY_ROOT/recheck.XXXXXX") || exit 2 -else - state=$(mktemp -d) || exit 2 -fi -mkdir -p "$state/closed" "$state/rearm" || exit 2 -held_recovery="" - -# How long to wait for the reopened event's own workflow run before declining to re-arm. -# Overridable so the self-test does not sleep. -CHECK_WAIT_SECONDS=${RECHECK_CHECK_WAIT_SECONDS:-90} -CHECK_POLL_SECONDS=${RECHECK_CHECK_POLL_SECONDS:-3} - -# Restore anything this run may have disturbed. Both loops decide from the pull request's real -# state, because a call that reports failure may still have been applied: `gh pr close` can time -# out after GitHub accepted it, and dropping the record on that nonzero exit would leave the PR -# closed with nothing tracking it. -# shellcheck disable=SC2317,SC2329 # invoked indirectly, by the EXIT trap below. Both codes are -# needed: shellcheck >= 0.11 reports the unused-looking function as SC2329 on its declaration, -# while older versions โ€” including the one CI installs โ€” report every line of its body as -# unreachable, SC2317. A directive naming only one version's code passes here and fails there. -settle() { - local original_status=$? f n st method headline body sha baseline before current recovery_failed=0 - for f in "$state/closed"/*; do - [ -e "$f" ] || continue - n=${f##*/} - if ! before=$(cat "$f") || ! current=$(gh pr view "$n" --repo "$repo" --json state,autoMergeRequest,headRefOid,baseRefOid,baseRefName,number,author,isCrossRepository) \ - || ! printf '%s' "$current" | jq -e --argjson before "$before" ' - type=="object" and .number==$before.number and .headRefOid==$before.headRefOid and - .baseRefName==$before.baseRefName and .author.login==$before.author.login and - .isCrossRepository==$before.isCrossRepository and - (.state=="OPEN" or .state=="CLOSED" or .state=="MERGED")' >/dev/null; then - echo "::error::#$n recovery identity is unverified; inspect and reopen it by hand" >&2 - recovery_failed=1 - continue - fi - st=$(printf '%s' "$current" | jq -r '.state') - if [ "$st" = "OPEN" ] || [ "$st" = "MERGED" ]; then - rm -f "$f" - continue - fi - echo "recheck-open-prs: reopening #$n, left closed (state=$st)" >&2 - # An ambiguous acknowledgement may have applied; only readback proves recovery. - gh pr reopen "$n" --repo "$repo" > /dev/null 2>&1 || true - if ! verify_reopened "$n" "$before"; then - echo "::error::#$n recovery reopen is unverified; reopen it by hand" >&2 - recovery_failed=1 - else - rm -f "$f" - fi - done - for f in "$state/rearm"/*; do - [ -e "$f" ] || continue - n=${f##*/} - # An explicit CI hold stays manual; keep the saved settings without retrying a merge. - case " $held_recovery " in - *" $n "*) - echo "::error::#$n auto-merge remains held; original settings retained for the operator" >&2 - recovery_failed=1 - continue ;; - esac - if ! before=$(cat "$state/rearm/$n/before"); then - echo "::error::#$n recovery record is unreadable; inspect auto-merge by hand" >&2 - recovery_failed=1 - continue - fi - # Already armed is insufficient: it must still match the original settings. - if verify_rearmed "$n" "$before"; then rm -rf "$f"; continue; fi - if ! verify_reopened "$n" "$before" || ! read_recovery "$n"; then - echo "::error::#$n auto-merge recovery state is unverified; inspect it by hand" >&2 - recovery_failed=1 - continue - fi - # The same wait the main path performs, and for the same reason: arming auto-merge while the - # pre-gate green is still the newest result can merge the PR before the new run exists. - if ! await_fresh_check "$sha" "$baseline"; then - echo "::error::#$n auto-merge was NOT restored: no pull_request run from the reopen appeared, and arming it now could merge the PR on the pre-gate result. Re-arm it by hand once its checks are running." >&2 - recovery_failed=1 - continue - fi - echo "recheck-open-prs: restoring auto-merge on #$n" >&2 - if ! verify_reopened "$n" "$before"; then - echo "::error::#$n auto-merge was NOT restored: reopened state moved or is unreadable." >&2 - recovery_failed=1 - continue - fi - rearm "$n" "$method" "$headline" "$body" "$sha" > /dev/null 2>&1 || true - if ! verify_rearmed "$n" "$before"; then - echo "::error::#$n auto-merge recovery is unverified; re-arm it by hand" >&2 - recovery_failed=1 - else - rm -rf "$f" - fi - done - if [ "$recovery_failed" -eq 0 ]; then - rm -rf "$state" - else - echo "::error::Unverified recovery; original settings retained in private records at $state" >&2 - fi - [ "$original_status" -ne 0 ] || [ "$recovery_failed" -eq 0 ] || exit 1 -} - -# The highest WORKFLOW RUN id for a `pull_request` event at a commit, or 0 when it has none. -# -# Workflow runs, not check runs, and this distinction is the whole point. Re-running an existing -# workflow keeps its run id and adds an attempt, but it creates fresh CHECK runs with new ids and -# the same name โ€” so a manual rerun of the pre-gate run would satisfy a check-run comparison while -# no run for the reopen existed at all. A new `pull_request` run id can only come from a new -# `pull_request` event, which is exactly what is being waited for. Another such event (a push, say) -# would satisfy it too, and legitimately: it also resolves a fresh merge ref. -# -# Ids increase, so a larger one later means a newer run โ€” no clock, and no assumption about either -# side's timekeeping. Exits non-zero when the read fails, so a caller can tell "none yet" (0) from -# "unknown". The parameters are GET fields rather than query text for the same reason as the -# listing: a value spliced into the path could change which runs are counted. -newest_pr_run() { - local sha=$1 observation - observation=$(gh api --paginate --slurp --method GET "repos/${repo}/actions/workflows/ci.yaml/runs" \ - -f event=pull_request -f head_sha="$sha" -F per_page=100) || return 1 - printf '%s' "$observation" | jq -er --arg sha "$sha" --arg repo "$repo" ' - # Keep the original numeric spelling: floor/equality can round a fraction. - def integer: type=="number" and (tojson|test("^(0|[1-9][0-9]*)$")) and .<=9007199254740991; - if type=="array" and length>0 and all(.[]; - type=="object" and (.total_count|integer) and (.workflow_runs|type=="array")) then - . as $pages | [.[].workflow_runs[]] as $runs | - if all($pages[]; .total_count==$pages[0].total_count) and - ($runs|length)==$pages[0].total_count and - all($runs[]; type=="object" and (.id|integer and .>0) and - .event=="pull_request" and .head_sha==$sha and - (.path|type=="string" and test("^\\.github/workflows/ci\\.yaml(@[^\\r\\n]+)?\\z")) and - .repository.full_name==$repo) and - ($runs|map(.id)|unique|length)==($runs|length) - then ($runs|map(.id)|max // 0) else error("incomplete or mismatched CI runs") end - else error("invalid CI run observation") end' -} - -# Every journal write is checked before close can clear the original merge request. -save_recovery() { - local n=$1 before=$2 baseline=$3 - mkdir -p "$state/rearm/$n" || return 1 - printf '%s' "$before" > "$state/rearm/$n/before" || return 1 - printf '%s' "$before" | jq -jr '.autoMergeRequest.mergeMethod // ""' > "$state/rearm/$n/method" || return 1 - printf '%s' "$before" | jq -jr '.autoMergeRequest.commitHeadline // ""' > "$state/rearm/$n/headline" || return 1 - printf '%s' "$before" | jq -jr '.autoMergeRequest.commitBody // ""' > "$state/rearm/$n/body" || return 1 - printf '%s' "$before" | jq -jr '.headRefOid' > "$state/rearm/$n/sha" || return 1 - printf '%s' "$baseline" > "$state/rearm/$n/baseline" || return 1 -} - -# Sentinel reads retain trailing newlines; cat's failure still propagates. -# Dynamic caller locals are intentional: both main and recovery use these values. -read_recovery() { - local n=$1 - method=$(cat "$state/rearm/$n/method") || return 1 - headline=$(cat "$state/rearm/$n/headline" && printf x) || return 1 - headline=${headline%x} - body=$(cat "$state/rearm/$n/body" && printf x) || return 1 - body=${body%x} - sha=$(cat "$state/rearm/$n/sha") || return 1 - baseline=$(cat "$state/rearm/$n/baseline") || return 1 -} - -# Block until a `pull_request` workflow run newer than $2 exists at commit $1. Auto-merge means "merge once the -# requirements are met", and immediately after a reopen the newest result at that commit is still -# the PRE-GATE green: arming there can merge the pull request in the window before Actions has -# created the run for the reopen, past the very gate this script exists to apply. Returns -# non-zero if no new run appears, and the caller then declines to arm โ€” an auto-merge a human -# must restore is recoverable, a merge that skipped a gate is not. -await_fresh_check() { - local sha=$1 baseline=$2 waited=0 now - # An absent sha or baseline is unknown, not zero: comparing against an invented lower bound - # would let any historical run satisfy the wait immediately. - [ -n "$sha" ] || return 1 - case "$baseline" in '' | *[!0-9]*) return 1 ;; esac - while [ "$waited" -lt "$CHECK_WAIT_SECONDS" ]; do - # A failed poll is "not yet", never "satisfied" โ€” the loop simply keeps waiting. - if now=$(newest_pr_run "$sha") && case "$now" in '' | *[!0-9]*) false ;; *) true ;; esac; then - [ "$now" -gt "$baseline" ] && return 0 - fi - sleep "$CHECK_POLL_SECONDS" - waited=$((waited + CHECK_POLL_SECONDS)) - done - return 1 -} - -# A successful write acknowledgement is not readback proof, including on unarmed PRs. -verify_reopened() { - local n=$1 before=$2 current - current=$(gh pr view "$n" --repo "$repo" --json state,autoMergeRequest,headRefOid,baseRefOid,baseRefName,number,author,isCrossRepository) || return 1 - printf '%s' "$current" | jq -e --argjson before "$before" ' - type=="object" and has("autoMergeRequest") and .autoMergeRequest==null and - .state=="OPEN" and .number==$before.number and .headRefOid==$before.headRefOid and - .baseRefOid==$before.baseRefOid and .baseRefName==$before.baseRefName and - .author.login==$before.author.login and .isCrossRepository==$before.isCrossRepository' >/dev/null -} - -# Verify the original merge strategy and metadata from a fresh identity-bound read. -verify_rearmed() { - local n=$1 before=$2 current - current=$(gh pr view "$n" --repo "$repo" --json state,autoMergeRequest,headRefOid,baseRefOid,baseRefName,number,author,isCrossRepository) || return 1 - printf '%s' "$current" | jq -e --argjson before "$before" ' - type=="object" and .number==$before.number and .headRefOid==$before.headRefOid and - .baseRefName==$before.baseRefName and .author.login==$before.author.login and - .isCrossRepository==$before.isCrossRepository and - (.state=="MERGED" or (.state=="OPEN" and .baseRefOid==$before.baseRefOid and - .autoMergeRequest.mergeMethod==$before.autoMergeRequest.mergeMethod and - (.autoMergeRequest.commitHeadline // "")==($before.autoMergeRequest.commitHeadline // "") and - (.autoMergeRequest.commitBody // "")==($before.autoMergeRequest.commitBody // "")))' >/dev/null -} - -# Re-arm auto-merge exactly as it was: the same strategy, and the same commit metadata. -# Recreating it as a default squash would silently change both the merge behaviour and the -# message someone chose deliberately. -rearm() { - local n=$1 method=$2 headline=$3 body=$4 sha=$5 flag - case "$method" in - MERGE) flag=--merge ;; - REBASE) flag=--rebase ;; - SQUASH) flag=--squash ;; - *) echo "::error::Unknown auto-merge method; refusing to guess." >&2; return 1 ;; - esac - [[ "$sha" =~ ^[0-9a-f]{40}$ ]] || return 1 - set -- "$n" --repo "$repo" --auto "$flag" --match-head-commit "$sha" - # A rebase carries no commit message of its own, so those flags apply to the other two only. - if [ "$flag" != "--rebase" ]; then - set -- "$@" --subject "$headline" --body "$body" - fi - gh pr merge "$@" -} - -# Update only a same-repository branch, retaining its complete prior history and merge settings. -# GitHub's expected_head_sha rejects an intervening push. Accepted asynchronous writes are not -# success evidence: read back the new head, verify both ancestors, then observe its PR event. -# Return 3 only for a current non-Dependabot branch that needs the existing reopen route. -refresh_same_repository() { - local n=$1 before=$2 old_head old_base relation current new_head new_base base_path final_base conflict waited=0 - old_head=$(printf '%s' "$before" | jq -r '.headRefOid') - # A PR's associated base OID is not proof of the current named branch tip. - base_path=$(printf '%s' "$base" | jq -sRr @uri) - if ! old_base=$(gh api "repos/$repo/branches/$base_path" --jq '.commit.sha'); then - echo "::error::#$n current base could not be read; left untouched" - return 1 - fi - if [[ ! "$old_head" =~ ^[0-9a-f]{40}$ || ! "$old_base" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::#$n commit identity is incomplete; left untouched" - return 1 - fi - if ! relation=$(gh api "repos/$repo/compare/$old_base...$old_head" --jq '.status'); then - echo "::error::#$n base ancestry could not be read; left untouched" - return 1 - fi - case "$relation" in - ahead|identical) - case "$(printf '%s' "$before" | jq -r '.author.login')" in - 'app/dependabot'|'dependabot[bot]') - # This is verification of an already-current head, not a newly triggered event. - # A pre-gate head cannot enter this route: it fails the named-base ancestry above. - if ! await_fresh_check "$old_head" 0; then - echo "::error::#$n contains the current base but no PR workflow run was observed; left open" - return 1 - fi - if ! current=$(gh pr view "$n" --repo "$repo" --json state,autoMergeRequest,headRefOid,baseRefOid,baseRefName,number,author,isCrossRepository) \ - || ! printf '%s' "$current" | jq -e --argjson before "$before" \ - 'has("autoMergeRequest") and .state=="OPEN" and .number==$before.number and .baseRefName==$before.baseRefName and - .headRefOid==$before.headRefOid and .isCrossRepository==false and - .author.login==$before.author.login and .autoMergeRequest==$before.autoMergeRequest' > /dev/null \ - || ! final_base=$(gh api "repos/$repo/branches/$base_path" --jq '.commit.sha') \ - || [ "$final_base" != "$old_base" ]; then - echo "::error::#$n moved during the check wait; current refresh is unverified" - return 1 - fi - echo " checked #$n โ€” its head contains the current base and has an observed PR run" - return 0 ;; - *) return 3 ;; - esac ;; - behind|diverged) ;; - *) echo "::error::#$n base ancestry is unknown; left untouched"; return 1 ;; - esac - if ! gh api --method PUT "repos/$repo/pulls/$n/update-branch" -f expected_head_sha="$old_head" > /dev/null; then - # A branch that conflicts with the base cannot be updated, and it cannot merge either: - # GitHub refuses to merge a conflicting pull request, and the push that resolves the - # conflict is a new head whose own `pull_request` event runs every current gate. Its stale - # green therefore cannot reach the base, so it is reported, not counted as a failure. Only - # GitHub's settled verdict about the head this run tried counts: an unknown, still - # computing or moved head stays a failure. - if conflict=$(gh api "repos/$repo/pulls/$n" --jq '[.head.sha, (.mergeable|tostring), .mergeable_state] | @tsv') \ - && [ "$conflict" = "$old_head"$'\tfalse\tdirty' ]; then - echo " skipped #$n โ€” it conflicts with $base, so it cannot merge until a push resolves that, and that push re-runs every required check" - return 4 - fi - echo "::error::#$n head-checked base update failed; no close or recreation attempted" - return 1 - fi - while [ "$waited" -lt "$CHECK_WAIT_SECONDS" ]; do - if ! current=$(gh pr view "$n" --repo "$repo" --json state,autoMergeRequest,headRefOid,baseRefOid,baseRefName,number,author,isCrossRepository) \ - || ! printf '%s' "$current" | jq -e --argjson before "$before" --arg base "$base" \ - 'has("autoMergeRequest") and .state=="OPEN" and .number==$before.number and .baseRefName==$base and - .isCrossRepository==false and .author.login==$before.author.login and - .autoMergeRequest==$before.autoMergeRequest and (.headRefOid|test("^[0-9a-f]{40}$")) and - (.baseRefOid|test("^[0-9a-f]{40}$"))' > /dev/null; then - echo "::error::#$n updated state is unreadable or changed; no recovery mutation attempted" - return 1 - fi - new_head=$(printf '%s' "$current" | jq -r '.headRefOid') - if ! new_base=$(gh api "repos/$repo/branches/$base_path" --jq '.commit.sha') \ - || [[ ! "$new_base" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::#$n current base became unreadable; refresh is unverified" - return 1 - fi - if [ "$new_head" != "$old_head" ]; then - if ! relation=$(gh api "repos/$repo/compare/$old_head...$new_head" --jq '.status') || [ "$relation" != ahead ]; then - echo "::error::#$n updated head does not prove preservation of the previous commits" - return 1 - fi - if ! relation=$(gh api "repos/$repo/compare/$new_base...$new_head" --jq '.status') \ - || { [ "$relation" != ahead ] && [ "$relation" != identical ]; }; then - echo "::error::#$n updated head does not prove inclusion of the current base" - return 1 - fi - if ! await_fresh_check "$new_head" 0; then - echo "::error::#$n base was updated, but no fresh pull_request workflow run was observed" - return 1 - fi - # A check observed at one head cannot certify a successor pushed during the wait. - if ! current=$(gh pr view "$n" --repo "$repo" --json state,autoMergeRequest,headRefOid,baseRefOid,baseRefName,number,author,isCrossRepository) \ - || ! printf '%s' "$current" | jq -e --argjson before "$before" --arg head "$new_head" --arg base "$base" \ - 'has("autoMergeRequest") and .state=="OPEN" and .number==$before.number and .baseRefName==$base and - .headRefOid==$head and .isCrossRepository==false and .author.login==$before.author.login and - .autoMergeRequest==$before.autoMergeRequest' > /dev/null \ - || ! final_base=$(gh api "repos/$repo/branches/$base_path" --jq '.commit.sha') \ - || [ "$final_base" != "$new_base" ]; then - echo "::error::#$n moved during the check wait; current refresh is unverified" - return 1 - fi - echo " refreshed #$n at $new_head โ€” prior commits and auto-merge settings preserved; current-head review required" - return 0 - fi - sleep "$CHECK_POLL_SECONDS" - waited=$((waited + CHECK_POLL_SECONDS)) - done - echo "::error::#$n accepted update did not produce an observed new head" - return 1 -} - -trap settle EXIT - -# `gh pr list --limit N` fetches at most N, so any cap silently skips the pull requests past it -# and leaves them on the pre-gate result โ€” the exact failure this script exists to prevent, just -# further down the list. `gh api --paginate` walks every page instead. -# -# The query parameters are passed as GET fields rather than interpolated into the path: a branch -# name may legally contain `&` or `#`, which spliced into a query string would silently select a -# different set of pull requests. `--method GET` is what keeps gh from turning the fields into a -# POST body. -# -# Auto-merge is deliberately NOT read here. A snapshot taken now could be minutes old by the time -# a given PR is processed, and re-arming from it would restore an auto-merge someone disabled in -# between โ€” a merge nobody asked for. It is read per PR, immediately before closing. -if ! gh api --paginate --slurp --method GET "repos/${repo}/pulls" \ - -f state=open -f base="$base" -F per_page=100 > "$state/inventory"; then - echo "recheck-open-prs: could not list open pull requests" >&2 - exit 2 -fi -if ! jq -es --arg repo "$repo" --arg base "$base" ' - length==1 and (.[0] | type=="array" and length>0 and all(.[]; type=="array") and - ([.[][]] | all(.[]; type=="object" and - (.number|type=="number" and (tojson|test("^[1-9][0-9]*$")) and .<=9007199254740991) and - (.title|type=="string") and .state=="open" and - .base.ref==$base and .base.repo.full_name==$repo) and - (map(.number)|unique|length)==length))' "$state/inventory" >/dev/null \ - || ! prs=$(jq -r '.[][]|[(.number|tostring),.title]|@tsv' "$state/inventory"); then - echo "recheck-open-prs: open pull request inventory is malformed or contradictory; no PR was changed" >&2 - exit 2 -fi - -count=$(printf '%s' "$prs" | awk 'NF { n++ } END { print n + 0 }') - -if [ "$count" -eq 0 ]; then - echo "recheck-open-prs: no open pull requests targeting $base โ€” nothing to re-trigger" - exit 0 -fi - -echo "recheck-open-prs: re-triggering required checks on $count open PR(s) targeting $base" - -failed=0 -done_count=0 -conflicted=0 - -while IFS=$'\t' read -r number title; do - [ -n "$number" ] || continue - # A line that is not a PR number means the listing was not what it claimed to be. Failing here - # keeps a malformed response from being read as a shorter list of real pull requests. - case "$number" in - '' | *[!0-9]*) - echo "recheck-open-prs: open pull request listing is malformed near '$number'" >&2 - exit 2 - ;; - esac - - if [ "$dry_run" -eq 1 ]; then - echo " would re-trigger #$number โ€” $title" - done_count=$((done_count + 1)) - continue - fi - - # Read auto-merge fresh, immediately before closing, so the decision to restore it is based on - # the state that is true now rather than when the sweep started. A read that fails leaves the - # PR untouched: closing it without knowing would risk silently dropping an armed auto-merge. - # ONE read, capturing everything this PR's handling depends on. Splitting it across calls made - # a transient failure on a later call indistinguishable from "no custom metadata", which would - # then be restored as GitHub's default message โ€” a silent change to someone's chosen commit. - # A failed read leaves the PR untouched: closing it without knowing its state would risk both - # reversing a deliberate closure and dropping an armed auto-merge. - if ! snapshot=$(gh pr view "$number" --repo "$repo" --json state,autoMergeRequest,headRefOid,baseRefOid,baseRefName,number,author,isCrossRepository) \ - || ! printf '%s' "$snapshot" | jq -e --argjson number "$number" --arg base "$base" \ - 'type=="object" and has("autoMergeRequest") and (.author.login|type=="string" and test("\\S")) and - .number==$number and .baseRefName==$base and (.isCrossRepository|type=="boolean") and - (.state=="OPEN" or .state=="CLOSED" or .state=="MERGED")' > /dev/null; then - echo "::error::#$number state could not be read; left untouched" - failed=$((failed + 1)) - continue - fi - - pr_state=$(printf '%s' "$snapshot" | jq -r '.state // ""') - # The listing is a snapshot; a maintainer may have closed or merged this PR since. Reopening it - # would reverse that deliberate act, and the `autoMergeRequest` read alone would not have - # noticed โ€” it succeeds for a closed pull request too. - if [ "$pr_state" != "OPEN" ]; then - echo " skipped #$number โ€” no longer open (state=${pr_state:-unknown})" - continue - fi - - if ! printf '%s' "$snapshot" | jq -e ' - (.headRefOid | type=="string" and test("^[0-9a-f]{40}$")) and - (.baseRefOid | type=="string" and test("^[0-9a-f]{40}$")) and - (.autoMergeRequest==null or (.autoMergeRequest | type=="object" and - (.mergeMethod=="MERGE" or .mergeMethod=="REBASE" or .mergeMethod=="SQUASH") and - (.commitHeadline | .==null or (type=="string" and (contains("\u0000")|not))) and - (.commitBody | .==null or (type=="string" and (contains("\u0000")|not)))))' >/dev/null; then - echo "::error::#$number identity or auto-merge strategy is unknown; left untouched" - failed=$((failed + 1)) - continue - fi - - if [ "$(printf '%s' "$snapshot" | jq -r '.isCrossRepository')" = false ]; then - refresh_same_repository "$number" "$snapshot" - refresh_status=$? - if [ "$refresh_status" -eq 0 ]; then - done_count=$((done_count + 1)) - continue - elif [ "$refresh_status" -eq 4 ]; then - conflicted=$((conflicted + 1)) - continue - elif [ "$refresh_status" -ne 3 ]; then - failed=$((failed + 1)) - continue - fi - fi - case "$(printf '%s' "$snapshot" | jq -r '.author.login')" in - 'app/dependabot'|'dependabot[bot]') - echo "::error::#$number is a Dependabot fork; closing would suppress its update, so it was left untouched" - failed=$((failed + 1)) - continue ;; - esac - - head_sha=$(printf '%s' "$snapshot" | jq -r '.headRefOid') - if ! check_baseline=$(newest_pr_run "$head_sha") || - case "$check_baseline" in '' | *[!0-9]*) true ;; *) false ;; esac; then - echo "::error::#$number run baseline could not be read; left untouched" - failed=$((failed + 1)) - continue - fi - - automerge=$(printf '%s' "$snapshot" | jq -r 'if .autoMergeRequest == null then "none" else "armed" end') - if [ "$automerge" = "armed" ]; then - # Capture the strategy and commit metadata before the close clears the request, so the - # restore puts back what was there rather than a default squash. - if ! save_recovery "$number" "$snapshot" "$check_baseline"; then - echo "::error::#$number recovery record could not be saved; left untouched" - rm -rf "$state/rearm/$number" - failed=$((failed + 1)) - continue - fi - fi - - # Close and reopen produce the `reopened` event that resolves a fresh merge ref. The head is - # untouched, so a green review at the current head stays current. The record is written first - # and is NOT removed when the close reports failure: a close can be applied and still report - # one, and only the trap's read of the real state can tell those apart. - if ! printf '%s' "$snapshot" > "$state/closed/$number"; then - echo "::error::#$number close recovery record could not be saved; left untouched" - rm -f "$state/closed/$number" - rm -rf "$state/rearm/$number" - failed=$((failed + 1)) - continue - fi - if ! gh pr close "$number" --repo "$repo" > /dev/null; then - echo "::error::#$number could not be closed; skipped without re-triggering" - failed=$((failed + 1)) - continue - fi - - if ! gh pr reopen "$number" --repo "$repo" > /dev/null; then - echo "::error::#$number was closed but could not be reopened" - failed=$((failed + 1)) - continue - fi - if ! await_fresh_check "$head_sha" "$check_baseline" || ! verify_reopened "$number" "$snapshot"; then - echo "::error::#$number reopen is unverified: no fresh PR event or matching OPEN readback; auto-merge was NOT restored, because it could merge the PR on the pre-gate result." - held_recovery="$held_recovery $number" - failed=$((failed + 1)) - continue - fi - rm -f "$state/closed/$number" - - if [ "$automerge" = "armed" ]; then - if ! read_recovery "$number" || ! rearm "$number" "$method" "$headline" "$body" "$head_sha" > /dev/null || - ! verify_rearmed "$number" "$snapshot"; then - # Left in the rearm set on purpose: once the close has cleared the request, a later run - # cannot tell that this PR ever had auto-merge armed, so the obligation has to survive - # here or it is lost for good. The trap retries it. - echo "::error::#$number was re-triggered but its auto-merge could not be re-armed" - failed=$((failed + 1)) - continue - fi - rm -rf "$state/rearm/$number" - echo " re-triggered #$number and re-armed auto-merge โ€” $title" - else - echo " re-triggered #$number โ€” $title" - fi - done_count=$((done_count + 1)) - # `printf '%s\n'`, never `printf '%s'`: command substitution strips the trailing newline, so - # feeding the value back without one makes `read` return false on the final line and drops the - # last pull request from the sweep โ€” silently, and reported as a smaller total. -done < <(printf '%s\n' "$prs") - -echo "recheck-open-prs: $done_count of $count current-base refreshes completed" -if [ "$conflicted" -gt 0 ]; then - echo "::notice::$conflicted pull request(s) conflict with $base and were not refreshed; they cannot merge until a push resolves the conflict, which re-runs their checks" -fi -if [ "$failed" -gt 0 ]; then - echo "::error::$failed pull request(s) could not be re-triggered; their required checks are still the pre-gate result" - exit 1 -fi -exit 0 diff --git a/scripts/recheck-open-prs.test.sh b/scripts/recheck-open-prs.test.sh deleted file mode 100755 index e485f337..00000000 --- a/scripts/recheck-open-prs.test.sh +++ /dev/null @@ -1,594 +0,0 @@ -#!/usr/bin/env bash -# Self-test for recheck-open-prs.sh. -# -# Hermetic: stubs `gh` on PATH and records every call, so nothing here reaches the network or -# mutates a real pull request. The stub keeps per-PR state (open/closed, auto-merge armed or not, -# its commit metadata) and answers reads from it, so the cases below assert what the script leaves -# BEHIND โ€” no pull request closed, no auto-merge lost โ€” rather than only which calls it made. -set -uo pipefail - -HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -SCRIPT="$HERE/recheck-open-prs.sh" - -pass=0 -fail=0 - -# Record a successful behavioral assertion. -ok() { - echo " โœ“ $1" - pass=$((pass + 1)) -} -# Record a failed assertion and its diagnostic output without ending the suite. -bad() { - echo " โœ— $1" - shift - [ "$#" -eq 0 ] || printf '%s\n' "$@" | sed 's/^/ /' - fail=$((fail + 1)) -} - -WORK=$(mktemp -d) -trap 'rm -rf "$WORK"' EXIT - -# make_gh [fail-verb] [armed-numbers] -# fail-verb that verb's FIRST call fails; later calls succeed, so a recovery path is -# exercised as one that can actually complete rather than one that cannot. -# armed-numbers space-separated PR numbers that start with auto-merge armed. -# The stub maintains real state under /db, so "was it left closed" is answerable. -make_gh() { - local dir="$1" listing="$2" fail_verb="${3:-}" armed="${4:-}" - mkdir -p "$dir/bin" "$dir/db" - printf '%s' "$listing" > "$dir/listing.tsv" - local n - for n in $armed; do - printf 'armed' > "$dir/db/am-$n" - printf 'custom subject %s' "$n" > "$dir/db/headline-$n" - printf 'custom body %s' "$n" > "$dir/db/body-$n" - done - cat > "$dir/bin/gh" <> "\$log" - [ -f "\$db/checkfail" ] && exit 1 - # A NEW pull_request workflow run id. A rerun of an existing run would not move this, which is - # exactly the distinction the script relies on. - id=\$(cat "\$db/checkbump" 2>/dev/null || printf '0') - sha="" - for arg in "\$@"; do case "\$arg" in head_sha=*) sha=\${arg#head_sha=} ;; esac; done - jq -nc --arg sha "\$sha" --argjson id "\$id" \ - '[{total_count:(if \$id==0 then 0 else 1 end),workflow_runs:(if \$id==0 then [] else [{id:\$id,event:"pull_request",head_sha:\$sha,path:".github/workflows/ci.yaml",repository:{full_name:"owner/name"}}] end)}]' - exit 0 -fi - -if [ "\$verb" = "api --paginate" ]; then - printf '%s\n' "api \$*" >> "\$log" - [ -f "$dir/listing-fails" ] && exit 1 - query_base=main - for arg in "\$@"; do case "\$arg" in base=*) query_base=\${arg#base=} ;; esac; done - jq -Rn --arg base "\$query_base" \ - '[inputs|select(length>0)|split("\\t")|.[0] as \$n|{number:(\$n|try tonumber catch \$n),title:.[1],state:"open",base:{ref:\$base,repo:{full_name:"owner/name"}}}]|if length==0 then [[]] else . as \$items | [range(0; length; 100) as \$offset | \$items[\$offset:\$offset+100]] end' < "$dir/listing.tsv" - exit 0 -fi - -n=\$3 -fields="" -prev="" -for a in "\$@"; do - [ "\$prev" = "--json" ] && fields=\$a - prev=\$a -done - -case "\$verb" in - "pr view") - printf '%s\n' "pr view \$n \$fields" >> "\$log" - [ -f "\$db/viewfail" ] && exit 1 - if [ -f "\$db/closed-\$n" ]; then st=CLOSED; else st=OPEN; fi - [ -f "\$db/merged-\$n" ] && st=MERGED - if [ -f "\$db/am-\$n" ]; then - method=\$(cat "\$db/method-\$n" 2>/dev/null || printf 'SQUASH') - head=\$(cat "\$db/headline-\$n" 2>/dev/null) - body=\$(cat "\$db/body-\$n" 2>/dev/null) - am=\$(printf '{"mergeMethod":"%s","commitHeadline":"%s","commitBody":"%s"}' "\$method" "\$head" "\$body") - else - am=null - fi - case "\$fields" in - "state,autoMergeRequest,headRefOid"|"state,autoMergeRequest,headRefOid,baseRefOid,baseRefName,number,author,isCrossRepository") - author=\$(cat "\$db/author-\$n" 2>/dev/null || printf 'devantler') - fork=\$(cat "\$db/fork-\$n" 2>/dev/null || printf 'true') - head_sha=1111111111111111111111111111111111111111 - base_sha=2222222222222222222222222222222222222222 - if [ -f "\$db/moved" ] && [ "\$(cat "\$db/checkbump" 2>/dev/null || printf 0)" -gt 0 ]; then - head_sha=3333333333333333333333333333333333333333 - fi - if [ "\$(cat "\$db/checkbump" 2>/dev/null || printf 0)" -gt 0 ]; then - [ ! -f "\$db/moved-author" ] || author=other-author - [ ! -f "\$db/moved-base" ] || base_sha=4444444444444444444444444444444444444444 - [ ! -f "\$db/moved-boundary" ] || fork=false - [ ! -f "\$db/unreadable-readback" ] || exit 1 - fi - printf '{"number":%s,"baseRefName":"main","state":"%s","autoMergeRequest":%s,"headRefOid":"%s","baseRefOid":"%s","author":{"login":"%s"},"isCrossRepository":%s}\n' "\$n" "\$st" "\$am" "\$head_sha" "\$base_sha" "\$author" "\$fork" - ;; - state) - printf '%s\n' "\$st" - ;; - autoMergeRequest) - if [ "\$am" = null ]; then printf 'none\n'; else printf 'armed\n'; fi - ;; - esac - # A hook the caller uses to change state between the sweep and this PR's close. - [ -x "$dir/on-view" ] && "$dir/on-view" "\$n" - exit 0 - ;; - "pr close") - printf '%s\n' "pr close \$n" >> "\$log" - if [ "\$fail_verb" = "close-applied" ] && [ ! -f "\$db/failed-close" ]; then - # The ambiguous case: GitHub applies the close, the client still reports failure. - : > "\$db/failed-close"; : > "\$db/closed-\$n"; rm -f "\$db/am-\$n"; exit 1 - fi - if [ "\$fail_verb" = "close" ] && [ ! -f "\$db/failed-close" ]; then - : > "\$db/failed-close"; exit 1 - fi - : > "\$db/closed-\$n" - # Closing a pull request clears an armed auto-merge, exactly as GitHub does. - rm -f "\$db/am-\$n" - exit 0 - ;; - "pr reopen") - printf '%s\n' "pr reopen \$n" >> "\$log" - if [ "\$fail_verb" = "reopen" ] && [ ! -f "\$db/failed-reopen" ]; then - : > "\$db/failed-reopen"; exit 1 - fi - [ ! -f "\$db/reopen-noop" ] || exit 0 - rm -f "\$db/closed-\$n" - [ ! -f "\$db/no-new-run" ] || exit 0 - # Reopening creates a new check run, so the high-water mark moves. - printf '%s' "\$(( \$(cat "\$db/checkbump" 2>/dev/null || printf '0') + 1 ))" > "\$db/checkbump" - exit 0 - ;; - "pr merge") - printf '%s\n' "pr merge \$*" >> "\$log" - [ ! -f "\$db/merge-noop" ] || exit 0 - if [ "\$fail_verb" = "merge" ] && [ ! -f "\$db/failed-merge" ]; then - : > "\$db/failed-merge"; exit 1 - fi - : > "\$db/am-\$n" - exit 0 - ;; -esac -printf '%s\n' "\$verb \$n" >> "\$log" -exit 0 -EOF - chmod +x "$dir/bin/gh" - : > "$dir/calls.log" -} - -# Run the production entrypoint against one stateful fixture repository. -run_script() { - local dir="$1" - shift - mkdir -p "$dir/temp" - env PATH="$dir/bin:$PATH" TMPDIR="$dir/temp" RECHECK_CHECK_WAIT_SECONDS=2 RECHECK_CHECK_POLL_SECONDS=1 \ - "$SCRIPT" --repo owner/name "$@" 2>&1 -} - -# Same stubbed PATH, every argument the caller's โ€” for cases that must pass a malformed --repo. -run_raw() { - local dir="$1" - shift - mkdir -p "$dir/temp" - env PATH="$dir/bin:$PATH" TMPDIR="$dir/temp" RECHECK_CHECK_WAIT_SECONDS=2 RECHECK_CHECK_POLL_SECONDS=1 \ - "$SCRIPT" "$@" 2>&1 -} - -# Count forge requests recorded by the fixture. -calls() { awk 'END { print NR }' "$1/calls.log"; } -# Left closed? The stub's own state, not an inference from the call log. -count_state() { - local dir=$1 prefix=$2 f c=0 - for f in "$dir"/"$prefix"*; do - [ -e "$f" ] && c=$((c + 1)) - done - printf '%s' "$c" -} -# Count fixture PRs that remain closed after the entrypoint exits. -left_closed() { count_state "$1/db" closed-; } -# Count fixture PRs whose auto-merge request remains armed. -armed_count() { count_state "$1/db" am-; } - -TWO_PRS=$'11\tfirst\n22\tsecond\n' - -echo "recheck-open-prs.sh self-test" - -# --- usage --------------------------------------------------------------- -d="$WORK/usage" -make_gh "$d" '' -out=$(run_raw "$d" --repo) -rc=$? -if [ "$rc" -eq 2 ]; then - ok "a missing --repo value is a usage error" -else - bad "a missing --repo value is a usage error" "got exit $rc: $out" -fi - -d="$WORK/slug" -make_gh "$d" '' -out=$(run_raw "$d" --repo not-a-slug) -rc=$? -if [ "$rc" -eq 2 ]; then - ok "a malformed repository slug is refused, never addressed" -else - bad "a malformed repository slug is refused, never addressed" "got exit $rc: $out" -fi - -# --- nothing to do ------------------------------------------------------- -d="$WORK/empty" -make_gh "$d" '' -out=$(run_script "$d") -rc=$? -if [ "$rc" -eq 0 ] && [[ $out == *"nothing to re-trigger"* ]] \ - && [ "$(grep -c '^pr ' "$d/calls.log")" -eq 0 ]; then - ok "an empty listing exits 0 and mutates nothing" -else - bad "an empty listing exits 0 and mutates nothing" "exit $rc: $out" "$(cat "$d/calls.log")" -fi - -# --- a failed listing is not an empty one -------------------------------- -d="$WORK/listfail" -make_gh "$d" "$TWO_PRS" -: > "$d/listing-fails" -out=$(run_script "$d") -rc=$? -if [ "$rc" -eq 2 ] && [ "$(grep -c '^pr ' "$d/calls.log")" -eq 0 ]; then - ok "a failed listing exits 2 rather than reading as no open PRs" -else - bad "a failed listing exits 2 rather than reading as no open PRs" "exit $rc: $out" -fi - -# --- the branch reaches the API as a field, not as path text ------------- -# A branch may legally contain `&` or `#`; spliced into a query string it would select a -# different set of pull requests, or truncate the query outright. -d="$WORK/encode" -make_gh "$d" '' -out=$(run_script "$d" --base 'release&state=closed') -rc=$? -api=$(grep '^api ' "$d/calls.log") -if [ "$rc" -eq 0 ] && [[ $api == *"--method GET"* ]] \ - && [[ $api == *"-f base=release&state=closed"* ]] && [[ $api != *"repos/owner/name/pulls?"* ]]; then - ok "the base branch is passed as a GET field, never spliced into the path" -else - bad "the base branch is passed as a GET field, never spliced into the path" "exit $rc" "$api" -fi - -# --- dry run ------------------------------------------------------------- -d="$WORK/dry" -make_gh "$d" "$TWO_PRS" -out=$(run_script "$d" --dry-run) -rc=$? -if [ "$rc" -eq 0 ] && [ "$(grep -c '^pr ' "$d/calls.log")" -eq 0 ] \ - && [[ $out == *"would re-trigger #11"* ]] && [[ $out == *"would re-trigger #22"* ]]; then - ok "--dry-run reports every PR and mutates nothing" -else - bad "--dry-run reports every PR and mutates nothing" "exit $rc: $out" "$(cat "$d/calls.log")" -fi - -# --- the happy path, and the ORDER that makes it safe -------------------- -d="$WORK/happy" -make_gh "$d" "$TWO_PRS" "" "22" -out=$(run_script "$d") -rc=$? -log=$(cat "$d/calls.log") -if [ "$rc" -eq 0 ] && [ "$(left_closed "$d")" -eq 0 ]; then - ok "every PR is left open" -else - bad "every PR is left open" "exit $rc" "$log" -fi -if [ "$(grep -n 'pr close 11' "$d/calls.log" | cut -d: -f1)" -lt \ - "$(grep -n 'pr reopen 11' "$d/calls.log" | cut -d: -f1)" ]; then - ok "close precedes reopen for the same PR" -else - bad "close precedes reopen for the same PR" "$log" -fi -if [ "$(grep -c 'pr view 11 state,autoMergeRequest,headRefOid' "$d/calls.log")" -ge 1 ] \ - && [ "$(grep -c 'pr view 22 state,autoMergeRequest,headRefOid' "$d/calls.log")" -ge 1 ]; then - ok "state and auto-merge are read per PR, immediately before closing it" -else - bad "state and auto-merge are read per PR, immediately before closing it" "$log" -fi -if [ "$(grep -c 'pr merge 22' "$d/calls.log")" -eq 1 ] \ - && [ "$(grep -c 'pr merge 11' "$d/calls.log")" -eq 0 ]; then - ok "auto-merge is re-armed only on the PR that had it armed" -else - bad "auto-merge is re-armed only on the PR that had it armed" "$log" -fi -# The maintainer's chosen squash message must survive the round trip; recreating the request with -# defaults would discard it silently. -if [[ $log == *"--subject custom subject 22"* ]] && [[ $log == *"--body custom body 22"* ]]; then - ok "a custom auto-merge subject and body are restored, not defaulted" -else - bad "a custom auto-merge subject and body are restored, not defaulted" "$log" -fi -if [[ $log == *"pr close 22"* ]]; then - ok "a draft PR is re-triggered too" -else - bad "a draft PR is re-triggered too" "$log" -fi - -# --- auto-merge disabled between the sweep and the close ----------------- -d="$WORK/amrace" -make_gh "$d" "$TWO_PRS" "" "11 22" -cat > "$d/on-view" < "$d/db/viewfail" -out=$(run_script "$d") -rc=$? -if [ "$rc" -eq 1 ] && [ "$(grep -c 'pr close' "$d/calls.log")" -eq 0 ] \ - && [[ $out == *"state could not be read"* ]]; then - ok "a PR whose state cannot be read is left untouched" -else - bad "a PR whose state cannot be read is left untouched" \ - "exit $rc" "$(cat "$d/calls.log")" "$out" -fi - -# --- the original merge strategy survives the round trip ----------------- -# Recreating a merge-commit or rebase auto-merge as a squash would change the merge behaviour the -# maintainer chose, not just its message. -d="$WORK/strategy" -make_gh "$d" "$TWO_PRS" "" "11 22" -printf 'REBASE' > "$d/db/method-11" -printf 'MERGE' > "$d/db/method-22" -out=$(run_script "$d") -rc=$? -log=$(cat "$d/calls.log") -if [ "$rc" -eq 0 ] && [[ $log == *"pr merge 11 --repo owner/name --auto --rebase"* ]] \ - && [[ $log == *"pr merge 22 --repo owner/name --auto --merge"* ]]; then - ok "the original auto-merge strategy is restored, not replaced with squash" -else - bad "the original auto-merge strategy is restored, not replaced with squash" "exit $rc" "$log" -fi -# A rebase carries no commit message, so the message flags must not ride along with it. -rebase_call=$(printf '%s\n' "$log" | sed -n '/pr merge.*pr merge 11 /p') -merge_call=$(printf '%s\n' "$log" | sed -n '/pr merge.*pr merge 22 /p') -if [[ $rebase_call != *--subject* && $rebase_call != *--body* ]] && - [[ $merge_call == *"--subject custom subject 22"* && $merge_call == *"--body custom body 22"* ]] && - [[ $rebase_call == *"--match-head-commit 1111111111111111111111111111111111111111"* ]]; then - ok "commit metadata accompanies a merge or squash, never a rebase" -else - bad "commit metadata accompanies a merge or squash, never a rebase" "$log" -fi - -# --- a PR closed after the listing is left alone ------------------------- -# The listing is a snapshot. Reopening a pull request the maintainer closed in the meantime would -# reverse a deliberate act, and a read of auto-merge alone would not notice: it succeeds for a -# closed pull request too. -d="$WORK/closedafter" -make_gh "$d" "$TWO_PRS" -cat > "$d/on-view" < "$d/db/closed-22" -exit 0 -EOF -chmod +x "$d/on-view" -out=$(run_script "$d") -rc=$? -if [ "$rc" -eq 0 ] && [ "$(grep -c 'pr close 22' "$d/calls.log")" -eq 0 ] \ - && [ "$(grep -c 'pr reopen 22' "$d/calls.log")" -eq 0 ] && [[ $out == *"skipped #22"* ]]; then - ok "a PR closed after the listing is skipped, not reopened" -else - bad "a PR closed after the listing is skipped, not reopened" "exit $rc" "$(cat "$d/calls.log")" "$out" -fi - -# --- auto-merge is not armed before the reopen's own check run exists ---- -# `--auto` merges as soon as the requirements read as met. Immediately after a reopen the newest -# result at that commit is still the PRE-GATE green, so arming there could merge the pull request -# past the very gate the sweep is applying. When no fresh run appears, declining to arm is the -# safe direction: an auto-merge a human restores is recoverable, a merge that skipped a gate is not. -d="$WORK/nofreshcheck" -make_gh "$d" "$TWO_PRS" "" "11" -# Freeze the high-water mark: reopening no longer produces a new workflow run. -sed '/printf.*checkbump.*+ 1.*>.*checkbump/s/.*/ :/' "$d/bin/gh" > "$d/frozen" -mv "$d/frozen" "$d/bin/gh" -chmod +x "$d/bin/gh" -out=$(run_script "$d") -rc=$? -log=$(cat "$d/calls.log") -if [ "$rc" -eq 1 ] && [ "$(grep -c 'pr merge 11' "$d/calls.log")" -eq 0 ] \ - && [[ $out == *"could merge the PR on the pre-gate result"* ]]; then - ok "auto-merge is not armed when no run from the reopen appears" -else - bad "auto-merge is not armed when no run from the reopen appears" "exit $rc" "$log" "$out" -fi -# The pull request itself must still be left open: declining to arm is not a reason to abandon it. -if [ "$(left_closed "$d")" -eq 0 ]; then - ok "declining to re-arm still leaves the PR open" -else - bad "declining to re-arm still leaves the PR open" "$(left_closed "$d") still closed" -fi - -# --- a rerun of the existing run does not satisfy the wait --------------- -# Re-running a workflow keeps its run id and adds an attempt, while creating fresh CHECK runs -# with new ids under the same name. Comparing check runs would therefore accept a manual rerun of -# the PRE-GATE run as proof the reopen registered, and re-arm against that stale green. Comparing -# `pull_request` run ids cannot: only a new event produces a new run id. -d="$WORK/rerun" -make_gh "$d" "$TWO_PRS" "" "11" -sed '/printf.*checkbump.*+ 1.*>.*checkbump/s/.*/ :/' "$d/bin/gh" > "$d/frozen" -mv "$d/frozen" "$d/bin/gh" -chmod +x "$d/bin/gh" -out=$(run_script "$d") -rc=$? -if [ "$rc" -eq 1 ] && [ "$(grep -c 'pr merge 11' "$d/calls.log")" -eq 0 ] \ - && [ "$(left_closed "$d")" -eq 0 ]; then - ok "a rerun that creates no new pull_request run does not satisfy the wait" -else - bad "a rerun that creates no new pull_request run does not satisfy the wait" \ - "exit $rc" "$(cat "$d/calls.log")" "$out" -fi - -# --- an unreadable run baseline leaves the PR untouched ---------------- -# Treating an unknown baseline as 0 would let any historical run satisfy the wait instantly, -# re-arming against the pre-gate green โ€” the window the baseline exists to close. -d="$WORK/baselinefail" -make_gh "$d" "$TWO_PRS" "" "11" -: > "$d/db/checkfail" -out=$(run_script "$d") -rc=$? -if [ "$rc" -eq 1 ] && [ "$(grep -c 'pr close 11' "$d/calls.log")" -eq 0 ] \ - && [ "$(grep -c 'pr merge 11' "$d/calls.log")" -eq 0 ] \ - && [[ $out == *"run baseline could not be read"* ]]; then - ok "an unreadable run baseline leaves the PR untouched, never assumed zero" -else - bad "an unreadable run baseline leaves the PR untouched, never assumed zero" \ - "exit $rc" "$(cat "$d/calls.log")" "$out" -fi - -# --- a malformed listing is an error, never a shorter list --------------- -d="$WORK/badlisting" -make_gh "$d" $'11\tfirst\nnot-a-number\tsecond\n' -out=$(run_script "$d" --dry-run) -rc=$? -if [ "$rc" -eq 2 ] && [[ $out == *"malformed"* ]]; then - ok "a malformed listing fails closed rather than reading as a shorter list" -else - bad "a malformed listing fails closed rather than reading as a shorter list" "exit $rc: $out" -fi - -# A Dependabot close suppresses its release even when the PR is immediately reopened. -# A fork cannot use the same-repository base-update route; it must fail without closing. -for author in 'app/dependabot' 'dependabot[bot]'; do - d="$WORK/dependabot-${author//[^a-z]/}" - make_gh "$d" "$TWO_PRS" '' '11' - printf '%s' "$author" > "$d/db/author-11" - out=$(run_script "$d") - rc=$? - if [ "$rc" -eq 1 ] && ! grep -q '^pr close 11$' "$d/calls.log" \ - && [ -f "$d/db/am-11" ] && [ ! -f "$d/db/closed-11" ]; then - ok "Dependabot fork $author stays open with auto-merge untouched" - else - bad "Dependabot fork $author stays open with auto-merge untouched" "exit $rc" "$(cat "$d/calls.log")" "$out" - fi -done - -# Completion needs the reopened event even when auto-merge was never armed. -for mode in no-new-run reopen-noop moved moved-author moved-base moved-boundary unreadable-readback; do - d="$WORK/unarmed-$mode" - make_gh "$d" $'11\tfirst\n' - : > "$d/db/$mode" - out=$(run_script "$d"); rc=$? - if [ "$rc" -eq 1 ] && [[ "$out" == *"0 of 1 current-base refreshes completed"* ]] && ! grep -q '^pr merge' "$d/calls.log"; then - ok "unarmed $mode reopen cannot report completion" - else bad "unarmed $mode reopen cannot report completion" "exit $rc: $out"; fi -done -d="$WORK/rearm-noop" -make_gh "$d" $'11\tfirst\n' '' 11 -: > "$d/db/merge-noop" -out=$(run_script "$d"); rc=$? -if [ "$rc" -eq 1 ] && [[ "$out" == *"0 of 1 current-base refreshes completed"* ]] && [ ! -f "$d/db/am-11" ]; then - ok "an acknowledged re-arm without matching state cannot report completion" -else bad "an acknowledged re-arm without matching state cannot report completion" "exit $rc: $out"; fi -for method in UNKNOWN ''; do - d="$WORK/method-$method" - make_gh "$d" $'11\tfirst\n' '' 11 - printf '%s' "$method" > "$d/db/method-11" - out=$(run_script "$d"); rc=$? - if [ "$rc" -eq 1 ] && ! grep -q '^pr close' "$d/calls.log" && [ -f "$d/db/am-11" ]; then - ok "unknown method '$method' refuses mutation instead of defaulting to squash" - else bad "unknown method '$method' refuses mutation" "exit $rc: $out"; fi -done - -echo "-----------------------------------------" -echo "recheck-open-prs.sh self-test: $pass passed, $fail failed" -[ "$fail" -eq 0 ]