import { formatDuration, shortSha } from "@/lib/format"; import type { Run } from "@/lib/runs"; /** How long something ran (or has been running), or null if it hasn't started. */ export function runDuration(item: { startedAt: number | null; finishedAt: number | null }) { if (!item.startedAt) return null; return formatDuration((item.finishedAt ?? Date.now()) - item.startedAt); } /** What started a run: "Commit 1a2b3c4 pushed by ana", "Manually run by Claude Code for ana", "Pull request #4 synchronize by ana". */ export function triggerText(run: Run) { const who = run.agentName && run.actor ? `${run.agentName} for ${run.actor}` : run.actor; const by = who ? ` by ${who}` : ""; const p = run.payload ?? {}; const num = (key: string) => ((p[key] as { number?: number } | undefined)?.number ?? "?"); const action = typeof p.action === "string" ? ` ${p.action.replace(/_/g, " ")}` : ""; switch (run.event) { case "workflow_dispatch": return `Manually run${by}`; case "pull_request": return `Pull request #${num("pull_request")}${action}${by}`; case "pull_request_review": return `Review of pull request #${num("pull_request")}${by}`; case "issues": return `Issue #${num("issue")}${action}${by}`; case "issue_comment": return `Comment on #${p.pull_request ? num("pull_request") : num("issue")}${by}`; default: return `Commit ${shortSha(run.sha)} pushed${by}`; } } export const refName = (ref: string) => ref.replace(/^refs\/(heads|tags)\//, "");