71 lines
2.9 KiB
TypeScript
71 lines
2.9 KiB
TypeScript
// PEP-278: accepted stories wait for the version that carries them (core/rollout.ts). Once a minute
|
|
// the watchdog learns each waiting story's merge commit from Gitea and lifts the mark when that
|
|
// commit runs — the story is «Готово» for real, one line goes to its chat thread.
|
|
import {
|
|
clearRolloutWait,
|
|
giteaForBot,
|
|
listRolloutWaits,
|
|
logEvent,
|
|
notify,
|
|
rolledOutWhere,
|
|
setRolloutWait,
|
|
taskPrNumber,
|
|
type GiteaClient,
|
|
type RolloutWait,
|
|
} from "@peppa/core";
|
|
import { projects, tasks, type Db } from "@peppa/db";
|
|
import { eq } from "drizzle-orm";
|
|
|
|
/** The commit that brought the story into the main branch; null — not known (yet), "none" — nothing was merged. */
|
|
async function mergeCommit(gitea: GiteaClient, project: { giteaOwner: string; giteaRepo: string }, n: number): Promise<string | "none"> {
|
|
const pull = await gitea.getPullRequest(project.giteaOwner, project.giteaRepo, n);
|
|
if (pull.merged) return pull.merge_commit_sha || pull.head.sha;
|
|
// PEP-93: closed without a merge because all its commits were in the base already.
|
|
if (pull.state === "closed") return pull.head.sha;
|
|
return "none";
|
|
}
|
|
|
|
export async function rolloutTick(db: Db, o: { gitea?: GiteaClient; root?: string; log?: (m: string) => void } = {}): Promise<number> {
|
|
if (process.env.PEPPA_ROLLOUT === "off") return 0;
|
|
const waits = await listRolloutWaits(db);
|
|
let done = 0;
|
|
for (const [taskId, w0] of waits) {
|
|
let w: RolloutWait = w0;
|
|
const [task] = await db.select().from(tasks).where(eq(tasks.id, taskId));
|
|
// The story left «Готово» (or is archived, or gone): nothing to wait for.
|
|
if (!task || task.status !== "done" || task.archivedAt) {
|
|
await clearRolloutWait(db, taskId);
|
|
continue;
|
|
}
|
|
const [project] = await db.select().from(projects).where(eq(projects.id, task.projectId));
|
|
if (!project) continue;
|
|
if (!w.sha) {
|
|
const n = taskPrNumber(project, task.prUrl);
|
|
let sha: string | "none" | null = n ? null : "none";
|
|
if (n) {
|
|
try {
|
|
sha = await mergeCommit(o.gitea ?? giteaForBot(), project, n);
|
|
} catch {
|
|
continue; // Gitea is down — the next minute
|
|
}
|
|
}
|
|
if (sha === "none") {
|
|
// Moved to «Готово» by hand with an unmerged result: there is no commit to wait for.
|
|
await clearRolloutWait(db, taskId);
|
|
await notify(db, { type: "task_updated", taskId });
|
|
continue;
|
|
}
|
|
w = { ...w, sha };
|
|
await setRolloutWait(db, taskId, w);
|
|
}
|
|
const where = await rolledOutWhere(db, project, w, { root: o.root });
|
|
if (!where) continue;
|
|
await clearRolloutWait(db, taskId);
|
|
await logEvent(db, { projectId: project.id, taskId, type: "task_rolled_out", payload: { env: where, sha: w.sha } });
|
|
await notify(db, { type: "task_updated", taskId });
|
|
o.log?.(`rollout: ${w.key} runs (${where}, ${w.sha?.slice(0, 7)})`);
|
|
done++;
|
|
}
|
|
return done;
|
|
}
|