Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .github/workflows/merge-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,20 @@ jobs:
# pending; otherwise judge the most recent non-cancelled result. A run
# cancelled because a duplicate superseded it is ignored; a check whose
# every run was cancelled counts as a failure.
#
# A run is finished when it has a conclusion. GitHub sets the conclusion
# only when a run completes, but it can leave `status` at "in_progress"
# on a run that has finished: on 2026-11-28 citrate-core's queue entry
# for #917 had a run with conclusion success and completed_at set whose
# status never left in_progress, and the gate waited on it for its full
# 120 minutes (fixed there in citrate-core #950). Keying "pending" on the
# missing conclusion still waits on every run that is queued or really
# running (both have conclusion null).
summary='[.check_runs[] | select(.name != "ci" and .name != "secret-scan")]
| group_by(.name)
| map({name: .[0].name,
pending: (map(select(.status != "completed")) | length),
last: (map(select(.status == "completed" and .conclusion != "cancelled"))
pending: (map(select(.conclusion == null)) | length),
last: (map(select(.conclusion != null and .conclusion != "cancelled"))
| sort_by(.completed_at) | last | .conclusion)})'
stable=0
while :; do
Expand Down
127 changes: 127 additions & 0 deletions scripts/ci/test_merge_gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/env python3
"""merge-gate.yml `ci` job: which checks are pending, which failed.

The `ci` job judges every other check run on the commit. These cases run the
workflow's own `summary`, `failed` and `pending` lines (read from the file, run
by bash and jq as on the runner) over check-run lists shaped like the GitHub
API's. Ported from citrate-core #950. The case that motivated it: on 2026-11-28
the merge-queue commit of citrate-core #917 carried a `lane-guard resolve` run
with conclusion success and completed_at set whose status never left
"in_progress", and the gate waited on it for its full 120 minutes.

Run: ``python3 -m unittest discover -s scripts/ci -p 'test_*.py'``
(needs bash and jq on PATH, as the runner has).
"""

from __future__ import annotations

import json
import re
import shutil
import subprocess
import unittest
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
WF = (ROOT / ".github" / "workflows" / "merge-gate.yml").read_text(encoding="utf-8")


def piece(pattern: str, what: str) -> str:
"""One line or block of the wait step, verbatim (bash strips the indentation)."""
m = re.search(pattern, WF, re.M)
if not m:
raise AssertionError(f"merge-gate.yml: no {what}")
return m.group(0)


SUMMARY = piece(r"^ *summary='[\s\S]*?'$", "summary filter")
FAILED = piece(r"^ *failed=\$\(jq .*$", "failed line")
PENDING = piece(r"^ *pending=\$\(jq .*$", "pending line")

T0 = "2026-11-28T20:27:12Z"


def run(name, status, conclusion, completed_at=None):
if completed_at is None and conclusion is not None:
completed_at = T0
return {"name": name, "status": status, "conclusion": conclusion, "completed_at": completed_at}


@unittest.skipUnless(shutil.which("bash") and shutil.which("jq"), "needs bash and jq")
class MergeGatePending(unittest.TestCase):
def judge(self, runs):
"""The gate's verdict on `runs`: (failed check names, pending count)."""
script = (
"set -euo pipefail\njson=$(cat)\n"
f"{SUMMARY}\n{FAILED}\n{PENDING}\n"
"printf '%s\\n%s\\n' \"$failed\" \"$pending\"\n"
)
out = subprocess.run(
["bash", "-c", script],
input=json.dumps({"check_runs": runs}),
capture_output=True,
text=True,
check=True,
).stdout
failed, pending = out.split("\n")[:2]
return failed, int(pending)

def test_run_with_conclusion_is_finished_even_if_status_in_progress(self):
# The two attempts of lane-guard resolve on core #917's queue commit.
r = self.judge([
run("lane-guard resolve", "in_progress", "success", "2026-11-28T20:27:12Z"),
run("lane-guard resolve", "completed", "success", "2026-11-28T21:22:17Z"),
run("rust (fmt, clippy, test, audit)", "completed", "success"),
])
self.assertEqual(r, ("", 0))

def test_queued_or_running_run_is_still_waited_on(self):
self.assertEqual(
self.judge([run("rust", "queued", None), run("node", "completed", "success")]),
("", 1),
)
self.assertEqual(self.judge([run("rust", "in_progress", None)]), ("", 1))

def test_a_check_whose_only_run_is_stuck_but_successful_passes(self):
# Review of #15: the latest finished run is chosen by conclusion, not status, so a lone
# stuck run that succeeded is not a false failure.
self.assertEqual(self.judge([run("x", "in_progress", "success")]), ("", 0))

def test_stuck_runs_failure_still_fails_the_gate(self):
self.assertEqual(self.judge([run("lane-guard", "in_progress", "failure")]), ("lane-guard", 0))

def test_latest_finished_run_decides_cancelled_ignored_all_cancelled_fails(self):
self.assertEqual(
self.judge([
run("node", "completed", "failure", "2026-11-28T20:00:00Z"),
run("node", "completed", "success", "2026-11-28T21:00:00Z"),
]),
("", 0),
)
self.assertEqual(
self.judge([
run("node", "completed", "success", "2026-11-28T20:00:00Z"),
run("node", "completed", "cancelled", "2026-11-28T21:00:00Z"),
]),
("", 0),
)
self.assertEqual(self.judge([run("node", "completed", "cancelled")]), ("node", 0))

def test_rerun_in_progress_keeps_check_pending(self):
self.assertEqual(
self.judge([
run("lane-guard", "completed", "failure", "2026-11-28T20:00:00Z"),
run("lane-guard", "in_progress", None),
]),
("", 1),
)

def test_gates_own_checks_are_never_judged(self):
self.assertEqual(
self.judge([run("ci", "in_progress", None), run("secret-scan", "in_progress", None)]),
("", 0),
)


if __name__ == "__main__":
unittest.main()