From 27398e0c8abf5b9383a581140662416cae7864e4 Mon Sep 17 00:00:00 2001 From: Lucas Koontz Date: Wed, 11 Nov 2026 00:21:19 -0800 Subject: [PATCH 1/2] feat: add the org Actions event policy that lets the CLA check run on pull_request_target --- README.md | 23 +++++++++++++++++ config/cla-actions-policy.json | 22 ++++++++++++++++ tests/test_cla_actions_policy.py | 43 ++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 config/cla-actions-policy.json create mode 100644 tests/test_cla_actions_policy.py diff --git a/README.md b/README.md index ca48892..8edb125 100644 --- a/README.md +++ b/README.md @@ -599,6 +599,29 @@ whether its author has signed. Four repos learned that the hard way, gating on `github.event_name == 'pull_request'` while triggering on events that are not `pull_request`, so their check quietly never asked anybody to sign. +### The `pull_request_target` policy + +GitHub blocks `pull_request_target` in public repositories by default from 2 Dec 2026, unless an Actions event policy allows it ([Securely using pull_request_target](https://docs.github.com/en/actions/reference/security/securely-using-pull_request_target)). Every caller triggers on `pull_request_target`, so without a policy the check never starts on a public repo. + +`config/cla-actions-policy.json` is that policy. It allows `pull_request_target` and `issue_comment` for `.github/workflows/cla.yml` in every repository, and names no other workflow. + +- It targets the wrapper by path, not repositories by name, so a new caller is covered the day it adds `cla.yml`. +- Its events are an allow-list. `issue_comment` is on it because signing and `recheck` arrive as comments, and the policy refuses any event it doesn't list. +- Allowing this one workflow is safe because the reusable checks out no pull request code: it runs a `gh api` step and the pinned action, nothing else. + +`tests/test_cla_actions_policy.py` fails the build if the target widens or an event drops. + +The `integrations/github` Terraform provider has no resource for Actions policies yet, so an org admin creates the policy with the [REST API](https://docs.github.com/en/rest/actions/policies), from a checkout of this repository: + +```sh +gh api -X POST orgs/mindsdb/actions/policies \ + -H "X-GitHub-Api-Version: 2026-03-10" --input config/cla-actions-policy.json +``` + +To read it back, `gh api orgs/mindsdb/actions/policies -H "X-GitHub-Api-Version: 2026-03-10"`. To change it, `PUT` the file to `orgs/mindsdb/actions/policies/`, and to remove it, `DELETE` that path. The list gives the id. + +A run the policy refuses ends as `startup_failure` with zero jobs. The org audit log records it as `workflows.actions_policy_violation` with `"allowed": false`, naming the repository and the event but not the workflow file. + ### Prerequisites (provisioned once, org level, scoped to the release-train repos) - **`mindsdb-release-train` GitHub App** with `Administration`, `Contents`, and diff --git a/config/cla-actions-policy.json b/config/cla-actions-policy.json new file mode 100644 index 0000000..5ab1bf3 --- /dev/null +++ b/config/cla-actions-policy.json @@ -0,0 +1,22 @@ +{ + "name": "CLA check: allow pull_request_target and issue_comment", + "enforcement": "active", + "conditions": { + "repository_name": { + "include": ["~ALL"], + "exclude": [] + }, + "workflow_path": { + "include": [".github/workflows/cla.yml"], + "exclude": [] + } + }, + "rules": [ + { + "type": "restrict_action_events", + "parameters": { + "allowed_events": ["pull_request_target", "issue_comment"] + } + } + ] +} diff --git a/tests/test_cla_actions_policy.py b/tests/test_cla_actions_policy.py new file mode 100644 index 0000000..dc33133 --- /dev/null +++ b/tests/test_cla_actions_policy.py @@ -0,0 +1,43 @@ +"""Guards on ``config/cla-actions-policy.json``, the org Actions event policy. + +GitHub blocks ``pull_request_target`` in public repositories by default unless an +Actions event policy allows it. This policy allows it for the CLA wrapper alone. +Both of its edges are security decisions, and nothing else fails when they drift: + +- Widen the workflow target and every ``pull_request_target`` workflow in the org + runs again, which is the exposure GitHub's block exists to close. +- Drop ``issue_comment`` and the policy's allow-list refuses the comment runs, so + nobody can sign or ``recheck``. +""" + +import json +from pathlib import Path + +_POLICY = Path(__file__).resolve().parents[1] / "config" / "cla-actions-policy.json" + +CLA_WRAPPER = ".github/workflows/cla.yml" + + +def load_policy() -> dict: + return json.loads(_POLICY.read_text()) + + +def test_targets_the_cla_wrapper_alone(): + workflow_path = load_policy()["conditions"]["workflow_path"] + + assert workflow_path["include"] == [CLA_WRAPPER] + assert workflow_path["exclude"] == [] + + +def test_allows_exactly_the_wrapper_triggers(): + rules = load_policy()["rules"] + + assert [rule["type"] for rule in rules] == ["restrict_action_events"] + assert sorted(rules[0]["parameters"]["allowed_events"]) == [ + "issue_comment", + "pull_request_target", + ] + + +def test_is_enforced(): + assert load_policy()["enforcement"] == "active" From 42c269baefadd3db04daf1bc7d774c6f30ba13d6 Mon Sep 17 00:00:00 2001 From: Lucas Koontz Date: Wed, 11 Nov 2026 00:26:11 -0800 Subject: [PATCH 2/2] fix: count only runs a deploy branch starts in both sweeps, and name the policy cause in the startup alert --- .github/workflows/notify-startup-failure.yml | 23 ++- README.md | 20 ++- scripts/branch_health.py | 28 +++- tests/test_branch_health.py | 43 ++++- tests/test_startup_failure_sweep.py | 165 +++++++++++++++++++ 5 files changed, 265 insertions(+), 14 deletions(-) create mode 100644 tests/test_startup_failure_sweep.py diff --git a/.github/workflows/notify-startup-failure.yml b/.github/workflows/notify-startup-failure.yml index ac99c96..f8ecf05 100644 --- a/.github/workflows/notify-startup-failure.yml +++ b/.github/workflows/notify-startup-failure.yml @@ -35,6 +35,13 @@ # the cap is enforced when the FILE LOADS, not when a job runs) # - a `uses: ./.github/workflows/x.yml` path that does not exist on the ref # - malformed YAML, an unknown `on:` filter, a bad matrix expression +# - an org Actions policy that blocks the run's event, which the org audit +# log records as `workflows.actions_policy_violation` +# +# Only runs the branch started for itself count: a push, a schedule, a manual +# dispatch, a `workflow_run` or a `repository_dispatch`. GitHub gives a pull +# request run its head branch, so the branch listing also holds the release pull +# request's runs, and those say nothing about the branch. # # Its consequence is the worst-shaped one available: a merge to a deploy branch # reports nothing, so the branch is believed deployed and is not. It has already @@ -260,9 +267,19 @@ jobs: # and neither sweep is asking about them: "the branch is not deployed" # is a statement about a pipeline. See SYNTHESIZED_PREFIX in # scripts/branch_health.py, which drops them from the other sweep. + # + # Only the events a branch starts for itself count. GitHub gives a + # pull request run its HEAD branch, so this listing also holds the + # runs of a release pull request whose head is the branch, and a CLA + # run an Actions policy refused there read as "staging is not + # deployed". The list is DEPLOY_EVENTS in scripts/branch_health.py; + # tests/test_startup_failure_sweep.py fails if the two drift. It + # filters before the newest run is picked, so a later pull request + # run can't hide a push that never started. jq --argjson cutoff "${CUTOFF}" --arg branch "${BRANCH}" ' [ .workflow_runs[] | select(.path | startswith("dynamic/") | not) + | select(.event | IN("push", "schedule", "workflow_dispatch", "workflow_run", "repository_dispatch")) | select(.conclusion == "success" or .conclusion == "failure" or .conclusion == "timed_out" or .conclusion == "startup_failure") | { id, path, name, conclusion, created_at, head_sha, html_url, @@ -339,7 +356,7 @@ jobs: type: "section", text: { type: "mrkdwn", - text: ":rotating_light: *<\($server)/\($repo)|\($repo_name)>* — \($findings[0] | length) pipeline(s) *never started*, so nothing in the run could report it. The branch is not deployed." + text: ":rotating_light: *<\($server)/\($repo)|\($repo_name)>* — \($findings[0] | length) workflow run(s) *never started*: GitHub refused them before any job ran, so nothing in the run could report it." } }] + ( $findings[0] | map({ @@ -348,14 +365,14 @@ jobs: { type: "mrkdwn", text: "*Workflow*\n<\($server)/\($repo)/actions/runs/\(.id)|\(.path)>" }, { type: "mrkdwn", text: "*Branch*\n<\($server)/\($repo)/tree/\(.branch)|\(.branch)>" }, { type: "mrkdwn", text: "*Commit*\n<\($server)/\($repo)/commit/\(.head_sha)|\(.head_sha[0:8])>" }, - { type: "mrkdwn", text: "*Pushed by*\n\(.actor)" } + { type: "mrkdwn", text: "*Started by*\n\(.actor)" } ] }) ) + [{ type: "context", elements: [{ type: "mrkdwn", - text: "`startup_failure` with no jobs: GitHub rejected the run before scheduling anything — check the caller/callee `permissions:` relationship and the workflow YAML on that ref. <\($sweep)|Sweep run>" + text: "`startup_failure` with no jobs. The cause is the caller/callee `permissions:` or the workflow YAML on that ref, or an org Actions policy that blocks the event (`workflows.actions_policy_violation` in the org audit log). <\($sweep)|Sweep run>" }] }] ) diff --git a/README.md b/README.md index 8edb125..52fae24 100644 --- a/README.md +++ b/README.md @@ -284,11 +284,15 @@ repeated `failed` always post. ### The one failure it cannot see: a run that never started -`notify-main-failure` is a job *inside* the pipeline, so it covers every failure where the run exists. It cannot cover `conclusion: startup_failure` — GitHub rejecting the run at load time, with **zero jobs** — because there is no job to put the alert in. The run appears in the Actions tab and nowhere else, and the branch is not deployed. +`notify-main-failure` is a job *inside* the pipeline, so it covers every failure where the run exists. It cannot cover `conclusion: startup_failure` — GitHub rejecting the run at load time, with **zero jobs** — because there is no job to put the alert in. The run appears in the Actions tab and nowhere else, so whatever it would have deployed didn't ship. -That is not a corner case. It is produced by a caller job granting narrower `permissions:` than a called workflow's jobs declare (the cap is checked when the file *loads*, not when a job runs), by a `uses: ./.github/workflows/x.yml` path that does not exist on the ref, and by malformed YAML. It has already cost auth `staging` ten hours of running the previous image across two merges, with the recovery message being the first thing the channel heard about it. +That is not a corner case. It is produced by a caller job granting narrower `permissions:` than a called workflow's jobs declare (the cap is checked when the file *loads*, not when a job runs), by a `uses: ./.github/workflows/x.yml` path that does not exist on the ref, by malformed YAML, and by an org Actions policy that blocks the run's event, which the org audit log records as `workflows.actions_policy_violation`. It has already cost one repository's `staging` ten hours of running the previous image across two merges, with the recovery message being the first thing the channel heard about it. -`notify-startup-failure.yml` watches from outside, on a schedule. It sweeps the deploy branches and alerts when the newest conclusive run of a workflow is a `startup_failure` inside the lookback window, restricted to runs where no job ran — precisely the set the in-run notify job could not have reported: +`notify-startup-failure.yml` watches from outside, on a schedule. It sweeps the deploy branches and alerts when the newest conclusive run of a workflow is a `startup_failure` inside the lookback window, restricted to runs where no job ran — precisely the set the in-run notify job could not have reported. The message names the run and both causes: the `permissions:` or YAML on that ref, or an Actions policy. + +**Only runs the branch started for itself count**: a `push`, a `schedule`, a `workflow_dispatch`, a `workflow_run` or a `repository_dispatch`. GitHub gives a pull request run its head branch, so a `staging` listing also returns the release pull request's runs, CI and the CLA check included, and those say nothing about whether `staging` deployed. Both sweeps share the list, `DEPLOY_EVENTS` in `scripts/branch_health.py`, and filter before they pick each workflow's newest run, so a later pull request run can't hide a red push. A deploy started by any other event goes unwatched. + +A caller: ```yaml name: Pipeline watchdog @@ -331,7 +335,7 @@ Age is measured from the current attempt's `run_started_at`, not the run's `crea Selection logic lives in `scripts/branch_health.py` with unit tests, because it is date arithmetic plus a two-way trigger and that does not belong in a jq expression. -Alerts repeat, by design: one failing push produces about three messages at that cadence and window, and then silence. Alerting only on the *transition* into a broken state gives exactly one message per break, which was rejected after replaying it against the auth incident — it would have said nothing about the second failing push, since the run before that one had also failed to start. A stateless sweep cannot be exactly-once, so the choice is a message that can be missed or a few that cannot, and the window is what bounds the few. +Alerts repeat, by design: one failing push produces about three messages at that cadence and window, and then silence. Alerting only on the *transition* into a broken state gives exactly one message per break, which was rejected after replaying it against that incident — it would have said nothing about the second failing push, since the run before that one had also failed to start. A stateless sweep cannot be exactly-once, so the choice is a message that can be missed or a few that cannot, and the window is what bounds the few. Two things to know when adding it. GitHub only runs `schedule` triggers from a repository's **default branch**, so a watchdog merged to `staging` and no further is inert — `workflow_dispatch` is there to prove it before it reaches `main`. And widening `lookback-minutes` on a manual dispatch replays a past incident, which is how to check it would have caught one. @@ -357,7 +361,7 @@ Two things to know when adding it. GitHub only runs `schedule` triggers from a r `zizmor` is advisory because pointing it at existing pipelines surfaces a backlog, and a lint job that is red on arrival gets ignored rather than fixed. Flip `zizmor-blocking: true` per repo once its backlog is triaged. -**The permission check is not redundant with either tool**, which was verified against a tree that had already broken a deploy branch: `actionlint` reported nothing, and `zizmor --persona=auditor` reported only its generic "no `permissions:` block" note, equally true of jobs that work fine. What it checks is that no called workflow declares a permission its caller lacks. GitHub caps a called workflow at the calling job's grant and enforces that cap when the workflow **file is loaded**, so a job in a shared reusable naming a scope one caller does not grant does not run with less — it rejects that caller's whole run as a `startup_failure` with **zero jobs**, which means the pipeline's own notify job cannot report it either. In `mindsdb/auth` that cost two silently undeployed merges to `staging` and ten hours of serving the previous image. +**The permission check is not redundant with either tool**, which was verified against a tree that had already broken a deploy branch: `actionlint` reported nothing, and `zizmor --persona=auditor` reported only its generic "no `permissions:` block" note, equally true of jobs that work fine. What it checks is that no called workflow declares a permission its caller lacks. GitHub caps a called workflow at the calling job's grant and enforces that cap when the workflow **file is loaded**, so a job in a shared reusable naming a scope one caller does not grant does not run with less — it rejects that caller's whole run as a `startup_failure` with **zero jobs**, which means the pipeline's own notify job cannot report it either. In one repository that cost two silently undeployed merges to `staging` and ten hours of serving the previous image. The rule it enforces: **a job in a shared reusable declares only what all its callers grant, and inherits anything only one of them needs.** Inheriting is the only thing that composes — the PR caller grants `pull-requests: write` and the comment posts, the push callers grant `contents: read` and the same job runs without a scope it never needed. And the reason it has to be a gate rather than a convention: a pull request only exercises the PR caller, which is usually the one that *does* grant the scope, so the mistake merges green and breaks on the merge commit. @@ -367,7 +371,7 @@ Blind spot to know about: it can only read local (`./.github/workflows/...`) cal ## The release-freeze contract -A freeze is one thing: the repo's `staging_frozen` custom property, set to `true` or `false`. A single org-level ruleset (terraform, `newprod/global/github`) blocks pushes to `staging` in every repo where it is `true`; the workflows never touch the ruleset. Four workflows care — freeze and unfreeze write it, and the two alerting paths read it to decide whether a red staging is worth interrupting anyone for. +A freeze is one thing: the repo's `staging_frozen` custom property, set to `true` or `false`. A single org-level ruleset, managed outside this repository, blocks pushes to `staging` in every repo where it is `true`; the workflows never touch the ruleset. Four workflows care — freeze and unfreeze write it, and the two alerting paths read it to decide whether a red staging is worth interrupting anyone for. They used to carry three separate copies of that knowledge, and only two of them took the name as an input; the alerting path hardcoded it. Renaming it in one repo would therefore have moved the freeze and left the alerting reading a name that no longer existed — silent in the direction that hurts, because a reader that cannot establish the state escalates, so every ordinary mid-week staging red would have paged the channel forever and the cause would have looked like a Slack problem. @@ -632,8 +636,8 @@ A run the policy refuses ends as `startup_failure` with zero jobs. The org audit **`secrets.RELEASE_APP_PRIVATE_KEY`** (org secret) — the private key reaches the reusable workflows via `secrets: inherit` in the caller. - The **`staging_frozen` org custom property** and the **`staging-freeze` org - ruleset** that targets repos where it is `true`, both defined in terraform - (`newprod/global/github/main.tf`). The workflows only set the property — they + ruleset** that targets repos where it is `true`, both managed outside this + repository. The workflows only set the property — they never touch the ruleset or the underlying branch protection. ### Caller wrappers diff --git a/scripts/branch_health.py b/scripts/branch_health.py index c1d0937..a5cf4db 100644 --- a/scripts/branch_health.py +++ b/scripts/branch_health.py @@ -92,6 +92,20 @@ # is asking. Naming one in `workflows:` still reports it: an explicit ask wins. SYNTHESIZED_PREFIX = "dynamic/" +# The events by which a deploy branch starts a run for itself. GitHub gives a +# pull request run its HEAD branch, so a `staging` listing also returns every run +# of the release pull request from `staging` to `main`: its CI, and the CLA +# check's `pull_request_target`. Those runs test a merge or check a contributor; +# they say nothing about whether the branch deployed. On 9 Nov 2026 an Actions +# policy refused one, and the startup sweep reported staging "not deployed" while +# the push from the same commit had deployed eight seconds earlier. +# +# An allow-list, so a deploy started by an event not named here goes unwatched. +# None was seen in the run listings of seven repositories' deploy branches. The +# startup sweep's jq in notify-startup-failure.yml carries the same list, and +# tests/test_startup_failure_sweep.py fails if the two drift. +DEPLOY_EVENTS = ("push", "schedule", "workflow_dispatch", "workflow_run", "repository_dispatch") + # The freeze workflow's per-repo wrapper keeps this name verbatim, because the # release-PR workflow chains off it by name. That makes it a stable handle for # "the window just opened" without this file knowing anything about the schedule, @@ -124,6 +138,16 @@ def started_at(run: dict) -> datetime: return parse_time(run.get("run_started_at") or run["created_at"]) +def started_by_the_branch(run: dict) -> bool: + """Whether the branch started this run for itself (see ``DEPLOY_EVENTS``). + + Synthesized runs carry the event ``dynamic`` and pass here, because + ``in_scope`` already decides them, and naming one in `workflows:` has to keep + reporting it. + """ + return run.get("event") in DEPLOY_EVENTS or (run.get("path") or "").startswith(SYNTHESIZED_PREFIX) + + def newest_conclusive_per_workflow(runs: Iterable[dict]) -> list[dict]: """One run per workflow file: the most recent that concluded either way. @@ -204,8 +228,10 @@ def select_red( just_froze = branch == staging_branch and freeze_opened_within(freeze_runs, cutoff=cutoff) + # Filter before picking the newest run, so a later pull request run can't + # stand in for a red push and hide it. findings = [] - for run in newest_conclusive_per_workflow(runs): + for run in newest_conclusive_per_workflow(r for r in runs if started_by_the_branch(r)): if run["conclusion"] not in RED: continue if not in_scope(run["path"], only=only, exclude=exclude): diff --git a/tests/test_branch_health.py b/tests/test_branch_health.py index 8b36daf..80d09ce 100644 --- a/tests/test_branch_health.py +++ b/tests/test_branch_health.py @@ -36,12 +36,14 @@ def run( conclusion="failure", minutes_ago=45, run_id=1, + event="push", ): created = NOW - timedelta(minutes=minutes_ago) return { "id": run_id, "path": path, "name": name, + "event": event, "conclusion": conclusion, "created_at": created.isoformat().replace("+00:00", "Z"), "head_sha": "abcdef1234567890", @@ -275,10 +277,14 @@ def test_the_other_synthesized_namespaces_are_out_too(self): assert select(runs, branch="main", frozen=False) == [] def test_an_allowlist_that_names_a_synthesized_run_still_reports_it(self): - """The default is a scope decision, not a ban: an explicit ask wins.""" + """The default is a scope decision, not a ban: an explicit ask wins. + + GitHub gives these runs the event ``dynamic``, which is not a deploy + event, so this also pins that the event filter leaves them to the scope. + """ path = "dynamic/dependabot/dependabot-updates" findings = health.select_red( - [run(path=path)], branch="main", cutoff=CUTOFF, settled=SETTLED, frozen=False, + [run(path=path, event="dynamic")], branch="main", cutoff=CUTOFF, settled=SETTLED, frozen=False, staging_branch="staging", only=[path], ) assert [f["path"] for f in findings] == [path] @@ -301,6 +307,39 @@ def test_exclusion_wins_over_an_allowlist(self): assert findings == [] +class TestOnlyRunsTheBranchStartedForItself: + """GitHub gives a pull request run its HEAD branch, so a `staging` listing also + returns every run of the release pull request from `staging` to `main`. Those + runs test a merge; they say nothing about whether staging deployed. + """ + + def test_a_red_pull_request_run_headed_staging_is_ignored(self): + runs = [run(path="ci.yml", event="pull_request")] + assert select(runs) == [] + + def test_a_red_comment_run_is_ignored(self): + runs = [run(path="cla.yml", event="issue_comment")] + assert select(runs) == [] + + def test_a_red_push_on_staging_is_still_reported(self): + runs = [run(path="publish-staging.yml", event="push")] + assert [f["path"] for f in select(runs)] == ["publish-staging.yml"] + + def test_a_later_pull_request_run_does_not_hide_a_red_push(self): + runs = [ + run(path="ci.yml", event="push", conclusion="failure", minutes_ago=60, run_id=1), + run(path="ci.yml", event="pull_request", conclusion="success", minutes_ago=40, run_id=2), + ] + assert [f["id"] for f in select(runs)] == [1] + + def test_every_deploy_event_counts(self): + runs = [ + run(path=f"{event}.yml", event=event, run_id=i) + for i, event in enumerate(health.DEPLOY_EVENTS) + ] + assert len(select(runs)) == len(health.DEPLOY_EVENTS) + + class TestUnreadableBranchIsNotAnAllClear: def test_a_branch_whose_history_fails_is_reported_as_unreadable(self, tmp_path, monkeypatch): """The summary used to print "No deploy branch is sitting red" here.""" diff --git a/tests/test_startup_failure_sweep.py b/tests/test_startup_failure_sweep.py new file mode 100644 index 0000000..59e9e13 --- /dev/null +++ b/tests/test_startup_failure_sweep.py @@ -0,0 +1,165 @@ +"""Tests for the startup-failure sweep's selection, the ``jq`` program inside +``notify-startup-failure.yml``. + +The program is read out of the workflow file and run with the real ``jq``, so the +tests check the code that ships rather than a copy of it. + +The case that matters is anton's 9 Nov 2026 run 42654716938. GitHub's Actions +policy refused a ``pull_request_target`` CLA run on the release pull request +whose head is ``staging``. GitHub gives a pull request run its head branch, so the +``staging`` listing returned it, and the sweep reported "the branch is not +deployed" while the push to ``staging`` from the same commit had deployed eight +seconds earlier. A pull request or comment run says nothing about a deploy +branch, so the sweep keeps only the events a branch starts for itself. +""" + +import importlib.util +import json +import re +import subprocess +from datetime import datetime, timezone +from pathlib import Path + +import yaml + +_ROOT = Path(__file__).resolve().parents[1] +_WORKFLOW = _ROOT / ".github" / "workflows" / "notify-startup-failure.yml" + +_spec = importlib.util.spec_from_file_location("branch_health", _ROOT / "scripts" / "branch_health.py") +health = importlib.util.module_from_spec(_spec) +_spec.loader.exec_module(health) + +# Wide enough to cover 9 Nov 2026 from any date the tests run on. +CUTOFF = int(datetime(2026, 1, 1, tzinfo=timezone.utc).timestamp()) + +# anton's two runs on `staging` from commit 42d4dde, 9 Nov 2026, trimmed to the +# fields the sweep reads. The push deployed; the CLA run never started. +ANTON_PUSH = { + "id": 42654693935, + "path": ".github/workflows/publish-staging.yml", + "name": "Staging pre-release and publish to PyPI", + "event": "push", + "head_branch": "staging", + "conclusion": "success", + "created_at": "2026-11-09T16:25:54Z", + "head_sha": "42d4dde3214fe4b9d5b9973a2fd201ba8f94b6fc", + "html_url": "https://github.com/mindsdb/anton/actions/runs/42654693935", + "display_title": "Merge pull request #765 from mindsdb/bugfix/day-dated-rows-last-week", +} +ANTON_CLA = { + "id": 42654716938, + "path": ".github/workflows/cla.yml", + "name": "MindsDB Anton CLA Assistant", + "event": "pull_request_target", + "head_branch": "staging", + "conclusion": "startup_failure", + "created_at": "2026-11-09T16:26:02Z", + "head_sha": "42d4dde3214fe4b9d5b9973a2fd201ba8f94b6fc", + "html_url": "https://github.com/mindsdb/anton/actions/runs/42654716938", + "display_title": "MindsDB Anton CLA Assistant", +} + +# Events seen on deploy branches that are not the branch starting a run for +# itself (run listings for staging and main across seven repositories, 11 Nov +# 2026), plus the pull request events a listing can return. +NOT_DEPLOY_EVENTS = ( + "pull_request", + "pull_request_target", + "pull_request_review", + "pull_request_review_comment", + "issue_comment", + "merge_group", +) + + +def sweep_program() -> str: + """The ``jq`` program the sweep step runs on each branch's run listing.""" + jobs = yaml.safe_load(_WORKFLOW.read_text())["jobs"] + step = next(s for s in jobs["sweep"]["steps"] if s.get("id") == "find") + match = re.search(r"jq --argjson cutoff \S+ --arg branch \S+ '(.*?)' \"runs-", step["run"], re.DOTALL) + assert match, "the sweep's jq program moved; update sweep_program()" + return match.group(1) + + +def sweep(runs: list[dict], *, branch: str = "staging") -> list[dict]: + result = subprocess.run( + ["jq", "--argjson", "cutoff", str(CUTOFF), "--arg", "branch", branch, sweep_program()], + input=json.dumps({"workflow_runs": runs}), + capture_output=True, + text=True, + check=True, + ) + return json.loads(result.stdout) + + +def alert_payload(findings: list[dict], tmp_path: Path) -> dict: + """The Slack payload the alert step builds from the sweep's findings.""" + jobs = yaml.safe_load(_WORKFLOW.read_text())["jobs"] + step = next(s for s in jobs["sweep"]["steps"] if s.get("name") == "Post the alert") + match = re.search(r"--arg sweep \"\$SWEEP_URL\" '(.*?)' > payload.json", step["run"], re.DOTALL) + assert match, "the alert's jq program moved; update alert_payload()" + reportable = tmp_path / "reportable.json" + reportable.write_text(json.dumps(findings)) + args = {"repo": "mindsdb/anton", "repo_name": "anton", "server": "https://github.com", "channel": "C1", "sweep": "https://sweep"} + argv = ["jq", "-n", "--slurpfile", "findings", str(reportable)] + for name, value in args.items(): + argv += ["--arg", name, value] + result = subprocess.run([*argv, match.group(1)], capture_output=True, text=True, check=True) + return json.loads(result.stdout) + + +def startup_failure(*, event: str, run_id: int = 1, path: str = ".github/workflows/deploy.yml") -> dict: + return {**ANTON_PUSH, "id": run_id, "path": path, "event": event, "conclusion": "startup_failure"} + + +class TestAnton9Nov: + def test_the_refused_cla_run_is_not_a_staging_run(self): + assert sweep([ANTON_CLA, ANTON_PUSH]) == [] + + def test_a_push_that_never_started_is_still_reported(self): + findings = sweep([startup_failure(event="push", run_id=7), ANTON_CLA]) + + assert [(f["id"], f["branch"]) for f in findings] == [(7, "staging")] + + +class TestEvents: + def test_the_jq_event_list_is_deploy_events(self): + match = re.search(r"select\(\.event \| IN\(([^)]*)\)\)", sweep_program()) + + assert match, "the sweep's event filter moved; update this test" + assert tuple(json.loads(f"[{match.group(1)}]")) == health.DEPLOY_EVENTS + + def test_every_deploy_event_is_reported(self): + runs = [ + startup_failure(event=event, run_id=i, path=f".github/workflows/{event}.yml") + for i, event in enumerate(health.DEPLOY_EVENTS) + ] + + assert len(sweep(runs)) == len(health.DEPLOY_EVENTS) + + def test_no_pull_request_or_comment_event_is_reported(self): + runs = [ + startup_failure(event=event, run_id=i, path=f".github/workflows/{event}.yml") + for i, event in enumerate(NOT_DEPLOY_EVENTS) + ] + + assert sweep(runs) == [] + + def test_a_later_pull_request_run_does_not_hide_a_push_that_never_started(self): + push = startup_failure(event="push", run_id=1) + later_pr = {**push, "id": 2, "event": "pull_request", "conclusion": "success", "created_at": "2026-11-09T17:00:00Z"} + + assert [f["id"] for f in sweep([push, later_pr])] == [1] + + +class TestMessage: + def test_names_both_causes_links_the_run_and_claims_no_deploy(self, tmp_path): + findings = sweep([startup_failure(event="push", run_id=7)]) + payload = alert_payload(findings, tmp_path) + texts = json.dumps(payload) + + assert "permissions:" in texts + assert "workflows.actions_policy_violation" in texts + assert "https://github.com/mindsdb/anton/actions/runs/7" in texts + assert "Started by" in texts + assert "not deployed" not in texts