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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions quest/m0/gate-required-workflows.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/cli/land-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/land-serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>${excerpt(name)}</code>`),
...waiting.pending.map((name) => `- the required check or workflow <code>${excerpt(name)}</code>`),
...(waiting.approval ? [`- an approving review, which ${plan.base.ref} requires`] : []),
].join("\n");
await github.request(`/repos/${plan.repository}/issues/${waiting.number}/comments`, {
Expand Down
80 changes: 64 additions & 16 deletions src/cli/land.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand All @@ -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 }
}`;

Expand Down Expand Up @@ -215,6 +216,7 @@ export const UPHELD: Record<string, string> = {
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",
};

Expand Down Expand Up @@ -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);
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ export function main(argv: string[], out: Output): number | Promise<number> {
"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 " +
Expand Down
32 changes: 27 additions & 5 deletions test/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) },
},
},
],
Expand All @@ -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<Pull, "runs">, after: unknown) {
function contextPage(repo: Repo, pull: Pick<Pull, "runs">, after: unknown) {
const start = typeof after === "string" ? Number(after) : 0;
const all = (pull.runs ?? []).map((run, index) =>
run.context
Expand All @@ -271,7 +278,14 @@ function contextPage(pull: Pick<Pull, "runs">, 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;
Expand All @@ -287,7 +301,9 @@ function graphql(repo: Repo, query: string, variables: Record<string, unknown>)
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(
Expand All @@ -297,7 +313,7 @@ function graphql(repo: Repo, query: string, variables: Record<string, unknown>)
data: {
repository: {
object: pull
? { statusCheckRollup: pull.checks && { contexts: contextPage(pull, variables.after) } }
? { statusCheckRollup: pull.checks && { contexts: contextPage(repo, pull, variables.after) } }
: null,
},
},
Expand Down Expand Up @@ -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] ?? "";
Expand Down
Loading
Loading