diff --git a/src/runner/autopilot.ts b/src/runner/autopilot.ts index a442c674..09512043 100644 --- a/src/runner/autopilot.ts +++ b/src/runner/autopilot.ts @@ -48,6 +48,8 @@ export const BACKOFF_MS = 60 * 60_000; export const BACKOFF_AFTER = 3; /** Checks younger than this count as current for the review that follows. */ export const CHECKS_FRESH_MS = 30 * 60_000; +/** How often the CI and mergeable state of a repo with PRs waiting for CI is read again. */ +export const WAITING_CI_RECHECK_MS = 10 * 60_000; export const AUTOPILOT_USER = "pipeline"; export const KEY_ENABLED = "pipeline_enabled"; @@ -228,12 +230,22 @@ export const LANES: Lane[] = [ }, { name: "merge", - steps: ["merge"], + steps: ["checks", "merge"], perRepo: true, detect: (db, { run }) => { - const due = buildQueue(db, { run }).filter((e) => eligible(e) && mergeApprovalOf(db, e.repo) && !refusedAsBefore(db, e)).map((e) => e.repo); + const queue = buildQueue(db, { run }).filter((e) => mergeApprovalOf(db, e.repo)); + const due = queue.filter((e) => eligible(e) && !refusedAsBefore(db, e)).map((e) => e.repo); const repos = [...new Set([...due, ...batchesDue(db)])].sort(); - return repos.map((repo) => ({ step: "merge", params: { repo } })); + // No other stage refreshes a PR that waits only for CI or for GitHub to compute its mergeable state. + const waiting = [...new Set(queue.filter((e) => e.state === "waiting-ci").map((e) => e.repo))].filter((repo) => { + if (repos.includes(repo)) return false; + const checks = lastJobEnd(db, "checks", repo); + return !(checks?.state === "done" && Date.now() - Date.parse(checks.endedAt) < WAITING_CI_RECHECK_MS); + }).sort(); + return [ + ...repos.map((repo) => ({ step: "merge", params: { repo } })), + ...waiting.map((repo) => ({ step: "checks", params: { repo }, gate: { step: "merge", params: { repo } } })), + ]; }, }, { diff --git a/test/pipeline-gaps.test.ts b/test/pipeline-gaps.test.ts index 9e1d6569..a92b51c8 100644 --- a/test/pipeline-gaps.test.ts +++ b/test/pipeline-gaps.test.ts @@ -11,6 +11,7 @@ import { applyUsedAnswers } from "../src/decisions-used.ts"; import { factsOf as decisionFacts, getDecision } from "../src/decisions.ts"; import { planLabels } from "../src/labels/plan.ts"; import { askMergeDecisions } from "../src/merge/index.ts"; +import { setMergeApproval } from "../src/merge/approval.ts"; import { buildQueue, eligible } from "../src/merge/queue.ts"; import { ActionError, actionsOf, fixAgain, reviewAgain, verifyAgain } from "../src/operator-actions.ts"; import { classifyListOf, factsOf, nextStepOf } from "../src/rank/next.ts"; @@ -118,6 +119,23 @@ describe("Merge? asked without a merge approval", () => { assert.ok(!e.outsideRule.some((r) => r.includes("no CI"))); }); + test("the merge lane refreshes checks for a repo whose PRs wait for CI or the mergeable state", () => { + const db = fresh(); + add(db, { n: 1, verify: "passed" }); + db.prepare("UPDATE pr_checks SET mergeable = 'UNKNOWN'").run(); + assert.equal(buildQueue(db, { run: "r1" })[0]!.state, "waiting-ci"); + const lane = LANES.find((l) => l.name === "merge")!; + const ctx = { run: "r1", jevRun: "r1", tracked: ["app"] }; + assert.deepEqual(lane.detect(db, ctx), [], "no merge approval"); + setMergeApproval(db, "op", "app", true); + assert.deepEqual(lane.detect(db, ctx), [{ step: "checks", params: { repo: "app" }, gate: { step: "merge", params: { repo: "app" } } }]); + const at = new Date().toISOString(); + db.prepare("INSERT INTO jobs (step, params, repo, requested_by, requested_at, state, started_at, ended_at, items) VALUES ('checks', '{}', 'app', 'pipeline', ?, 'done', ?, ?, 1)").run(at, at, at); + assert.deepEqual(lane.detect(db, ctx), [], "checks just ran"); + db.prepare("UPDATE pr_checks SET mergeable = 'MERGEABLE'").run(); + assert.deepEqual(lane.detect(db, ctx), [{ step: "merge", params: { repo: "app" } }]); + }); + test("a PR held by an open Duplicate? decision is held-duplicate and not eligible", () => { const db = fresh(); add(db, { n: 1, verify: "passed" });