peppa/packages/runner/src/platform-build.ts
Асель 82c003e209 PEP-60: platform Telegram channel — check, releases, outages/restarts, platform digest
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 19:21:04 +05:00

58 lines
2.8 KiB
TypeScript

// New builds of Peppa (PEP-60). The watchdog looks at HEAD of Peppa's checkout about once a
// minute; a commit that differs from the last announced one and belongs to `main` is a
// release: posted to the platform channel with the stories from the commit messages. Works
// now (dev from the working copy: a story branch isn't announced, returning to an updated
// main is) and in prod (PEP-18: ~/peppa/current is a worktree of a `main` commit).
import { execFileSync } from "node:child_process";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { announcePlatformBuild, getPlatformBuild, headCommit, peppaVersion, platformProjectKey } from "@peppa/core";
import type { Db } from "@peppa/db";
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../../..");
const git = (root: string, args: string[]) => execFileSync("git", args, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] });
/** True when `commit` is reachable from the branch (default `main`). */
export function onMainBranch(root: string, commit: string, branch = process.env.PEPPA_MAIN_BRANCH || "main"): boolean {
try {
git(root, ["merge-base", "--is-ancestor", commit, branch]);
return true;
} catch {
return false;
}
}
/** Platform stories mentioned in the commits `prev..commit` (all of HEAD's history is too much: none then). */
export function storiesBetween(root: string, prev: string | null, commit: string, key = platformProjectKey()): string[] {
if (!prev) return [];
let log = "";
try {
log = git(root, ["log", "--format=%s%n%b", `${prev}..${commit}`]);
} catch {
return [];
}
const re = new RegExp(`\\b${key}-(\\d+)\\b`, "gi");
const nums = new Set<number>();
for (const m of log.matchAll(re)) nums.add(Number(m[1]));
return [...nums].sort((a, b) => a - b).map((n) => `${key}-${n}`);
}
let lastCheck = 0;
/** Watchdog: announces a new build of Peppa once (first run only remembers it). */
export async function platformBuildTick(db: Db, o: { root?: string; now?: Date; force?: boolean; log?: (m: string) => void } = {}): Promise<boolean> {
const nowMs = (o.now ?? new Date()).getTime();
if (!o.force && Math.abs(nowMs - lastCheck) < 60_000) return false;
lastCheck = nowMs;
const root = o.root ?? process.env.PEPPA_ROOT ?? ROOT;
const commit = headCommit(root);
if (!commit) return false;
const prev = await getPlatformBuild(db);
if (prev?.commit === commit || !onMainBranch(root, commit)) return false;
const tasks = storiesBetween(root, prev?.commit ?? null, commit);
const posted = await announcePlatformBuild(db, { version: peppaVersion(root), commit, tasks }, { now: o.now });
o.log?.(`platform build ${commit.slice(0, 7)}${tasks.length ? ` (${tasks.join(", ")})` : ""}${posted ? " → channel" : ""}`);
return posted;
}