// OxygenFlow in the editor, without VS Code: how runs and jobs look (icon, // colour, labels) and which runs just finished. Shared by the Flow view, the // status bar item and the tests. import type { FlowRun } from "./hubapi"; /** A codicon and a theme colour id for a run's or job's status. */ export interface Look { /** Codicon id; `~spin` makes it turn. */ icon: string; /** A theme colour id, e.g. `testing.iconPassed`. */ color: string; /** A short word for screen readers and tooltips. */ word: string; } export function look(status: string): Look { switch (status) { case "passed": return { icon: "pass-filled", color: "testing.iconPassed", word: "passed" }; case "failed": return { icon: "error", color: "testing.iconFailed", word: "failed" }; case "error": return { icon: "warning", color: "testing.iconErrored", word: "could not run" }; case "running": return { icon: "sync~spin", color: "oxygenhub.flowRunning", word: "running" }; case "queued": return { icon: "clock", color: "testing.iconQueued", word: "queued" }; case "skipped": return { icon: "debug-step-over", color: "testing.iconSkipped", word: "skipped" }; default: return { icon: "circle-large-outline", color: "descriptionForeground", word: status }; } } export function live(status: string): boolean { return status === "queued" || status === "running"; } /** "3.2 s", "4 min 10 s". */ export function duration(ms: number): string { const s = Math.max(0, ms) / 1000; if (s < 60) { return `${s.toFixed(1)} s`; } return `${Math.floor(s / 60)} min ${Math.floor(s % 60)} s`; } /** "just now", "5 minutes ago", as the hub says it. */ export function ago(ms: number, now = Date.now()): string { const s = Math.max(0, Math.floor((now - ms) / 1000)); const unit = (n: number, w: string) => `${n} ${w}${n === 1 ? "" : "s"} ago`; if (s < 45) { return "just now"; } if (s < 90) { return "a minute ago"; } if (s < 2700) { return unit(Math.ceil(s / 60), "minute"); } if (s < 5400) { return "an hour ago"; } if (s < 79200) { return unit(Math.ceil(s / 3600), "hour"); } return unit(Math.ceil(s / 86400), "day"); } /** What a run ran on: its bookmark, or the change for a run on one change. */ export function subject(run: FlowRun): string { return run.bookmark || `change ${(run.heads[0] ?? "").slice(0, 12)}`; } /** "#12 main". */ export function runLabel(run: FlowRun): string { return `#${run.number} ${subject(run)}`; } /** "passed in 2 min 3 s · push by andy · 5 minutes ago". */ export function runDescription(run: FlowRun, now = Date.now()): string { const took = run.finished_ms ? ` in ${duration(run.finished_ms - run.created_ms)}` : ""; const queued = run.status === "queued" && run.queue_position ? ` (#${run.queue_position} in line)` : ""; const trigger = run.trigger.startsWith("rerun:") ? `re-run of #${run.trigger.slice(6)}` : run.trigger; return `${look(run.status).word}${took}${queued} · ${trigger} by ${run.actor} · ${ago(run.created_ms, now)}`; } /** * Runs that were live at the last look and are finished now, so the editor * can say so once. `before` maps a run's key to its last status. */ export function newlyFinished(before: Map, runs: { key: string; run: FlowRun }[]): { key: string; run: FlowRun }[] { return runs.filter(({ key, run }) => { const was = before.get(key); return was !== undefined && live(was) && !live(run.status); }); }