diff --git a/README.md b/README.md index a578d9fc..7326ce65 100644 --- a/README.md +++ b/README.md @@ -415,8 +415,9 @@ this command's scope. 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 +head is still going (no pending check, no finished job whose GitHub Actions run +is still going, and, once a cancelled check makes it read the head's runs, no +unfinished run, since each may still create jobs), every check and workflow the base branch names is 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 @@ -770,7 +771,8 @@ 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; checks count 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`, +except that a required workflow also waits until its run, or a run of the same workflow replacing a cancelled one, finishes. 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 deleted file mode 100644 index 8fc4f5d3..00000000 --- a/quest/m0/gate-required-workflows.md +++ /dev/null @@ -1,11 +0,0 @@ -# [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/src/cli/land-queue.ts b/src/cli/land-queue.ts index 3d282c1a..0f5929e0 100644 --- a/src/cli/land-queue.ts +++ b/src/cli/land-queue.ts @@ -721,7 +721,7 @@ export async function ownPass( for (const waiting of plan.waiting ?? []) { open.add(waiting.number); const waits = [ - ...waiting.pending.map((name) => `the required check \`${name}\` to pass`), + ...waiting.pending.map((name) => `the required check or workflow \`${name}\` to pass`), ...(waiting.approval ? [`an approving review, which ${base} requires`] : []), ]; await own.show(waiting.number, waiting.head, { diff --git a/src/cli/land-serve.ts b/src/cli/land-serve.ts index 7d584d65..6dfb9957 100644 --- a/src/cli/land-serve.ts +++ b/src/cli/land-serve.ts @@ -336,7 +336,7 @@ async function reportStalled(github: GitHub, plan: Plan, state: string, now: num for (const waiting of plan.waiting ?? []) { if (kept[waiting.number] === waiting.enabledAt || now - Date.parse(waiting.enabledAt) < STALLED_AFTER) continue; const list = [ - ...waiting.pending.map((name) => `- the required check ${excerpt(name)}`), + ...waiting.pending.map((name) => `- the required check or workflow ${excerpt(name)}`), ...(waiting.approval ? [`- an approving review, which ${plan.base.ref} requires`] : []), ].join("\n"); await github.request(`/repos/${plan.repository}/issues/${waiting.number}/comments`, { diff --git a/src/cli/land.ts b/src/cli/land.ts index 23e3bb42..73f63b18 100644 --- a/src/cli/land.ts +++ b/src/cli/land.ts @@ -17,8 +17,9 @@ import { requirements, string, } from "./github"; +import type { WorkflowRun } from "../github/checks"; import { appOf } from "../github/checks"; -import { classifyCheck } from "./status"; +import { classifyCheck, reported, type Needs } from "./status"; import type { Method } from "./hold"; import { committedSources, snapshot } from "./tree"; @@ -165,7 +166,7 @@ export interface Waiting { enabledAt: string; /** The pinned head it waits on. */ head: string; - /** The required checks still pending on its head. */ + /** The required checks, and required workflows as `owner/name:path`, still pending on its head. */ pending: string[]; /** It still needs an approving review its base requires, which an automated reviewer may give. */ approval: boolean; @@ -175,7 +176,7 @@ export interface Waiting { const CONTEXTS = `pageInfo { hasNextPage endCursor } nodes { __typename - ... on CheckRun { databaseId name status conclusion detailsUrl checkSuite { databaseId app { databaseId } workflowRun { file { path repositoryName } } } } + ... on CheckRun { databaseId name status conclusion detailsUrl checkSuite { databaseId status app { databaseId } workflowRun { file { path repositoryName } } } } ... on StatusContext { context state createdAt targetUrl } }`; @@ -215,6 +216,7 @@ export const UPHELD: Record = { pull_request: "each PR's review decision must not block it, and its review threads must be resolved if the rule says so", required_status_checks: "each required check must pass on each PR's head, from the app the rule names", + workflows: "each job of each required workflow must pass on each PR's head, which waits until the workflow reports", copilot_code_review: "it requests a review and blocks nothing", }; @@ -345,6 +347,13 @@ export async function resolveQueue( `${name}'s ${ref} has ruleset rules the gate does not uphold, and its merge bypasses them: ${refused.join("; ")}. It upholds only ${Object.keys(UPHELD).join(", ")}`, ); } + // A workflow from a repository the token cannot read is named by id, which no job can match. + const unreadable = needs.workflows.filter((key) => key.startsWith("repository ")); + if (unreadable.length > 0) { + throw new Error( + `${name}'s ${ref} requires workflows from repositories the gate cannot read, so no job can satisfy them: ${unreadable.join(", ")}`, + ); + } // Classic protection's required checks are already among `needs.checks`. const classic = needs.classic ? await classicProtection(github, repository, ref) : null; const unmodeled = classic === null ? [] : unheldClassic(classic, target.method); @@ -362,7 +371,10 @@ export async function resolveQueue( rule.parameters != null && object(rule.parameters).required_review_thread_resolution === true, ); - const required = needs.checks.filter((check) => check.name !== LAND_CHECK); + const required: Needs = { + checks: needs.checks.filter((check) => check.name !== LAND_CHECK), + workflows: needs.workflows, + }; let sha: string | undefined; const nodes: ObjectValue[] = []; @@ -434,11 +446,12 @@ export async function resolveQueue( const commit = object(object(array(object(node.commits).nodes)[0]).commit); const rollup = commit.oid === approved ? commit.statusCheckRollup : undefined; const found = await contexts(github, repository, approved, rollup); - const unmet = checks((await currentChecks(github, repository, approved, found)).checks, required); + const current = await currentChecks(github, repository, approved, found); + const unmet = checks(current.checks, required, current.runs, found); // 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) { - eject("failed_checks", `required checks failed on its head: ${unmet.failing.join(", ")}`); + eject("failed_checks", `required checks or workflows failed on its head: ${unmet.failing.join(", ")}`); } else if (unmet.pending.length > 0 || approval) { // It keeps its auto-merge and its place, and joins a batch once its checks pass and it is approved. plan.waiting!.push({ @@ -491,28 +504,63 @@ export async function contexts( } } -/** The checks of `required` that commit `sha` failed, and those still pending or not yet reported. */ +/** + * The checks of `required` that commit `sha` failed, and those still pending or not yet reported. A queue + * build's commit is no PR head, so the workflows a ruleset runs only on PRs are not asked of it. + */ export async function requiredChecks( github: GitHub, repository: Repository, sha: string, required: RequiredCheck[], ): Promise<{ failing: string[]; pending: string[] }> { - return checks( - (await currentChecks(github, repository, sha, await contexts(github, repository, sha))).checks, - required, - ); + return checks((await currentChecks(github, repository, sha, await contexts(github, repository, sha))).checks, { + checks: required, + workflows: [], + }); } /** - * 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. + * The required checks a head failed, and those pending or not yet reported, each from its app, and the + * required workflows any job of which failed, or that nothing reports, or whose run, or a run of the same + * workflow replacing a cancelled one, is still going, since it may still create a failing job; `quest status` + * reads only the jobs. 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(latest: ObjectValue[], required: RequiredCheck[]): { failing: string[]; pending: string[] } { +function checks( + latest: ObjectValue[], + required: Needs, + runs: WorkflowRun[] = [], + all: ObjectValue[] = latest, +): { failing: string[]; pending: string[] } { const failing: string[] = []; const pending: string[] = []; - for (const need of required) { + for (const key of required.workflows) { + const jobs = latest.filter((context) => reported(context).workflow === key); + const suites = new Set( + all + .filter((context) => reported(context).workflow === key && context.checkSuite != null) + .map((context) => Number(object(context.checkSuite).databaseId)), + ); + const replaced = new Set(runs.filter((run) => suites.has(run.check_suite_id)).map((run) => run.workflow_id)); + const failed = jobs.filter((context) => classifyCheck(context).outcome === "failing"); + // A finished job does not finish its run, which may still create jobs, and a run replacing a cancelled one may have none yet. + const going = + jobs.length === 0 || + jobs.some( + (context) => + classifyCheck(context).outcome === "pending" || + (context.checkSuite != null && object(context.checkSuite).status !== "COMPLETED"), + ) || + // A run's REST repository and path name where it runs, not its workflow file, so a replacement is + // matched by workflow id to a run whose check suite reported one of this workflow's jobs. + runs.some((run) => run.status !== "completed" && replaced.has(run.workflow_id)); + if (failed.length > 0) + failing.push(`${key} (${failed.map((context) => classifyCheck(context).check.name).join(", ")})`); + else if (going) pending.push(key); + } + for (const need of required.checks) { let result: "passed" | "failing" | "pending" = "pending"; let reported = false; for (const context of latest) { diff --git a/src/cli/main.ts b/src/cli/main.ts index d7cc9b49..3870da98 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -503,7 +503,7 @@ export function main(argv: string[], out: Output): number | Promise { "The head's first failing required check prints as checks:failed, and every required check passing as checks:passed, " + "each once per head, with the required check's name as the actor and its details URL. " + "Results count as quest status counts them, so neutral and skipped results pass. " + - "checks:passed waits until no check is pending, no GitHub Actions run is unfinished, and every check and workflow the base branch names is present, then names the last required check that passed. " + + "checks:passed waits until no check is pending, no finished job's GitHub Actions run is still going, no run read to replace a cancelled check is unfinished, and every check and workflow the base branch names is present, then names the last required check that passed. " + "Optional failures print separately as check:failed; a base branch requiring no checks requires them all. " + "When auto-merge stays enabled on a head other than the current one, which GitHub allows across a writer's push, " + "auto-merge:moved prints once per head, with the account that enabled it as the actor and a URL naming the head it " + diff --git a/test/github.ts b/test/github.ts index 861cec8b..b495d8cb 100644 --- a/test/github.ts +++ b/test/github.ts @@ -35,6 +35,10 @@ export type Pull = Issue & { commit?: string; /** The check suite of the workflow run that reported it. */ suite?: number; + /** The path of the workflow file in this repository whose run reported it. */ + workflow?: string; + /** Whether that run is still going, so it may create more jobs. */ + running?: boolean; }[]; review: "APPROVED" | "CHANGES_REQUESTED" | "REVIEW_REQUIRED" | null; /** Oldest first; `commit` is the one a review was submitted on, the head by default. */ @@ -158,6 +162,9 @@ export type Repo = { name: string; head_branch: string; head_sha: string; + /** The workflow file it runs, and the repository that holds it. */ + path?: string; + repository?: { full_name: string }; status: string; conclusion: string | null; html_url: string; @@ -246,7 +253,7 @@ function queueTarget(repo: Repo, pull: Pull) { { commit: { oid: headOf(repo, pull), - statusCheckRollup: pull.checks && { contexts: contextPage(pull, null) }, + statusCheckRollup: pull.checks && { contexts: contextPage(repo, pull, null) }, }, }, ], @@ -258,7 +265,7 @@ function queueTarget(repo: Repo, pull: Pull) { export const ACTIONS_APP = 15368; /** One page of a head's check runs and commit statuses, as `statusCheckRollup.contexts` pages them. */ -function contextPage(pull: Pick, after: unknown) { +function contextPage(repo: Repo, pull: Pick, after: unknown) { const start = typeof after === "string" ? Number(after) : 0; const all = (pull.runs ?? []).map((run, index) => run.context @@ -271,7 +278,14 @@ function contextPage(pull: Pick, after: unknown) { status: "COMPLETED", conclusion: run.conclusion, detailsUrl: run.url, - checkSuite: { databaseId: run.suite ?? null, app: { databaseId: run.app ?? ACTIONS_APP } }, + checkSuite: { + databaseId: run.suite ?? null, + status: run.running ? "IN_PROGRESS" : "COMPLETED", + app: { databaseId: run.app ?? ACTIONS_APP }, + workflowRun: run.workflow + ? { file: { path: run.workflow, repositoryName: `${repo.owner}/${repo.name}` } } + : null, + }, }, ); const end = start + 100; @@ -287,7 +301,9 @@ function graphql(repo: Repo, query: string, variables: Record) if (runs) return { data: { - repository: { object: { statusCheckRollup: { contexts: contextPage({ runs }, variables.after) } } }, + repository: { + object: { statusCheckRollup: { contexts: contextPage(repo, { runs }, variables.after) } }, + }, }, }; const pull = repo.pulls.find( @@ -297,7 +313,7 @@ function graphql(repo: Repo, query: string, variables: Record) data: { repository: { object: pull - ? { statusCheckRollup: pull.checks && { contexts: contextPage(pull, variables.after) } } + ? { statusCheckRollup: pull.checks && { contexts: contextPage(repo, pull, variables.after) } } : null, }, }, @@ -476,6 +492,12 @@ export function fakeGitHub(world: World) { grants.delete(token); return new Response(null, { status: 204 }); } + const byId = path.match(/^\/repositories\/(\d+)$/); + if (byId) { + const found = world.repos.find((r) => r.id === Number(byId[1])); + if (!found || !scope?.repositories.has(`${found.owner}/${found.name}`)) return notFound(); + return json({ full_name: `${found.owner}/${found.name}` }); + } const match = path.match(/^\/repos\/([^/]+)\/([^/]+)(\/.*)?$/); const fullName = match && `${match[1]}/${match[2]}`; const tail = match?.[3] ?? ""; diff --git a/test/land.test.ts b/test/land.test.ts index 700074b1..7dccb62f 100644 --- a/test/land.test.ts +++ b/test/land.test.ts @@ -1197,6 +1197,96 @@ describe.skipIf(process.platform === "win32")("quest land", () => { expect(plan.eject.map((ejected) => [ejected.number, ejected.reason])).toEqual([[2, "failed_checks"]]); }); + test("refuses a required workflow from a repository it cannot read", async () => { + const forge = new Forge(); + forge.repo.rules = [ + { + type: "workflows", + ruleset_id: 4, + parameters: { workflows: [{ path: ".github/workflows/ci.yml", repository_id: 999 }] }, + }, + ]; + await expect(resolveQueue(REPOSITORY, { base: "main", method: "squash" }, await forge.deps())).rejects.toThrow( + "requires workflows from repositories the gate cannot read, so no job can satisfy them: repository 999:.github/workflows/ci.yml", + ); + }); + + test("counts every job of a required workflow, and waits on one nothing reports", async () => { + const forge = new Forge(); + const CI = ".github/workflows/ci.yml"; + forge.repo.rules = [ + { type: "workflows", ruleset_id: 4, parameters: { workflows: [{ path: CI, repository_id: 10 }] } }, + ]; + const runs = ( + ...jobs: { name: string; conclusion: string; workflow?: string; running?: boolean; suite?: number }[] + ) => ({ + pull: { + runs: [{ name: "test", conclusion: "SUCCESS", url: "u" }, ...jobs.map((job) => ({ url: "u", ...job }))], + }, + }); + forge.pull("Ran", { "1.txt": "1\n" }, runs({ name: "build", conclusion: "SUCCESS", workflow: CI })); + forge.pull("Never ran", { "2.txt": "2\n" }, runs()); + forge.pull( + "Other workflow", + { "3.txt": "3\n" }, + runs({ name: "build", conclusion: "SUCCESS", workflow: ".github/workflows/other.yml" }), + ); + forge.pull( + "Failed", + { "4.txt": "4\n" }, + runs( + { name: "build", conclusion: "SUCCESS", workflow: CI }, + { name: "lint", conclusion: "FAILURE", workflow: CI }, + ), + ); + // A passed job whose run is still going may yet be followed by a failing one. + forge.pull( + "Going", + { "5.txt": "5\n" }, + runs({ name: "build", conclusion: "SUCCESS", workflow: CI, running: true }), + ); + // A newer run cancelled this one and has yet to report a job; the cancelled job drops, the passed one stays. + const replaced = forge.pull( + "Replaced", + { "6.txt": "6\n" }, + runs( + { name: "build", conclusion: "SUCCESS", workflow: CI }, + { name: "lint", conclusion: "CANCELLED", workflow: CI, suite: 10 }, + ), + ); + for (const [id, status, started] of [ + [1, "completed", "2026-11-12T10:00:00Z"], + [2, "queued", "2026-11-12T11:00:00Z"], + ] as const) + (forge.repo.actionRuns ??= []).push({ + id, + workflow_id: 7, + check_suite_id: id * 10, + run_started_at: started, + name: "CI", + head_branch: replaced.branch ?? "", + head_sha: replaced.head!, + // A required workflow from another repository runs here, under its own path form. + path: `org/.github/${CI}@refs/heads/main`, + repository: { full_name: "upstream/project" }, + status, + conclusion: status === "queued" ? null : "cancelled", + html_url: "u", + jobs: [], + }); + const plan = await resolveQueue(REPOSITORY, { base: "main", method: "squash" }, await forge.deps()); + expect(plan.queue.map((entry) => entry.number)).toEqual([1]); + expect(plan.waiting?.map(({ number, pending }) => [number, pending])).toEqual([ + [2, [`upstream/project:${CI}`]], + [3, [`upstream/project:${CI}`]], + [5, [`upstream/project:${CI}`]], + [6, [`upstream/project:${CI}`]], + ]); + expect(plan.eject.map((ejected) => [ejected.number, ejected.detail])).toEqual([ + [4, `required checks or workflows failed on its head: upstream/project:${CI} (lint)`], + ]); + }); + test("counts a required check only from the app its rule names", async () => { const forge = new Forge(); forge.repo.rules = [ @@ -1262,9 +1352,9 @@ describe.skipIf(process.platform === "win32")("quest land", () => { ).toHaveLength(1); forge.repo.rules.push( { - type: "workflows", + type: "update", ruleset_id: 4, - parameters: { workflows: [{ path: ".github/workflows/ci.yml", repository_id: 10 }] }, + parameters: null, }, { type: "pull_request", @@ -1274,7 +1364,7 @@ describe.skipIf(process.platform === "win32")("quest land", () => { ); const refused = resolveQueue(REPOSITORY, { base: "main", method: "squash" }, await forge.deps()); await expect(refused).rejects.toThrow( - "workflows from ruleset 4; pull_request (allowed_merge_methods without squash) from ruleset 5", + "update from ruleset 4; pull_request (allowed_merge_methods without squash) from ruleset 5", ); await expect(refused).rejects.toThrow(`It upholds only ${Object.keys(UPHELD).join(", ")}`); expect(forge.repo.pulls[0].landing).toBe(true); @@ -1734,7 +1824,7 @@ describe.skipIf(process.platform === "win32")("host daemon", () => { expect(await round(picked + STALLED_AFTER - 1)).toEqual([]); expect(forge.comment(1)).toBeUndefined(); expect(await round(picked + STALLED_AFTER)).toEqual(["stalled 1 test"]); - expect(forge.comment(1)).toContain("the required check test"); + expect(forge.comment(1)).toContain("the required check or workflow test"); expect(forge.comment(1)).toContain("2 hours after it was picked"); // It stays queued, and is told only once for this pick. expect(await round(picked + 2 * STALLED_AFTER)).toEqual([]); @@ -4733,10 +4823,12 @@ describe.skipIf(process.platform === "win32")("the watcher's own queue", () => { const summary = () => String(forge.repo.checkRuns!.filter((item) => item.name === QUEUE_CHECK).at(-1)!.output.summary); await pass(); - expect(summary()).toContain("the required check `test` to pass, an approving review, which main requires"); + expect(summary()).toContain( + "the required check or workflow `test` to pass, an approving review, which main requires", + ); pick.review = "APPROVED"; await pass(); - expect(summary()).toContain("the required check `test` to pass."); + expect(summary()).toContain("the required check or workflow `test` to pass."); expect(summary()).not.toContain("approving review"); });