Skip to content
308 linesCodeBlameRaw
1/**
2 * Pieces of the Actions pages: how a run, job or step stands, how long it
3 * took, and a job's log as GitHub shows one, with its groups folded and
4 * its errors and warnings marked.
5 */
6import { AlertTriangle, Ban, Check, ChevronRight, CircleDashed, CircleSlash, Clock, Hourglass, Info, Loader2, ShieldAlert, X } from "lucide-react";
7import { useEffect, useMemo, useRef, useState } from "react";
8import { Link } from "react-router";
9
10import type { CommitStatus, Conclusion, LogChunk, WorkflowNote } from "@g1t/contracts";
11
12import { Card } from "./ui/card";
13import { Hint } from "./ui/hint";
14import { type Line, blocks, highlight, searchLog } from "../lib/log-lines";
15
16/**
17 * Where a run, job or step stands. `of` says which: a run's `pending` waits
18 * for its concurrency group and its `waiting` for its environments' rules,
19 * while a job's `pending` is held at its environment and its `waiting`
20 * waits for the jobs it needs. `environment` is the job's.
21 */
22type Standing = { status: string; conclusion: Conclusion | null; of?: "run" | "job"; environment?: string | null };
23
24/** Held until a person (or a wait timer) lets it through. */
25function heldWord({ status, of = "run", environment }: Standing): string | null {
26 if (of === "run" && status === "action_required") return "Approval required";
27 if (of === "run" && status === "waiting") return "Waiting for review";
28 if (of === "job" && status === "pending") return environment ? `Waiting to deploy to ${environment}` : "Waiting at its environment";
29 return null;
30}
31
32/** One icon for where a run, job or step stands. */
33export function StatusIcon({ status, conclusion, of = "run", environment, size = 16 }: Standing & { size?: number }) {
34 if (status === "in_progress") return <Loader2 size={size} className="shrink-0 animate-spin text-warn" aria-label="Running" />;
35 const held = heldWord({ status, conclusion, of, environment });
36 if (held)
37 return status === "action_required" ? (
38 <ShieldAlert size={size} className="shrink-0 text-warn" aria-label={held} />
39 ) : (
40 <Hourglass size={size} className="shrink-0 text-warn" aria-label={held} />
41 );
42 if (status !== "completed")
43 return <Clock size={size} className="shrink-0 text-faint" aria-label={status === "pending" ? "Waiting its turn" : "Queued"} />;
44 switch (conclusion) {
45 case "success":
46 return (
47 <span className="inline-flex shrink-0 rounded-full bg-success/15 p-0.5 text-success" aria-label="Succeeded">
48 <Check size={size - 4} strokeWidth={3} />
49 </span>
50 );
51 case "failure":
52 return (
53 <span className="inline-flex shrink-0 rounded-full bg-danger/15 p-0.5 text-danger" aria-label="Failed">
54 <X size={size - 4} strokeWidth={3} />
55 </span>
56 );
57 case "cancelled":
58 return <Ban size={size} className="shrink-0 text-faint" aria-label="Cancelled" />;
59 default:
60 return <CircleDashed size={size} className="shrink-0 text-faint" aria-label="Skipped" />;
61 }
62}
63
64export function standingWord(standing: Standing): string {
65 const { status, conclusion } = standing;
66 if (status === "in_progress") return "Running";
67 const held = heldWord(standing);
68 if (held) return held;
69 if (status === "pending") return "Waiting for its concurrency group";
70 if (status === "waiting") return "Waiting for the jobs it needs";
71 if (status === "calling") return "Running the workflow it calls";
72 if (status !== "completed") return "Queued";
73 return { success: "Succeeded", failure: "Failed", cancelled: "Cancelled", skipped: "Skipped" }[conclusion ?? "skipped"];
74}
75
76/** `1m 12s`, from two times, or from a start until now. */
77export function duration(start: string | null, end: string | null): string {
78 if (!start) return "";
79 const ms = (end ? new Date(end).getTime() : Date.now()) - new Date(start).getTime();
80 const seconds = Math.max(0, Math.round(ms / 1000));
81 if (seconds < 60) return `${seconds}s`;
82 const minutes = Math.floor(seconds / 60);
83 if (minutes < 60) return `${minutes}m ${seconds % 60}s`;
84 return `${Math.floor(minutes / 60)}h ${minutes % 60}m`;
85}
86
87/**
88 * How long something ran, as text. While it is still running the server and
89 * the browser count to a different now, so the text may differ on hydration
90 * (as TimeAgo's does).
91 */
92export function Duration({ start, end, className }: { start: string | null; end: string | null; className?: string }) {
93 return (
94 <span className={className} suppressHydrationWarning>
95 {duration(start, end)}
96 </span>
97 );
98}
99
100/** `main` from `refs/heads/main`, `v1.2` from a tag, `#12` for a pull request. */
101export function shortRef(ref: string): string {
102 const pull = /^refs\/pull\/(\d+)\//.exec(ref);
103 if (pull) return `#${pull[1]}`;
104 return ref.replace(/^refs\/(heads|tags)\//, "");
105}
106
107const LINE_STYLE: Record<Line["kind"], string> = {
108 text: "text-fg/85",
109 error: "text-danger",
110 warning: "text-warn",
111 notice: "text-accent",
112 debug: "text-faint",
113 command: "text-muted",
114};
115
116function LogLine({ line, number, query = "" }: { line: Line; number: number; query?: string }) {
117 const text = line.text.replace(/^(Error|Warning|Notice): /, "");
118 return (
119 <div className={`flex gap-4 px-4 hover:bg-raised/40 ${line.kind === "error" ? "bg-danger/5" : ""}`}>
120 <span className="w-8 shrink-0 select-none text-right text-faint/70">{number}</span>
121 <span className={`min-w-0 whitespace-pre-wrap break-all ${LINE_STYLE[line.kind]}`}>
122 {line.kind === "error" && <span className="font-semibold">Error: </span>}
123 {line.kind === "warning" && <span className="font-semibold">Warning: </span>}
124 {query
125 ? highlight(text, query).map((piece, index) =>
126 piece.match ? (
127 <mark key={index} className="rounded-sm bg-accent/30 text-fg">
128 {piece.text}
129 </mark>
130 ) : (
131 piece.text
132 ),
133 )
134 : text}
135 </span>
136 </div>
137 );
138}
139
140/**
141 * A step's log. With `query`, only the lines holding it (any case), groups
142 * opened, each match marked.
143 */
144export function LogText({ text, query = "" }: { text: string; query?: string }) {
145 const parsed = useMemo(() => blocks(text), [text]);
146 const found = useMemo(() => searchLog(text, query), [text, query]);
147 if (!text.trim()) return <p className="px-4 py-2 text-xs text-faint">No output.</p>;
148 if (query.trim()) {
149 return (
150 <div className="py-1 font-mono text-xs leading-5">
151 {found.map(({ line, number }) => (
152 <LogLine key={number} line={line} number={number} query={query} />
153 ))}
154 </div>
155 );
156 }
157 return (
158 <div className="py-1 font-mono text-xs leading-5">
159 {parsed.map((block, index) =>
160 block.kind === "line" ? (
161 <LogLine key={index} line={block.line} number={block.number} />
162 ) : (
163 <details key={index} className="group/log">
164 <summary className="flex cursor-pointer list-none items-center gap-1.5 px-4 text-fg/85 hover:bg-raised/40">
165 <span className="w-8 shrink-0" />
166 <ChevronRight size={12} className="shrink-0 text-faint transition-transform group-open/log:rotate-90" />
167 {block.title}
168 </summary>
169 {block.lines.map(({ line, number }) => (
170 <LogLine key={number} line={line} number={number} />
171 ))}
172 </details>
173 ),
174 )}
175 </div>
176 );
177}
178
179/**
180 * A job's log, fetched as it grows, split by step: `render` gets each
181 * step's text (0 is the job's setup).
182 */
183export function useJobLog(url: string, live: boolean): Map<number, string> {
184 const [chunks, setChunks] = useState<LogChunk[]>([]);
185 const after = useRef(0);
186 useEffect(() => {
187 after.current = 0;
188 setChunks([]);
189 }, [url]);
190 useEffect(() => {
191 let stopped = false;
192 let timer: ReturnType<typeof setTimeout> | undefined;
193 const load = async () => {
194 try {
195 // Pages of up to 500 chunks, until there is no more for now.
196 for (;;) {
197 const response = await fetch(`${url}?after=${after.current}`);
198 if (!response.ok) break;
199 const page = (await response.json()) as { chunks: LogChunk[]; done: boolean };
200 if (stopped) return;
201 if (page.chunks.length > 0) {
202 after.current = page.chunks[page.chunks.length - 1].seq;
203 setChunks((known) => [...known, ...page.chunks]);
204 }
205 if (page.chunks.length < 500) {
206 if (!page.done && live) timer = setTimeout(load, 2000);
207 break;
208 }
209 }
210 } catch {
211 if (!stopped && live) timer = setTimeout(load, 4000);
212 }
213 };
214 load();
215 return () => {
216 stopped = true;
217 if (timer) clearTimeout(timer);
218 };
219 }, [url, live]);
220 return useMemo(() => {
221 const byStep = new Map<number, string>();
222 for (const chunk of chunks) byStep.set(chunk.step, (byStep.get(chunk.step) ?? "") + chunk.text);
223 return byStep;
224 }, [chunks]);
225}
226
227const NOTE_ICON = {
228 info: <Info size={14} className="mt-0.5 shrink-0 text-muted" />,
229 warning: <AlertTriangle size={14} className="mt-0.5 shrink-0 text-warn" />,
230 unsupported: <CircleSlash size={14} className="mt-0.5 shrink-0 text-danger" />,
231};
232
233/** What in a workflow runs differently on g1t, worst first. */
234export function Notes({ notes }: { notes: WorkflowNote[] }) {
235 if (notes.length === 0) return null;
236 const order = { unsupported: 0, warning: 1, info: 2 };
237 const sorted = [...notes].sort((a, b) => order[a.severity] - order[b.severity]);
238 const blocking = notes.filter((n) => n.severity === "unsupported").length;
239 return (
240 <Card asChild className="group">
241 <details open={blocking > 0}>
242 <summary className="cursor-pointer list-none px-4 py-2.5 text-sm">
243 <span className="font-medium">How this runs on g1t</span>
244 <span className="text-muted">
245 {" · "}
246 {blocking > 0 ? `${blocking} thing${blocking === 1 ? "" : "s"} g1t cannot run yet` : `${notes.length} note${notes.length === 1 ? "" : "s"}`}
247 </span>
248 </summary>
249 <ul className="space-y-2 border-t border-line px-4 py-3 text-sm">
250 {sorted.map((note, index) => (
251 <li key={index} className="flex gap-2">
252 {NOTE_ICON[note.severity]}
253 <span>
254 {note.job && <span className="font-mono text-xs text-muted">{note.job}: </span>}
255 {note.message}
256 </span>
257 </li>
258 ))}
259 </ul>
260 </details>
261 </Card>
262 );
263}
264
265
266/** What workflow runs said about a pull request's head, each linking to its run. */
267export function WorkflowStatuses({ statuses }: { statuses: CommitStatus[] }) {
268 if (statuses.length === 0) return null;
269 const standing = (state: CommitStatus["state"]) =>
270 state === "pending"
271 ? { status: "in_progress", conclusion: null }
272 : { status: "completed", conclusion: (state === "success" ? "success" : "failure") as Conclusion };
273 const failed = statuses.filter((s) => s.state === "failure" || s.state === "error").length;
274 const pending = statuses.filter((s) => s.state === "pending").length;
275 return (
276 <Card asChild className="p-4">
277 <section>
278 <h3 className="text-sm font-medium">
279 {failed > 0 ? `${failed} workflow${failed === 1 ? "" : "s"} failed` : pending > 0 ? "Workflows running" : "Workflows passed"}
280 </h3>
281 <ul className="mt-3 space-y-2 text-sm">
282 {statuses.map((status) => {
283 const path = status.targetUrl?.replace(/^https:\/\/g1t\.sh/, "") ?? null;
284 const row = (
285 <>
286 <StatusIcon {...standing(status.state)} size={14} />
287 <span className="min-w-0 truncate">{status.context}</span>
288 </>
289 );
290 return (
291 <li key={status.context}>
292 {path ? (
293 <Hint label={status.description}>
294 <Link to={path} className="flex items-center gap-2 hover:text-fg">
295 {row}
296 </Link>
297 </Hint>
298 ) : (
299 <span className="flex items-center gap-2">{row}</span>
300 )}
301 </li>
302 );
303 })}
304 </ul>
305 </section>
306 </Card>
307 );
308}