| 9d879c0 | | | 1 | export interface InvocationRun { |
| 9d879c0 | | | 2 | id: number; |
| 9d879c0 | | | 3 | status: string; |
| 9d879c0 | | | 4 | trigger_type?: string | null; |
| 9d879c0 | | | 5 | trigger_ref?: string | null; |
| 9d879c0 | | | 6 | commit_id: string | null; |
| 9d879c0 | | | 7 | commit_message: string | null; |
| 9d879c0 | | | 8 | created_at: string; |
| 9d879c0 | | | 9 | } |
| 9d879c0 | | | 10 | |
| 9d879c0 | | | 11 | export interface InvocationGroup<T extends InvocationRun = InvocationRun> { |
| 9d879c0 | | | 12 | key: string; |
| 9d879c0 | | | 13 | commitId: string | null; |
| 9d879c0 | | | 14 | commitMessage: string | null; |
| 9d879c0 | | | 15 | triggerType: string | null | undefined; |
| 9d879c0 | | | 16 | triggerRef: string | null; |
| 9d879c0 | | | 17 | runs: T[]; |
| 9d879c0 | | | 18 | newestRunId: number; |
| 9d879c0 | | | 19 | newestMs: number; |
| 9d879c0 | | | 20 | } |
| 9d879c0 | | | 21 | |
| 9d879c0 | | | 22 | const INVOCATION_WINDOW_MS = 120000; |
| 9d879c0 | | | 23 | |
| 9d879c0 | | | 24 | export function groupByInvocation<T extends InvocationRun>(runs: T[]): InvocationGroup<T>[] { |
| 9d879c0 | | | 25 | const sorted = [...runs].sort((a, b) => b.id - a.id); |
| 9d879c0 | | | 26 | const groups: InvocationGroup<T>[] = []; |
| 9d879c0 | | | 27 | |
| 9d879c0 | | | 28 | for (const run of sorted) { |
| 9d879c0 | | | 29 | const runMs = Date.parse(run.created_at); |
| 9d879c0 | | | 30 | const last = groups[groups.length - 1]; |
| 9d879c0 | | | 31 | const sameSeed = |
| 9d879c0 | | | 32 | last && |
| 9d879c0 | | | 33 | (last.commitId ?? "") === (run.commit_id ?? "") && |
| 9d879c0 | | | 34 | (last.triggerType ?? "") === (run.trigger_type ?? "") && |
| 9d879c0 | | | 35 | (last.triggerRef ?? "") === (run.trigger_ref ?? ""); |
| 9d879c0 | | | 36 | const withinWindow = |
| 9d879c0 | | | 37 | sameSeed && |
| 9d879c0 | | | 38 | Number.isFinite(runMs) && |
| 9d879c0 | | | 39 | last.newestMs - runMs <= INVOCATION_WINDOW_MS; |
| 9d879c0 | | | 40 | |
| 9d879c0 | | | 41 | if (withinWindow) { |
| 9d879c0 | | | 42 | last.runs.push(run); |
| 9d879c0 | | | 43 | continue; |
| 9d879c0 | | | 44 | } |
| 9d879c0 | | | 45 | |
| 9d879c0 | | | 46 | groups.push({ |
| 9d879c0 | | | 47 | key: `inv-${run.id}`, |
| 9d879c0 | | | 48 | commitId: run.commit_id, |
| 9d879c0 | | | 49 | commitMessage: run.commit_message, |
| 9d879c0 | | | 50 | triggerType: run.trigger_type, |
| 9d879c0 | | | 51 | triggerRef: run.trigger_ref ?? null, |
| 9d879c0 | | | 52 | runs: [run], |
| 9d879c0 | | | 53 | newestRunId: run.id, |
| 9d879c0 | | | 54 | newestMs: Number.isFinite(runMs) ? runMs : 0, |
| 9d879c0 | | | 55 | }); |
| 9d879c0 | | | 56 | } |
| 9d879c0 | | | 57 | |
| 9d879c0 | | | 58 | for (const group of groups) { |
| 9d879c0 | | | 59 | // Keep build numbers in ascending order within each invocation. |
| 9d879c0 | | | 60 | group.runs.sort((a, b) => a.id - b.id); |
| 9d879c0 | | | 61 | } |
| 9d879c0 | | | 62 | |
| 9d879c0 | | | 63 | return groups; |
| 9d879c0 | | | 64 | } |
| 9d879c0 | | | 65 | |
| 9d879c0 | | | 66 | export function formatTriggerType(triggerType: string | null | undefined): string { |
| 9d879c0 | | | 67 | if (!triggerType) return "Build"; |
| 9d879c0 | | | 68 | return triggerType.replace("_", " ").replace(/\b\w/g, (m) => m.toUpperCase()); |
| 9d879c0 | | | 69 | } |
| 9d879c0 | | | 70 | |
| 9d879c0 | | | 71 | export function getInvocationStatus<T extends InvocationRun>(runs: T[]): string { |
| 9d879c0 | | | 72 | const statuses = new Set(runs.map((run) => run.status)); |
| 9d879c0 | | | 73 | if (statuses.has("running")) return "running"; |
| 9d879c0 | | | 74 | if (statuses.has("failed")) return "failed"; |
| 9d879c0 | | | 75 | if (statuses.has("pending")) return "pending"; |
| 9d879c0 | | | 76 | if (statuses.has("cancelled")) return "cancelled"; |
| 9d879c0 | | | 77 | return "passed"; |
| 9d879c0 | | | 78 | } |
| 9d879c0 | | | 79 | |
| 9d879c0 | | | 80 | function slugify(input: string): string { |
| 9d879c0 | | | 81 | return input |
| 9d879c0 | | | 82 | .toLowerCase() |
| 9d879c0 | | | 83 | .replace(/['"]/g, "") |
| 9d879c0 | | | 84 | .replace(/[^a-z0-9]+/g, "-") |
| 9d879c0 | | | 85 | .replace(/^-+|-+$/g, "") |
| 9d879c0 | | | 86 | .slice(0, 72); |
| 9d879c0 | | | 87 | } |
| 9d879c0 | | | 88 | |
| 9d879c0 | | | 89 | export function getInvocationBaseSlug<T extends InvocationRun>(invocation: InvocationGroup<T>): string { |
| 9d879c0 | | | 90 | if (invocation.commitId) return invocation.commitId.substring(0, 8).toLowerCase(); |
| 9d879c0 | | | 91 | const fromMessage = invocation.commitMessage ? slugify(invocation.commitMessage) : ""; |
| 9d879c0 | | | 92 | if (fromMessage) return fromMessage; |
| 9d879c0 | | | 93 | const fromTrigger = invocation.triggerType ? slugify(invocation.triggerType) : ""; |
| 9d879c0 | | | 94 | return fromTrigger || `run-${invocation.newestRunId}`; |
| 9d879c0 | | | 95 | } |
| 9d879c0 | | | 96 | |
| 8a2c7d4 | | | 97 | export function getInvocationRunSlug<T extends InvocationRun>( |
| 8a2c7d4 | | | 98 | invocation: InvocationGroup<T>, |
| 8a2c7d4 | | | 99 | allInvocations?: InvocationGroup<T>[], |
| 8a2c7d4 | | | 100 | ): string { |
| 8a2c7d4 | | | 101 | const base = getInvocationBaseSlug(invocation); |
| 8a2c7d4 | | | 102 | if (allInvocations) { |
| 8a2c7d4 | | | 103 | const duplicates = allInvocations.filter( |
| 8a2c7d4 | | | 104 | (g) => getInvocationBaseSlug(g) === base |
| 8a2c7d4 | | | 105 | ); |
| 8a2c7d4 | | | 106 | if (duplicates.length > 1) { |
| 8a2c7d4 | | | 107 | return `${base}-${invocation.newestRunId}`; |
| 8a2c7d4 | | | 108 | } |
| 8a2c7d4 | | | 109 | } |
| 8a2c7d4 | | | 110 | return base; |
| 9d879c0 | | | 111 | } |
| 9d879c0 | | | 112 | |
| 9d879c0 | | | 113 | export function getSeedRunIdFromInvocationSlug(slug: string): number | null { |
| 8a2c7d4 | | | 114 | // Match "run-<id>" format |
| 8a2c7d4 | | | 115 | const runMatch = slug.match(/^run-(\d+)$/); |
| 8a2c7d4 | | | 116 | if (runMatch) { |
| 8a2c7d4 | | | 117 | const id = Number.parseInt(runMatch[1], 10); |
| 8a2c7d4 | | | 118 | return Number.isNaN(id) ? null : id; |
| 8a2c7d4 | | | 119 | } |
| 8a2c7d4 | | | 120 | // Match disambiguated "<base>-<id>" suffix (e.g. "5fbd7f38-42") |
| 8a2c7d4 | | | 121 | const suffixMatch = slug.match(/-(\d+)$/); |
| 8a2c7d4 | | | 122 | if (suffixMatch) { |
| 8a2c7d4 | | | 123 | const id = Number.parseInt(suffixMatch[1], 10); |
| 8a2c7d4 | | | 124 | return Number.isNaN(id) ? null : id; |
| 8a2c7d4 | | | 125 | } |
| 8a2c7d4 | | | 126 | return null; |
| 9d879c0 | | | 127 | } |