- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/ci.ts?version=310&format=json
210 linesFirst import 73% · runsteps 24% · groups 2% · onboard 1% · review 1%Last change runsteps in version 301, 5 versions changed it
You're reading version 310. The newest is 318, and no version since then changed this file.
1// What the project's CI says (#39): the server runs abso.json's setup, build
2// and check on each new version of shared. status and log show it; abso ci
3// shows it whole, and one version's job with the end of its output.
5import { ApiError, type CiJob, type CiMark, type CiStatus } from "./api.ts";
6import { bool, type Parsed, str, UsageError } from "./args.ts";
7import { type Env, type Output, open, plural } from "./commands.ts";
8import { when } from "./history.ts";
10/** Seconds since an ISO time, as "35 s" or "4 min". */
11function ago(iso: string, now: number): string {
12 const s = Math.max(0, Math.round((now - Date.parse(iso)) / 1000));
13 return s < 120 ? `${s} s` : `${Math.round(s / 60)} min`;
14}
16/** An ISO time as HH:MM UTC, with the date when it isn't today. */
17function clock(iso: string, now: number): string {
18 const t = new Date(iso).toISOString();
19 return t.slice(0, 10) === new Date(now).toISOString().slice(0, 10)
20 ? `${t.slice(11, 16)} UTC`
21 : `${t.slice(0, 16).replace("T", " ")} UTC`;
22}
24/** The lines status shows about CI: is shared green or red, and where the preview is. Nothing from a server without CI. */
25export function ciText(ci: CiStatus | undefined, shared: number, now = Date.now()): string[] {
26 if (ci === undefined) return [];
27 const lines: string[] = [];
28 const running =
29 ci.running === null
30 ? ""
31 : ` Version ${ci.running.version} is being checked (${ago(ci.running.since, now)} so far${ci.running.bisect ? ", a bisect step" : ""}).`;
32 const red = ci.red;
33 if (red !== null) {
34 if (red.bisecting) {
35 lines.push(
36 `Shared is red: version ${red.version} fails its check, and CI is bisecting for the version that broke it${red.lastGood === null ? "" : ` (version ${red.lastGood} passed)`}.${running}`,
37 );
38 } else if (red.agent !== null) {
39 lines.push(
40 `Shared is red since version ${red.version}, by ${red.agent}${red.message === null ? "" : ` ("${red.message}")`}${red.lastGood === null ? "" : `; version ${red.lastGood} was the last to pass`}. abso ci ${red.version} shows why.${running}`,
41 );
42 } else {
43 lines.push(
44 `Shared is red: version ${red.version} fails its check${red.lastGood === null ? ", and no version has passed yet" : `; version ${red.lastGood} was the last to pass`}. abso ci ${red.version} shows why.${running}`,
45 );
46 }
47 } else if (ci.checked !== null) {
48 const v = ci.checked.version;
49 // Newer versions with no job running: skipped (said below) or not reached yet.
50 const newer =
51 shared > v ? (ci.running !== null ? running : ci.skipped ? "" : ` Versions after ${v} aren't checked yet.`) : "";
52 if (ci.checked.result === "pass") lines.push(`Shared is green: version ${v} passed its check.${newer}`);
53 else if (ci.checked.result === "fail") lines.push(`Version ${v} failed its check. abso ci ${v} shows why.${newer}`);
54 else
55 lines.push(
56 `CI couldn't check version ${v} (the box failed twice, or the pipeline file has nothing to run).${newer}`,
57 );
58 } else {
59 lines.push(
60 ci.running === null
61 ? "Not checked yet: CI has not finished a job on this project."
62 : `Not checked yet.${running}`,
63 );
64 }
65 // Not running, and not queued either (#39, GAME - CEO: "aren't checked yet" read as queued while CI was off).
66 if (ci.skipped) {
67 const s = ci.skipped;
68 lines.push(
69 `CI didn't run ${s.from < s.version ? `versions ${s.from}-${s.version}` : `version ${s.version}`}: ${s.reason}. ${s.from < s.version ? "They" : "It"} won't be checked unless a later version's job covers ${s.from < s.version ? "them" : "it"}.`,
70 );
71 }
72 if (ci.waiting) lines.push(`CI is waiting: ${ci.waiting.reason}; next try ${clock(ci.waiting.until, now)}.`);
73 if (ci.latest?.url) {
74 const behind =
75 ci.behind === null || ci.behind === 0 ? "shared itself" : `${plural(ci.behind, "version")} behind shared`;
76 lines.push(
77 `Preview of the newest passing build (version ${ci.latest.version}, ${behind}), no token needed: ${ci.latest.url}`,
78 );
79 }
80 return lines;
81}
83/** A stored pipeline run as runRuns lists it: what the CI lines need of it. */
84interface StoredRun {
85 readonly id: number;
86 readonly version: number;
87 readonly state: string;
88 readonly file: string;
89}
91/** The stored pipeline runs, newest first, from the server's `pipelines` read; empty for anything else. */
92export function storedRuns(raw: unknown): StoredRun[] {
93 const runs = (raw as { runs?: unknown } | null)?.runs;
94 if (!Array.isArray(runs)) return [];
95 return runs.filter(
96 (r): r is StoredRun =>
97 typeof r === "object" &&
98 r !== null &&
99 Number.isInteger((r as StoredRun).id) &&
100 Number.isInteger((r as StoredRun).version) &&
101 typeof (r as StoredRun).state === "string" &&
102 typeof (r as StoredRun).file === "string",
103 );
104}
106/** One stored run in a line, with where its jobs are. */
107export function runLine(run: StoredRun): string {
108 const how = run.state === "planned" ? "was planned, not run," : run.state === "running" ? "is running" : run.state;
109 return `Pipeline run ${run.id} (${run.file}) ${how} on version ${run.version}. abso pipeline runs ${run.id} shows its jobs.`;
110}
112/**
113 * Whether the built-in check (abso.json's setup, build and check) had nothing of its own to run. With stored runs, that is
114 * about the built-in check, not the project's CI, so it isn't said as the project's (abso 719).
115 */
116export function builtInHasNothing(ci: CiStatus): boolean {
117 return ci.red === null && ci.skipped?.reason.startsWith("nothing to run") === true;
118}
120/** A shared version's mark in abso log; "not checked" says nothing, since most versions in a busy hour are covered by a newer one's job. */
121export function ciMarkText(mark: CiMark | undefined): string {
122 switch (mark) {
123 case "pass":
124 return ", check passed";
125 case "fail":
126 return ", check FAILED";
127 case "running":
128 return ", being checked";
129 case "waiting":
130 return ", waiting for CI";
131 default:
132 return "";
133 }
134}
136function jobText(job: CiJob): string[] {
137 if (job.result === "running") return [`Version ${job.version} is being checked now.`];
138 if (job.result === "waiting") return [`Version ${job.version} isn't checked yet: CI hasn't reached it.`];
139 if (job.result === "not checked")
140 return [
141 `Version ${job.version} had no job of its own: a newer version's job covered it, or none ran. abso bisect checks it in your box.`,
142 ];
143 const covered =
144 job.from !== undefined && job.from < job.version ? ` (standing in for versions ${job.from}-${job.version})` : "";
145 const took = [
146 job.seconds === undefined ? "" : `${job.seconds} s`,
147 job.cold ? "cold box" : "",
148 job.bisect ? "a bisect step" : "",
149 ]
150 .filter((w) => w !== "")
151 .join(", ");
152 const head =
153 job.result === "pass"
154 ? `Version ${job.version} passed${covered}${took === "" ? "" : ` (${took})`}${job.built ? "; its build is kept for the preview" : ""}.`
155 : job.result === "fail"
156 ? `Version ${job.version} failed at ${job.step ?? "a step"}${job.exit === undefined || job.exit === null ? "" : ` (exit ${job.exit})`}${covered}${took === "" ? "" : `, ${took}`}.`
157 : `Version ${job.version} wasn't checked${job.step === undefined ? "" : ` (${job.step})`}: the box failed twice, or the pipeline file has nothing to run.`;
158 const output = job.output?.trimEnd() ?? "";
159 return [
160 head,
161 ...(output === "" ? [] : [`The end of its output${job.at ? `, ${when(Date.parse(job.at))}` : ""}:`, output]),
162 ];
163}
165export async function runCi(args: Parsed, env: Env): Promise<Output> {
166 if (args.positionals.length > 1)
167 throw new UsageError("ci takes at most one version: abso ci [<version>] | abso ci --revoke");
168 const { api } = open(env);
169 const typed = args.positionals[0] ?? str(args, "version");
170 try {
171 if (bool(args, "revoke")) {
172 if (typed !== undefined) throw new UsageError("--revoke takes no version");
173 const ci = await api.ciRevoke();
174 return {
175 text: [
176 `Every preview link handed out so far is ended.${ci.latest?.url ? ` The new one: ${ci.latest.url}` : " A new one comes with the next passing build."}`,
177 ].join("\n"),
178 json: { ...ci },
179 };
180 }
181 if (typed !== undefined) {
182 if (!/^\d+$/.test(typed)) throw new UsageError(`a version is a number (got ${JSON.stringify(typed)})`);
183 const [job, raw] = await Promise.all([api.ci(Number(typed)), api.pipelineRuns().catch(() => null)]);
184 const run = storedRuns(raw).find((r) => r.version === job.version);
185 // A version the built-in check never ran, with a pipeline run of its own: the run is its CI (abso 719).
186 const text = run !== undefined && job.result === "none" ? [runLine(run)] : jobText(job);
187 return { text: text.join("\n"), json: { ...job } };
188 }
189 const [ci, status, raw] = await Promise.all([api.ci(), api.status(), api.pipelineRuns().catch(() => null)]);
190 const newest = storedRuns(raw)[0];
191 const lines =
192 newest === undefined
193 ? ciText(ci, status.version)
194 : builtInHasNothing(ci)
195 ? [runLine(newest)]
196 : [...ciText(ci, status.version), runLine(newest)];
197 return {
198 text: [
199 `shared is at version ${status.version}.`,
200 ...lines,
201 "abso ci <version> shows one version's job and the end of its output.",
202 ].join("\n"),
203 json: { ...ci, version: status.version },
204 };
205 } catch (e) {
206 if (e instanceof ApiError && e.status === 404)
207 throw new ApiError("this server has no CI yet (#39); abso serve still builds a preview of a version", 404);
208 throw e;
209 }
210}