- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/ci.ts?draft=login-timeout&format=json
161 linesFirst import 98% · review 2%0 versions changed it
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 lines.push(`CI couldn't check version ${v} (the box failed twice, or abso.json has nothing to run).${newer}`);
55 } else {
56 lines.push(
57 ci.running === null
58 ? "Not checked yet: CI has not finished a job on this project."
59 : `Not checked yet.${running}`,
60 );
61 }
62 // Not running, and not queued either (#39, GAME - CEO: "aren't checked yet" read as queued while CI was off).
63 if (ci.skipped) {
64 const s = ci.skipped;
65 lines.push(
66 `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"}.`,
67 );
68 }
69 if (ci.waiting) lines.push(`CI is waiting: ${ci.waiting.reason}; next try ${clock(ci.waiting.until, now)}.`);
70 if (ci.latest?.url) {
71 const behind =
72 ci.behind === null || ci.behind === 0 ? "shared itself" : `${plural(ci.behind, "version")} behind shared`;
73 lines.push(
74 `Preview of the newest passing build (version ${ci.latest.version}, ${behind}), no token needed: ${ci.latest.url}`,
75 );
76 }
77 return lines;
78}
80/** 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. */
81export function ciMarkText(mark: CiMark | undefined): string {
82 switch (mark) {
83 case "pass":
84 return ", check passed";
85 case "fail":
86 return ", check FAILED";
87 case "running":
88 return ", being checked";
89 case "waiting":
90 return ", waiting for CI";
91 default:
92 return "";
93 }
94}
96function jobText(job: CiJob): string[] {
97 if (job.result === "running") return [`Version ${job.version} is being checked now.`];
98 if (job.result === "waiting") return [`Version ${job.version} isn't checked yet: CI hasn't reached it.`];
99 if (job.result === "not checked")
100 return [
101 `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.`,
102 ];
103 const covered =
104 job.from !== undefined && job.from < job.version ? ` (standing in for versions ${job.from}-${job.version})` : "";
105 const took = [
106 job.seconds === undefined ? "" : `${job.seconds} s`,
107 job.cold ? "cold box" : "",
108 job.bisect ? "a bisect step" : "",
109 ]
110 .filter((w) => w !== "")
111 .join(", ");
112 const head =
113 job.result === "pass"
114 ? `Version ${job.version} passed${covered}${took === "" ? "" : ` (${took})`}${job.built ? "; its build is kept for the preview" : ""}.`
115 : job.result === "fail"
116 ? `Version ${job.version} failed at ${job.step ?? "a step"}${job.exit === undefined || job.exit === null ? "" : ` (exit ${job.exit})`}${covered}${took === "" ? "" : `, ${took}`}.`
117 : `Version ${job.version} wasn't checked${job.step === undefined ? "" : ` (${job.step})`}: the box failed twice, or abso.json has nothing to run.`;
118 const output = job.output?.trimEnd() ?? "";
119 return [
120 head,
121 ...(output === "" ? [] : [`The end of its output${job.at ? `, ${when(Date.parse(job.at))}` : ""}:`, output]),
122 ];
123}
125export async function runCi(args: Parsed, env: Env): Promise<Output> {
126 if (args.positionals.length > 1)
127 throw new UsageError("ci takes at most one version: abso ci [<version>] | abso ci --revoke");
128 const { api } = open(env);
129 const typed = args.positionals[0] ?? str(args, "version");
130 try {
131 if (bool(args, "revoke")) {
132 if (typed !== undefined) throw new UsageError("--revoke takes no version");
133 const ci = await api.ciRevoke();
134 return {
135 text: [
136 `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."}`,
137 ].join("\n"),
138 json: { ...ci },
139 };
140 }
141 if (typed !== undefined) {
142 if (!/^\d+$/.test(typed)) throw new UsageError(`a version is a number (got ${JSON.stringify(typed)})`);
143 const job = await api.ci(Number(typed));
144 return { text: jobText(job).join("\n"), json: { ...job } };
145 }
146 const [ci, status] = await Promise.all([api.ci(), api.status()]);
147 const lines = ciText(ci, status.version);
148 return {
149 text: [
150 `shared is at version ${status.version}.`,
151 ...lines,
152 "abso ci <version> shows one version's job and the end of its output.",
153 ].join("\n"),
154 json: { ...ci, version: status.version },
155 };
156 } catch (e) {
157 if (e instanceof ApiError && e.status === 404)
158 throw new ApiError("this server has no CI yet (#39); abso serve still builds a preview of a version", 404);
159 throw e;
160 }
161}