From 4ba5ec24f5f40b1038361557d7d7b2a2df3991a9 Mon Sep 17 00:00:00 2001 From: luisleo526 Date: Mon, 2 Nov 2026 01:32:42 +0800 Subject: [PATCH] release: handle-upstream dispatches publish after it has tagged handle-upstream now starts publish.yml itself, as the last normal step of its job in the bump, retag and republish modes, once the tag push has succeeded, with a second App token limited to Actions write on this repository and the tag taken from the job's own step output. A retag or republish of a version older than the latest ready release does not dispatch (it says why and succeeds), and a failed dispatch fails the job so the existing producer incident fires. Reconcile also reports a hub tag that has neither a candidate nor an image after a 30 minute grace period, through the existing incident, and never dispatches. The dry-run harness models the dispatch, the operator notices and docs say it, and the tests pin all of it. Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/handle-upstream.yml | 73 +++++- .github/workflows/release-reconcile.yml | 2 +- README.md | 8 +- docs/release-chain.md | 58 ++++- scripts/release_pipeline.py | 60 ++++- tests/test_release_binding.py | 113 ++++++++- tests/test_release_pair.py | 67 +++++ tests/test_release_workflows.py | 309 +++++++++++++++++++++--- tools/release-dry-run/README.md | 17 ++ tools/release-dry-run/dry_run.py | 179 ++++++++++++++ tools/release-dry-run/ready_replay.py | 5 +- tools/release-dry-run/shims/shim.py | 26 ++ 12 files changed, 849 insertions(+), 68 deletions(-) diff --git a/.github/workflows/handle-upstream.yml b/.github/workflows/handle-upstream.yml index f59bbda..4f90f6f 100644 --- a/.github/workflows/handle-upstream.yml +++ b/.github/workflows/handle-upstream.yml @@ -14,8 +14,8 @@ name: handle-upstream # artifacts, and the release version becomes the pair version. # Hpo joins at the single cutoff in scripts/release_pair.py: then an event # probes both partners and the tag records all three versions. -# The handler pushes the tag with the App token. Publication is a separate -# operator dispatch of publish.yml on main with that tag as input (build the +# The handler pushes the tag with the App token, then automatically dispatches +# publish.yml on main with its own tag output when policy allows (build the # combined image -> fan out to the MCPs and the app). Idempotent, with # half-failed-release recovery, a downgrade guard, and serialized concurrency. @@ -314,17 +314,22 @@ jobs: printf '%s\n' "$line" >> "$GITHUB_STEP_SUMMARY" - name: Re-publish (recover a half-failed prior release) + id: republish if: steps.decide.outputs.mode == 'republish' env: TAG: v${{ steps.decide.outputs.cur_ver }} run: | set -euo pipefail - line="versions unchanged but image missing for ${TAG}; after the tag exists on main and handle-upstream is green, a user with write access must run: gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=${TAG}" - echo "::warning title=pineforge publish needed::${line}" + # Republish assumes the tag: a missing tag fails this job on purpose + # at this idempotent push, so the existing producer alert fires. + git push origin "$TAG" + echo "tag=$TAG" >> "$GITHUB_OUTPUT" + line="versions unchanged but image missing for ${TAG}; handle-upstream automatically dispatches publish.yml when allowed by recovery policy" + echo "::notice title=pineforge publish automatic::${line}" printf '%s\n' "$line" >> "$GITHUB_STEP_SUMMARY" - exit 0 - name: Bump VERSION, commit, tag + id: bump if: steps.decide.outputs.mode == 'bump' env: E: ${{ steps.decide.outputs.engine }} @@ -348,8 +353,9 @@ jobs: [ -n "$commit" ] || { echo "::error::VERSION is ${RELEASE} but main has no release commit for it; refusing to tag"; exit 1; } git tag -a "v${RELEASE}" "$commit" -m "$(tag_message "$RELEASE")" git push origin "v${RELEASE}" - line="pushed v${RELEASE} at ${commit} (${components}); after the tag exists on main and handle-upstream is green, a user with write access must run: gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=v${RELEASE}" - echo "::notice title=pineforge publish needed::${line}" + echo "tag=v${RELEASE}" >> "$GITHUB_OUTPUT" + line="pushed v${RELEASE} at ${commit} (${components}); handle-upstream automatically dispatches publish.yml when allowed by recovery policy" + echo "::notice title=pineforge publish automatic::${line}" printf '%s\n' "$line" >> "$GITHUB_STEP_SUMMARY" exit 0 fi @@ -387,10 +393,59 @@ jobs: git tag -a "v${next}" -m "$(tag_message "$next")" git push origin HEAD:main git push origin "v${next}" - line="pushed v${next} (${components}); after the tag exists on main and handle-upstream is green, a user with write access must run: gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=v${next}" - echo "::notice title=pineforge publish needed::${line}" + echo "tag=v${next}" >> "$GITHUB_OUTPUT" + line="pushed v${next} (${components}); handle-upstream automatically dispatches publish.yml when allowed by recovery policy" + echo "::notice title=pineforge publish automatic::${line}" printf '%s\n' "$line" >> "$GITHUB_STEP_SUMMARY" + - name: Check publication recovery policy + id: publish-policy + if: steps.decide.outputs.mode == 'bump' || steps.decide.outputs.mode == 'republish' + env: + GH_TOKEN: ${{ steps.app.outputs.token }} + TAG: ${{ steps.bump.outputs.tag || steps.republish.outputs.tag }} + RECOVERY: ${{ steps.decide.outputs.mode == 'republish' || steps.decide.outputs.retag == 'true' }} + run: | + set -euo pipefail + policy="$(python3 scripts/release_pipeline.py publish-policy --tag "$TAG" --recovery "$RECOVERY")" + printf '%s\n' "$policy" >> "$GITHUB_OUTPUT" + if printf '%s\n' "$policy" | grep -qx 'allowed=false'; then + latest="$(printf '%s\n' "$policy" | sed -n 's/^latest_ready=//p')" + line="automatic publication skipped for ${TAG}: recovery is older than latest ready ${latest}; manual recovery: gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=${TAG}" + echo "::notice title=pineforge publish skipped::${line}" + printf '%s\n' "$line" >> "$GITHUB_STEP_SUMMARY" + fi + + - name: Mint hub Actions token + id: publish-app + if: steps.publish-policy.outputs.allowed == 'true' + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + with: + app-id: ${{ secrets.PINEFORGE_APP_ID }} + private-key: ${{ secrets.PINEFORGE_APP_PRIVATE_KEY }} + owner: ${{ github.repository_owner }} + repositories: pineforge-release + permission-actions: write + github-api-url: https://api.github.com + + - name: Dispatch publication + if: steps.publish-policy.outputs.allowed == 'true' + env: + GH_TOKEN: ${{ steps.publish-app.outputs.token }} + TAG: ${{ steps.bump.outputs.tag || steps.republish.outputs.tag }} + run: | + set -euo pipefail + if gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag="$TAG"; then + line="automatically dispatched publish.yml on main for ${TAG}" + echo "::notice title=pineforge publish dispatched::${line}" + printf '%s\n' "$line" >> "$GITHUB_STEP_SUMMARY" + else + line="automatic publication dispatch failed for ${TAG}; manual recovery: gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=${TAG}" + echo "::error title=pineforge publish dispatch failed::${line}" + printf '%s\n' "$line" >> "$GITHUB_STEP_SUMMARY" + exit 1 + fi + - name: Report failed upstream handling if: ${{ failure() && !cancelled() }} env: diff --git a/.github/workflows/release-reconcile.yml b/.github/workflows/release-reconcile.yml index 8f7dc1a..58c5052 100644 --- a/.github/workflows/release-reconcile.yml +++ b/.github/workflows/release-reconcile.yml @@ -33,7 +33,7 @@ jobs: repositories: pineforge-release permission-contents: write github-api-url: https://api.github.com - - name: Verify artifacts and replay missed stable producer events + - name: Replay missed stable producer events and report stalled tagged publication env: GH_TOKEN: ${{ steps.app.outputs.token }} RELEASE_REPORT_OWNER: ${{ vars.RELEASE_REPORT_OWNER }} diff --git a/README.md b/README.md index a2f4f3a..0fd44e3 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ is the release tag, with its `v`: `v1.0.0`). ``` {engine, codegen, HPO} releases → existing producer events → handle-upstream.yml - → lockstep VERSION + tag push (App) → operator dispatch on main with tag input + → lockstep VERSION + tag push (App) → automatic dispatch on main with tag input → publish.yml: guard tag shape, existence and ancestry → build verified SHA → release-finalize.yml: packages + measured release facts + immutable manifest → pineforge-release → consumer PR → own CI + E2E → merge/deploy → served receipt @@ -168,7 +168,7 @@ is the release tag, with its `v`: `v1.0.0`). - `handle-upstream.yml` — receives `repository_dispatch` from engine / codegen-oss / HPO (`{version, prerelease, run_id}`), applies the pairing rule, sets `VERSION`, commits + pushes the tag (idempotent, with half-failed-release recovery and a - downgrade guard). Publication then needs the operator command below. + downgrade guard). Publication dispatches automatically when recovery policy allows. - `publish.yml` — only by dispatch on `main` with a required `tag` input: first checks the release tag's shape, existence and reachability from main. It checks out the verified commit SHA, checks the pair, waits for upstream @@ -182,10 +182,10 @@ is the release tag, with its `v`: `v1.0.0`). this diagram; each consumer gets an App token scoped to its configured target. A prerelease reaches the application by hand-off, with no repository dispatch and no App token. Stable consumers without verified completion stay pending. -- `release-reconcile.yml` validates and repairs missed stable producer events; +- `release-reconcile.yml` repairs missed stable producer events and reports tagged publication gaps after a grace period; `release-receipts.yml` validates consumer evidence and repairs dropped callbacks. -After the tag exists on main and `handle-upstream` is green, a user with write access runs publication or replay: +Bump/retag/republish dispatch publication automatically when allowed; for older-version recovery or a failed dispatch, a user with write access runs: `gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=`. The first `tag-guard` job requires a main-ref dispatch, validates the complete release tag pattern, fetches tags and main with full history, and requires the diff --git a/docs/release-chain.md b/docs/release-chain.md index 1a48e3c..7de3ab2 100644 --- a/docs/release-chain.md +++ b/docs/release-chain.md @@ -51,6 +51,9 @@ verified commit SHA, with no persisted credentials, so moving a tag between jobs cannot change the tree built. A failed or skipped guard prevents finalization; failure reporting first refuses to check out or run repository code unless the guard succeeded. +The publication job also requires the tag to equal `v` plus the checked-out +`VERSION`, reads component pins from the annotated tag message (with the existing +pair-version or legacy fallback), and applies the pairing rule before building. The reusable `release-finalize.yml` call inherits the caller's main ref. Tag pushes, release events and `workflow_run` do not start publication. @@ -64,14 +67,17 @@ own published artifacts are verified before the existing partner/pair decision. Incomplete lockstep waits. Invalid flags, source mismatches, authentication, rate limits and transport failures fail visibly. -`release-reconcile.yml` wakes hourly or manually. It paginates the +`release-reconcile.yml` is configured to wake hourly or manually. It paginates the three fixed producer repositories, excludes drafts/prereleases/non-product tags and versions older than hub VERSION. It checks whether the desired stable version already has a hub Git tag before downloading any artifacts. Only a version without that tag has its artifacts verified and the **same producer -events** sent back to the existing handler. The handler pushes the hub tag; -after the tag exists on main and `handle-upstream` is green, a user with write -access dispatches `publish.yml` on `main` with that tag using the command below. +events** sent back to the existing handler. After pushing the hub tag successfully, +the handler automatically dispatches `publish.yml` on `main` in bump (including +retag) and republish modes when policy allows; noop and wait never dispatch. +The input tag comes only from the same job's successful tag-step output, never +the event payload. Republish first idempotently pushes its assumed existing tag; +a missing tag fails the job and triggers the producer incident. Finalization then continues even while the release is a draft awaiting prerequisites. Older missed versions are superseded by the newest desired stable version; they are never replayed as downgrades after it. Duplicate notifications reuse @@ -79,17 +85,43 @@ the handler's existing decision. A stored candidate makes an unchanged-version decision a no-op even while the fixed image tag is absent. Nothing calls a producer's version-bump job. -Recovery of an existing hub tag emits an operator notice and never force-pushes -or rewrites the tag. Publication and replay use the same operator command, -run by a user with write access after the tag exists on main and -`handle-upstream` is green: +The post-push `publish-policy` check reuses the finalizer's `finalized_version(store)` +and semver ordering. A normal bump is allowed. Retag/republish recovery older +than the latest ready release succeeds with a notice and step summary, without +minting the dispatch token or dispatching. Equal versions are allowed because +recovery restores the image of the latest ready version; newer versions are +allowed too. An unreadable release store fails the policy check. + +The existing App token keeps Contents write only. A second mint requests +Actions write only, limited to `pineforge-release`, immediately before dispatch. +The second token's blast radius is "dispatch, cancel or re-run ANY workflow of this hub"; the control is the pinned step list (the mint is immediately followed by the dispatch step, the failure reporter is last), which `tests/test_release_workflows.py` freezes. +The job keeps the `release-automation` environment; required reviewers there +can still pause the chain. The owner must confirm the App's Actions write +permission on the hub and the environment's reviewers. + +A failed dispatch fails the job and fires the existing producer incident; +the pushed tag remains and a rerun is safe. Older-version recovery and failed +dispatch notices retain this manual command for a user with write access: `gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=`. Use the actual existing tag as `tag`. Pushing a tag alone publishes nothing. -Both a new tag push and retag recovery leave publication to the operator; -an unchanged version with an image or verified candidate keeps its existing no-op. +Recovery never force-pushes or rewrites the tag; an unchanged version with an +image or verified candidate keeps its existing no-op. Tags predating this workflow need a reviewed migration; the hub does not invent new manifests for historical images without candidate verification. +For an existing hub tag, reconcile reports a deduplicated incident if no +candidate draft and no fixed version image exist after the grace period in +`PUBLISH_START_GRACE_MINUTES`. The clock is the annotated tagger date. +Lightweight tags, unreadable tag metadata, and store or registry errors fail +closed. This tagged-publication check never dispatches publication. + +Live activation caveat: when last read, `release-reconcile`, `release-finalize`, +`release-receipts`, `facts-fanout` and `facts-descriptions` were +`disabled_manually`; the reconcile incident safety net and their scheduled/push +triggers remain inactive until the owner activates them. The reason for those +settings and whether human stable publication was an intentional public-image +gate remain owner decisions; this change does not alter live settings. + Exception on record: on 2026-10-25 the `v1.7.0` tag was re-pointed from `73d2a1eed8393d46cae0fa8d1d785ac2919d5a10` to `ab95811bc18dc7a593ce27f864e340bd270fef79` (the licence-sums fix, #121) by the maintainer's decision after a full pre-proof; the published image's revision label is `ab95811bc18dc7a593ce27f864e340bd270fef79`. ## Candidate and readiness @@ -239,8 +271,8 @@ The sender reuses the existing App token mint, scoped to one target with Contents write. The existing App must be installed on the private compute repository with the App's full permission set, to be confirmed in the App's settings before the owner decides **O-1**. App permissions apply to every -repository in the installation: the existing App already holds Actions write -for the hub, and installing it on compute grants that and every other App +repository in the installation: the documented App permission set includes Actions write +for the hub (the owner must confirm it live), and installing it on compute grants that and every other App permission there. The mint uses `owner: github.repository_owner`, so compute joins the same owner's installation; each mint's requested permissions limit that token rather than the App installation. @@ -1173,7 +1205,7 @@ use `ubuntu-latest` per owner decision 10-21, including publication workflows. | Name / setting | Scope | Purpose | |---|---|---| | `PINEFORGE_APP_ID`, `PINEFORGE_APP_PRIVATE_KEY` (existing) | Hub `release-automation` environment | Short-lived, one-target installation tokens; no PAT fallback. | -| App installation permissions | The App's full permission set on every installed repository, including the private compute repository | Confirm the full set in the App's settings before the owner decides O-1. The App already holds Actions write for the hub; installing it on compute grants that and every other App permission there. The mint uses `owner: github.repository_owner` and joins the same owner's installation. Hub tag pushes and producer-event reconciliation; consumer dispatch and completion evidence reads. Publication and replay use the write-access operator's login. Each mint asks only for the permissions needed by that job: hub Contents write; receipt consumers Contents read/write, Actions read and Pull requests read; delivery-only compute uses only the existing Contents-write dispatch mint. Consumer PR writers in other repos separately need Pull requests write without branch bypass. | +| App installation permissions | The App's full permission set on every installed repository, including the private compute repository | Confirm the full set in the App's settings before the owner decides O-1, including the documented Actions write for the hub; installing it on compute grants that and every other App permission there. The mint uses `owner: github.repository_owner` and joins the same owner's installation. Hub tag pushes and producer-event reconciliation; consumer dispatch and completion evidence reads. Automatic publication uses a separate hub mint with Actions write only; manual recovery uses a write-access operator's login. Each mint asks only for the permissions needed by that job: hub Contents write or the separate hub Actions write; receipt consumers Contents read/write, Actions read and Pull requests read; delivery-only compute uses only the existing Contents-write dispatch mint. Consumer PR writers in other repos separately need Pull requests write without branch bypass. | | `RELEASE_HOSTED_MCP_REPOSITORY`, `RELEASE_APPLICATION_REPOSITORY`, `FACTS_WEB_REPOSITORY` (existing) | Hub `release-automation` environment secrets | Private short repository names for neutral hosted/application/web targets. Public offline/data/license names are fixed in workflows. | | `RELEASE_COMPUTE_REPOSITORY` (new secret) | Hub `release-automation` environment secrets | Private short name of the compute target repository; the application target likewise lives in `RELEASE_APPLICATION_REPOSITORY`. Both reuse the existing one-target App mint. No values belong in source or public audit assets. | | `RELEASE_RECEIPT_POLICY` (new secret) | Hub repository Actions secret (read by the receipt verify job, which runs in the `release-automation` environment; do not also create it in the environment, it would shadow this one) | JSON map of each enabled receipt role to its trusted receipt workflow/artifact/base and full CI/E2E/deploy job inventories, plus optional application-only `post_merge`. Compute needs no entry. Keep private workflow/job details out of public source. | diff --git a/scripts/release_pipeline.py b/scripts/release_pipeline.py index 72dfc42..cd61708 100644 --- a/scripts/release_pipeline.py +++ b/scripts/release_pipeline.py @@ -37,6 +37,7 @@ # Delay after each reserved attempt: two retries, then an acknowledgement grace # period before a terminal failure. Unsent reservations are released by cleanup. DELIVERY_BACKOFF_MINUTES = (15, 60, 15) +PUBLISH_START_GRACE_MINUTES = 30 def now(): @@ -121,7 +122,12 @@ def raw(self, ref): # Public pull token, never a GitHub credential. Reading the actual OCI # response bytes avoids CLI pretty-printing/newline changes to digests. client = Client(token="") - token = decode(client.request("https://ghcr.io/token?service=ghcr.io&scope=repository:" + HUB + ":pull"))["token"] + try: + token = decode(client.request("https://ghcr.io/token?service=ghcr.io&scope=repository:" + HUB + ":pull"))["token"] + except Pending: + # Only a manifest 404 means absence; an unreadable token endpoint + # must not authorize a missing-image incident or an alias write. + raise Invalid("registry authentication unavailable") from None return client.request("https://ghcr.io/v2/" + HUB + "/manifests/" + ref, accept="application/vnd.oci.image.index.v1+json,application/vnd.docker.distribution.manifest.list.v2+json,application/vnd.oci.image.manifest.v1+json,application/vnd.docker.distribution.manifest.v2+json", extra_headers={"Authorization": "Bearer " + token}) @@ -198,6 +204,17 @@ def finalized_version(store): return max(ready) if ready else None +def publish_policy(store, tag, recovery): + """Read the finalizer's ready frontier; storage errors never authorize publication.""" + require(tag.startswith("v"), "publication needs a release tag") + v = version(tag.removeprefix("v")) + newest = finalized_version(store) + # Equality is allowed: recovery restores the image of the latest ready + # version itself. A normal bump is always allowed; only recovery is ordered. + allowed = not recovery or newest is None or v >= newest + return allowed, newest + + def release_superseded(event, rows): """Stable targets yield to stable releases; prereleases yield to either channel.""" ready = [] @@ -878,7 +895,12 @@ def run_link(): def reconcile(client, store): - """Replay stable public releases; never execute producer version-bump logic.""" + """Replay stable producer events; report stalled tagged publication, never dispatch publish. + + Lightweight hub tags raise: only an annotated tagger date can start the + publication grace clock. Store, registry and tag metadata errors propagate; + they never count as absence. Producer version-bump logic is never executed. + """ floor = version(Path("VERSION").read_text().strip()) failures, candidates = [], [] for component, repo in PRODUCERS.items(): @@ -903,8 +925,31 @@ def reconcile(client, store): except Pending: pass else: - # A hub tag hands responsibility to publish/finalize, including drafts - # awaiting facts. Do not re-download producer artifacts or re-dispatch. + tag = "v" + str(desired) + rows = [row for row in store.releases() if row["tag_name"] == tag] + require(len(rows) <= 1, "ambiguous hub release tag") + # Do not use candidate_exists here: it treats Pending as missing, + # whereas even an asset read 404 during reconciliation must fail closed. + if rows and store.read(rows[0], CANDIDATE) is not None: + return + if Registry().digest(str(desired)) is not None: + return + # Only a tag with neither candidate nor image needs its tagger clock, so a + # published release whose hub tag is lightweight is never touched here. + obj = client.api(f"repos/{HUB}/git/ref/tags/{tag}")["object"] + require(obj["type"] == "tag", "publication grace requires an annotated hub tag") + match(COMMIT, obj["sha"]) + annotated = client.api(f"repos/{HUB}/git/tags/{obj['sha']}") + timestamp = annotated["tagger"]["date"] + tagged_at = dt.datetime.fromisoformat(timestamp.replace("Z", "+00:00")) + require(tagged_at.tzinfo is not None, "hub tagger date needs a timezone") + if age_minutes(timestamp) > PUBLISH_START_GRACE_MINUTES: + report_issue(client, f"publish-start-{tag}", f"Release {tag}: publication has not started", + f"Hub tag {tag} has no candidate draft and no image after the publication start grace period.\n\n" + f"Manual recovery: gh workflow run publish.yml --repo {HUB} --ref main -f tag={tag}\n\n" + + run_link()) + # Responsibility stays with publish/finalize. No producer downloads or + # dispatches for an existing hub tag, including drafts awaiting facts. return for v, component in sorted(set(candidates), key=lambda item: (item[0], item[1])): if v != desired: @@ -934,6 +979,9 @@ def main(): p.add_argument("--hpo", default="") p = sub.add_parser("candidate-exists") p.add_argument("tag") + p = sub.add_parser("publish-policy") + p.add_argument("--tag", required=True) + p.add_argument("--recovery", choices=("true", "false"), required=True) p = sub.add_parser("candidate") for field in ("file", "tag", "notes-file", "source-tag", "source-list", "amd64", "arm64"): p.add_argument("--" + field, required=True) @@ -971,6 +1019,10 @@ def main(): args.codegen, args.hpo, args.hub_commit))) elif args.cmd == "candidate-exists": print("exists=" + ("true" if candidate_exists(store, args.tag) else "false")) + elif args.cmd == "publish-policy": + allowed, newest = publish_policy(store, args.tag, args.recovery == "true") + print("allowed=" + str(allowed).lower()) + print("latest_ready=" + (str(newest) if newest is not None else "")) elif args.cmd == "candidate": obj = decode(Path(args.file).read_bytes()) obj.update(sources={"release_tag": args.source_tag, "list_asset": Path(args.source_list).name, diff --git a/tests/test_release_binding.py b/tests/test_release_binding.py index 8210329..ead090d 100644 --- a/tests/test_release_binding.py +++ b/tests/test_release_binding.py @@ -9,6 +9,7 @@ import base64 import copy +import datetime as dt import hashlib import io import json @@ -1903,6 +1904,10 @@ def __init__(self): super().__init__() self.dispatched = [] self.hub_tag = False + self.hub_ref = {"type": "tag", "sha": "a" * 40} + self.hub_annotation = {"tagger": {"date": "2026-10-16T00:00:00Z"}, + "object": {"type": "commit", "sha": "d" * 40}} + self.issues, self.issue_writes = [], [] def tag_commit(self, repo, tag): if repo == contract.HUB: @@ -1912,12 +1917,22 @@ def tag_commit(self, repo, tag): return super().tag_commit(repo, tag) def pages(self, route): + if route == f"repos/{contract.HUB}/issues?state=all": + return copy.deepcopy(self.issues) role = next(role for role, repo in artifacts.PRODUCERS.items() if route == f"repos/{repo}/releases") return [self.releases[role], {"tag_name": "v1.5.9", "draft": False, "prerelease": False}, {"tag_name": "v9.9.9", "draft": True, "prerelease": False}, {"tag_name": "v9.9.9rc1" if role == "hpo" else "v9.9.9-rc.1", "draft": False, "prerelease": True}] def api(self, route, **kwargs): + if route == f"repos/{contract.HUB}/git/ref/tags/v1.6.0": + return {"object": self.hub_ref} + if route == f"repos/{contract.HUB}/git/tags/{'a' * 40}": + return self.hub_annotation + if route == f"repos/{contract.HUB}/issues" and kwargs.get("method") == "POST": + self.issue_writes.append(copy.deepcopy(kwargs["data"])) + self.issues.append({"number": len(self.issues) + 1, "state": "open", **kwargs["data"]}) + return self.issues[-1] if route == f"repos/{contract.HUB}/dispatches": assert kwargs["method"] == "POST" self.dispatched.append(kwargs["data"]) @@ -1954,11 +1969,107 @@ def test_missing_upload_does_not_replay_that_producer_and_auth_errors_are_not_pe def test_existing_hub_tag_skips_all_producer_downloads_and_redispatches(self): client = self.client() client.hub_tag = True + store = MemoryStore(fixture_manifest()) with patch.object(pipeline.Path, "read_text", return_value="1.5.9\n"), \ patch.object(client, "download", side_effect=AssertionError("handled version must not download artifacts")): for _ in range(3): - pipeline.reconcile(client, None) + pipeline.reconcile(client, store) self.assertEqual(client.dispatched, []) + self.assertEqual(client.issue_writes, []) + + def tagged_reconcile(self, client, store, registry): + with patch.object(pipeline.Path, "read_text", return_value="1.5.9\n"), \ + patch.object(pipeline, "Registry", return_value=registry): + pipeline.reconcile(client, store) + + def test_tag_without_candidate_or_image_reports_only_after_grace_and_deduplicates(self): + class Clock(dt.datetime): + @classmethod + def now(cls, tz=None): + return cls(2026, 11, 2, 12, tzinfo=dt.timezone.utc) + for offset, expected in ((-1, 0), (0, 0), (1, 1)): + client = self.client() + client.hub_tag = True + stamp = Clock.now() - dt.timedelta(minutes=pipeline.PUBLISH_START_GRACE_MINUTES + offset) + client.hub_annotation["tagger"]["date"] = stamp.isoformat() + with self.subTest(offset=offset), patch.object(pipeline.dt, "datetime", Clock), \ + patch.object(pipeline, "producer", side_effect=AssertionError("tagged release must not verify producers")), \ + patch.dict(os.environ, {"RELEASE_REPORT_OWNER": "", "RELEASE_REPORT_TOKEN": "", "GITHUB_RUN_ID": "42"}): + for _ in range(2): + self.tagged_reconcile(client, MemoryStore(), MemoryRegistry()) + self.assertEqual(len(client.issue_writes), expected) + self.assertEqual(client.dispatched, []) + if expected: + issue = client.issue_writes[0] + self.assertIn("", issue["body"]) + self.assertIn("no candidate draft and no image", issue["body"]) + self.assertIn("gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=v1.6.0", + issue["body"]) + + def test_tag_with_candidate_or_image_never_reports_missing_publication(self): + for present in ("candidate", "image"): + client = self.client() + client.hub_tag = True + store = MemoryStore(fixture_manifest()) if present == "candidate" else MemoryStore() + registry = MemoryRegistry() + if present == "image": + registry.tags["1.6.0"] = "sha256:" + "b" * 64 + with self.subTest(present=present), patch.object(pipeline, "report_issue") as alert: + self.tagged_reconcile(client, store, registry) + alert.assert_not_called() + self.assertEqual(client.dispatched, []) + + def test_published_release_with_a_lightweight_hub_tag_is_left_alone(self): + for present in ("candidate", "image"): + client = self.client() + client.hub_tag = True + client.hub_ref["type"] = "commit" # a lightweight tag: no tagger clock exists + store = MemoryStore(fixture_manifest()) if present == "candidate" else MemoryStore() + registry = MemoryRegistry() + if present == "image": + registry.tags["1.6.0"] = "sha256:" + "b" * 64 + with self.subTest(present=present), patch.object(pipeline, "report_issue") as alert: + self.tagged_reconcile(client, store, registry) + alert.assert_not_called() + self.assertEqual(client.dispatched, []) + + def test_tagged_publication_probe_errors_are_never_absence(self): + for boundary in ("releases", "read", "registry", "tag"): + for error in (contract.Invalid("HTTP 503"), contract.Pending("unreadable store")): + client = self.client() + client.hub_tag = True + store, registry = MemoryStore(fixture_manifest()), MemoryRegistry() + target, method = {"releases": (store, "releases"), "read": (store, "read"), + "registry": (registry, "digest"), "tag": (client, "api")}[boundary] + if boundary in ("registry", "tag"): + store = MemoryStore() # the tag is read only when neither candidate nor image exists + with self.subTest(boundary=boundary, error=type(error).__name__), \ + patch.object(target, method, side_effect=error), patch.object(pipeline, "report_issue") as alert: + with self.assertRaises((contract.Invalid, contract.Pending)): + self.tagged_reconcile(client, store, registry) + alert.assert_not_called() + self.assertEqual(client.dispatched, []) + + def test_lightweight_tag_and_unusable_tagger_date_fail_closed(self): + for mutation in ("lightweight", "missing", "malformed", "timezone"): + client = self.client() + client.hub_tag = True + if mutation == "lightweight": + client.hub_ref["type"] = "commit" + else: + client.hub_annotation["tagger"] = { + "missing": {}, "malformed": {"date": "yesterday"}, + "timezone": {"date": "2026-10-16T00:00:00"}}[mutation] + with self.subTest(mutation=mutation), patch.object(pipeline, "report_issue") as alert: + with self.assertRaises((contract.Invalid, ValueError, KeyError, TypeError)): + self.tagged_reconcile(client, MemoryStore(), MemoryRegistry()) + alert.assert_not_called() + self.assertEqual(client.dispatched, []) + + def test_registry_token_error_is_not_an_absent_manifest(self): + with patch.object(pipeline.Client, "request", side_effect=contract.Pending("token endpoint unavailable")), \ + self.assertRaises(contract.Invalid): + pipeline.Registry().digest("1.6.0") def test_unknown_hub_tag_probe_errors_do_not_authorize_replay(self): client = self.client() diff --git a/tests/test_release_pair.py b/tests/test_release_pair.py index 094607c..3373844 100644 --- a/tests/test_release_pair.py +++ b/tests/test_release_pair.py @@ -9,18 +9,22 @@ """ from __future__ import annotations +import io import re import subprocess import sys import unittest +from contextlib import redirect_stderr, redirect_stdout from itertools import permutations from pathlib import Path +from unittest.mock import patch REPO = Path(__file__).resolve().parent.parent SCRIPT = REPO / "scripts" / "release_pair.py" sys.path.insert(0, str(SCRIPT.parent)) import release_pair as rp # noqa: E402 +import release_pipeline as pipeline # noqa: E402 def run(*args: str, stdin: str = "") -> subprocess.CompletedProcess: @@ -50,6 +54,69 @@ def check(release: str, engine: str, codegen: str, newest: str = "") -> subproce "--newest-stable", newest) +class PublishPolicyTest(unittest.TestCase): + """Exercise the CLI and the real ready-frontier selector, with external storage fixtures.""" + + class Store: + def __init__(self): + self.rows = [ + {"tag_name": "v1.9.2", "draft": False, "assets": [{"name": "release-manifest.json"}]}, + {"tag_name": "v1.10.0", "draft": False, "assets": [{"name": "release-manifest.json"}]}, + {"tag_name": "v3.0.0", "draft": True, "assets": [{"name": "release-manifest.json"}]}, + {"tag_name": "v4.0.0-rc.1", "draft": False, "assets": [{"name": "release-manifest.json"}]}, + {"tag_name": "v5.0.0", "draft": False, "assets": []}, + {"tag_name": "v6.0.0", "draft": False, "assets": [{"name": "release-manifest.json"}]}, + ] + self.assets = {("v6.0.0", "release-rejected.json"): b'{"rejected":true}\n'} + + def releases(self): + return self.rows + + def read(self, row, name): + return self.assets.get((row["tag_name"], name)) + + def cli(self, tag, recovery, store): + stdout, stderr = io.StringIO(), io.StringIO() + with patch.object(sys, "argv", ["release_pipeline.py", "publish-policy", "--tag", tag, + "--recovery", recovery]), \ + patch.object(pipeline, "Client"), patch.object(pipeline, "Store", return_value=store), \ + redirect_stdout(stdout), redirect_stderr(stderr): + result = pipeline.main() + return result, stdout.getvalue(), stderr.getvalue() + + def test_recovery_order_uses_semver_and_the_existing_ready_frontier(self): + for tag, allowed in (("v1.9.2", "false"), ("v1.10.0-rc.12", "false"), + ("v1.10.0", "true"), ("v1.10.1-rc.1", "true"), ("v1.11.0", "true")): + with self.subTest(tag=tag): + result, stdout, stderr = self.cli(tag, "true", self.Store()) + self.assertEqual(result, 0, stderr) + self.assertEqual(stdout, f"allowed={allowed}\nlatest_ready=1.10.0\n") + self.assertEqual(stderr, "") + + def test_normal_bump_is_allowed_even_below_the_frontier(self): + result, stdout, stderr = self.cli("v1.9.2", "false", self.Store()) + self.assertEqual(result, 0, stderr) + self.assertEqual(stdout, "allowed=true\nlatest_ready=1.10.0\n") + + def test_empty_ready_frontier_allows_recovery(self): + store = self.Store() + store.rows = [] + result, stdout, stderr = self.cli("v0.1.26", "true", store) + self.assertEqual(result, 0, stderr) + self.assertEqual(stdout, "allowed=true\nlatest_ready=\n") + + def test_unreadable_store_never_authorizes_a_bump_or_recovery(self): + for recovery in ("true", "false"): + for failure in (pipeline.Invalid("HTTP 403"), pipeline.Pending("store unavailable")): + for operation in ("releases", "read"): + with self.subTest(recovery=recovery, operation=operation, failure=type(failure).__name__), \ + patch.object(self.Store, operation, side_effect=failure): + result, stdout, stderr = self.cli("v1.10.0", recovery, self.Store()) + self.assertNotEqual(result, 0) + self.assertNotIn("allowed=", stdout) + self.assertTrue(stderr) + + class VersionTest(unittest.TestCase): def test_prerelease_sorts_below_its_release(self): ordered = ["0.1.25", "0.13.1", "1.0.0-alpha.1", "1.0.0-beta.1", "1.0.0-rc.1", diff --git a/tests/test_release_workflows.py b/tests/test_release_workflows.py index e251ab0..5554535 100644 --- a/tests/test_release_workflows.py +++ b/tests/test_release_workflows.py @@ -145,6 +145,135 @@ def test_the_harness_is_synced_by_the_script_from_the_pinned_tag(self): self.assertNotIn("PF_ENGINE_REF", self.text) +class HandlerPublicationReplayTest(unittest.TestCase): + """Replay the real post-decision steps with local git and external-service fixtures.""" + + def replay(self, root, **kwargs): + sys.path.insert(0, str(REPO / "tools/release-dry-run")) + from dry_run import PublicationReplay + return PublicationReplay(REPO, root, **kwargs) + + def dispatches(self, result): + return [row for row in result["calls"] if row.get("action") == "workflow run"] + + def mints(self, result): + return [row for row in result["calls"] if row.get("tool") == "actions/create-github-app-token"] + + def test_bump_dispatches_the_pushed_output_tag_on_main_for_both_release_lines(self): + for version, expected in (("0.1.26", "v0.1.26"), ("1.6.0-rc.1", "v1.6.0-rc.1"), ("1.6.0", "v1.6.0")): + with self.subTest(version=version), tempfile.TemporaryDirectory() as tmp: + replay = self.replay(Path(tmp), version=version) + result = replay.run() + self.assertTrue(result["ok"], result["text"]) + sent = self.dispatches(result) + self.assertEqual(len(sent), 1) + self.assertEqual(sent[0]["workflow"], "publish.yml") + self.assertEqual(sent[0]["repo"], "pineforge-4pass/pineforge-release") + self.assertEqual(sent[0]["ref"], "main") + self.assertEqual(sent[0]["inputs"], {"tag": expected}) + self.assertTrue(sent[0]["remote_tag"], "the dispatch observed the tag on origin") + self.assertEqual(result["steps"]["bump"]["outputs"]["tag"], expected) + self.assertEqual(result["executed"][-3:], + ["Check publication recovery policy", "Mint hub Actions token", "Dispatch publication"]) + self.assertEqual(len(self.mints(result)), 1) + self.assertEqual(self.mints(result)[0]["permissions"], {"permission-actions": "write"}) + self.assertEqual(self.mints(result)[0]["repositories"], "pineforge-release") + + def test_dispatch_tag_comes_from_the_same_job_even_with_a_different_payload_tag(self): + for payload in ("v99.0.0", "v0.1.0", "v1.6.0;$(touch payload-executed)"): + for mode, retag in (("bump", False), ("bump", True), ("republish", False)): + with self.subTest(payload=payload, mode=mode, retag=retag), tempfile.TemporaryDirectory() as tmp: + replay = self.replay(Path(tmp), payload_tag=payload, mode=mode, retag=retag) + result = replay.run() + self.assertTrue(result["ok"], result["text"]) + self.assertEqual([row["inputs"] for row in self.dispatches(result)], [{"tag": "v1.6.0"}]) + self.assertFalse((replay.work / "payload-executed").exists()) + + def test_noop_and_wait_do_not_mint_or_dispatch_publication(self): + for mode in ("noop", "wait"): + with self.subTest(mode=mode), tempfile.TemporaryDirectory() as tmp: + result = self.replay(Path(tmp), mode=mode).run() + self.assertTrue(result["ok"], result["text"]) + self.assertEqual(self.dispatches(result), []) + self.assertEqual(self.mints(result), []) + self.assertNotIn("Check publication recovery policy", result["executed"]) + + def test_retag_and_republish_skip_older_recovery_but_allow_equal_and_newer(self): + for mode, retag in (("bump", True), ("republish", False)): + for latest, allowed in (("1.6.1", False), ("1.6.0", True), ("1.5.9", True)): + with self.subTest(mode=mode, latest=latest), tempfile.TemporaryDirectory() as tmp: + result = self.replay(Path(tmp), mode=mode, retag=retag, latest=latest).run() + self.assertTrue(result["ok"], result["text"]) + self.assertEqual(len(self.dispatches(result)), 1 if allowed else 0) + self.assertEqual(len(self.mints(result)), 1 if allowed else 0) + if not allowed: + manual = "gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=v1.6.0" + self.assertIn("::notice title=pineforge publish skipped::", result["text"]) + self.assertIn("older than latest ready 1.6.1", result["text"]) + self.assertIn(manual, result["text"]) + self.assertIn(manual, result["summary"]) + + def test_failed_dispatch_fails_the_job_keeps_the_tag_and_reports_the_producer_incident(self): + with tempfile.TemporaryDirectory() as tmp: + replay = self.replay(Path(tmp)) + (replay.state / "workflow-dispatch.fail").touch() + result = replay.run() + self.assertFalse(result["ok"]) + self.assertEqual(result["failed_step"], "Dispatch publication") + self.assertEqual(len(self.dispatches(result)), 1) + self.assertTrue(self.dispatches(result)[0]["failed"]) + self.assertTrue(self.dispatches(result)[0]["remote_tag"]) + manual = "gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=v1.6.0" + self.assertIn(manual, result["text"]) + self.assertIn(manual, result["summary"]) + self.assertIn("::error title=pineforge publish dispatch failed::", result["text"]) + self.assertEqual(result["executed"][-1], "Report failed upstream handling") + issues = [row for row in result["calls"] if row.get("action") == "issue create"] + self.assertEqual(len(issues), 1) + self.assertIn("", issues[0]["body"]["body"]) + (replay.state / "workflow-dispatch.fail").unlink() + retried = replay.run(mode="republish") + self.assertTrue(retried["ok"], retried["text"]) + self.assertEqual([row["inputs"] for row in self.dispatches(retried)], [{"tag": "v1.6.0"}]) + + def test_tag_push_failure_or_missing_republish_tag_never_authorizes_dispatch(self): + for mode, options in (("bump", {"reject_push": True}), + ("republish", {"tag_present": False}), + ("republish", {"reject_push": True})): + with self.subTest(mode=mode, options=options), tempfile.TemporaryDirectory() as tmp: + result = self.replay(Path(tmp), mode=mode, **options).run() + self.assertFalse(result["ok"]) + self.assertEqual(result["failed_step"], "Bump VERSION, commit, tag" if mode == "bump" + else "Re-publish (recover a half-failed prior release)") + self.assertEqual(self.dispatches(result), []) + self.assertEqual(self.mints(result), []) + output = result["steps"].get("republish" if mode == "republish" else "bump", {}).get("outputs", {}) + self.assertNotIn("tag", output) + self.assertEqual(result["executed"][-1], "Report failed upstream handling") + + def test_store_error_fails_policy_before_token_mint_or_dispatch(self): + with tempfile.TemporaryDirectory() as tmp: + replay = self.replay(Path(tmp)) + (replay.state / "store-read.fail").touch() + result = replay.run() + self.assertFalse(result["ok"]) + self.assertEqual(result["failed_step"], "Check publication recovery policy") + self.assertEqual(self.mints(result), []) + self.assertEqual(self.dispatches(result), []) + self.assertEqual(result["executed"][-1], "Report failed upstream handling") + + def test_dispatch_shim_rejects_other_refs_missing_or_extra_inputs(self): + for arguments in (["publish.yml", "--repo", "pineforge-4pass/pineforge-release", "--ref", "topic", "-f", "tag=v1.6.0"], + ["publish.yml", "--repo", "pineforge-4pass/pineforge-release", "--ref", "main"], + ["publish.yml", "--repo", "pineforge-4pass/pineforge-release", "--ref", "main", + "-f", "tag=v1.6.0", "-f", "extra=true"]): + with self.subTest(arguments=arguments), tempfile.TemporaryDirectory() as tmp: + replay = self.replay(Path(tmp)) + done = subprocess.run(["gh", "workflow", "run", *arguments], cwd=replay.work, + env=replay.env, capture_output=True, text=True) + self.assertNotEqual(done.returncode, 0) + + class PublishTest(unittest.TestCase): @classmethod def setUpClass(cls): @@ -281,6 +410,32 @@ class ReleaseTriggerWorkflowTest(unittest.TestCase): "(needs.publish.result == 'success' || needs.publish.result == 'skipped') }}" ) OPERATOR_COMMAND = "gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=" + TAG_OUTPUTS = "${{ steps.bump.outputs.tag || steps.republish.outputs.tag }}" + PUBLICATION_IF = "steps.publish-policy.outputs.allowed == 'true'" + POLICY_RUN = textwrap.dedent("""\ + set -euo pipefail + policy="$(python3 scripts/release_pipeline.py publish-policy --tag "$TAG" --recovery "$RECOVERY")" + printf '%s\\n' "$policy" >> "$GITHUB_OUTPUT" + if printf '%s\\n' "$policy" | grep -qx 'allowed=false'; then + latest="$(printf '%s\\n' "$policy" | sed -n 's/^latest_ready=//p')" + line="automatic publication skipped for ${TAG}: recovery is older than latest ready ${latest}; manual recovery: gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=${TAG}" + echo "::notice title=pineforge publish skipped::${line}" + printf '%s\\n' "$line" >> "$GITHUB_STEP_SUMMARY" + fi + """) + DISPATCH_RUN = textwrap.dedent("""\ + set -euo pipefail + if gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag="$TAG"; then + line="automatically dispatched publish.yml on main for ${TAG}" + echo "::notice title=pineforge publish dispatched::${line}" + printf '%s\\n' "$line" >> "$GITHUB_STEP_SUMMARY" + else + line="automatic publication dispatch failed for ${TAG}; manual recovery: gh workflow run publish.yml --repo pineforge-4pass/pineforge-release --ref main -f tag=${TAG}" + echo "::error title=pineforge publish dispatch failed::${line}" + printf '%s\\n' "$line" >> "$GITHUB_STEP_SUMMARY" + exit 1 + fi + """) INPUT = ( ' inputs:\n' ' tag:\n' @@ -438,42 +593,96 @@ def assert_no_publish_dispatch(self, text): self.assertNotRegex(code, pattern, "no workflow may dispatch any workflow") def assert_handler(self, text): - self.assert_no_publish_dispatch(text) wf = self.parse(text) - steps = wf.get("jobs", {}).get("bump", {}).get("steps", []) + job = wf.get("jobs", {}).get("bump", {}) + self.assertEqual(job.get("environment"), "release-automation") + steps = job.get("steps", []) + self.assertEqual([row.get("name") for row in steps], [ + "Mint org App token", "Checkout main (App token bypasses the ruleset)", + "Validate payload", "Validate producer event JSON types", + "Verify the dispatched producer artifacts", "Configure git identity", + "Log in to GHCR (read, for the image-exists probe)", + "Resolve the landed component pins", "Probe the partner releases (pairing line)", + "Decide action (pairing rule + idempotent + recover half-failed release + downgrade guard)", + "No-op", "Wait for the partner release (1.0 pair incomplete)", + "Re-publish (recover a half-failed prior release)", "Bump VERSION, commit, tag", + "Check publication recovery policy", "Mint hub Actions token", "Dispatch publication", + "Report failed upstream handling", + ], "the narrow mint must immediately precede dispatch; the failure reporter is last") app = next((row for row in steps if row.get("id") == "app"), {}) - self.assertEqual(app.get("with", {}).get("permission-contents"), "write") - self.assertNotIn("permission-actions", app.get("with", {})) - self.assertNotRegex(_step(text, "Mint org App token"), r"(?m)^\s*permission-actions\s*:") + self.assertEqual(app.get("with"), { + "app-id": "${{ secrets.PINEFORGE_APP_ID }}", + "private-key": "${{ secrets.PINEFORGE_APP_PRIVATE_KEY }}", + "owner": "${{ github.repository_owner }}", "repositories": "pineforge-release", + "permission-contents": "write", "github-api-url": "https://api.github.com", + }) + self.assertEqual(steps[-3], { + "name": "Mint hub Actions token", "id": "publish-app", "if": self.PUBLICATION_IF, + "uses": "actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510", + "with": { + "app-id": "${{ secrets.PINEFORGE_APP_ID }}", + "private-key": "${{ secrets.PINEFORGE_APP_PRIVATE_KEY }}", + "owner": "${{ github.repository_owner }}", "repositories": "pineforge-release", + "permission-actions": "write", "github-api-url": "https://api.github.com", + }, + }) + self.assertEqual(steps[-2], { + "name": "Dispatch publication", "if": self.PUBLICATION_IF, + "env": {"GH_TOKEN": "${{ steps.publish-app.outputs.token }}", "TAG": self.TAG_OUTPUTS}, + "run": self.DISPATCH_RUN, + }) + self.assertEqual(steps[-4], { + "name": "Check publication recovery policy", "id": "publish-policy", + "if": "steps.decide.outputs.mode == 'bump' || steps.decide.outputs.mode == 'republish'", + "env": { + "GH_TOKEN": "${{ steps.app.outputs.token }}", "TAG": self.TAG_OUTPUTS, + "RECOVERY": "${{ steps.decide.outputs.mode == 'republish' || steps.decide.outputs.retag == 'true' }}", + }, + "run": self.POLICY_RUN, + }) + self.assertEqual(steps[-1], { + "name": "Report failed upstream handling", "if": "${{ failure() && !cancelled() }}", + "env": {"GH_TOKEN": "${{ github.token }}", "RELEASE_REPORT_OWNER": "${{ vars.RELEASE_REPORT_OWNER }}"}, + "run": "python3 scripts/release_pipeline.py alert --stage producer", + }) + # Only this completely pinned step may dispatch a workflow. Keep the + # generic no-dispatch check over every other step and every other file. + dispatch_body = _step(text, "Dispatch publication") + self.assert_no_publish_dispatch(text.replace(dispatch_body, "", 1)) replay = next((row for row in steps if row.get("name", "").startswith("Re-publish")), {}) bump = next((row for row in steps if row.get("name", "").startswith("Bump VERSION")), {}) + self.assertEqual(replay.get("id"), "republish") + self.assertEqual(bump.get("id"), "bump") self.assertEqual(replay.get("if"), "steps.decide.outputs.mode == 'republish'") self.assertEqual(replay.get("env", {}).get("TAG"), "v${{ steps.decide.outputs.cur_ver }}") for step in (replay, bump): + self.assertNotIn("continue-on-error", step) self.assertNotIn("GH_TOKEN", step.get("env", {})) self.assertEqual(bump.get("name"), "Bump VERSION, commit, tag") self.assertEqual(bump.get("if"), "steps.decide.outputs.mode == 'bump'") summary = 'printf \'%s\\n\' "$line" >> "$GITHUB_STEP_SUMMARY"' replay_run = replay.get("run", "") - self.assertIn(self.OPERATOR_COMMAND + '${TAG}"', replay_run) - self.assertIn('echo "::warning title=pineforge publish needed::${line}"', replay_run) + self.assertIn('git push origin "$TAG"\n' + 'echo "tag=$TAG" >> "$GITHUB_OUTPUT"', replay_run) + self.assertIn("a missing tag fails this job on purpose", replay_run) + self.assertIn("automatically dispatches publish.yml when allowed", replay_run) + self.assertIn('echo "::notice title=pineforge publish automatic::${line}"', replay_run) self.assertIn(summary, replay_run) - self.assertEqual(replay_run.splitlines()[-1], "exit 0") run = bump.get("run", "") commands = [line.strip() for line in run.splitlines()] for variable in ("RELEASE", "next"): - # Both paths print the operator command only after the tag push succeeds. push = 'git push origin "v${' + variable + '}"' self.assertIn(push, commands) at = commands.index(push) - self.assertLess(at + 3, len(commands)) - self.assertTrue(commands[at + 1].startswith('line="pushed v${' + variable + '}')) - self.assertIn(self.OPERATOR_COMMAND + 'v${' + variable + '}"', commands[at + 1]) - self.assertEqual(commands[at + 2], 'echo "::notice title=pineforge publish needed::${line}"') - self.assertEqual(commands[at + 3], summary) + # a mutant that deletes a trailing line must fail as an assertion, not as an IndexError + self.assertGreater(len(commands), at + 4, "the output and notice lines must follow the tag push") + self.assertEqual(commands[at + 1], 'echo "tag=v${' + variable + '}" >> "$GITHUB_OUTPUT"') + self.assertTrue(commands[at + 2].startswith('line="pushed v${' + variable + '}')) + self.assertIn("automatically dispatches publish.yml when allowed", commands[at + 2]) + self.assertEqual(commands[at + 3], 'echo "::notice title=pineforge publish automatic::${line}"') + self.assertEqual(commands[at + 4], summary) if variable == "RELEASE": - self.assertLess(at + 4, len(commands)) - self.assertEqual(commands[at + 4], "exit 0") + self.assertGreater(len(commands), at + 5, "the retag path must end with exit 0") + self.assertEqual(commands[at + 5], "exit 0") def assert_release_triggers(self, text): wf = self.parse(text) @@ -490,23 +699,26 @@ def assert_release_triggers(self, text): self.assertEqual(push.get("branches"), ["main"]) self.assertLessEqual(set(push), {"branches", "paths", "paths-ignore"}, "no tag or branch-ignore filters") - def test_publish_uses_operator_dispatch_and_handler_prints_the_next_command(self): + def test_publish_keeps_main_dispatch_and_handler_dispatches_after_tagging(self): self.assert_publish((WORKFLOWS / "publish.yml").read_text()) self.assert_handler((WORKFLOWS / "handle-upstream.yml").read_text()) - def test_no_workflow_dispatches_publish(self): + def test_only_the_pinned_handler_step_may_dispatch_a_workflow(self): found = [] for path in sorted(WORKFLOWS.iterdir()): if path.suffix in {".yml", ".yaml"}: found.append(path.name) with self.subTest(workflow=path.name): - self.assert_no_publish_dispatch(path.read_text()) + if path.name == "handle-upstream.yml": + self.assert_handler(path.read_text()) + else: + self.assert_no_publish_dispatch(path.read_text()) self.assertIn("handle-upstream.yml", found) self.assertIn("publish.yml", found) - def test_no_dispatch_pin_refuses_cli_javascript_and_api_steps_on_real_text(self): + def test_dispatch_pin_refuses_additional_cli_javascript_and_api_steps_on_real_text(self): text = (WORKFLOWS / "handle-upstream.yml").read_text() - self.assert_no_publish_dispatch(text) + self.assert_handler(text) anchor = " - name: Report failed upstream handling\n" steps = ( " run: " + self.OPERATOR_COMMAND + "v1.6.2\n", @@ -532,7 +744,7 @@ def test_no_dispatch_pin_refuses_cli_javascript_and_api_steps_on_real_text(self) for step in steps: mutant = self.mutate(text, anchor, " - name: Forbidden publish dispatch\n" + step + anchor) with self.subTest(step=step), self.assertRaises(AssertionError): - self.assert_no_publish_dispatch(mutant) + self.assert_handler(mutant) def test_every_release_workflow_has_only_main_ref_trigger_types(self): found = [] @@ -649,37 +861,48 @@ def test_publish_refuses_bad_triggers_and_each_broken_input_boundary_on_real_tex with self.assertRaises(AssertionError): self.assert_publish(late_guard) - def test_handler_pin_refuses_dispatch_permissions_tokens_or_broken_operator_notices(self): + def test_handler_pin_refuses_wider_tokens_unsafe_dispatch_and_broken_notices(self): text = (WORKFLOWS / "handle-upstream.yml").read_text() self.assert_handler(text) - for tag in ('${TAG}', 'v${RELEASE}', 'v${next}'): - command = self.OPERATOR_COMMAND + tag - for replacement in (command.replace("--ref main", '--ref topic'), - command.removesuffix(" -f tag=" + tag), ""): - mutant = self.mutate(text, command, replacement) - with self.subTest(command=command, replacement=replacement), self.assertRaises(AssertionError): - self.assert_handler(mutant) for before, after in ( (" permission-contents: write\n", " permission-contents: write\n permission-actions: write\n"), + (" permission-actions: write\n", + " permission-actions: write\n permission-contents: write\n"), + (" permission-actions: write", " permission-actions: read"), + (" - name: Mint hub Actions token\n", + " - name: Mint hub Actions token\n continue-on-error: true\n"), + (" - name: Dispatch publication\n", + " - name: Dispatch publication\n continue-on-error: true\n"), + ('-f tag="$TAG"; then', '-f tag="${{ github.event.client_payload.tag }}"; then'), + ('-f tag="$TAG"; then', '-f tag="$TAG" || true; then'), + ('-f tag="$TAG"; then', '-f tag="$TAG" --ref topic; then'), + ('-f tag="$TAG"; then', '-f tag="$TAG"; then\n gh workflow run publish.yml --ref main -f tag="$TAG"'), (' env:\n TAG: v${{ steps.decide.outputs.cur_ver }}', ' env:\n GH_TOKEN: ${{ steps.app.outputs.token }}\n TAG: v${{ steps.decide.outputs.cur_ver }}'), (' env:\n E:', ' env:\n GH_TOKEN: ${{ steps.app.outputs.token }}\n E:'), (" if: steps.decide.outputs.mode == 'republish'", " if: always()"), - (" if: steps.decide.outputs.mode == 'bump'", " if: always()"), + # trailing newline: the publish-policy step's own `if:` starts with the same text + (" if: steps.decide.outputs.mode == 'bump'\n", " if: always()\n"), (" TAG: v${{ steps.decide.outputs.cur_ver }}", " TAG: v0.0.0"), (" - name: Bump VERSION, commit, tag\n", " - name: Bump VERSION, commit, tag (fires publish.yml)\n"), - ('echo "::warning title=pineforge publish needed::${line}"', 'echo "$line"'), + (' git push origin "$TAG"\n', ""), ): mutant = self.mutate(text, before, after) with self.subTest(anchor=before), self.assertRaises(AssertionError): self.assert_handler(mutant) - for name, annotation, notices in (("Re-publish", "warning", 1), ("Bump VERSION", "notice", 2)): + for before, after in ( + (" if: " + self.PUBLICATION_IF + "\n", " if: always()\n"), + ("TAG: " + self.TAG_OUTPUTS, "TAG: ${{ github.event.client_payload.tag }}"), + ): + mutant = self.mutate(text, before, after, count=2) + with self.subTest(anchor=before), self.assertRaises(AssertionError): + self.assert_handler(mutant) + for name, notices in (("Re-publish", 1), ("Bump VERSION", 2)): body = _step(text, name) for command, count in ( ('printf \'%s\\n\' "$line" >> "$GITHUB_STEP_SUMMARY"', notices), - ('echo "::' + annotation + ' title=pineforge publish needed::${line}"', notices), - ("exit 0", 1), + ('echo "::notice title=pineforge publish automatic::${line}"', notices), ): matches = list(re.finditer(re.escape(command), body)) self.assertEqual(len(matches), count, "each notice mutation must have its real anchor") @@ -688,6 +911,22 @@ def test_handler_pin_refuses_dispatch_permissions_tokens_or_broken_operator_noti mutant = self.mutate(text, body, changed) with self.subTest(step=name, command=command, occurrence=match.start()), self.assertRaises(AssertionError): self.assert_handler(mutant) + for name in ("Check publication recovery policy", "Dispatch publication"): + body = _step(text, name) + command = self.OPERATOR_COMMAND + "${TAG}" + for before in (command, 'printf \'%s\\n\' "$line" >> "$GITHUB_STEP_SUMMARY"'): + mutant = self.mutate(text, body, body.replace(before, "")) + with self.subTest(step=name, anchor=before), self.assertRaises(AssertionError): + self.assert_handler(mutant) + mint = _step(text, "Mint hub Actions token") + dispatch = _step(text, "Dispatch publication") + without_mint = self.mutate(text, mint, "") + for mutant in (self.mutate(without_mint, dispatch, dispatch + "\n " + mint), + text + "\n - name: Late step\n run: true\n", + self.mutate(text, " repositories: pineforge-release\n", + " repositories: pineforge-release,other\n", count=2)): + with self.assertRaises(AssertionError): + self.assert_handler(mutant) def test_generic_trigger_pin_refuses_forbidden_events_and_tag_pushes_in_every_real_workflow(self): for path in sorted(WORKFLOWS.iterdir()): diff --git a/tools/release-dry-run/README.md b/tools/release-dry-run/README.md index beed94c..2a6b0ea 100644 --- a/tools/release-dry-run/README.md +++ b/tools/release-dry-run/README.md @@ -20,6 +20,23 @@ must detect removal of the runtime gate, early latest tags and bypassed finalize dependencies. The CI aggregate `release/ready` requires this replay as well as the existing release/source/harness and fingerprint jobs. +The default `--ready` suite also runs the handler's real post-decision shell, +tag pushes to a local origin, the `publish-policy` CLI and failure reporter. +It checks automatic `publish.yml` dispatch on `main` with exactly one `tag` +input from the bump/republish output, including conflicting payload tags, +noop/wait, older/equal/newer recovery, missing tags, rejected pushes, unreadable +storage, and dispatch failure followed by a safe rerun. `PublicationReplay` +injects only external API responses into subprocesses; production commands have +no fixture-success branch. The shim records whether the tag was already on +origin at dispatch time. A `workflow-dispatch.fail` file in a replay's state +directory makes `gh workflow run` fail. + +Tagged-publication reconcile cases use a frozen clock and the production +`PUBLISH_START_GRACE_MINUTES` constant: before/at/after grace, candidate/image +presence, deduplicated incidents, lightweight tags and store/registry errors. +The default suite includes the workflow pin mutations for widened tokens, +changed order/conditions, error suppression, payload tags and extra dispatches. + No token is minted, no registry is written and no real HTTP is allowed. This replay does not claim a real image passed E2E: `publish.yml` separately runs `release_smoke.py` on both pushed child digests in CI before storing a verified diff --git a/tools/release-dry-run/dry_run.py b/tools/release-dry-run/dry_run.py index 6fdfccc..d2a43b2 100755 --- a/tools/release-dry-run/dry_run.py +++ b/tools/release-dry-run/dry_run.py @@ -73,6 +73,185 @@ def git(*args, cwd=None, env=None, stdin=None): return proc.stdout.strip() +def publication_fixture_request(self, url, *, method="GET", data=None, **kwargs): + """External API boundary for publication replay; unknown routes fail without HTTP.""" + from release_contract import Invalid, canonical + state = Path(os.environ["HARNESS_STATE"]) + releases = json.loads((state / "publication-releases.json").read_text()) + route = url.removeprefix("https://api.github.com/") + if not url.startswith("https://api.github.com/"): + raise AssertionError("unmodeled publication fixture origin") + if route == f"repos/{REPO}/releases?per_page=100&page=1": + if (state / "store-read.fail").exists(): + raise Invalid("fixture release store unavailable") + return canonical(releases) + if re.fullmatch(rf"repos/{REPO}/releases/[0-9]+", route) and method == "GET": + return canonical(next(row for row in releases if str(row["id"]) == route.rsplit("/", 1)[1])) + issues_path = state / "publication-issues.json" + issues = json.loads(issues_path.read_text()) if issues_path.exists() else [] + if route == f"repos/{REPO}/issues?state=all&per_page=100&page=1" and method == "GET": + return canonical(issues) + if route == f"repos/{REPO}/issues" and method == "POST": + row = {"number": len(issues) + 1, "state": "open", **data} + issues.append(row) + issues_path.write_bytes(canonical(issues)) + wfrun.record(state, tool="github-api", action="issue create", body=data) + return canonical(row) + if re.fullmatch(rf"repos/{REPO}/issues/[0-9]+", route) and method == "PATCH": + row = next(row for row in issues if str(row["number"]) == route.rsplit("/", 1)[1]) + row.update(data) + issues_path.write_bytes(canonical(issues)) + wfrun.record(state, tool="github-api", action="issue update", body=data) + return canonical(row) + raise AssertionError(f"unmodeled publication fixture request: {method} {route}") + + +class PublicationReplay: + """Run the handler's real post-decision steps, including policy CLI and failure reporting. + + The existing decision is an input to this replay (arrival/decision replays + cover it separately). Git, tag pushes, harness sync and shell are real; + only GitHub API, App mint and engine downloads use external-service fixtures. + """ + + def __init__(self, checkout, root, *, version="1.6.0", mode="bump", retag=False, + latest=None, payload_tag="v99.0.0", tag_present=True, reject_push=False): + self.checkout, self.root = Path(checkout), Path(root) + self.version, self.mode, self.retag = version, mode, retag + self.state, self.work, self.origin = self.root / "state", self.root / "work", self.root / "origin.git" + self.state.mkdir() + self.work.mkdir() + self.env = wfrun.hermetic_git(wfrun.clean_env(), self.root) + self.env.update({ + "PATH": str(HERE / "shims") + os.pathsep + self.env.get("PATH", os.defpath), + "HARNESS_STATE": str(self.state), "PYTHONDONTWRITEBYTECODE": "1", + "GITHUB_RUN_ID": "42", "RELEASE_REPORT_OWNER": "", "RELEASE_REPORT_TOKEN": "", + "GIT_AUTHOR_NAME": "release-fixture", "GIT_AUTHOR_EMAIL": "release-fixture@localhost", + "GIT_COMMITTER_NAME": "release-fixture", "GIT_COMMITTER_EMAIL": "release-fixture@localhost", + }) + # A subprocess-only external-client injection leaves the production + # command untouched. It also forbids accidental real socket connections. + fixture = self.root / "python-fixture" + fixture.mkdir() + (fixture / "sitecustomize.py").write_text( + "import socket\n" + "def no_network(*args, **kwargs):\n" + " raise AssertionError('real networking forbidden in publication replay')\n" + "socket.create_connection = no_network\n" + "import release_artifacts\n" + "from dry_run import publication_fixture_request\n" + "release_artifacts.Client.request = publication_fixture_request\n", encoding="utf-8") + self.env["PYTHONPATH"] = os.pathsep.join(map(str, (fixture, HERE, self.work / "scripts"))) + shutil.copytree(self.checkout / "scripts", self.work / "scripts") + (self.work / "docker").mkdir() + sync = (self.work / "scripts/sync-harness.sh").read_text() + manifest = re.search(r"^FILES=\(\n(.*?)^\)$", sync, re.S | re.M) + if manifest is None: + raise RuntimeError("missing harness manifest") + http = {} + for name in manifest.group(1).split(): + shutil.copyfile(self.checkout / "docker" / name, self.work / "docker" / name) + shutil.copyfile(self.checkout / "docker" / name, self.state / name) + http[f"{ENGINE_RAW}/v{version}/docker/{name}"] = {"status": 200, "file": name} + (self.state / "http.json").write_text(json.dumps(http)) + rows = [] if latest is None else [{"id": 1, "tag_name": "v" + latest, "draft": False, + "assets": [{"name": "release-manifest.json", "state": "uploaded"}]}] + (self.state / "publication-releases.json").write_text(json.dumps(rows)) + (self.state / "actions.jsonl").touch() + current = version if retag or mode == "republish" else ("0.1.25" if rp.parse(version).line == "legacy" else "1.5.9") + (self.work / "VERSION").write_text(current + "\n") + hpo = version if rp.hpo_pairing(rp.parse(version)) else "" + components = f"engine {version} + codegen {version}" + (f" + hpo {hpo}" if hpo else "") + git("init", "-q", "--bare", self.origin, env=self.env) + git("init", "-q", self.work, env=self.env) + git("add", "-A", cwd=self.work, env=self.env) + git("commit", "-q", "-m", f"chore(release): v{current} — {components}", cwd=self.work, env=self.env) + git("remote", "add", "origin", self.origin, cwd=self.work, env=self.env) + if mode == "republish" and tag_present: + message = f"pineforge-release v{version}\n\nengine={version}\ncodegen={version}\n" + (f"hpo={hpo}\n" if hpo else "") + git("tag", "-a", "v" + version, "-m", message, cwd=self.work, env=self.env) + git("push", "-q", "origin", "HEAD:main", *([] if reject_push else ["--tags"]), + cwd=self.work, env=self.env) + if reject_push: + hook = self.origin / "hooks/pre-receive" + hook.write_text("#!/bin/sh\nwhile read old new ref; do\n" + " case \"$ref\" in refs/tags/*) exit 1 ;; esac\n" + "done\n", encoding="utf-8") + hook.chmod(0o755) + self.context = { + "github": {"repository_owner": "pineforge-4pass", "token": "fixture-hub-token", + "event": {"client_payload": {"tag": payload_tag, "version": payload_tag}}}, + "vars": {"RELEASE_REPORT_OWNER": ""}, + "secrets": {"PINEFORGE_APP_ID": "fixture-app-id", "PINEFORGE_APP_PRIVATE_KEY": "fixture-key"}, + "steps": { + "app": {"outputs": {"token": "fixture-contents-token"}}, + "decide": {"outputs": {"mode": mode, "cur_ver": version, "release": version, + "engine": version, "codegen": version, "hpo": hpo, + "line": rp.parse(version).line, "retag": str(retag).lower(), "awaiting": "codegen"}}, + "in": {"outputs": {"component": "engine", "version": version, "hpo_pep440": rp.parse(version).pep440}}, + "pins": {"outputs": {"eprev": "1.5.9", "cprev": "1.5.9", "hprev": ""}}, + }, + } + + def run(self, *, mode=None): + import copy + from unittest.mock import patch + ctx = copy.deepcopy(self.context) + if mode is not None: + ctx["steps"]["decide"]["outputs"].update(mode=mode, retag="false") + workflow = wfrun.yaml.load((self.checkout / ".github/workflows/handle-upstream.yml").read_text(), + Loader=wfrun.yaml.BaseLoader) + steps = workflow["jobs"]["bump"]["steps"] + start = next(i for i, row in enumerate(steps) if row.get("name") == "No-op") + mark = len((self.state / "actions.jsonl").read_text().splitlines()) + summary = self.root / "summary" + summary.write_text("") + executed, transcript, failed = [], [], "" + call = wfrun.call + + def status_call(name, args): + if name in ("success", "failure", "cancelled", "always"): + return {"success": not failed, "failure": bool(failed), "cancelled": False, "always": True}[name] + return call(name, args) + + for index, step in enumerate(steps[start:]): + condition = step.get("if") + explicit_status = re.search(r"\b(?:always|failure|success|cancelled)\s*\(", condition or "") + if failed and not explicit_status: + continue + with patch.object(wfrun, "call", side_effect=status_call): + if not wfrun.evaluate_if(condition, ctx): + continue + name = step["name"] + executed.append(name) + sid = step.get("id") + if "uses" in step: + if not step["uses"].startswith("actions/create-github-app-token@"): + raise AssertionError("unmodeled publication replay action") + with_ = {k: wfrun.interpolate(v, ctx) for k, v in step["with"].items()} + wfrun.record(self.state, tool="actions/create-github-app-token", + repositories=with_.get("repositories"), owner=with_.get("owner"), + permissions={k: v for k, v in with_.items() if k.startswith("permission-")}) + ctx["steps"][sid] = {"outputs": {"token": "fixture-actions-token"}} + continue + output = self.root / f"output-{index}" + output.write_text("") + env = {**self.env, "GITHUB_OUTPUT": str(output), "GITHUB_STEP_SUMMARY": str(summary)} + env.update({k: wfrun.interpolate(v, ctx) for k, v in step.get("env", {}).items()}) + done = subprocess.run(["bash", "--noprofile", "--norc", "-e", "-o", "pipefail", "-c", + wfrun.interpolate(step["run"], ctx)], + cwd=self.work, env=env, capture_output=True, text=True) + transcript.append(done.stdout + done.stderr) + if sid: + ctx["steps"][sid] = {"outputs": wfrun.parse_output_file(output), + "outcome": "success" if done.returncode == 0 else "failure"} + if done.returncode and not failed and step.get("continue-on-error") != "true": + failed = name + calls = [json.loads(line) for line in (self.state / "actions.jsonl").read_text().splitlines()[mark:]] + return {"ok": not failed, "failed_step": failed, "steps": ctx["steps"], "executed": executed, + "text": "\n".join(transcript), "summary": summary.read_text(), "calls": calls} + + class Result: """A job run: wfrun's JobResult plus the shim calls it made.""" diff --git a/tools/release-dry-run/ready_replay.py b/tools/release-dry-run/ready_replay.py index c263386..28224d6 100644 --- a/tools/release-dry-run/ready_replay.py +++ b/tools/release-dry-run/ready_replay.py @@ -25,7 +25,10 @@ def main(argv=None): if args.k: loader.testNamePatterns = ["*" + args.k + "*"] suite = unittest.TestSuite(loader.loadTestsFromName(name) for name in - ("test_release_binding", "test_release_binding_workflows", "test_release_runtime_smoke")) + ("test_release_binding", "test_release_binding_workflows", "test_release_runtime_smoke", + "test_release_pair.PublishPolicyTest", + "test_release_workflows.HandlerPublicationReplayTest", + "test_release_workflows.ReleaseTriggerWorkflowTest")) if not suite.countTestCases(): parser.error("no replay cases selected") stream = sys.stderr diff --git a/tools/release-dry-run/shims/shim.py b/tools/release-dry-run/shims/shim.py index c9fe712..ffa4005 100644 --- a/tools/release-dry-run/shims/shim.py +++ b/tools/release-dry-run/shims/shim.py @@ -16,6 +16,7 @@ releases-.json REST `repos///releases` answer release-pineforge-hpo-v.json the hpo release and its asset digests gh-release-list.fail present: `gh release list` fails + workflow-dispatch.fail present: the modeled publication workflow dispatch fails """ from __future__ import annotations @@ -216,6 +217,31 @@ def curl(argv): # ------------------------------------------------------------------------ gh def gh(argv): + if argv[:2] == ["workflow", "run"]: + try: + pos, opts, flags = parse(argv[2:], {"--repo", "--ref", "-f"}) + body, _ = api_fields(opts) + supported = (pos == ["publish.yml"] and not flags + and set(opts) == {"--repo", "--ref", "-f"} + and opts["--repo"] == ["pineforge-4pass/pineforge-release"] + and opts["--ref"] == ["main"] and len(opts["-f"]) == 1 + and set(body) == {"tag"} and re.fullmatch("v" + SEMVER, body["tag"])) + except (IndexError, ValueError, TypeError): + supported = False + if not supported: + record(tool="gh", unsupported=argv) + say("gh", "publication dispatch requires main and exactly one tag input") + return 1 + # Observe the real local origin at the instant of dispatch. The oracle + # checks this instead of assuming that an earlier tag command pushed it. + remote = subprocess.run(["git", "ls-remote", "--exit-code", "origin", "refs/tags/" + body["tag"]], + capture_output=True, text=True) + failed = (STATE / "workflow-dispatch.fail").exists() + record(tool="gh", action="workflow run", workflow=pos[0], repo=opts["--repo"][0], + ref=opts["--ref"][0], inputs=body, remote_tag=remote.returncode == 0, failed=failed) + say("gh", "workflow dispatch failed (state says so)" if failed + else f"WOULD DISPATCH publish.yml on main: {json.dumps(body, sort_keys=True)}") + return 1 if failed else 0 if argv[:2] == ["release", "list"]: _, opts, flags = parse(argv[2:], {"--limit", "-L", "--json", "-q", "--jq", "-R", "--repo"}) if (STATE / "gh-release-list.fail").exists():