Move CI job grouping into lib

The status chip's grouping, dedupe and summary rules were untested inside
the component, and a wrong CI status is very visible.
This commit is contained in:
Rūdolfs Ošiņš
2026-10-01 18:20:24 +02:00
parent 974235b1fe
commit ca3b059a62
3 changed files with 366 additions and 156 deletions
+8 -156
View File
@@ -1,12 +1,15 @@
<script lang="ts">
import type { Author } from "@bindings/cob/Author";
import type { Job } from "@bindings/repo/Job";
import type { Run } from "@bindings/repo/Run";
import { SvelteSet } from "svelte/reactivity";
import type { RunView, Status } from "@app/lib/ciJobs";
import {
aggregateStatus,
groupJobs,
statusLabel,
totalCounts,
} from "@app/lib/ciJobs";
import { cachedListJobs } from "@app/lib/invoke";
import { authorForNodeId, safeHttpUrl } from "@app/lib/utils";
import { authorForNodeId } from "@app/lib/utils";
import Button from "@app/components/Button.svelte";
import Icon from "@app/components/Icon.svelte";
@@ -22,160 +25,9 @@
const { commit, rid, styleHeight = "2rem" }: Props = $props();
type Status = Run["status"];
type RunView = {
run: Run;
label: string;
safeLog: string | undefined;
};
type HostGroup = {
host: string;
runs: RunView[];
};
type Counts = { succeeded: number; failed: number; started: number };
type NodeGroup = {
nodeKey: string;
author: Author;
hosts: HostGroup[];
flatRuns?: RunView[];
inlineHost?: string;
counts: Counts;
status: Status;
};
const collapsed = new SvelteSet<string>();
let popoverExpanded: boolean = $state(false);
function parseUrl(s: string): URL | undefined {
try {
return new URL(s);
} catch {
return undefined;
}
}
function runLabel(run: Run, url: URL | undefined): string {
if (url && url.host === "github.com") {
const m = url.pathname.match(/\/actions\/runs\/(\d+)/);
if (m) return m[1];
}
return run.runId.slice(0, 8);
}
function isTerminal(status: Status): boolean {
return status === "succeeded" || status === "failed";
}
function aggregateStatus(c: Counts): Status {
if (c.failed) return "failed";
if (c.started) return "started";
return "succeeded";
}
function statusLabel(c: Counts): string {
const parts: string[] = [];
if (c.succeeded) parts.push(`${c.succeeded} passed`);
if (c.failed) parts.push(`${c.failed} failed`);
if (c.started) parts.push(`${c.started} running`);
return parts.join(" · ");
}
function totalCounts(groups: NodeGroup[]): Counts {
const total: Counts = { succeeded: 0, failed: 0, started: 0 };
for (const g of groups) {
total.succeeded += g.counts.succeeded;
total.failed += g.counts.failed;
total.started += g.counts.started;
}
return total;
}
function groupJobs(jobs: Job[]): NodeGroup[] {
const byNode: Record<
string,
{
author: Author;
byHost: Record<string, RunView[]>;
seen: Map<string, RunView>;
}
> = {};
const nodeOrder: string[] = [];
for (const job of jobs) {
for (const run of job.runs) {
const url = parseUrl(run.log);
const host = url && url.host ? url.host : "(unknown host)";
const safeLog =
url?.host !== "no.url.example.com" ? safeHttpUrl(run.log) : undefined;
const view: RunView = {
run,
label: runLabel(run, url),
safeLog,
};
const key = run.node.did;
let entry = byNode[key];
if (!entry) {
entry = { author: run.node, byHost: {}, seen: new Map() };
byNode[key] = entry;
nodeOrder.push(key);
}
const existing = entry.seen.get(run.runId);
if (existing) {
// The same run can appear under multiple job COBs. Keep one view
// per runId, but let a terminal status override a stale "started"
// one when COBs polled at different times disagree. We mutate the
// existing view in place because it is the same object referenced
// from its host bucket below.
if (!isTerminal(existing.run.status) && isTerminal(run.status)) {
existing.run = run;
existing.label = view.label;
existing.safeLog = view.safeLog;
}
continue;
}
entry.seen.set(run.runId, view);
const bucket = entry.byHost[host] ?? (entry.byHost[host] = []);
bucket.push(view);
}
}
return nodeOrder.map(nodeKey => {
const { author, byHost } = byNode[nodeKey];
const hosts: HostGroup[] = Object.entries(byHost).map(([host, runs]) => ({
host,
runs,
}));
const counts: Counts = { succeeded: 0, failed: 0, started: 0 };
for (const { runs } of hosts) {
for (const v of runs) counts[v.run.status]++;
}
const group: NodeGroup = {
nodeKey,
author,
hosts,
counts,
status: aggregateStatus(counts),
};
if (hosts.length === 1) {
group.flatRuns = hosts[0].runs;
if (!aliasMatchesHost(author.alias, hosts[0].host)) {
group.inlineHost = hosts[0].host;
}
}
return group;
});
}
function aliasMatchesHost(alias: string | undefined, host: string): boolean {
if (!alias) return false;
const a = alias.toLowerCase();
if (host === a) return true;
if (host.endsWith("." + a)) return true;
return host.split(".").includes(a);
}
// Held in a `$derived`; see `ReviewCodeThread`. The TTL here means a re-run
// would refetch and blank the chip.
const jobsPromise = $derived(cachedListJobs(rid, commit).catch(() => []));