diff --git a/README.md b/README.md index b1c740a2..a578d9fc 100644 --- a/README.md +++ b/README.md @@ -417,12 +417,14 @@ CI results are read for the current head. The first required check to fail prints `checks:failed` right away. `checks:passed` prints once nothing on the head is still going (no pending check and no unfinished GitHub Actions run, which may still create jobs), every check and workflow the base branch names is -present (except `quest land`'s, which no head carries), and every required +present (except `quest land`'s, which no head carries), each required check +from the app its requirement names, if any, and every required check passed; it names the last required check that passed and is dated when the head went idle. Each prints once per head, with the check's name (spaces as `_`) in the actor field and its details URL. Results count as -`quest status` counts them, so neutral and skipped results pass and a rerun -supersedes an earlier run of the same check. Each job of a +`quest status` counts them, so neutral and skipped results pass, a rerun +supersedes an earlier run of the same check from the same workflow, and a cancelled check a newer run +of its workflow replaced is dropped. Each job of a workflow a ruleset requires is required. Requiredness comes from the base branch's protection and rulesets, readable with read access. A failed optional check prints separately as `check:failed`, @@ -501,8 +503,8 @@ comes last, and a missing value prints as `-`: | `auto-merge` | Method, who enabled it, when, and the head it was enabled on. | | `merge-queue` | Queue state, position, and when it was enqueued. | | `landing-stopped` | The reason the latest merge-queue entry, auto-merge, or [`quest land`](#land-a-batch-with-quest-land) queue entry ended (such as `merge_conflict`, `failed_checks`, `manual`, `manually_disabled`, or `merged`), who ended it, and when; `-` once the PR has entered the merge queue or auto-merge again since. | -| `checks` | `failure` if any check fails, else `pending` if any is pending, else `success`, and `passed=`, `failing=`, `pending=` counts, all from each check's latest run; `-` when the head has no checks. | -| `check` | `failing` or `pending`, `required` or `optional`, the result, details URL, and name. | +| `checks` | `failure` if any check fails, else `pending` if any is pending or a requirement is unreported, else `success`, and `passed=`, `failing=`, `pending=` counts, all from the checks that count; `-` when the head has no checks and the base requires none. | +| `check` | `failing` or `pending`, `required` or `optional`, the result, details URL, and name. A required check or required workflow nothing reports yet prints as `pending required expected -` with its name, followed by `(from app )` when the requirement names an app, or the workflow's repository and path, as `owner/name:path`. | | `review` | `current` or `stale` against the head, state, reviewer, time, and URL of each reviewer's latest review. | | `checkpoint` | Each bot reviewer that approves or requests changes, such as one whose approval the base requires, its round, and its review's URL, when that review is of the head and its latest verdict there, ignoring plain comments, is neither an approval nor a dismissal, at the end of its third, sixth, ... round: [`/quest-review`'s design checkpoint](#review-locally) for an online reviewer. A round is one commit it reviewed; a dismissed review counts as one that approves or requests changes. Only printed when due. | | `reacted:` | Actor, time, and the PR or review reacted to, as `quest wait` names reactions. | @@ -514,9 +516,19 @@ The node ID is what GraphQL's `addPullRequestReviewThreadReply` and `resolveReviewThread` take. Neutral and skipped checks pass. Only each check's latest run counts, as for GitHub's mergeability: a rerun, or the same workflow run again on the head, supersedes an earlier run of that name from the same -app, and a newer commit status supersedes an older one of its context. GitHub's -own rollup state counts the superseded runs too. A required check -is one branch protection or a ruleset requires for this PR. `--json` prints the +app and workflow file, and a newer commit status supersedes an older one of its context. A check +that reported `cancelled` also stops counting once a newer GitHub Actions run of +its workflow exists on the head, from any event, as when `cancel-in-progress` +cancels a run whose jobs, such as a matrix job under its unexpanded name, the +newer run never reports; a rerun counts as newer than every run that started +before it. Any other result keeps counting, as does a cancelled run nothing +replaced. Only then does it read the head's workflow runs, which needs Actions +read access. GitHub's own rollup state counts the superseded runs too. A required +check is one branch protection or a ruleset requires for this PR, or a job of a +workflow a ruleset requires. Like `quest wait`, it reads an open PR's +requirements from the base, matches a required check by name and, when the +requirement names one, its app, as GitHub and `quest land` do, and leaves out +`quest land`'s check, which no head carries. `--json` prints the same facts as one object with the fields `url`, `state`, `draft`, `head`, `mergeable`, `mergeState`, `reviewDecision`, `autoMerge`, `mergeQueue`, `landingStopped`, `checks` (`state`, `passed`, `failing`, `pending`), `reviews`, `checkpoints` (`reviewer`, `round`, `url`), `reactions`, @@ -561,8 +573,8 @@ are not told apart. `quest merge` takes no label: the head-pinned auto-merge or merge-queue entry it creates is the queue signal, so a queued PR shows no hold. It refuses while any session holds the PR, so release your own hold first. `--land` needs the full 40-character -head SHA and refuses once the head moves, or while the latest run of any -check on it failed, required or not, naming each; pending checks do not block. +head SHA and refuses once the head moves, or while any check on it that +counts in `quest status` failed, required or not, naming each; pending checks do not block. It also refuses a head that fails `quest check --no-stubs --base --branch `, which it runs on git objects it fetches, so nothing from the head runs, and ignores findings the base already has; run a `quest` the PR @@ -758,8 +770,7 @@ down to one PR, and each batch that lands grows it back by one; the order and size persist in the state directory. Conflicting additions stay queued. A PR whose required checks are still running, or that still needs an approving review the base requires, stays queued and sits out -batches until they pass and it is approved; only each check's latest run counts, -as in `quest status`. If it still waits two hours after +batches until they pass and it is approved; checks count as in `quest status`. If it still waits two hours after its pick (twice the default `--timeout`), the daemon comments once on that pick, naming what it waits for, since a check that never reports or an approval nobody gives would hold it silently. The sandbox checks the tip once, without diff --git a/quest/m0/gate-required-workflows.md b/quest/m0/gate-required-workflows.md new file mode 100644 index 00000000..8fc4f5d3 --- /dev/null +++ b/quest/m0/gate-required-workflows.md @@ -0,0 +1,11 @@ +# [S] Gate counts required workflows + +## Goal + +The gate's check matching (`checks()` in `src/cli/land.ts`) counts the base's required workflows (ruleset `workflows` rules) the same way `quest status` and `quest wait` do, so the gate never lands a head whose required workflow never ran or has not passed. + +## Plan + +- Settled on [moq-dev/quest#674](https://github.com/moq-dev/quest/pull/674): keep this as its own quest in m0, since it is the same disagreement between readers that #674 fixes for status and wait. +- Reuse the shared `requiredOf` and `unreported` from `src/cli/status.ts`: a job in a required workflow counts as required, and a required workflow path nothing reports keeps the PR waiting rather than landing. +- Test the gate with a base whose ruleset requires a workflow that has not run on the head. diff --git a/quest/m0/superseded-runs.md b/quest/m0/superseded-runs.md deleted file mode 100644 index 39a1dad3..00000000 --- a/quest/m0/superseded-runs.md +++ /dev/null @@ -1,59 +0,0 @@ -# [M] Ignore checks from superseded workflow runs - -## Goal - -`quest status`, `quest merge`, `quest wait` and `quest land`, including its -daemons, stop failing a head on a check that reported `cancelled` once a -newer GitHub Actions run of the same workflow exists on that head. This -includes a matrix job the cancelled run left under an unexpanded name, such -as `Tests (${{ matrix.os }})`. A completed check keeps today's rule, where -each check's latest report counts. A cancelled run that nothing replaced -still fails. All of these readers agree on the head's state. - -## Plan - -Decided 2026-11-12 (user, /quest-plan of the follow-ups from a -/quest-complete run on kixelated/token-max): ✅ drop the cancelled checks of -replaced workflow runs (rejected: treating every cancelled check as passing, -since a genuinely cancelled required run would then pass; fixing each -repository's workflow, since every repository would need it and token-max's -`edited` trigger is deliberate). - -Decided 2026-11-12 (user, /quest-review design checkpoint): ✅ `quest status` -judges the base's required checks as `quest wait` does, with the same -exceptions, such as the gate's own `quest land` check. It reports each -missing required check or required workflow as a pending line, the -workflow named by its path. A cancelled -pull_request run can be replaced by a push run that lacks a PR-only required -job, and status would otherwise read success while land and wait read -pending (rejected: leaving it to the implementer; letting a cancelled run -yield only to a run of its own event, which fails the push/pull_request -race again). - -What happened: in kixelated/token-max, editing a PR body right after a push -starts an `edited` run, which cancels the push's `synchronize` run through -`cancel-in-progress`. The cancelled run's matrix job reports under its -unexpanded name, which no newer run reuses. `latestContexts` -(`src/github/checks.ts`) keys check runs by app and name, so it keeps that -job as the latest, and `quest merge` refused -[kixelated/token-max#75](https://github.com/kixelated/token-max/pull/75), -[#76](https://github.com/kixelated/token-max/pull/76) and -[#78](https://github.com/kixelated/token-max/pull/78) until the cancelled run -was rerun. - -Recommendations: - -- Drop only checks that reported `cancelled`, not whole runs: a check the - cancelled run completed before it stopped keeps counting. Dropping a run because a newer - run of its event exists would also drop passed checks that the newer run - gated off, such as jobs an `edited` run skips by `github.event.action`. -- land-bump (`latestRuns` in `src/cli/land-bump.ts`) already treats a run - that a newer run cancelled as replaced, and reads the head's runs over - REST, which also sees a newer run that has no check runs yet. Share one - rule with it where that stays simple. -- Workflow-run data needs Actions read. `quest land serve` mints its API - token with only `checks` and `statuses` read (`src/cli/land-serve.ts`). - Add `actions: "read"`, which the GitHub App already grants, since - land-bump and land-watch request it. -- Count a rerun of the cancelled run as current, since rerunning it is the - workaround today. diff --git a/src/cli/github.ts b/src/cli/github.ts index cc117c0a..993a486a 100644 --- a/src/cli/github.ts +++ b/src/cli/github.ts @@ -1,6 +1,7 @@ // The GitHub client shared by the commands that talk to GitHub, outside the repository-independent core. import { execFileSync } from "node:child_process"; import { setTimeout as sleep } from "node:timers/promises"; +import { cancelled, currentContexts, latestContexts, type RollupContext, type WorkflowRun } from "../github/checks"; import { git } from "./git"; const API = "https://api.github.com"; @@ -505,7 +506,7 @@ export interface RequiredCheck { /** What a base branch requires, read from branch protection and rulesets. */ export interface Requirements { checks: RequiredCheck[]; - /** Workflow files that must pass as a whole, so each of their jobs is required. */ + /** Workflow files that must pass as a whole, so each of their jobs is required, from {@link workflowKey}. */ workflows: string[]; /** Every rule the branch's active rulesets apply: `type`, `parameters`, and the ruleset's source. */ rules: ObjectValue[]; @@ -530,6 +531,32 @@ async function branchRules(github: GitHub, path: string): Promise ); } +/** + * A workflow file as requirements name it: its repository's `owner/name` and its path, since a ruleset may + * require a workflow from another repository at the same path as a local one. + */ +export function workflowKey(repository: string, path: string): string { + return `${repository.toLowerCase()}:${path}`; +} + +/** The workflow files `rules` require, each keyed by {@link workflowKey} from its rule's `repository_id`. */ +async function requiredWorkflows(github: GitHub, rules: ObjectValue[]): Promise { + const names = new Map(); + const keys: string[] = []; + for (const row of rules + .filter((rule) => rule.type === "workflows") + .flatMap((rule) => array(object(rule.parameters).workflows))) { + const id = Number(row.repository_id); + if (!names.has(id)) { + const response = await github.request(`/repositories/${id}`, { allow: [404] }); + // A repository the token cannot see is still required; nothing on the head can match its name. + names.set(id, response.ok ? string(object(await response.json()).full_name) : `repository ${id}`); + } + keys.push(workflowKey(names.get(id)!, string(row.path))); + } + return [...new Set(keys)]; +} + /** What `branch` of `repository` requires, read with read access from branch protection and rulesets. */ export async function requirements(github: GitHub, repository: Repository, branch: string): Promise { const base = `/repos/${encodeURIComponent(repository.owner)}/${encodeURIComponent(repository.repo)}`; @@ -560,16 +587,51 @@ export async function requirements(github: GitHub, repository: Repository, branc (check, index) => checks.findIndex((other) => other.name === check.name && other.app === check.app) === index, ), - workflows: rules - .filter((rule) => rule.type === "workflows") - .flatMap((rule) => array(object(rule.parameters).workflows)) - .map((row) => string(row.path)), + workflows: await requiredWorkflows(github, rules), rules, // Unknown counts as on, so a reader that needs the settings reads them rather than assume none. classic: protection.enabled !== false, }; } +/** Every GitHub Actions workflow run on `head`. Reading them needs Actions read access. */ +export async function workflowRuns(github: GitHub, repository: Repository, head: string): Promise { + const runs: WorkflowRun[] = []; + let path: string | undefined = + `/repos/${encodeURIComponent(repository.owner)}/${encodeURIComponent(repository.repo)}/actions/runs?head_sha=${head}&per_page=100`; + while (path) { + const response: Response = await github.request(path); + for (const row of array(object(await response.json()).workflow_runs)) + runs.push({ + ...row, + id: Number(row.id), + workflow_id: Number(row.workflow_id), + check_suite_id: Number(row.check_suite_id), + // A run not yet started, such as one awaiting approval, has no start time, so its creation stands in. + run_started_at: string(row.run_started_at ?? row.created_at), + }); + path = link(response, "next"); + } + return runs; +} + +/** + * Each check's latest report on `head` that still counts, as {@link currentContexts} judges it, and the + * head's workflow runs when they were read: only when a latest report is a cancelled check run, the only + * report they can drop. + */ +export async function currentChecks( + github: GitHub, + repository: Repository, + head: string, + contexts: T[], +): Promise<{ checks: T[]; runs?: WorkflowRun[] }> { + const latest = latestContexts(contexts); + if (!latest.some(cancelled)) return { checks: latest }; + const runs = await workflowRuns(github, repository, head); + return { checks: currentContexts(contexts, runs), runs }; +} + /** * Whether a merged PR in `repository` had `commit` as its head or below it, as when `quest land` merged * the base into the head before squashing; false for a commit GitHub does not have. diff --git a/src/cli/hold.ts b/src/cli/hold.ts index 2e9e39e0..48d242f0 100644 --- a/src/cli/hold.ts +++ b/src/cli/hold.ts @@ -7,6 +7,7 @@ import { cleanAutoMergeRefusal, immediateMerge, queueHead } from "../github/merg import { repositoryPath } from "./claims"; import { array, + currentChecks, GitHub, type GitHubDependencies, GitHubGraphQLError, @@ -16,7 +17,6 @@ import { requirements, string, } from "./github"; -import { latestContexts } from "../github/checks"; import { ALLOWS, LAND_CHECK, METHOD_NAMES, contexts, enqueue } from "./land"; import { QUEUE_CHECK } from "./land-queue"; import type { LandingCheck } from "./landed"; @@ -345,12 +345,12 @@ export async function land(pr: PullRequest, request: LandRequest, deps: GitHubDe } /** - * Refuse a head on which any check's latest run failed, required or not, naming each: landing it + * Refuse a head on which any current check failed, required or not, naming each: landing it * would spend a queue build on a known failure. Pending and absent checks do not block, nor does the * own queue's informational check, which reports how a past pick ended rather than the head. */ async function passing(github: GitHub, pr: PullRequest, url: string, sha: string): Promise { - const failing = latestContexts(await contexts(github, pr, sha)) + const failing = (await currentChecks(github, pr, sha, await contexts(github, pr, sha))).checks .filter((context) => context.name !== QUEUE_CHECK) .map(classifyCheck) .filter(({ outcome }) => outcome === "failing") diff --git a/src/cli/land-bump.ts b/src/cli/land-bump.ts index 7c3969f5..fae7c546 100644 --- a/src/cli/land-bump.ts +++ b/src/cli/land-bump.ts @@ -3,7 +3,7 @@ import { spawnSync } from "node:child_process"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; import { isAbsolute, join, relative, sep } from "node:path"; -import { checksState } from "../github/checks"; +import { checksState, currentContexts, newerRun, replacedRun, type WorkflowRun } from "../github/checks"; import { array, GitHub, @@ -13,6 +13,7 @@ import { type Repository, requirements, string, + workflowRuns, } from "./github"; import { land } from "./hold"; import { commandEnvironment, contexts, git, type RemoteRead } from "./land"; @@ -420,23 +421,13 @@ async function settle(target: Repository, record: Bump, connection: BumpConnecti // A workflow's later jobs report only once they start, and a queued workflow may have no check run yet, // so Actions is judged by its workflow runs. Another app's suite that never ran a check stays queued // forever, and counts for nothing. - const runs = object(await (await github.request(`${path}/actions/runs?head_sha=${head}&per_page=100`)).json()); - const all = array(runs.workflow_runs); + const all = await workflowRuns(github, target, head); const suites = object(await (await github.request(`${path}/commits/${head}/check-suites?per_page=100`)).json()); const listed = array(suites.check_suites); - if (Number(runs.total_count) > all.length || Number(suites.total_count) > listed.length) - throw new Error(`${head} has more workflow runs or check suites than one page, more than a bump reads`); + if (Number(suites.total_count) > listed.length) + throw new Error(`${head} has more check suites than one page, more than a bump reads`); const workflows = latestRuns(all); - // The check runs of a replaced workflow run, such as one a newer run cancelled, are replaced with it. - const replaced = new Set( - all - .filter((run) => !workflows.includes(run) && run.check_suite_id != null) - .map((run) => Number(run.check_suite_id)), - ); - const found = (await contexts(github, target, head)).filter((context) => { - const suite = object(context.checkSuite ?? {}).databaseId; - return suite == null || !replaced.has(Number(suite)); - }); + const found = currentContexts(await contexts(github, target, head), all); const state = found.length === 0 ? "PENDING" : checksState(found); if (state === "FAILURE") return fail(`a check failed on its head ${head}`); if (workflows.some((run) => run.status === "completed" && !PASSING.includes(String(run.conclusion)))) @@ -464,17 +455,16 @@ async function settle(target: Repository, record: Bump, connection: BumpConnecti } /** - * The workflow runs on a head that still count: each workflow's latest run per event. A cancelled run - * also yields to a newer run of its workflow from another event, as when push and pull_request share a - * `cancel-in-progress` concurrency group, so a run a newer one replaced counts as replaced, not failed. + * The workflow runs on a head that still count: each workflow's latest run per event, by when its latest + * attempt started. A cancelled run also yields to a newer run of its workflow from another event, as when + * push and pull_request share a `cancel-in-progress` concurrency group, so a run a newer one replaced + * counts as replaced, not failed, as {@link currentContexts} treats its cancelled checks. */ -export function latestRuns(runs: ObjectValue[]): ObjectValue[] { +export function latestRuns(runs: WorkflowRun[]): WorkflowRun[] { return runs.filter((run) => { - const newer = runs.filter( - (other) => other.workflow_id === run.workflow_id && Number(other.id) > Number(run.id), - ); + const newer = runs.filter((other) => other.workflow_id === run.workflow_id && newerRun(other, run)); if (newer.some((other) => other.event === run.event)) return false; - return !(run.conclusion === "cancelled" && newer.length > 0); + return !(run.conclusion === "cancelled" && replacedRun(run, runs)); }); } diff --git a/src/cli/land-serve.ts b/src/cli/land-serve.ts index 6ea67006..7d584d65 100644 --- a/src/cli/land-serve.ts +++ b/src/cli/land-serve.ts @@ -838,6 +838,8 @@ export async function serve( administration: "read", checks: "read", statuses: "read", + // The head's workflow runs, which say whether a newer run replaced a cancelled check. + actions: "read", }, { fetch, now: Date.now }, ), diff --git a/src/cli/land.ts b/src/cli/land.ts index 708f1750..23e3bb42 100644 --- a/src/cli/land.ts +++ b/src/cli/land.ts @@ -13,10 +13,11 @@ import { type Repository, type RequiredCheck, array, + currentChecks, requirements, string, } from "./github"; -import { appOf, latestContexts } from "../github/checks"; +import { appOf } from "../github/checks"; import { classifyCheck } from "./status"; import type { Method } from "./hold"; import { committedSources, snapshot } from "./tree"; @@ -174,7 +175,7 @@ export interface Waiting { const CONTEXTS = `pageInfo { hasNextPage endCursor } nodes { __typename - ... on CheckRun { databaseId name status conclusion detailsUrl checkSuite { databaseId app { databaseId } } } + ... on CheckRun { databaseId name status conclusion detailsUrl checkSuite { databaseId app { databaseId } workflowRun { file { path repositoryName } } } } ... on StatusContext { context state createdAt targetUrl } }`; @@ -432,7 +433,8 @@ export async function resolveQueue( // The queue's first page holds the head's checks, unless the head moved from the pin since. const commit = object(object(array(object(node.commits).nodes)[0]).commit); const rollup = commit.oid === approved ? commit.statusCheckRollup : undefined; - const unmet = checks(await contexts(github, repository, approved, rollup), required); + const found = await contexts(github, repository, approved, rollup); + const unmet = checks((await currentChecks(github, repository, approved, found)).checks, required); // An approval can still arrive on the pinned head, such as an automated reviewer's, so it waits like a check. const approval = node.reviewDecision === "REVIEW_REQUIRED"; if (unmet.failing.length > 0) { @@ -496,18 +498,20 @@ export async function requiredChecks( sha: string, required: RequiredCheck[], ): Promise<{ failing: string[]; pending: string[] }> { - return checks(await contexts(github, repository, sha), required); + return checks( + (await currentChecks(github, repository, sha, await contexts(github, repository, sha))).checks, + required, + ); } /** - * The required checks a head failed, and those pending or not yet reported, each from its app. Only - * each check's latest run counts, as for GitHub's mergeability, so a rerun supersedes a cancelled or - * failed run. + * The required checks a head failed, and those pending or not yet reported, each from its app. Only the + * head's current checks count, from {@link currentChecks}: each check's latest run, as for GitHub's + * mergeability, so a rerun supersedes a cancelled or failed run, without a cancelled run a newer one replaced. */ -function checks(contexts: ObjectValue[], required: RequiredCheck[]): { failing: string[]; pending: string[] } { +function checks(latest: ObjectValue[], required: RequiredCheck[]): { failing: string[]; pending: string[] } { const failing: string[] = []; const pending: string[] = []; - const latest = latestContexts(contexts); for (const need of required) { let result: "passed" | "failing" | "pending" = "pending"; let reported = false; diff --git a/src/cli/main.ts b/src/cli/main.ts index 2f83fd4c..d7cc9b49 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -540,7 +540,7 @@ export function main(argv: string[], out: Output): number | Promise { "in one call. It reports facts; it does not decide whether the PR is ready or whose review counts.\n\n" + "Plain output prints one record per line, its type first and any free text (a check name or path) last: " + "url, state, draft, head, mergeable, merge-state, review-decision, review-record, auto-merge, merge-queue, " + - "landing-stopped, and checks, whose state and counts follow each check's latest run; review-record gives whether the PR body's ## Review record " + + "landing-stopped, and checks, whose state and counts follow the checks that count, pending while an open PR's base requires a check or workflow nothing reports; review-record gives whether the PR body's ## Review record " + "covers the head, as current, diff-identical (git diff ... prints the same as for the head it names), " + "stale, or unknown when GitHub cannot compare them, then the head it names last; auto-merge gives its method, who enabled it and when, and the head it was " + "enabled on, which differs from head once a push follows, as GitHub keeps auto-merge across a writer's " + @@ -548,7 +548,10 @@ export function main(argv: string[], out: Output): number | Promise { "activity, which GitHub lists up to minutes late); landing-stopped is the latest exit from the merge queue, auto-merge, or " + "quest land's queue, with the reason (such as merge_conflict, failed_checks, manual, or manually_disabled), " + "actor, and time, until the PR enters the merge queue or auto-merge again. Then " + - "a check line per failing or pending check, marked required or optional, counting only each check's latest run; a review line per " + + "a check line per failing or pending check, marked required or optional, counting only each check's latest run (by name, app, and workflow file) " + + "and dropping a cancelled check once a newer GitHub Actions run of its workflow exists on the head (reading the head's " + + "workflow runs, which needs Actions read access); a required check or workflow nothing reports prints as pending required " + + "expected, with its name, followed by (from app ) when the requirement names an app, or the workflow's repository and path, as owner/name:path; a review line per " + "reviewer's latest review, marked current or stale against the head; a checkpoint line per bot " + "reviewer that approves or requests changes, its round, and its review, when that review is of the " + "head, while its latest verdict on the head is neither an approval nor a dismissal, and ends its third, sixth, ... round (one commit it reviewed), as " + diff --git a/src/cli/status.ts b/src/cli/status.ts index 571f9b76..9668769e 100644 --- a/src/cli/status.ts +++ b/src/cli/status.ts @@ -2,6 +2,7 @@ import { actor, array, + currentChecks, GitHub, GitHubDeadline, type GitHubDependencies, @@ -9,10 +10,13 @@ import { object, type ObjectValue, type PullRequest, + type Repository, + requirements, string, + workflowKey, } from "./github"; -import { checksState, contextOutcome, latestContexts } from "../github/checks"; -import { EJECTED, pinned } from "./land"; +import { appOf, checksState, contextOutcome } from "../github/checks"; +import { EJECTED, LAND_CHECK, pinned } from "./land"; import { reviewSection } from "../core"; import { readHoldStatus, type HoldFact, type HoldReport } from "./hold"; @@ -43,7 +47,7 @@ const STATUS = `query($owner: String!, $repo: String!, $number: Int!, __typename ... on CheckRun { databaseId name status conclusion detailsUrl isRequired(pullRequestNumber: $number) - checkSuite { app { databaseId } } + checkSuite { databaseId app { databaseId } workflowRun { file { path repositoryName } } } } ... on StatusContext { context state createdAt targetUrl isRequired(pullRequestNumber: $number) } } @@ -199,7 +203,14 @@ export async function pullRequestStatus(pr: PullRequest, deps: GitHubDependencie const ownership = await readHoldStatus(pr, deps); if (ownership.head !== first!.headRefOid) throw new Error("the PR head changed while reading its holds; rerun quest status"); - const status = summarize(first!, rollup, contexts, reviewNodes, threadNodes); + const head = string(first!.headRefOid); + const { checks: current } = await currentChecks(github, pr, head, contexts); + // Only an open PR can still merge, and a closed one's base may be gone. + const needs: Needs = + first!.state === "OPEN" + ? await requiredOf(github, pr, string(first!.baseRefName)) + : { checks: [], workflows: [] }; + const status = summarize(first!, rollup, current, needs, reviewNodes, threadNodes); status.reviewRecord = await reviewRecord(github, pr, first!, deps.warn); if (status.autoMerge !== null) status.autoMerge.head = await enabledHead(github, first!, deps.warn); return { ...status, holds: ownership.holds, starterHold: ownership.starter }; @@ -313,27 +324,90 @@ export async function headBefore( return pushes.find((push) => push.time < time)?.sha ?? null; } +/** The checks and workflow files a base requires of a PR head. */ +export interface Needs { + /** Required checks, each from its app, or from any when `app` is null. */ + checks: { name: string; app: number | null }[]; + /** Workflow files that must pass as a whole, from {@link workflowKey}. */ + workflows: string[]; +} + +/** What `branch` requires of a PR head. The gate's own check is never on one, so it is left out. */ +export async function requiredOf(github: GitHub, repository: Repository, branch: string): Promise { + const found = await requirements(github, repository, branch); + return { checks: found.checks.filter((check) => check.name !== LAND_CHECK), workflows: found.workflows }; +} + +/** A check as a requirement matches it: its name, its app, and the workflow file a GitHub Actions job runs in. */ +export interface Reported { + name: string; + /** The app behind a check run; null for a commit status. */ + app: number | null; + /** The workflow file a GitHub Actions job runs in, from {@link workflowKey}. */ + workflow?: string; +} + +/** A rollup context as a requirement matches it; queries ask for `checkSuite { workflowRun { file { path repositoryName } } }`. */ +export function reported(context: ObjectValue): Reported { + const run = context.__typename === "CheckRun" && context.checkSuite ? object(context.checkSuite).workflowRun : null; + const file = run ? object(run).file : null; + return { + name: string(context.__typename === "CheckRun" ? context.name : context.context), + app: appOf(context), + ...(file ? { workflow: workflowKey(string(object(file).repositoryName), string(object(file).path)) } : {}), + }; +} + +/** + * The required checks and workflows nothing on the head reports yet: a check by its name, from its app when + * the requirement names one, as GitHub and `quest land` match it, and a workflow by its repository and path. + */ +export function unreported(found: Reported[], needs: Needs): string[] { + return [ + ...needs.checks + .filter( + (need) => + !found.some((entry) => entry.name === need.name && (need.app === null || entry.app === need.app)), + ) + .map((need) => (need.app === null ? need.name : `${need.name} (from app ${need.app})`)), + ...needs.workflows.filter((key) => !found.some((entry) => entry.workflow === key)), + ]; +} + +/** + * The PR's state from its pages. `current` holds the head's checks that count, from {@link currentChecks}; a + * check is required when the base requires it or its workflow, and a requirement nothing reports is pending. + */ function summarize( pull: ObjectValue, rollup: ObjectValue | null, - contexts: ObjectValue[], + current: ObjectValue[], + needs: Needs, reviewNodes: ObjectValue[], threadNodes: ObjectValue[], ): Status { const head = string(pull.headRefOid); const url = string(pull.url); + const missing = unreported(current.map(reported), needs); // The rollup's own state counts runs a newer run of the same check replaced. + const state = checksState(current); const checks = { - state: rollup === null ? null : lower(checksState(contexts)), + state: + rollup === null && missing.length === 0 + ? null + : lower(state === "SUCCESS" && missing.length > 0 ? "PENDING" : state), passed: 0, failing: [], pending: [], } as Status["checks"]; - for (const context of latestContexts(contexts)) { + for (const context of current) { const { check, outcome } = classifyCheck(context); + const workflow = reported(context).workflow; + if (workflow !== undefined && needs.workflows.includes(workflow)) check.required = true; if (outcome === "passed") checks.passed++; else checks[outcome].push(check); } + for (const name of missing) checks.pending.push({ name, required: true, result: "expected", url: null }); const reactions: Reaction[] = []; const reacted = (owner: ObjectValue, target: string) => { diff --git a/src/cli/wait.ts b/src/cli/wait.ts index 75438b5e..39bb8efa 100644 --- a/src/cli/wait.ts +++ b/src/cli/wait.ts @@ -4,6 +4,7 @@ import { createHash } from "node:crypto"; import { actor, array, + currentChecks, GitHub, type GitHubDependencies, GitHubInputError, @@ -11,12 +12,18 @@ import { object, type ObjectValue, type PullRequest, - requirements, string, } from "./github"; -import { LAND_CHECK } from "./land"; -import { latestContexts } from "../github/checks"; -import { BRANCH_UPDATES, classifyCheck, enabledHead } from "./status"; +import { + BRANCH_UPDATES, + classifyCheck, + enabledHead, + type Needs, + type Reported, + reported, + requiredOf, + unreported, +} from "./status"; const POLL = 5 * 60_000; /** @@ -65,7 +72,7 @@ const CHECKS = `query($owner: String!, $repo: String!, $number: Int!, $after: St __typename ... on CheckRun { databaseId name status conclusion completedAt detailsUrl isRequired(pullRequestNumber: $number) - checkSuite { status app { databaseId } workflowRun { file { path } } } + checkSuite { databaseId status app { databaseId } workflowRun { file { path repositoryName } } } } ... on StatusContext { context state createdAt targetUrl isRequired(pullRequestNumber: $number) } } @@ -117,19 +124,10 @@ interface Cursor { checks: { since: number; reported: string[] }; } -/** The check names and workflow files a base branch requires. */ -interface Needs { - names: Set; - workflows: string[]; -} - /** One check run or commit status on the head, with its result once it has one. */ -interface Check { - name: string; +interface Check extends Reported { required: boolean; url: string | null; - /** The workflow file a GitHub Actions job runs in. */ - workflow?: string; /** A GitHub Actions job whose workflow run has not finished, so it may still create jobs. */ running: boolean; result?: { outcome: "passed" | "failed"; point: string; time: number }; @@ -356,7 +354,7 @@ async function watch( }; /** GraphQL says which checks the base branch requires, through branch protection or rulesets. */ const checks = async (): Promise< - { head: string; base: string; checks: Check[]; pull: ObjectValue } | undefined + { head: string; base: string; checks: Check[]; unfinished: boolean; pull: ObjectValue } | undefined > => { const rows: ObjectValue[] = []; let head: string | undefined; @@ -375,22 +373,20 @@ async function watch( const page = object(contexts.pageInfo); after = page.hasNextPage === true ? string(page.endCursor) : null; } while (after !== null); - return { head: head!, base: string(pull.baseRefName), checks: latestContexts(rows).map(check), pull }; + const current = await currentChecks(github, pr, head!, rows); + // A replacing run may have no check run yet, so an unfinished run it read keeps the head busy. + const unfinished = current.runs?.some((run) => run.status !== "completed") ?? false; + return { head: head!, base: string(pull.baseRefName), checks: current.checks.map(check), unfinished, pull }; }; /** - * What the base branch requires, read once per wait. Only a requirement says whether a missing - * check has yet to start, or whether the repository requires none. The gate's own check is never - * on a PR head, so a gated base waits only for the rest. + * What the base branch requires, read once per wait, as `quest status` reads it. Only a requirement + * says whether a missing check has yet to start, or whether the repository requires none. */ const known = new Map(); const required = async (branch: string): Promise => { const cached = known.get(branch); if (cached) return cached; - const found = await requirements(github, pr, branch); - const needs = { - names: new Set(found.checks.map((check) => check.name).filter((name) => name !== LAND_CHECK)), - workflows: found.workflows, - }; + const needs = await requiredOf(github, pr, branch); known.set(branch, needs); return needs; }; @@ -497,7 +493,7 @@ async function watch( const head = await checks(); const reached = head ? [ - ...checkActivities(head.head, head.checks, await required(head.base), cursor.checks, prUrl), + ...checkActivities(head, await required(head.base), cursor.checks, prUrl), ...(await movedAutoMerge(head.head, head.pull)), ] : []; @@ -575,12 +571,10 @@ function check(row: ObjectValue): Check { const run = row.__typename === "CheckRun"; const { check: classified, outcome } = classifyCheck(row); const suite = run && row.checkSuite ? object(row.checkSuite) : undefined; - const file = suite?.workflowRun ? object(suite.workflowRun).file : null; const found: Check = { - name: classified.name, + ...reported(row), required: classified.required, url: classified.url, - ...(file ? { workflow: string(object(file).path) } : {}), running: suite?.workflowRun ? suite.status !== "COMPLETED" : false, }; if (outcome === "pending") return found; @@ -594,10 +588,17 @@ function check(row: ObjectValue): Check { * reported once per head when reached at or after the starting point. A base branch that requires * no checks requires them all. */ -function checkActivities(head: string, all: Check[], needs: Needs, state: Cursor["checks"], prUrl: string): Activity[] { +function checkActivities( + { head, checks: all, unfinished }: { head: string; checks: Check[]; unfinished: boolean }, + needs: Needs, + state: Cursor["checks"], + prUrl: string, +): Activity[] { const inWorkflow = (entry: Check) => entry.workflow !== undefined && needs.workflows.includes(entry.workflow); const required = - needs.names.size || needs.workflows.length ? all.filter((entry) => entry.required || inWorkflow(entry)) : all; + needs.checks.length || needs.workflows.length + ? all.filter((entry) => entry.required || inWorkflow(entry)) + : all; const reached = ( outcome: "passed" | "failed", entry: Check | undefined, @@ -624,9 +625,7 @@ function checkActivities(head: string, all: Check[], needs: Needs, state: Cursor // may still create. Its date is the last completion, but its name and link // always identify a required check that passed. const idle = - all.every((entry) => entry.result && !entry.running) && - [...needs.names].every((name) => all.some((entry) => entry.name === name)) && - needs.workflows.every((path) => all.some((entry) => entry.workflow === path)); + !unfinished && all.every((entry) => entry.result && !entry.running) && unreported(all, needs).length === 0; const passed = idle && settled.length === required.length && settled.every((entry) => entry.result.outcome === "passed") ? settled.at(-1) diff --git a/src/github/checks.ts b/src/github/checks.ts index 735136c6..a3b2e70e 100644 --- a/src/github/checks.ts +++ b/src/github/checks.ts @@ -15,18 +15,27 @@ export function appOf(context: RollupContext): number | null { return app?.databaseId == null ? null : Number(app.databaseId); } +/** The repository and path of the workflow file a check run's job runs in, when the query asked for them. */ +function workflowOf(context: RollupContext): [unknown, unknown] | null { + const suite = context.checkSuite as + { workflowRun?: { file?: { repositoryName?: unknown; path?: unknown } | null } | null } | null | undefined; + const file = suite?.workflowRun?.file; + return file ? [file.repositoryName ?? null, file.path ?? null] : null; +} + /** * Each check's latest report on the head, in rollup order. A check run is keyed by its app and name, - * as a required check is, and the latest is the one created last: the highest `databaseId`. A commit + * as a required check is, and by the workflow file a GitHub Actions job runs in, so a required workflow's + * job never loses to another workflow's same-named job; and the latest is the one created last: the highest `databaseId`. A commit * status is keyed by its context, and the latest is the newest `createdAt`, or on a tie the one listed * later. A check run and a status with the same name stay apart, since GitHub requires both. - * Queries must ask for a check run's `databaseId name checkSuite { app { databaseId } }` and a + * Queries must ask for a check run's `databaseId name checkSuite { app { databaseId } workflowRun { file { path repositoryName } } }` and a * status's `context createdAt`. */ export function latestContexts(contexts: T[]): T[] { const key = (context: T) => context.__typename === "CheckRun" - ? JSON.stringify(["run", appOf(context), context.name]) + ? JSON.stringify(["run", appOf(context), context.name, workflowOf(context)]) : JSON.stringify(["status", context.context]); const newer = (context: T, seen: T) => context.__typename === "CheckRun" @@ -40,6 +49,49 @@ export function latestContexts(contexts: T[]): T[] { return contexts.filter((context) => latest.get(key(context)) === context); } +/** A GitHub Actions workflow run on a head, as `GET /actions/runs` lists it. */ +export interface WorkflowRun { + id: number; + workflow_id: number; + check_suite_id: number; + /** When its latest attempt started, so a rerun is newer than runs that started before it. */ + run_started_at: string; + [field: string]: unknown; +} + +/** Whether `run` started after `other`: by its latest attempt's start, then by ID on a tie. */ +export function newerRun(run: WorkflowRun, other: WorkflowRun): boolean { + const started = Date.parse(run.run_started_at) - Date.parse(other.run_started_at); + if (Number.isNaN(started)) throw new Error(`workflow run ${run.id} or ${other.id} has no valid start time`); + return started === 0 ? run.id > other.id : started > 0; +} + +/** Whether a newer run of `run`'s workflow exists among `runs`, from any event. */ +export function replacedRun(run: WorkflowRun, runs: WorkflowRun[]): boolean { + return runs.some((other) => other.workflow_id === run.workflow_id && newerRun(other, run)); +} + +/** Whether a context is a check run that reported cancelled, the only result a replaced run's check drops. */ +export function cancelled(context: RollupContext): boolean { + return context.__typename === "CheckRun" && String(context.conclusion).toUpperCase() === "CANCELLED"; +} + +/** + * Each check's latest report that still counts: a check that reported cancelled drops once a newer run of + * its workflow exists on the head, leaving the check unreported rather than reviving an older report, as when `cancel-in-progress` cancels a run for a newer one, which may + * never report the cancelled run's names, such as a matrix job's unexpanded name. Any other result keeps + * counting, as does a cancelled run nothing replaced. A rerun reports anew in its run's check suite, so + * the rollup holds only its latest attempt. Queries must also ask for a check run's `conclusion` and + * `checkSuite { databaseId }`; `runs` are the head's workflow runs, needed only when a latest context is + * {@link cancelled}. + */ +export function currentContexts(contexts: T[], runs: WorkflowRun[]): T[] { + const replaced = new Set(runs.filter((run) => replacedRun(run, runs)).map((run) => run.check_suite_id)); + const suite = (context: T) => + Number((context.checkSuite as { databaseId?: unknown } | null | undefined)?.databaseId); + return latestContexts(contexts).filter((context) => !(cancelled(context) && replaced.has(suite(context)))); +} + /** Whether a context passed, failed, or is still pending. Neutral and skipped check runs pass. */ export function contextOutcome(context: RollupContext): "passed" | "failing" | "pending" { if (context.__typename === "CheckRun") { @@ -51,9 +103,9 @@ export function contextOutcome(context: RollupContext): "passed" | "failing" | " return state === "SUCCESS" ? "passed" : "failing"; } -/** The head's check state from its latest contexts: any failing fails, else any pending is pending. */ -export function checksState(contexts: RollupContext[]): "SUCCESS" | "FAILURE" | "PENDING" { - const outcomes = latestContexts(contexts).map(contextOutcome); +/** The state of a head's current contexts, from {@link currentContexts}: any failing fails, else any pending is pending. */ +export function checksState(current: RollupContext[]): "SUCCESS" | "FAILURE" | "PENDING" { + const outcomes = current.map(contextOutcome); if (outcomes.includes("failing")) return "FAILURE"; return outcomes.includes("pending") ? "PENDING" : "SUCCESS"; } diff --git a/test/checks.test.ts b/test/checks.test.ts index cbf00fd2..da4ab38d 100644 --- a/test/checks.test.ts +++ b/test/checks.test.ts @@ -1,6 +1,6 @@ import { expect, test } from "vitest"; -import { checksState, contextOutcome, latestContexts } from "../src/github/checks"; +import { checksState, contextOutcome, currentContexts, latestContexts, type WorkflowRun } from "../src/github/checks"; const ACTIONS = 15368; const run = (name: string, databaseId: number, conclusion: string | null, app = ACTIONS) => ({ @@ -30,22 +30,22 @@ const moq7325 = [ test("only the latest run of each check counts, as on moq-dev/moq#7325", () => { expect(latestContexts(moq7325)).toEqual([moq7325[2], moq7325[3]]); - expect(checksState(moq7325)).toBe("SUCCESS"); + expect(checksState(latestContexts(moq7325))).toBe("SUCCESS"); }); test("a newer run decides the check whatever order the rollup lists it in", () => { - expect(checksState([run("test", 9, null), run("test", 3, "SUCCESS")])).toBe("PENDING"); - expect(checksState([run("test", 3, "SUCCESS"), run("test", 9, "FAILURE")])).toBe("FAILURE"); - expect(checksState([run("test", 9, "SUCCESS"), run("test", 3, "FAILURE")])).toBe("SUCCESS"); + expect(checksState(latestContexts([run("test", 9, null), run("test", 3, "SUCCESS")]))).toBe("PENDING"); + expect(checksState(latestContexts([run("test", 3, "SUCCESS"), run("test", 9, "FAILURE")]))).toBe("FAILURE"); + expect(checksState(latestContexts([run("test", 9, "SUCCESS"), run("test", 3, "FAILURE")]))).toBe("SUCCESS"); }); test("the same name from two apps, or as a run and a status, counts as separate checks", () => { const twoApps = [run("test", 1, "FAILURE", 99), run("test", 2, "SUCCESS")]; expect(latestContexts(twoApps)).toEqual(twoApps); - expect(checksState(twoApps)).toBe("FAILURE"); + expect(checksState(latestContexts(twoApps))).toBe("FAILURE"); const runAndStatus = [run("deploy", 1, "SUCCESS"), status("deploy", "FAILURE", "2026-10-07T12:00:00Z")]; expect(latestContexts(runAndStatus)).toEqual(runAndStatus); - expect(checksState(runAndStatus)).toBe("FAILURE"); + expect(checksState(latestContexts(runAndStatus))).toBe("FAILURE"); }); test("a commit status counts by its newest report, and on a tie the one listed later", () => { @@ -75,3 +75,92 @@ test("outcomes follow GitHub's passing results", () => { ]); expect(checksState([])).toBe("SUCCESS"); }); + +const job = (name: string, databaseId: number, conclusion: string, suite: number) => ({ + ...run(name, databaseId, conclusion), + checkSuite: { databaseId: suite, app: { databaseId: ACTIONS } }, +}); +const workflowRun = (id: number, started: string, workflow = 1): WorkflowRun => ({ + id, + workflow_id: workflow, + check_suite_id: id * 10, + run_started_at: started, +}); + +// kixelated/token-max's head daaaadc: an `edited` run cancelled each `synchronize` run through +// `cancel-in-progress`, and the cancelled run's matrix job kept its unexpanded name, which the newest +// run never reports. GitHub's rollup state was FAILURE. +const tokenMax = { + runs: [ + workflowRun(1, "2026-11-12T23:39:32Z"), + workflowRun(2, "2026-11-12T23:39:42Z"), + workflowRun(3, "2026-11-12T23:42:53Z"), + workflowRun(4, "2026-11-12T23:42:56Z"), + ], + contexts: [ + job("Tests (${{ matrix.os }})", 11, "SKIPPED", 10), + job("Check", 12, "FAILURE", 10), + job("Tests (${{ matrix.os }})", 21, "SKIPPED", 20), + job("Check", 22, "FAILURE", 20), + job("Tests (${{ matrix.os }})", 31, "CANCELLED", 30), + job("Check", 32, "FAILURE", 30), + job("Tests (macos-15)", 41, "SUCCESS", 40), + job("Check", 42, "SUCCESS", 40), + ], +}; + +test("a cancelled check drops once a newer run of its workflow exists, as on kixelated/token-max's daaaadc", () => { + expect(checksState(latestContexts(tokenMax.contexts))).toBe("FAILURE"); + const current = currentContexts(tokenMax.contexts, tokenMax.runs); + expect(current.map((context) => context.databaseId)).toEqual([41, 42]); + expect(checksState(current)).toBe("SUCCESS"); +}); + +test("a cancelled check counts while nothing replaced its run, and other results always count", () => { + const contexts = [job("test", 1, "CANCELLED", 10), job("lint", 2, "FAILURE", 10), job("build", 3, "CANCELLED", 20)]; + // Run 2 is another workflow, so it replaces nothing of run 1's. + expect( + checksState( + currentContexts(contexts, [ + workflowRun(1, "2026-11-12T10:00:00Z"), + workflowRun(2, "2026-11-12T11:00:00Z", 2), + ]), + ), + ).toBe("FAILURE"); + const replaced = currentContexts(contexts, [ + workflowRun(1, "2026-11-12T10:00:00Z"), + workflowRun(9, "2026-11-12T11:00:00Z"), + ]); + expect(replaced.map((context) => context.name)).toEqual(["lint", "build"]); + // Another app's cancelled check has no workflow run to be replaced by. + expect(currentContexts([run("deploy", 1, "CANCELLED", 99)], [workflowRun(1, "2026-11-12T10:00:00Z")])).toHaveLength( + 1, + ); +}); + +test("a rerun is newer than every run that started before its latest attempt", () => { + // Run 1 was rerun after run 2 started, and its rerun cancelled run 2. + const runs = [workflowRun(1, "2026-11-12T12:00:00Z"), workflowRun(2, "2026-11-12T11:00:00Z")]; + // The rerun reports anew, after the run it cancelled. + const contexts = [job("test", 2, "CANCELLED", 20), job("test", 3, "SUCCESS", 10)]; + expect(currentContexts(contexts, runs)).toEqual([contexts[1]]); + // Runs that started in the same second order by ID. + const tied = [workflowRun(1, "2026-11-12T12:00:00Z"), workflowRun(2, "2026-11-12T12:00:00Z")]; + expect(currentContexts([job("test", 1, "CANCELLED", 10)], tied)).toEqual([]); + expect(currentContexts([job("test", 1, "CANCELLED", 20)], tied)).toHaveLength(1); +}); + +test("a job keeps its latest report per workflow file, so another workflow's same-named job never hides it", () => { + const inWorkflow = (databaseId: number, conclusion: string, repositoryName: string, path: string) => ({ + ...run("build", databaseId, conclusion), + checkSuite: { databaseId, app: { databaseId: ACTIONS }, workflowRun: { file: { path, repositoryName } } }, + }); + const ci = inWorkflow(1, "FAILURE", "example/project", ".github/workflows/ci.yml"); + const docs = inWorkflow(2, "SUCCESS", "example/project", ".github/workflows/docs.yml"); + const policy = inWorkflow(3, "SUCCESS", "example/policies", ".github/workflows/ci.yml"); + expect(latestContexts([ci, docs, policy])).toEqual([ci, docs, policy]); + expect(checksState(latestContexts([ci, docs, policy]))).toBe("FAILURE"); + // A rerun in the same workflow still supersedes. + const rerun = inWorkflow(4, "SUCCESS", "example/project", ".github/workflows/ci.yml"); + expect(latestContexts([ci, rerun])).toEqual([rerun]); +}); diff --git a/test/github.ts b/test/github.ts index b6b1a51e..861cec8b 100644 --- a/test/github.ts +++ b/test/github.ts @@ -26,7 +26,16 @@ export type Pull = Issue & { checks: "SUCCESS" | "FAILURE" | "PENDING" | null; /** The check runs behind `checks`, oldest first. */ /** `commit` is the one a run reports on; the head by default. */ - runs?: { name: string; conclusion: string; url: string; app?: number; context?: boolean; commit?: string }[]; + runs?: { + name: string; + conclusion: string; + url: string; + app?: number; + context?: boolean; + commit?: string; + /** The check suite of the workflow run that reported it. */ + suite?: number; + }[]; review: "APPROVED" | "CHANGES_REQUESTED" | "REVIEW_REQUIRED" | null; /** Oldest first; `commit` is the one a review was submitted on, the head by default. */ reviews?: { state: string; author: string; body?: string; commit?: string }[]; @@ -143,6 +152,9 @@ export type Repo = { /** GitHub Actions workflow runs, as `GET /actions/runs` lists them, with their jobs and each job's log. */ actionRuns?: { id: number; + workflow_id?: number; + check_suite_id?: number; + run_started_at?: string; name: string; head_branch: string; head_sha: string; @@ -259,7 +271,7 @@ function contextPage(pull: Pick, after: unknown) { status: "COMPLETED", conclusion: run.conclusion, detailsUrl: run.url, - checkSuite: { app: { databaseId: run.app ?? ACTIONS_APP } }, + checkSuite: { databaseId: run.suite ?? null, app: { databaseId: run.app ?? ACTIONS_APP } }, }, ); const end = start + 100; diff --git a/test/hold.test.ts b/test/hold.test.ts index e240cc96..e80b3016 100644 --- a/test/hold.test.ts +++ b/test/hold.test.ts @@ -51,6 +51,8 @@ class FakePull { rules: ObjectValue[] = []; /** The head's check runs, as its `statusCheckRollup` lists them. */ checks: ObjectValue[] = []; + /** The head's workflow runs, as `GET /actions/runs` lists them. */ + runs: ObjectValue[] = []; autoMerge: ObjectValue | null = null; queueEntry: ObjectValue | null = null; labels: string[] = []; @@ -114,6 +116,8 @@ class FakePull { return json(200, []); } if (url.pathname === "/repos/example/project/branches/main") return json(200, { protection: {} }); + if (url.pathname === "/repos/example/project/actions/runs" && url.searchParams.get("head_sha") === this.head) + return json(200, { total_count: this.runs.length, workflow_runs: this.runs }); if (url.pathname === `/repos/example/project/compare/main...${this.head}`) { this.comparisons.push(`${url.pathname}${url.search}`); return json(200, { status: this.comparison, base_commit: { sha: this.base } }); @@ -344,6 +348,28 @@ describe("merge --land", () => { expect(pull.writes).not.toEqual([]); }); + test("lands past a cancelled check once a newer run of its workflow exists, as on kixelated/token-max#78", async () => { + const pull = new FakePull(); + const run = (databaseId: number, name: string, conclusion: string, suite: number) => ({ + __typename: "CheckRun", + databaseId, + name, + status: "COMPLETED", + conclusion, + detailsUrl: `https://ci.example/${databaseId}`, + checkSuite: { databaseId: suite, app: { databaseId: 15368 } }, + }); + pull.checks = [run(1, "Tests (${{ matrix.os }})", "CANCELLED", 10), run(2, "Tests (macos-15)", "SUCCESS", 20)]; + pull.runs = [{ id: 1, workflow_id: 7, check_suite_id: 10, run_started_at: "2026-11-12T23:42:53Z" }]; + expect(await refused(land(PR, { sha: HEAD, method: "merge", check: clean }, pull.deps()))).toContain( + "Tests (${{ matrix.os }})", + ); + expect(pull.writes).toEqual([]); + pull.runs.push({ id: 2, workflow_id: 7, check_suite_id: 20, run_started_at: "2026-11-12T23:42:56Z" }); + await land(PR, { sha: HEAD, method: "merge", check: clean }, pull.deps()); + expect(pull.writes).not.toEqual([]); + }); + test("refuses when the token cannot see the auto-merge setting", async () => { const pull = new FakePull(); pull.allowAutoMerge = null; diff --git a/test/land-bump.test.ts b/test/land-bump.test.ts index 22ea34cf..4896339d 100644 --- a/test/land-bump.test.ts +++ b/test/land-bump.test.ts @@ -244,6 +244,9 @@ const bare = (world: World) => world.remote.slice("file://".length); beforeEach(() => landed.mockClear()); +/** A workflow run as `GET /actions/runs` lists it, before its status. */ +const STARTED = { id: 1, workflow_id: 10, event: "push", check_suite_id: 100, run_started_at: "2026-11-12T12:00:00Z" }; + describe("questInput", () => { test("finds the one root input locking the source, skipping follows and other inputs", () => { expect(questInput(JSON.parse(lockFile(OLD, true)), SOURCE)).toEqual({ @@ -383,11 +386,11 @@ describe("bumpPass", () => { target.suites[head][0].status = "completed"; // A queued workflow with no check run yet still holds it. target.suites[head].push({ status: "queued", latest_check_runs_count: 0, app: { slug: "github-actions" } }); - target.runs[head] = [{ status: "queued", conclusion: null }]; + target.runs[head] = [{ ...STARTED, status: "queued", conclusion: null }]; expect(await bumpPass(ctx, NEW)).toEqual([]); expect(landed).not.toHaveBeenCalled(); - target.runs[head] = [{ status: "completed", conclusion: "success" }]; + target.runs[head] = [{ ...STARTED, status: "completed", conclusion: "success" }]; expect(await bumpPass(ctx, NEW)).toEqual(["bump landing moq-dev/widget 7"]); expect(landed).toHaveBeenCalledExactlyOnceWith( { ...target.target, number: 7 }, @@ -403,8 +406,20 @@ describe("bumpPass", () => { const head = git(bare(target), "rev-parse", BUMP_BRANCH); // Push and pull_request share a cancel-in-progress group: the pull_request run cancelled the push run, // whose later job reported cancelled before the newer run started it. - const cancelled = { id: 1, workflow_id: 10, event: "push", check_suite_id: 100 }; - const newer = { id: 2, workflow_id: 10, event: "pull_request", check_suite_id: 200 }; + const cancelled = { + id: 1, + workflow_id: 10, + event: "push", + check_suite_id: 100, + run_started_at: "2026-11-12T12:00:00Z", + }; + const newer = { + id: 2, + workflow_id: 10, + event: "pull_request", + check_suite_id: 200, + run_started_at: "2026-11-12T12:00:01Z", + }; const run = (suite: number, id: number, name: string, status: string, conclusion?: string) => ({ __typename: "CheckRun", databaseId: id, @@ -444,7 +459,7 @@ describe("bumpPass", () => { { id: 5, workflow_id: 30, event: "push", conclusion: "failure" }, { id: 6, workflow_id: 30, event: "pull_request", conclusion: "success" }, { id: 7, workflow_id: 40, event: "push", conclusion: "cancelled" }, - ]; + ].map((run) => ({ ...run, check_suite_id: run.id * 10, run_started_at: "2026-11-12T12:00:00Z" })); // Another event's failure still counts, as does a cancellation nothing replaced. expect(latestRuns(runs).map((run) => run.id)).toEqual([2, 4, 5, 6, 7]); }); diff --git a/test/land.test.ts b/test/land.test.ts index a497eebe..700074b1 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -393,6 +393,7 @@ class Forge { pull_requests: "write", issues: "write", metadata: "read", + actions: "read", ...permissions, }, }), @@ -1151,6 +1152,51 @@ describe.skipIf(process.platform === "win32")("quest land", () => { expect(plan.waiting?.map(({ number }) => number)).toEqual([3]); }); + test("drops a cancelled check once a newer run of its workflow exists, even one with no checks yet", async () => { + const forge = new Forge(); + const test = (conclusion: string, suite: number) => ({ name: "test", conclusion, url: "u", suite }); + const workflow = (pull: Pull, runs: [number, string, string][]) => { + for (const [id, conclusion, started] of runs) + (forge.repo.actionRuns ??= []).push({ + id, + workflow_id: 7, + check_suite_id: id * 10, + run_started_at: started, + name: "CI", + head_branch: pull.branch ?? "", + head_sha: pull.head!, + status: conclusion === "queued" ? "queued" : "completed", + conclusion: conclusion === "queued" ? null : conclusion, + html_url: "u", + jobs: [], + }); + }; + // As on kixelated/token-max#78: an edit's run cancelled the push's run, and has yet to report a check. + const replaced = forge.pull("Replaced", { "1.txt": "1\n" }, { pull: { runs: [test("CANCELLED", 10)] } }); + workflow(replaced, [ + [1, "cancelled", "2026-11-12T23:42:53Z"], + [2, "queued", "2026-11-12T23:42:56Z"], + ]); + // A cancelled run that nothing replaced still fails. + const alone = forge.pull("Alone", { "2.txt": "2\n" }, { pull: { runs: [test("CANCELLED", 30)] } }); + workflow(alone, [[3, "cancelled", "2026-11-12T23:42:53Z"]]); + // Rerunning the cancelled run makes it the newest, so the run it cancelled in turn is the replaced one. + // The rerun reports anew, after the run it cancelled. + const rerun = forge.pull( + "Rerun", + { "3.txt": "3\n" }, + { pull: { runs: [test("CANCELLED", 50), test("SUCCESS", 40)] } }, + ); + workflow(rerun, [ + [4, "success", "2026-11-12T23:50:00Z"], + [5, "cancelled", "2026-11-12T23:43:00Z"], + ]); + const plan = await resolveQueue(REPOSITORY, { base: "main", method: "squash" }, await forge.deps()); + expect(plan.queue.map((entry) => entry.number)).toEqual([3]); + expect(plan.waiting?.map(({ number }) => number)).toEqual([1]); + expect(plan.eject.map((ejected) => [ejected.number, ejected.reason])).toEqual([[2, "failed_checks"]]); + }); + test("counts a required check only from the app its rule names", async () => { const forge = new Forge(); forge.repo.rules = [ diff --git a/test/status.test.ts b/test/status.test.ts index 7cec8117..c38be319 100644 --- a/test/status.test.ts +++ b/test/status.test.ts @@ -16,6 +16,8 @@ const page = (nodes: Node[], next?: string) => ({ }); /** A recorded GraphQL response; omitted connections stand for ones the query skipped. */ +const CI = ".github/workflows/ci.yml"; + function pull( fields: { head?: string; @@ -116,6 +118,12 @@ function setup( warnings?: string[]; /** The base-to-head diff per compared head, or the status GitHub refuses it with. */ diffs?: Record; + /** The base's active ruleset rules. */ + rules?: Node[]; + /** The base branch is gone, so reading it fails. */ + noBase?: boolean; + /** The head's workflow runs, as `GET /actions/runs` lists them. */ + runs?: Node[]; } = {}, ) { const queries: Record[] = []; @@ -141,6 +149,19 @@ function setup( return Response.json({ message: "Resource not accessible by integration" }, { status: 403 }); return Response.json(page.rows, { headers: page.next ? { Link: `<${page.next}>; rel="next"` } : {} }); } + // Ruleset workflows name their repository by ID: 1 is this one, 2 another. + if (path.pathname === "/repositories/1") return Response.json({ full_name: "example/project" }); + if (path.pathname === "/repositories/2") return Response.json({ full_name: "example/policies" }); + if (path.pathname === "/repos/example/project/branches/main") + return options.noBase + ? Response.json({ message: "Branch not found" }, { status: 404 }) + : Response.json({ protection: { enabled: false } }); + if (path.pathname === "/repos/example/project/rules/branches/main") + return Response.json(options.rules ?? []); + if (path.pathname === "/repos/example/project/actions/runs") { + if (!options.runs) throw new Error("unexpected workflow run request"); + return Response.json({ total_count: options.runs.length, workflow_runs: options.runs }); + } const compare = /^\/repos\/example\/project\/compare\/main\.\.\.([0-9a-f]{40})$/.exec(path.pathname); if (compare) { expect(new Headers(init?.headers).get("Accept")).toBe("application/vnd.github.diff"); @@ -187,36 +208,45 @@ function setup( } test("reports checks by requiredness, each reviewer's latest review against the head, and open threads", async () => { - const fixture = setup([ - pull({ - checks: page([ - run("lint", "COMPLETED", "FAILURE", true), - run("docs", "COMPLETED", "CANCELLED", false), - run("test", "IN_PROGRESS", null, true), - run("build", "COMPLETED", "SUCCESS", true), - run("publish", "COMPLETED", "SKIPPED", false), - { - __typename: "StatusContext", - context: "ci/legacy", - state: "ERROR", - targetUrl: null, - isRequired: false, - }, - { __typename: "StatusContext", context: "deploy", state: "EXPECTED", targetUrl: "", isRequired: true }, - ]), - reviews: page([ - review("alice", "APPROVED", B, 1), - review("bob", "CHANGES_REQUESTED", B, 2), - review("alice", "COMMENTED", A, 3, [ - { content: "THUMBS_UP", createdAt: "2026-10-07T12:04:00Z", user: { login: "codex[bot]" } }, + const fixture = setup( + [ + pull({ + checks: page([ + run("lint", "COMPLETED", "FAILURE", true), + run("docs", "COMPLETED", "CANCELLED", false), + run("test", "IN_PROGRESS", null, true), + run("build", "COMPLETED", "SUCCESS", true), + run("publish", "COMPLETED", "SKIPPED", false), + { + __typename: "StatusContext", + context: "ci/legacy", + state: "ERROR", + targetUrl: null, + isRequired: false, + }, + { + __typename: "StatusContext", + context: "deploy", + state: "EXPECTED", + targetUrl: "", + isRequired: true, + }, ]), - review("viewer", "PENDING", A, 4), - review("gone", "COMMENTED", null, 5), - ]), - threads: page([thread("T1", true), thread("T2", false), thread("T3", false, null, "author")]), - reactions: [{ content: "EYES", createdAt: "2026-10-07T12:00:00Z", user: null }], - }), - ]); + reviews: page([ + review("alice", "APPROVED", B, 1), + review("bob", "CHANGES_REQUESTED", B, 2), + review("alice", "COMMENTED", A, 3, [ + { content: "THUMBS_UP", createdAt: "2026-10-07T12:04:00Z", user: { login: "codex[bot]" } }, + ]), + review("viewer", "PENDING", A, 4), + review("gone", "COMMENTED", null, 5), + ]), + threads: page([thread("T1", true), thread("T2", false), thread("T3", false, null, "author")]), + reactions: [{ content: "EYES", createdAt: "2026-10-07T12:00:00Z", user: null }], + }), + ], + { runs: [] }, + ); const status = await pullRequestStatus(pr, fixture.deps); expect(fixture.queries).toHaveLength(1); expect(fixture.holdQueries).toHaveLength(1); @@ -462,6 +492,242 @@ test("drops the landing exit once the PR enters the merge queue or auto-merge ag expect(await stopped(ejected, { mergeQueueEntry: queue("2026-10-07T12:00:59Z") })).toBe("merge_conflict"); }); +test("drops a cancelled check a newer run of its workflow replaced, as on kixelated/token-max#78", async () => { + const job = (name: string, conclusion: string, databaseId: number, suite: number) => ({ + ...run(name, "COMPLETED", conclusion, false), + databaseId, + checkSuite: { + databaseId: suite, + app: { databaseId: 15368 }, + workflowRun: { file: { path: CI, repositoryName: "example/project" } }, + }, + }); + const runs = (started: string) => [ + { id: 1, workflow_id: 7, check_suite_id: 10, run_started_at: "2026-11-12T23:42:53Z" }, + { id: 2, workflow_id: 7, check_suite_id: 20, run_started_at: started }, + ]; + const checks = () => + page([job("Tests (${{ matrix.os }})", "CANCELLED", 1, 10), job("Tests (macos-15)", "SUCCESS", 2, 20)]); + const fixture = setup([pull({ checks: checks(), reviews: page([]), threads: page([]) })], { + runs: runs("2026-11-12T23:42:56Z"), + }); + const status = await pullRequestStatus(pr, fixture.deps); + expect(status.checks).toEqual({ state: "success", passed: 1, failing: [], pending: [] }); + // A rerun of the cancelled run started later, so its run is the newest and its cancellation counts. + const rerun = setup([pull({ checks: checks(), reviews: page([]), threads: page([]) })], { + runs: runs("2026-11-12T23:40:00Z"), + }); + expect((await pullRequestStatus(pr, rerun.deps)).checks.state).toBe("failure"); +}); + +test("a required check or workflow nothing reports is pending, except the gate's own check", async () => { + const job = (name: string, workflow: string) => ({ + ...run(name, "COMPLETED", "SUCCESS", false), + databaseId: 1, + checkSuite: { + databaseId: 10, + app: { databaseId: 15368 }, + workflowRun: { file: { path: workflow, repositoryName: "example/project" } }, + }, + }); + const rules = [ + { + type: "required_status_checks", + parameters: { + required_status_checks: [{ context: "test" }, { context: "lint" }, { context: "quest land" }], + }, + }, + { + type: "workflows", + parameters: { + workflows: [ + { path: CI, repository_id: 1 }, + { path: ".github/workflows/pr.yml", repository_id: 1 }, + ], + }, + }, + ]; + const fixture = setup([pull({ checks: page([job("test", CI)]), reviews: page([]), threads: page([]) })], { + rules, + }); + const status = await pullRequestStatus(pr, fixture.deps); + expect(statusLines(status).filter((line) => line.startsWith("check"))).toEqual([ + "checks pending passed=1 failing=0 pending=2", + "check pending required expected - lint", + "check pending required expected - example/project:.github/workflows/pr.yml", + ]); + // With no checks on the head at all, the requirements still leave it pending. + const empty = setup([pull({ checks: null, reviews: page([]), threads: page([]) })], { rules }); + expect((await pullRequestStatus(pr, empty.deps)).checks.state).toBe("pending"); +}); + +test("a required check from another app than its requirement names stays pending, and a closed PR reads no requirements", async () => { + const rules = [ + { + type: "required_status_checks", + parameters: { required_status_checks: [{ context: "test", integration_id: 99 }] }, + }, + ]; + const test = { + ...run("test", "COMPLETED", "SUCCESS", false), + databaseId: 1, + checkSuite: { + databaseId: 10, + app: { databaseId: 15368 }, + workflowRun: { file: { path: CI, repositoryName: "example/project" } }, + }, + }; + const open = setup([pull({ checks: page([test]), reviews: page([]), threads: page([]) })], { rules }); + expect(statusLines(await pullRequestStatus(pr, open.deps)).filter((line) => line.startsWith("check"))).toEqual([ + "checks pending passed=1 failing=0 pending=1", + "check pending required expected - test (from app 99)", + ]); + // A merged PR's base may be gone, and nothing it requires can change the PR anymore. + const merged = setup([pull({ state: "MERGED", checks: page([test]), reviews: page([]), threads: page([]) })], { + rules: undefined, + noBase: true, + }); + expect((await pullRequestStatus(pr, merged.deps)).checks.state).toBe("success"); +}); + +test("a replacing run that has not started yet stands in by its creation time", async () => { + const job = { + ...run("Tests (${{ matrix.os }})", "COMPLETED", "CANCELLED", false), + databaseId: 1, + checkSuite: { + databaseId: 10, + app: { databaseId: 15368 }, + workflowRun: { file: { path: CI, repositoryName: "example/project" } }, + }, + }; + const fixture = setup([pull({ checks: page([job]), reviews: page([]), threads: page([]) })], { + runs: [ + { id: 1, workflow_id: 7, check_suite_id: 10, run_started_at: "2026-11-12T23:42:53Z" }, + { id: 2, workflow_id: 7, check_suite_id: 20, run_started_at: null, created_at: "2026-11-12T23:42:56Z" }, + ], + }); + expect((await pullRequestStatus(pr, fixture.deps)).checks).toEqual({ + state: "success", + passed: 0, + failing: [], + pending: [], + }); +}); + +test("a required workflow matches its repository as well as its path", async () => { + const build = { + ...run("build", "COMPLETED", "SUCCESS", false), + databaseId: 1, + checkSuite: { + databaseId: 10, + app: { databaseId: 15368 }, + workflowRun: { file: { path: CI, repositoryName: "example/project" } }, + }, + }; + // The rule requires example/policies's ci.yml, which the local ci.yml at the same path does not satisfy. + const fixture = setup([pull({ checks: page([build]), reviews: page([]), threads: page([]) })], { + rules: [{ type: "workflows", parameters: { workflows: [{ path: CI, repository_id: 2 }] } }], + }); + const status = await pullRequestStatus(pr, fixture.deps); + expect(status.checks.state).toBe("pending"); + expect(status.checks.pending.map((check) => check.name)).toEqual([`example/policies:${CI}`]); +}); + +test("a required workflow counts as reported when another workflow's same-named job is the newer one", async () => { + const build = (databaseId: number, path: string) => ({ + ...run("build", "COMPLETED", "SUCCESS", false), + databaseId, + checkSuite: { + databaseId: databaseId * 10, + app: { databaseId: 15368 }, + workflowRun: { file: { path, repositoryName: "example/project" } }, + }, + }); + const fixture = setup( + [ + pull({ + checks: page([build(1, CI), build(2, ".github/workflows/docs.yml")]), + reviews: page([]), + threads: page([]), + }), + ], + { rules: [{ type: "workflows", parameters: { workflows: [{ path: CI, repository_id: 1 }] } }] }, + ); + expect((await pullRequestStatus(pr, fixture.deps)).checks.state).toBe("success"); +}); + +test("a required workflow's failed job still fails when another workflow's same-named job passed later", async () => { + const build = (databaseId: number, path: string, conclusion: string) => ({ + ...run("build", "COMPLETED", conclusion, false), + databaseId, + checkSuite: { + databaseId: databaseId * 10, + app: { databaseId: 15368 }, + workflowRun: { file: { path, repositoryName: "example/project" } }, + }, + }); + const fixture = setup( + [ + pull({ + checks: page([build(1, CI, "FAILURE"), build(2, ".github/workflows/docs.yml", "SUCCESS")]), + reviews: page([]), + threads: page([]), + }), + ], + { rules: [{ type: "workflows", parameters: { workflows: [{ path: CI, repository_id: 1 }] } }] }, + ); + const status = await pullRequestStatus(pr, fixture.deps); + expect(status.checks.state).toBe("failure"); + expect(status.checks.failing.map((check) => [check.name, check.required])).toEqual([["build", true]]); +}); + +test("a required workflow whose only job a queued run replaced is pending", async () => { + const build = { + ...run("build", "COMPLETED", "CANCELLED", false), + databaseId: 1, + checkSuite: { + databaseId: 10, + app: { databaseId: 15368 }, + workflowRun: { file: { path: CI, repositoryName: "example/project" } }, + }, + }; + const fixture = setup([pull({ checks: page([build]), reviews: page([]), threads: page([]) })], { + rules: [{ type: "workflows", parameters: { workflows: [{ path: CI, repository_id: 1 }] } }], + runs: [ + { id: 1, workflow_id: 7, check_suite_id: 10, run_started_at: "2026-11-12T23:42:53Z" }, + { id: 2, workflow_id: 7, check_suite_id: 20, run_started_at: null, created_at: "2026-11-12T23:42:56Z" }, + ], + }); + const status = await pullRequestStatus(pr, fixture.deps); + expect(status.checks.state).toBe("pending"); + expect(status.checks.pending.map((check) => check.name)).toEqual([`example/project:${CI}`]); +}); + +test("a job in a required workflow counts as required", async () => { + const fixture = setup( + [ + pull({ + checks: page([ + { + ...run("build", "COMPLETED", "FAILURE", false), + databaseId: 1, + checkSuite: { + databaseId: 10, + app: { databaseId: 15368 }, + workflowRun: { file: { path: CI, repositoryName: "example/project" } }, + }, + }, + ]), + reviews: page([]), + threads: page([]), + }), + ], + { rules: [{ type: "workflows", parameters: { workflows: [{ path: CI, repository_id: 1 }] } }] }, + ); + const status = await pullRequestStatus(pr, fixture.deps); + expect(status.checks.failing.map((check) => [check.name, check.required])).toEqual([["build", true]]); +}); + test("counts only each check's newest run from its app", async () => { const rerun = (name: string, conclusion: string, databaseId: number, app = 15368) => ({ ...run(name, "COMPLETED", conclusion, true), @@ -485,7 +751,7 @@ test("counts only each check's newest run from its app", async () => { const status = await pullRequestStatus(pr, fixture.deps); // The fixture answers whatever is asked, so the query must ask for what tells runs apart. expect(fixture.texts[0].replace(/\s+/g, " ")).toContain( - "... on CheckRun { databaseId name status conclusion detailsUrl isRequired(pullRequestNumber: $number) checkSuite { app { databaseId } } }", + "... on CheckRun { databaseId name status conclusion detailsUrl isRequired(pullRequestNumber: $number) checkSuite { databaseId app { databaseId } workflowRun { file { path repositoryName } } } }", ); expect(status.checks.passed).toBe(2); expect(status.checks.failing.map((check) => check.name)).toEqual(["test", "lint"]); diff --git a/test/wait.test.ts b/test/wait.test.ts index f3233bb6..79fcac48 100644 --- a/test/wait.test.ts +++ b/test/wait.test.ts @@ -88,6 +88,10 @@ interface CheckFixture { running?: boolean; /** A check run's ID, which grows with each run; its place in the page by default. */ id?: number; + /** The app behind a check run; GitHub Actions by default. */ + app?: number; + /** The check suite of the workflow run behind a check run. */ + suite?: number; } /** The head's checks; `null` means the head has no checks at all. */ @@ -119,7 +123,14 @@ function checks( isRequired: entry.required ?? false, checkSuite: { status: entry.result && !entry.running ? "COMPLETED" : "IN_PROGRESS", - workflowRun: { file: { path: entry.workflow ?? ".github/workflows/check.yml" } }, + databaseId: entry.suite ?? null, + app: { databaseId: entry.app ?? ACTIONS }, + workflowRun: { + file: { + path: entry.workflow ?? ".github/workflows/check.yml", + repositoryName: "example/project", + }, + }, }, }; return response({ @@ -165,6 +176,8 @@ function setup( contextsOnly?: boolean; ruled?: string[]; rules?: unknown[]; + /** The head's workflow runs, as `GET /actions/runs` lists them, one listing per read; the last repeats. */ + runs?: unknown[][]; } = {}, ) { let now = Date.parse("2026-10-07T12:10:00Z"); @@ -203,6 +216,7 @@ function setup( sideCalls.push({ ...call, body }); return side.graphql?.shift() ?? reactions(); } + if (parsed.pathname === "/repositories/1") return Response.json({ full_name: "example/project" }); if (parsed.pathname === `${base}/branches/main`) { ruleCalls.push(call); const contexts = side.protected ?? []; @@ -217,6 +231,11 @@ function setup( }, }); } + if (parsed.pathname === `${base}/actions/runs` && side.runs) { + ruleCalls.push(call); + const listed = side.runs.length > 1 ? side.runs.shift()! : side.runs[0]!; + return response({ total_count: listed.length, workflow_runs: listed }); + } if (parsed.pathname === `${base}/rules/branches/main`) { ruleCalls.push(call); if (parsed.searchParams.get("page") === "2") return response(side.rules); @@ -1077,7 +1096,7 @@ test("a rerun supersedes a cancelled run of a required check, which never wakes expect(lines[0]).toMatch(/^2026-10-07T12:08:00Z checks:passed check /); const query = (fixture.checkCalls[0].body as { query: string }).query.replace(/\s+/g, " "); expect(query).toContain("... on CheckRun { databaseId name"); - expect(query).toContain("checkSuite { status app { databaseId }"); + expect(query).toContain("checkSuite { databaseId status app { databaseId }"); }); test("a superseded workflow run's failed gate never wakes the wait, as on moq-dev/moq#7325", async () => { @@ -1135,7 +1154,7 @@ test("a required check named on a later page of rules holds back checks:passed", checks([{ name: "check", required: true, result: "SUCCESS", time: LATER }]), checks([ { name: "check", required: true, result: "SUCCESS", time: LATER }, - { name: "deploy", required: true, result: "SUCCESS", time: LATEST }, + { name: "deploy", required: true, result: "SUCCESS", time: LATEST, app: 42 }, ]), ], ruled: ["check"], @@ -1157,6 +1176,59 @@ test("a required check named on a later page of rules holds back checks:passed", ]); }); +test("a required check from another app than its requirement names holds back checks:passed", async () => { + const fixture = setup([pull(), response([pushed()]), response([committed(A)]), response([committed(A)])], [A], { + checks: [ + checks([{ name: "check", required: true, result: "SUCCESS", time: LATER, app: 99 }]), + checks([ + { name: "check", required: true, result: "SUCCESS", time: LATER, app: 99 }, + { name: "check", required: true, result: "SUCCESS", time: LATEST, id: 5 }, + ]), + ], + ruled: ["check"], + }); + const lines = await waitForActivity(pr, {}, fixture.deps); + expect(fixture.sleeps).toEqual([300_000]); + expect(lines).toEqual([expect.stringMatching(/^2026-10-07T12:08:00Z checks:passed check /)]); +}); + +test("a cancelled check a newer run replaced never fails the wait, which stays busy until that run finishes", async () => { + const run = (id: number, status: string) => ({ + id, + workflow_id: 7, + check_suite_id: id * 10, + status, + // A run not started yet has no start time. + run_started_at: status === "queued" ? null : "2026-10-07T12:01:00Z", + created_at: `2026-10-07T12:0${id}:00Z`, + }); + const cancelled: CheckFixture = { + name: "Tests (${{ matrix.os }})", + result: "CANCELLED", + time: LATER, + id: 1, + suite: 10, + }; + const fixture = setup([pull(), response([pushed()]), response([committed(A)]), response([committed(A)])], [A], { + checks: [ + checks([cancelled, { name: "check", required: true, result: "SUCCESS", time: LATER, id: 2 }]), + checks([ + cancelled, + { name: "check", required: true, result: "SUCCESS", time: LATER, id: 2 }, + { name: "later", result: "SUCCESS", time: LATEST, id: 3 }, + ]), + ], + ruled: ["check"], + runs: [ + [run(1, "completed"), run(2, "queued")], + [run(1, "completed"), { ...run(2, "completed"), run_started_at: "2026-10-07T12:02:00Z" }], + ], + }); + const lines = await waitForActivity(pr, {}, fixture.deps); + expect(fixture.sleeps).toEqual([300_000]); + expect(lines).toEqual([expect.stringMatching(/^2026-10-07T12:08:00Z checks:passed check /)]); +}); + const CI = ".github/workflows/ci.yml"; const workflowRule = { type: "workflows", parameters: { workflows: [{ path: CI, repository_id: 1 }] } }; @@ -1187,6 +1259,21 @@ test("a required workflow requires its jobs, keeps optional checks optional, and expect(lines[0]).toMatch(/^2026-10-07T12:08:00Z checks:passed gate /); }); +test("a required workflow's job counts toward checks:passed when another workflow's same-named job is newer", async () => { + const fixture = setup([pull(), response([pushed()]), response([committed(A)]), response([committed(A)])], [A], { + checks: [ + checks([ + { name: "build", result: "SUCCESS", time: LATER, workflow: CI, id: 1 }, + { name: "build", result: "SUCCESS", time: LATEST, workflow: ".github/workflows/docs.yml", id: 2 }, + ]), + ], + rules: [workflowRule], + }); + const lines = await waitForActivity(pr, {}, fixture.deps); + expect(lines).toHaveLength(1); + expect(lines[0]).toMatch(/ checks:passed /); +}); + test("a failing job in a required workflow wakes the wait", async () => { const fixture = setup([pull(), response([pushed()]), response([committed(A)])], [A], { checks: [ @@ -1219,7 +1306,7 @@ test("checks:passed is dated when the head goes idle, after required checks fini expect(lines[0]).toMatch(/^2026-10-07T12:08:00Z checks:passed check /); const query = (fixture.checkCalls[0].body as { query: string }).query.replace(/\s+/g, " "); expect(query).toContain("... on CheckRun { databaseId name"); - expect(query).toContain("checkSuite { status app { databaseId }"); + expect(query).toContain("checkSuite { databaseId status app { databaseId }"); }); test("with no required checks, an unfinished workflow run holds back checks:passed", async () => {