Files
mesh-catalog/modules/mesh-catalog/heard.ts
T

93 lines
4.7 KiB
TypeScript

// What the catalogue makes of a build outcome it hears (novox/hq issue 712).
//
// Kept apart from index.ts, which subscribes at its top level, so the decision can be tested without a bus.
import type { Made } from "./store.js";
/** What the build seat says when it has built something, and what the control plane replays. */
export interface Built {
module?: string;
commit?: string;
repository?: string;
path?: string;
ref?: string;
manifest?: unknown;
/** What this build published, each pinned as anything else would name it. */
made?: Made[];
/**
* Every artifact this was built on top of, so the edge is derived rather than declared.
*
* References, not module names: the builder sees a pinned image and cannot see which module
* produced it. Turning that into an edge between module-versions is this module's job.
*/
against?: string[];
/**
* This is history, not news — the mesh re-announcing a build this catalogue was not there for.
*
* Registered exactly as any other, and announced as nothing. A module that moved months ago is
* not something anything should act on now: emitting `upgraded` would have the control plane
* decide about a rollout, and `rebuild-needed` would ask for builds of things that are already
* current.
*/
replay?: boolean;
/** Why the build failed; a failed build made no module-version. */
failed?: string;
/** A dry run is looked at, never taken in (novox/hq issue 240). */
"dry-run"?: boolean;
/** A pull request's merge check or a branch tried before one (to-be 45 §9, issue 686): nothing merged. */
"check-outcome"?: unknown;
check?: unknown;
/** The repository's default branch as the build seat read the forge at the build (ADR 0238); empty from a seat older than the rule. */
trunk?: string;
/** Whether the commit is on `trunk`. */
"on-trunk"?: boolean;
/** The branches the commit is on. */
branches?: string[];
}
/** The events a build outcome reaches this module on: the build seat's, as it happens, and the control plane's replay. */
export const BUILD_EVENTS = [
// The build seat node-build-agent and the retired mesh-build-machine went with build-agent (novox/hq ADR 0524 §5,
// ADR 0525 §10): no build seat says `built` since R4g, so what the mesh holds is asked when this module starts.
// What the mesh already held, when this module asks what it missed (novox/hq ADR 0134).
"mesh-controller.built-before",
] as const;
/**
* Whether a build outcome is a module-version to place in the graph, and why not when it is not.
*
* It mirrors two of the rules the controller keeps before it registers a build (mesh-controller
* cmd/mesh-controller/main.go `builds.Built`, build.go `publishable`): a merge check, a try and a dry run are
* never published, and **only a commit on its repository's trunk is** (ADR 0238) — a branch built through
* the `build` verb is checked, never a module's version. Where the seat names no trunk (one older than the
* rule) it is placed, as the controller lets it through. A failure made nothing and is passed over too.
*
* Not mirrored, because the catalogue cannot see what they read: the branch a module follows when it is not
* the default one (the controller's `follows`; such a module's builds off the default branch are passed over
* here and reach the graph by the replay), ADR 0266's rule that a module registers only from the repository
* it is built from, a superseded request, a build in the scratch namespace, one that failed its gate, and a
* build whose source is unchanged. Each needs the controller's records. This is safe for what the mesh
* sends, which is chosen from the controller's own module table and never from this graph; that the graph
* can still hold a build the controller refused under those rules is a follow-up (hq issue 712).
*/
export function placeable(body: Built):
| { placed: "yes"; module: string; commit: string }
| { placed: "no"; why: string; fault: boolean } {
if (body["check-outcome"] != null || body.check != null) {
return { placed: "no", why: "a merge check or a try: nothing of it merged", fault: false };
}
if (body["dry-run"]) {
return { placed: "no", why: "a dry run is looked at, never taken in", fault: false };
}
if (body.failed) {
return { placed: "no", why: "a failed build made no module-version", fault: false };
}
if (body.trunk && !body["on-trunk"] && !(body.branches ?? []).includes(body.trunk)) {
return { placed: "no", why: `the commit is not on its repository's trunk ${body.trunk}: checked, never published (ADR 0238)`, fault: false };
}
if (!body.module || !body.commit) {
return { placed: "no", why: "it named no module or no commit", fault: true };
}
return { placed: "yes", module: body.module, commit: body.commit };
}