diff --git a/src/kiro_crew/dashboard/handlers/source_providers.py b/src/kiro_crew/dashboard/handlers/source_providers.py index bc31cf38fc25..cb214172cfe8 100644 --- a/src/kiro_crew/dashboard/handlers/source_providers.py +++ b/src/kiro_crew/dashboard/handlers/source_providers.py @@ -227,8 +227,6 @@ "_fetch_github_checks", "_fetch_github_issue", "_github_check", - "_github_check_identity", - "_github_check_rank", "_github_checks", "_github_comment", "_github_issue_comment", diff --git a/src/kiro_crew/dashboard/source_providers/github.py b/src/kiro_crew/dashboard/source_providers/github.py index 4cf237f06597..2d76a9f8801a 100644 --- a/src/kiro_crew/dashboard/source_providers/github.py +++ b/src/kiro_crew/dashboard/source_providers/github.py @@ -34,99 +34,44 @@ def _github_check(item: dict[str, Any]) -> dict[str, Any]: "conclusion": conclusion, "bucket": bucket, "url": item.get("detailsUrl") or item.get("targetUrl") or "", - "startedAt": item.get("startedAt") or "", + "startedAt": item.get("startedAt") or item.get("createdAt") or "", "completedAt": item.get("completedAt") or "", } -def _github_check_identity( - item: dict[str, Any], check: dict[str, Any], index: int -) -> tuple[str, ...]: - """The identity two rollup rows must share to be the same check. - - NOT the display name alone. ``workflow`` separates two workflows that publish - a check with the same job name — including every matrix leg of an Actions - job, since GitHub appends the matrix values to the check-run name even when - the workflow sets an explicit ``name:`` (``Backend Tests (3.12, 4)``), so - sibling shards never share an identity and one shard's failure can never be - folded into another's success. - - The row KIND separates GitHub's two rollup shapes. ``__typename`` comes - straight from the GraphQL union and is present on every row ``gh`` returns; a - row without it (a hand-built dict) is classified from the fields each shape - carries — ``status``/``conclusion`` are check-run-only and ``context`` is - status-only. Do NOT discriminate on the absence of ``name``: a status row - carrying both ``context`` and ``name`` would be read as a check-run and - collide with a nameless one, which ``_github_check`` normalizes to the same - ``"Check"`` placeholder. - - A check-run with NO workflow (published by an app outside Actions) is left - deliberately UNCOLLAPSED — its per-row detail URL, else its position, joins - the identity. Such rows are the one case this payload cannot adjudicate: the - requested ``statusCheckRollup`` fields carry no check-suite or run-attempt - id, so a superseded re-run is indistinguishable from a same-named check from - a different app. Over-counting a re-run is a cosmetic miss; collapsing two - apps would hide a real failure behind the other's later success, so the tie - breaks toward never hiding red. - """ - kind = str(item.get("__typename") or "") - if not kind: - status_shaped = "context" in item and not ("status" in item or "conclusion" in item) - kind = "StatusContext" if status_shaped else "CheckRun" - if kind == "CheckRun" and not check["workflow"]: - return (kind, "", check["name"], check["url"] or f"#{index}") - return (kind, check["workflow"], check["name"]) - - -def _github_check_rank(check: dict[str, Any]) -> tuple[str, str]: - """Recency key for two rows that share a check identity. - - ``startedAt`` leads: an OLDER run that finished must not outrank a NEWER one - that is still going (no ``completedAt`` yet), which is exactly what comparing - ``completedAt`` first would do — the panel would show a stale pass while its - replacement was mid-flight. GitHub leaves ``startedAt`` null while a check-run - is still QUEUED, so a started-less row that is still outstanding sorts above - every timestamp instead of losing to the completed run it supersedes. - """ - started = str(check.get("startedAt") or "") - if not started and check.get("bucket") == "pending": - return ("\uffff", "") - return (started, str(check.get("completedAt") or "")) - - def _github_checks(rollup: list[Any]) -> list[dict[str, Any]]: - """Project GitHub's status-check rollup, keeping only the LATEST run per check. + """Project GitHub's status-check rollup, dropping the rows a newer run displaced. ``statusCheckRollup`` returns EVERY check-run recorded against the head sha, - not one per check. The same workflow file can be dispatched twice for one sha - (a push immediately followed by an edit event, say), producing two check - suites whose jobs each contribute a row — and a concurrency group cancels the - first suite, so the loser lands as ``CANCELLED``. Rendering the raw rollup - therefore (a) inflated the totals the panel reports (observed 49 rows where - GitHub's own UI counted 41) and (b) let a superseded ``CANCELLED`` row roll up - to a red CI glyph on a pull request whose replacement run passed — a red that - no amount of refreshing could clear, because the stale row is genuinely still - in the provider payload. - - GitHub's UI collapses each check to its latest run; mirror that. Identity - comes from ``_github_check_identity``, which is deliberately conservative: - anything it cannot prove is the same check stays its own row, because - over-counting is cosmetic while collapsing two distinct checks would hide a - failure behind another's success. - - First-appearance order is preserved (dict insertion order survives value - replacement) so a re-run does not reshuffle the list under the caller. + not one per check. A concurrency group that cancels a run in favour of a + newer dispatch of the same workflow leaves the loser's rows in the rollup as + ``CANCELLED``, so rendering the raw rollup inflated the panel's totals and + let a superseded cancellation paint the CI glyph red on a pull request whose + replacement run passed. + + The rows arrive flat from ``_github_rollup_read`` (each check run's + workflow-run fields lifted beside its own), and the collapse is the rule the + structured monitor's GitHub provider enforces, called rather than copied so + the glyph and the monitor cannot disagree (imported at call time, because + loading the gateway with monitors off must not load the monitor's provider + runtime): identity is the workflow + DEFINITION id, the run's triggering event and the check name; newest is the + greatest run id; and a row is dropped only when its older run concluded + ``CANCELLED`` and the row itself is ``COMPLETED``+``CANCELLED``. Two rows of + one run, a row without a run identity, and every status context are kept. + Anything the rollup cannot prove was displaced stays, because over-counting + is cosmetic while dropping a live failure turns the glyph green over red. + + Input order is preserved. """ - best: dict[tuple[str, ...], dict[str, Any]] = {} - for index, item in enumerate(rollup): - if not isinstance(item, dict): - continue - check = _github_check(item) - identity = _github_check_identity(item, check, index) - previous = best.get(identity) - if previous is None or _github_check_rank(check) >= _github_check_rank(previous): - best[identity] = check - return list(best.values()) + from kiro_crew.monitoring.github_pull_request import _mark_superseded_rows + + rows: list[object] = [item for item in rollup if isinstance(item, dict)] + return [ + _github_check(row) + for row, superseded in _mark_superseded_rows(rows) + if not superseded and isinstance(row, dict) + ] def _github_comment(item: dict[str, Any], kind: str) -> dict[str, Any]: @@ -157,6 +102,22 @@ def _github_comment(item: dict[str, Any], kind: str) -> dict[str, Any]: "comments(first:10){nodes{databaseId}}}}}}}" ) +# The head commit's rollup, with each check run's workflow-run identity. Owner and +# repository travel through gh's raw-string `-f` (the typed `-F` would turn an +# all-digit name into an integer the `String!` variable rejects); only the number +# is `-F`. The two ids are nullable on the wire (an app-created check run has a +# check suite but no workflow run), and the suite's conclusion is the run's own. +_GITHUB_ROLLUP_QUERY = ( + "query($o:String!,$r:String!,$n:Int!,$c:String){repository(owner:$o,name:$r)" + "{pullRequest(number:$n){headRefOid commits(last:1){nodes{commit{oid " + "statusCheckRollup{contexts(first:100,after:$c){totalCount pageInfo{hasNextPage endCursor} " + "nodes{__typename ... on CheckRun{name status conclusion startedAt completedAt " + "detailsUrl checkSuite{conclusion workflowRun{databaseId event " + "workflow{databaseId name}}}} " + "... on StatusContext{context state targetUrl createdAt}}}}}}}}}}" +) +_GITHUB_ROLLUP_MAX_PAGES = 10 + def _github_thread_ids(payload: Any) -> set[str]: """Return review-thread IDs scoped to the queried pull request.""" @@ -462,27 +423,51 @@ async def _github_rollup_read(ref: SourceRef) -> tuple[list[dict[str, Any]], str PR itself is) so callers that pair this read with a separate core read can detect the two straddling a push and refuse to render another commit's checks. + + It is a GraphQL read rather than ``gh pr view --json statusCheckRollup`` + because the collapse needs each check run's workflow-run identity (definition + id, event, run id, run conclusion), which the ``--json`` payload does not + carry. Every page must name the same head, both on the pull request and on + the commit the rollup hangs off; a disagreement, a malformed page, or a board + past the page cap raises, so the caller degrades the checks section instead + of rendering a partial or mixed board. """ - data = await runner._run_json( - "gh", - "pr", - "view", - ref.url, - "--json", - "statusCheckRollup,headRefOid", - max_output_bytes=runner._CHECKS_OUTPUT_BYTES, - ) - if not isinstance(data, dict): - raise SourceProviderError("GitHub returned an invalid checks payload") - # The panel polls the checks endpoint while checks are pending and writes - # the result straight over the full payload's `checks`, so every consumer - # MUST collapse identically — an uncollapsed reply would re-inflate the - # counts and resurrect a superseded CANCELLED failure on the first poll - # after the panel opens. - return ( - _github_checks(projection._as_list(data.get("statusCheckRollup"))), - str(data.get("headRefOid") or ""), - ) + from kiro_crew.monitoring.github_pull_request import _rollup_page + + rows: list[dict[str, Any]] = [] + head = "" + cursor = "" + for _ in range(_GITHUB_ROLLUP_MAX_PAGES): + argv = ["gh", "api", "graphql", "-f", f"query={_GITHUB_ROLLUP_QUERY}"] + if cursor: + argv += ["-f", f"c={cursor}"] + argv += ["-f", f"o={ref.owner}", "-f", f"r={ref.repo}", "-F", f"n={ref.number}"] + data = await runner._run_json(*argv, max_output_bytes=runner._CHECKS_OUTPUT_BYTES) + try: + if not isinstance(data, dict) or data.get("errors"): + raise TypeError + pull = data["data"]["repository"]["pullRequest"] + page_head = str(pull.get("headRefOid") or "") + page_rows, revision, _total, has_next, next_cursor = _rollup_page(pull) + except (KeyError, TypeError, ValueError, AttributeError): + raise SourceProviderError("GitHub returned an invalid checks payload") from None + if head and page_head != head: + raise SourceProviderError("GitHub head moved while reading checks") + head = page_head + if revision and head and revision != head: + raise SourceProviderError("GitHub returned checks for a different commit") + rows.extend(page_rows) + if not has_next: + # The panel polls the checks endpoint while checks are pending and + # writes the result straight over the full payload's `checks`, so + # every consumer MUST collapse identically — an uncollapsed reply + # would re-inflate the counts and resurrect a superseded CANCELLED + # failure on the first poll after the panel opens. + return _github_checks(rows), head + cursor = next_cursor or "" + # A board past the cap reads unavailable rather than in part: a partial read + # could keep a displaced row whose successor sits on a page never fetched. + raise SourceProviderError("GitHub check rollup exceeds the page limit") async def _fetch_github_checks(ref: SourceRef) -> list[dict[str, Any]]: diff --git a/test/test_source_providers.py b/test/test_source_providers.py index 9e11d180651f..7efbf5960c35 100644 --- a/test/test_source_providers.py +++ b/test/test_source_providers.py @@ -18,6 +18,7 @@ from kiro_crew import github_runner from kiro_crew.dashboard.handlers import source_providers as source +from kiro_crew.dashboard.source_providers import github as github_source from kiro_crew.sandbox import spawn_shim_argv @@ -92,88 +93,143 @@ def test_github_check_active_status_is_pending_even_with_success_conclusion() -> assert check["bucket"] == "pending" -def test_github_checks_keep_only_the_latest_run_per_check() -> None: - """One head sha can carry several runs of the same check (two dispatches of - the same workflow), which inflated every count the panel reported.""" +def _run_row( + name: str, + *, + run: int, + conclusion: str, + run_conclusion: str, + status: str = "COMPLETED", + definition: int = 1, + event: str = "pull_request", + workflow: str = "CI", +) -> dict: + """One check-run row in the flat shape `_github_rollup_read` hands the collapse.""" + return { + "__typename": "CheckRun", + "name": name, + "status": status, + "conclusion": conclusion, + "workflowName": workflow, + "workflowRunId": run, + "workflowRunEvent": event, + "workflowDefinitionId": definition, + "workflowRunConclusion": run_conclusion, + } + + +def test_github_checks_drop_a_row_its_cancelled_run_left_behind() -> None: + """A concurrency-group cancellation whose replacement run passed must not + roll up to `failed` -- the stale row is still genuinely in the payload.""" rollup = [ - { - "name": "GPT Review", - "workflowName": "GPT Review", - "status": "COMPLETED", - "conclusion": "CANCELLED", - "startedAt": "2026-07-28T21:17:23Z", - "completedAt": "2026-07-28T21:18:00Z", - }, - { - "name": "GPT Review", - "workflowName": "GPT Review", - "status": "COMPLETED", - "conclusion": "FAILURE", - "startedAt": "2026-07-28T21:20:44Z", - "completedAt": "2026-07-28T21:25:00Z", - }, - { - "name": "GPT Review", - "workflowName": "GPT Review", - "status": "COMPLETED", - "conclusion": "SUCCESS", - "startedAt": "2026-07-28T21:43:12Z", - "completedAt": "2026-07-28T21:47:00Z", - }, + _run_row("Review", run=10, conclusion="CANCELLED", run_conclusion="CANCELLED"), + _run_row("Review", run=11, conclusion="SUCCESS", run_conclusion="SUCCESS"), ] checks = source._github_checks(rollup) - assert [(check["name"], check["conclusion"]) for check in checks] == [("GPT Review", "SUCCESS")] + assert [check["bucket"] for check in checks] == ["passed"] + assert source._rollup_ci([check["bucket"] for check in checks]) == "passed" -def test_github_checks_superseded_cancellation_does_not_paint_ci_red() -> None: - """A concurrency-group cancellation whose replacement run passed must not - roll up to `failed` — that red survived every refresh, because the stale row - is still genuinely in the provider payload.""" +def test_github_checks_keep_a_failure_from_an_older_run() -> None: + """A newer run id proves only that a run started later, not that it replaced + the older one; an older run that FAILED is a live verdict and stays.""" rollup = [ - { - "name": "Review", - "workflowName": "Review", - "status": "COMPLETED", - "conclusion": "CANCELLED", - "startedAt": "2026-07-28T20:56:29Z", - "completedAt": "2026-07-28T20:57:00Z", - }, - { - "name": "Review", - "workflowName": "Review", - "status": "COMPLETED", - "conclusion": "SUCCESS", - "startedAt": "2026-07-28T20:58:05Z", - "completedAt": "2026-07-28T21:00:24Z", - }, + _run_row("Review", run=10, conclusion="FAILURE", run_conclusion="FAILURE"), + _run_row("Review", run=11, conclusion="SUCCESS", run_conclusion="SUCCESS"), ] - buckets = [check["bucket"] for check in source._github_checks(rollup)] + checks = source._github_checks(rollup) + + assert [check["conclusion"] for check in checks] == ["FAILURE", "SUCCESS"] + assert source._rollup_ci([check["bucket"] for check in checks]) == "failed" + + +@pytest.mark.parametrize("failure_first", [True, False]) +def test_github_checks_keep_both_rows_of_one_run(failure_first: bool) -> None: + """A workflow can publish a check run under its own job's display name, so + two rows of ONE run are both live. Both arrival orders: under a last-wins + rule one order passes for the wrong reason.""" + failure = _run_row("Fork PR Description", run=5, conclusion="FAILURE", run_conclusion="FAILURE") + success = _run_row("Fork PR Description", run=5, conclusion="SUCCESS", run_conclusion="FAILURE") + rollup = [failure, success] if failure_first else [success, failure] + + checks = source._github_checks(rollup) - assert buckets == ["passed"] - assert source._rollup_ci(buckets) == "passed" + assert len(checks) == 2 + assert source._rollup_ci([check["bucket"] for check in checks]) == "failed" -def test_github_checks_queued_rerun_outranks_the_run_it_supersedes() -> None: - """GitHub leaves `startedAt` null while a check-run is QUEUED. Ranking that - row below the completed run it replaces would show a stale pass.""" +def test_github_checks_do_not_group_two_workflow_files_sharing_one_name() -> None: + """Two workflow files may carry one `name:`; the identity is the workflow + DEFINITION, so one file's newer run cannot displace the other's rows.""" rollup = [ - { - "name": "CI", - "workflowName": "CI", - "status": "COMPLETED", - "conclusion": "SUCCESS", - "startedAt": "2026-07-28T20:00:00Z", - "completedAt": "2026-07-28T20:05:00Z", - }, - {"name": "CI", "workflowName": "CI", "status": "QUEUED", "conclusion": None}, + _run_row("Lint", run=10, conclusion="CANCELLED", run_conclusion="CANCELLED", definition=1), + _run_row("Lint", run=11, conclusion="SUCCESS", run_conclusion="SUCCESS", definition=2), + ] + + checks = source._github_checks(rollup) + + assert len(checks) == 2 + assert source._rollup_ci([check["bucket"] for check in checks]) == "failed" + + +def test_github_checks_do_not_group_runs_of_different_events() -> None: + """One file on `push` and `pull_request` runs twice for one commit; those are + concurrent dispatches, not an attempt and its replacement.""" + rollup = [ + _run_row("CI", run=10, conclusion="CANCELLED", run_conclusion="CANCELLED", event="push"), + _run_row("CI", run=11, conclusion="SUCCESS", run_conclusion="SUCCESS"), + ] + + assert len(source._github_checks(rollup)) == 2 + + +def test_github_checks_keep_a_cancelled_row_of_a_live_run() -> None: + """A row reaches CANCELLED inside a live run too (fail-fast, a failed + `needs`); only a run that was itself cancelled proves displacement.""" + rollup = [ + _run_row("CI", run=10, conclusion="CANCELLED", run_conclusion="FAILURE"), + _run_row("CI", run=11, conclusion="SUCCESS", run_conclusion="SUCCESS"), + ] + + assert len(source._github_checks(rollup)) == 2 + + +def test_github_checks_keep_a_cancelled_newest_run() -> None: + """Dropping the newest run's cancellation would revive the verdict it replaced.""" + rollup = [ + _run_row("CI", run=10, conclusion="SUCCESS", run_conclusion="SUCCESS"), + _run_row("CI", run=11, conclusion="CANCELLED", run_conclusion="CANCELLED"), + ] + + assert [check["conclusion"] for check in source._github_checks(rollup)] == [ + "SUCCESS", + "CANCELLED", + ] + + +def test_github_checks_keep_rows_without_a_run_identity() -> None: + """A row the host did not identify takes no part in the collapse.""" + rollup = [ + {"name": "Review", "workflowName": "Review", "status": "COMPLETED", "conclusion": c} + for c in ("CANCELLED", "SUCCESS") + ] + + assert len(source._github_checks(rollup)) == 2 + + +def test_github_checks_queued_rerun_keeps_the_glyph_pending() -> None: + """A queued re-run is live; the panel must not read green over it.""" + rollup = [ + _run_row("CI", run=10, conclusion="SUCCESS", run_conclusion="SUCCESS"), + _run_row("CI", run=11, conclusion="", run_conclusion="", status="QUEUED"), ] checks = source._github_checks(rollup) - assert [check["bucket"] for check in checks] == ["pending"] + assert source._rollup_ci([check["bucket"] for check in checks]) == "running" def test_github_checks_do_not_collapse_across_publishers() -> None: @@ -319,21 +375,16 @@ def test_github_checks_never_collapse_workflowless_check_runs() -> None: assert source._rollup_ci([check["bucket"] for check in checks]) == "failed" -def test_github_checks_preserve_first_appearance_order() -> None: - """A re-run replaces a row in place instead of reshuffling the list.""" +def test_github_checks_preserve_input_order() -> None: + """Collapsing removes rows; it never reshuffles the ones it keeps.""" rollup = [ - {"name": "A", "workflowName": "W", "status": "COMPLETED", "conclusion": "SUCCESS"}, - {"name": "B", "workflowName": "W", "status": "COMPLETED", "conclusion": "SUCCESS"}, - { - "name": "A", - "workflowName": "W", - "status": "COMPLETED", - "conclusion": "FAILURE", - "startedAt": "2026-07-28T21:00:00Z", - }, + _run_row("A", run=10, conclusion="CANCELLED", run_conclusion="CANCELLED"), + _run_row("B", run=10, conclusion="CANCELLED", run_conclusion="CANCELLED"), + _run_row("C", run=11, conclusion="SUCCESS", run_conclusion="SUCCESS"), + _run_row("A", run=11, conclusion="SUCCESS", run_conclusion="SUCCESS"), ] - assert [check["name"] for check in source._github_checks(rollup)] == ["A", "B"] + assert [check["name"] for check in source._github_checks(rollup)] == ["B", "C", "A"] def test_safe_error_redacts_credentials_and_exfiltration_urls() -> None: @@ -2182,6 +2233,33 @@ def test_parse_source_url_rejects_untrusted_shapes(url: str) -> None: source.parse_source_url(url) +def _rollup_reply(rows: list[dict], head: str = "abc123") -> dict: + """A one-page `gh api graphql` rollup reply carrying *rows* at *head*.""" + nodes = [ + {"__typename": "StatusContext" if "context" in row else "CheckRun", **row} for row in rows + ] + contexts = { + "totalCount": len(nodes), + "pageInfo": {"hasNextPage": False, "endCursor": None}, + "nodes": nodes, + } + commit = {"oid": head, "statusCheckRollup": {"contexts": contexts}} + pull = {"headRefOid": head, "commits": {"nodes": [{"commit": commit}]}} + return {"data": {"repository": {"pullRequest": pull}}} + + +def _suite(run: int, run_conclusion: str, *, definition: int = 1, event: str = "pull_request"): + """The check-suite edge GitHub nests a check run's workflow-run identity under.""" + return { + "conclusion": run_conclusion, + "workflowRun": { + "databaseId": run, + "event": event, + "workflow": {"databaseId": definition, "name": "Review"}, + }, + } + + @pytest.mark.asyncio async def test_fetch_github_normalizes_commits_checks_comments_and_files(monkeypatch) -> None: limits: dict[str, int | None] = {} @@ -2248,6 +2326,8 @@ async def fake_run(*argv: str, **kwargs: int): "line": 9, } ] + if "statusCheckRollup" in command: + return _rollup_reply([{"name": "test", "status": "COMPLETED", "conclusion": "SUCCESS"}]) if "graphql" in command: return { "data": { @@ -2310,9 +2390,13 @@ async def fake_run(*argv: str, **kwargs: int): == source._DISCUSSION_OUTPUT_BYTES ) assert ( - next(limit for command, limit in limits.items() if "graphql" in command) + next(limit for command, limit in limits.items() if "reviewThreads" in command) == source._DISCUSSION_OUTPUT_BYTES ) + assert ( + next(limit for command, limit in limits.items() if "statusCheckRollup" in command) + == source._CHECKS_OUTPUT_BYTES + ) @pytest.mark.asyncio @@ -2334,10 +2418,12 @@ async def fake_run(*argv: str, **_kwargs: int): should_fail = ( (failed_endpoint == "files" and "/files?" in command) or (failed_endpoint == "comments" and "/comments?" in command) - or (failed_endpoint == "threads" and "graphql" in command) + or (failed_endpoint == "threads" and "reviewThreads" in command) ) if should_fail: raise source.SourceProviderError("secondary request failed") + if "statusCheckRollup" in command: + return _rollup_reply([]) return {} if "graphql" in command else [] monkeypatch.setattr(source, "_run_json", fake_run) @@ -2362,12 +2448,7 @@ async def fake_run(*argv: str, **kwargs: int): command = " ".join(argv) commands.append((command, kwargs.get("max_output_bytes"))) if "statusCheckRollup" in command: - return { - "statusCheckRollup": [ - {"name": "test", "status": "COMPLETED", "conclusion": "SUCCESS"} - ], - "headRefOid": "abc123", - } + return _rollup_reply([{"name": "test", "status": "COMPLETED", "conclusion": "SUCCESS"}]) if "pr view" in command: return {"number": 12, "title": "Split", "state": "OPEN", "headRefOid": "abc123"} return {} if "graphql" in command else [] @@ -2387,7 +2468,8 @@ async def fake_run(*argv: str, **kwargs: int): (command, limit) for command, limit in commands if "statusCheckRollup" in command ] assert len(rollup_reads) == 1 - assert rollup_reads[0][0].endswith("statusCheckRollup,headRefOid") + assert rollup_reads[0][0].startswith("gh api graphql -f query=") + assert rollup_reads[0][0].endswith("-f o=acme -f r=repo -F n=12") assert rollup_reads[0][1] == source._CHECKS_OUTPUT_BYTES assert data["checks"][0]["bucket"] == "passed" assert "checks" not in data["partialSections"] @@ -2431,12 +2513,10 @@ async def test_fetch_github_discards_rollup_from_a_different_head(monkeypatch) - async def fake_run(*argv: str, **_kwargs: int): command = " ".join(argv) if "statusCheckRollup" in command: - return { - "statusCheckRollup": [ - {"name": "test", "status": "COMPLETED", "conclusion": "SUCCESS"} - ], - "headRefOid": "pushed-after-core-read", - } + return _rollup_reply( + [{"name": "test", "status": "COMPLETED", "conclusion": "SUCCESS"}], + head="pushed-after-core-read", + ) if "pr view" in command: return {"number": 12, "state": "OPEN", "headRefOid": "abc123"} return {} if "graphql" in command else [] @@ -2672,24 +2752,22 @@ async def test_fetch_github_checks_collapses_superseded_runs(monkeypatch) -> Non resurrect the inflated counts and the superseded failure it just fixed.""" async def fake_run(*_argv: str, **_kwargs: int): - return { - "statusCheckRollup": [ + return _rollup_reply( + [ { "name": "Review", - "workflowName": "Review", "status": "COMPLETED", "conclusion": "CANCELLED", - "startedAt": "2026-07-28T20:56:29Z", + "checkSuite": _suite(10, "CANCELLED"), }, { "name": "Review", - "workflowName": "Review", "status": "COMPLETED", "conclusion": "SUCCESS", - "startedAt": "2026-07-28T20:58:05Z", + "checkSuite": _suite(11, "SUCCESS"), }, ] - } + ) monkeypatch.setattr(source, "_run_json", fake_run) @@ -2697,34 +2775,107 @@ async def fake_run(*_argv: str, **_kwargs: int): source.parse_source_url("https://github.com/acme/repo/pull/12") ) + assert [(check["workflow"], check["bucket"]) for check in checks] == [("Review", "passed")] + + +@pytest.mark.asyncio +async def test_fetch_github_checks_keeps_both_rows_of_one_run(monkeypatch) -> None: + """Measured shape: a workflow publishing a check run under its own job's + display name leaves two rows of ONE run, SUCCESS and FAILURE. The read must + keep both whichever arrives first, or the glyph reads green over red.""" + failure = { + "name": "Fork PR Description", + "status": "COMPLETED", + "conclusion": "FAILURE", + "checkSuite": _suite(7, "FAILURE"), + } + success = {**failure, "conclusion": "SUCCESS"} + for rows in ([failure, success], [success, failure]): + + async def fake_run(*_argv: str, _rows: list = rows, **_kwargs: int): + return _rollup_reply(_rows) + + monkeypatch.setattr(source, "_run_json", fake_run) + + checks = await source._fetch_github_checks( + source.parse_source_url("https://github.com/acme/repo/pull/12") + ) + + assert source._rollup_ci([check["bucket"] for check in checks]) == "failed" + + +@pytest.mark.asyncio +async def test_github_rollup_read_follows_pages_and_refuses_a_moved_head(monkeypatch) -> None: + """Pages are joined before the collapse (a successor can sit on a later page), + and a page naming another head discards the whole read.""" + first = _rollup_reply( + [ + { + "name": "Review", + "status": "COMPLETED", + "conclusion": "CANCELLED", + "checkSuite": _suite(10, "CANCELLED"), + } + ] + ) + contexts = first["data"]["repository"]["pullRequest"]["commits"]["nodes"][0]["commit"][ + "statusCheckRollup" + ]["contexts"] + contexts["pageInfo"] = {"hasNextPage": True, "endCursor": "CUR1"} + second_rows = [ + { + "name": "Review", + "status": "COMPLETED", + "conclusion": "SUCCESS", + "checkSuite": _suite(11, "SUCCESS"), + } + ] + commands: list[str] = [] + second_head = "abc123" + + async def fake_run(*argv: str, **_kwargs: int): + command = " ".join(argv) + commands.append(command) + return _rollup_reply(second_rows, head=second_head) if "c=CUR1" in command else first + + monkeypatch.setattr(source, "_run_json", fake_run) + ref = source.parse_source_url("https://github.com/acme/repo/pull/12") + + checks, head = await source._github_rollup_read(ref) + + assert head == "abc123" + assert len(commands) == 2 assert [check["bucket"] for check in checks] == ["passed"] + second_head = "pushed-mid-read" + with pytest.raises(source.SourceProviderError): + await source._github_rollup_read(ref) + @pytest.mark.asyncio async def test_github_check_status_collapses_superseded_runs(monkeypatch) -> None: - """The chip glyph reads the same latest-run-per-check collapse the panel - does, so a superseded CANCELLED row cannot leave the sidebar red.""" + """The chip glyph reads the same collapse the panel does, so a superseded + CANCELLED row cannot leave the sidebar red.""" - async def fake_run(*_argv: str, **_kwargs: int): - return { - "state": "OPEN", - "statusCheckRollup": [ - { - "name": "Review", - "workflowName": "Review", - "status": "COMPLETED", - "conclusion": "CANCELLED", - "startedAt": "2026-07-28T20:56:29Z", - }, - { - "name": "Review", - "workflowName": "Review", - "status": "COMPLETED", - "conclusion": "SUCCESS", - "startedAt": "2026-07-28T20:58:05Z", - }, - ], - } + async def fake_run(*argv: str, **_kwargs: int): + if "statusCheckRollup" in " ".join(argv): + return _rollup_reply( + [ + { + "name": "Review", + "status": "COMPLETED", + "conclusion": "CANCELLED", + "checkSuite": _suite(10, "CANCELLED"), + }, + { + "name": "Review", + "status": "COMPLETED", + "conclusion": "SUCCESS", + "checkSuite": _suite(11, "SUCCESS"), + }, + ] + ) + return {"state": "OPEN"} monkeypatch.setattr(source, "_run_json", fake_run) @@ -2741,7 +2892,7 @@ async def test_github_check_status_carries_settled_merge_state(monkeypatch) -> N async def fake_run(*argv: str, **_kwargs: int): command = " ".join(argv) if "statusCheckRollup" in command: - return {"statusCheckRollup": [], "headRefOid": "abc123"} + return _rollup_reply([]) assert "mergeable,mergeStateStatus" in command return {"state": "OPEN", "mergeable": "CONFLICTING", "mergeStateStatus": "DIRTY"} @@ -2759,7 +2910,9 @@ async def test_github_check_status_omits_unsettled_merge_state( ) -> None: """"Still computing" must not overwrite the answer the full payload has.""" - async def fake_run(*_argv: str, **_kwargs: int): + async def fake_run(*argv: str, **_kwargs: int): + if "statusCheckRollup" in " ".join(argv): + return _rollup_reply([]) return {"state": "OPEN", "mergeable": raw_mergeable, "mergeStateStatus": "UNKNOWN"} monkeypatch.setattr(source, "_run_json", fake_run) @@ -2802,12 +2955,10 @@ async def test_github_check_status_marks_stale_rollup_unavailable(monkeypatch) - async def fake_run(*argv: str, **_kwargs: int): command = " ".join(argv) if "statusCheckRollup" in command: - return { - "statusCheckRollup": [ - {"name": "test", "status": "COMPLETED", "conclusion": "SUCCESS"} - ], - "headRefOid": "pushed-after-core-read", - } + return _rollup_reply( + [{"name": "test", "status": "COMPLETED", "conclusion": "SUCCESS"}], + head="pushed-after-core-read", + ) return {"state": "OPEN", "headRefOid": "abc123"} monkeypatch.setattr(source, "_run_json", fake_run) @@ -2827,7 +2978,7 @@ async def test_github_check_status_still_fails_when_core_read_fails(monkeypatch) async def fake_run(*argv: str, **_kwargs: int): command = " ".join(argv) if "statusCheckRollup" in command: - return {"statusCheckRollup": [], "headRefOid": "abc123"} + return _rollup_reply([]) raise source.SourceProviderError("core read failed") monkeypatch.setattr(source, "_run_json", fake_run) @@ -3944,11 +4095,9 @@ async def test_fetch_github_checks_uses_one_call_without_rewriting_cache(monkeyp source._CACHE[url] = (1.0, 21, {"provider": "github", "checks": []}) cached = source._CACHE[url] run = AsyncMock( - return_value={ - "statusCheckRollup": [ - {"name": "test", "status": "IN_PROGRESS", "conclusion": "SUCCESS"} - ] - } + return_value=_rollup_reply( + [{"name": "test", "status": "IN_PROGRESS", "conclusion": "SUCCESS"}] + ) ) monkeypatch.setattr(source, "_run_json", run) @@ -3956,11 +4105,16 @@ async def test_fetch_github_checks_uses_one_call_without_rewriting_cache(monkeyp run.assert_awaited_once_with( "gh", - "pr", - "view", - url, - "--json", - "statusCheckRollup,headRefOid", + "api", + "graphql", + "-f", + f"query={github_source._GITHUB_ROLLUP_QUERY}", + "-f", + "o=acme", + "-f", + "r=repo", + "-F", + "n=12", max_output_bytes=source._CHECKS_OUTPUT_BYTES, ) assert checks[0]["bucket"] == "pending" diff --git a/test/test_source_providers_refactor_facade.py b/test/test_source_providers_refactor_facade.py index 6c54d5b949a7..6dc0adf9bee2 100644 --- a/test/test_source_providers_refactor_facade.py +++ b/test/test_source_providers_refactor_facade.py @@ -84,7 +84,7 @@ _fetch_gitlab_issue _fetch_issue_uncached _fetch_jira_issue _fetch_pull_request_checks_uncached _fetch_pull_request_uncached _fetch_repo_visibility _finish_inflight _flatten_paginated _flush_check_updates _full_payload_ttl _get_jira_auth - _gh_conditional_get _github_check _github_check_identity _github_check_rank _github_checks + _gh_conditional_get _github_check _github_checks _github_comment _github_dismiss_review _github_graphql_dismisses_stale _github_issue_comment _github_issue_reactions _github_linked_changes _github_merge_state _github_pending_review _github_pending_review_comments _github_pull_request_head_sha