| 59e6667 | | | 1 | import { spinner, log, note } from "@clack/prompts"; |
| 69d1a72 | | | 2 | import { hubRequest } from "../api.js"; |
| 69d1a72 | | | 3 | import { getRepoSlug } from "../config.js"; |
| 0d9d723 | | | 4 | import { STATUS_COLORS, STATUS_ICONS, RESET, formatDurationRange } from "../format.js"; |
| 69d1a72 | | | 5 | |
| 69d1a72 | | | 6 | interface PipelineRun { |
| 69d1a72 | | | 7 | id: number; |
| 69d1a72 | | | 8 | pipeline_name: string; |
| 69d1a72 | | | 9 | status: string; |
| 69d1a72 | | | 10 | trigger_ref: string | null; |
| 69d1a72 | | | 11 | commit_id: string | null; |
| 69d1a72 | | | 12 | started_at: string | null; |
| 69d1a72 | | | 13 | finished_at: string | null; |
| 69d1a72 | | | 14 | duration_ms: number | null; |
| 69d1a72 | | | 15 | } |
| 69d1a72 | | | 16 | |
| 69d1a72 | | | 17 | interface PipelineStep { |
| 69d1a72 | | | 18 | id: number; |
| 69d1a72 | | | 19 | name: string; |
| 69d1a72 | | | 20 | image: string; |
| 69d1a72 | | | 21 | status: string; |
| 69d1a72 | | | 22 | started_at: string | null; |
| 69d1a72 | | | 23 | finished_at: string | null; |
| 69d1a72 | | | 24 | step_index: number; |
| 69d1a72 | | | 25 | } |
| 69d1a72 | | | 26 | |
| 69d1a72 | | | 27 | export async function ciStatus(args: string[]) { |
| 69d1a72 | | | 28 | const slug = await getRepoSlug(args); |
| 69d1a72 | | | 29 | |
| 69d1a72 | | | 30 | // Find run ID: first positional arg that's a number |
| 69d1a72 | | | 31 | let runId = args.find((a) => !a.startsWith("--") && /^\d+$/.test(a)); |
| 69d1a72 | | | 32 | |
| 69d1a72 | | | 33 | if (!runId) { |
| 69d1a72 | | | 34 | // Get latest run |
| 59e6667 | | | 35 | const s = spinner(); |
| 59e6667 | | | 36 | s.start("Fetching latest run"); |
| 69d1a72 | | | 37 | const { runs } = await hubRequest<{ runs: any[] }>( |
| 69d1a72 | | | 38 | `/api/repos/${slug}/canopy/runs?limit=1` |
| 69d1a72 | | | 39 | ); |
| 69d1a72 | | | 40 | if (runs.length === 0) { |
| 59e6667 | | | 41 | s.stop("No runs found"); |
| 59e6667 | | | 42 | log.info("No pipeline runs."); |
| 69d1a72 | | | 43 | return; |
| 69d1a72 | | | 44 | } |
| 69d1a72 | | | 45 | runId = String(runs[0].id); |
| 59e6667 | | | 46 | s.stop(`Run #${runId}`); |
| 69d1a72 | | | 47 | } |
| 69d1a72 | | | 48 | |
| 59e6667 | | | 49 | const s2 = spinner(); |
| 59e6667 | | | 50 | s2.start(`Fetching run #${runId}`); |
| 69d1a72 | | | 51 | const { run, steps } = await hubRequest<{ run: PipelineRun; steps: PipelineStep[] }>( |
| 69d1a72 | | | 52 | `/api/repos/${slug}/canopy/runs/${runId}` |
| 69d1a72 | | | 53 | ); |
| 59e6667 | | | 54 | s2.stop(`${run.pipeline_name}`); |
| 69d1a72 | | | 55 | |
| 69d1a72 | | | 56 | // Header |
| 69d1a72 | | | 57 | const color = STATUS_COLORS[run.status] ?? ""; |
| 59e6667 | | | 58 | const headerLines = [ |
| 59e6667 | | | 59 | `Run #${run.id} ${color}${run.status}${RESET} branch: ${run.trigger_ref ?? "-"}`, |
| 59e6667 | | | 60 | ]; |
| 59e6667 | | | 61 | if (run.commit_id) headerLines.push(`Commit: ${run.commit_id.slice(0, 12)}`); |
| 59e6667 | | | 62 | note(headerLines.join("\n"), run.pipeline_name); |
| 69d1a72 | | | 63 | |
| 69d1a72 | | | 64 | // Steps |
| 69d1a72 | | | 65 | for (const step of steps) { |
| 69d1a72 | | | 66 | const icon = STATUS_ICONS[step.status] ?? "?"; |
| 0d9d723 | | | 67 | const dur = formatDurationRange(step.started_at, step.finished_at); |
| 69d1a72 | | | 68 | const durStr = dur ? ` (${dur})` : ""; |
| 59e6667 | | | 69 | log.message(`${icon} ${step.step_index}. ${step.name}${durStr}`); |
| 69d1a72 | | | 70 | } |
| 69d1a72 | | | 71 | } |