| 1 | import { spinner, log } from "@clack/prompts"; |
| 2 | import { hubRequest } from "../api.js"; |
| 3 | import { getRepoSlug } from "../config.js"; |
| 4 | import { STATUS_COLORS, colorStatus, formatDuration, formatTime } from "../format.js"; |
| 5 | |
| 6 | interface PipelineRun { |
| 7 | id: number; |
| 8 | pipeline_name: string; |
| 9 | status: string; |
| 10 | trigger_ref: string | null; |
| 11 | commit_id: string | null; |
| 12 | started_at: string | null; |
| 13 | finished_at: string | null; |
| 14 | duration_ms: number | null; |
| 15 | created_at: string; |
| 16 | } |
| 17 | |
| 18 | export async function ciRuns(args: string[]) { |
| 19 | const slug = await getRepoSlug(args); |
| 20 | |
| 21 | const statusIdx = args.indexOf("--status"); |
| 22 | const status = statusIdx !== -1 ? args[statusIdx + 1] : undefined; |
| 23 | |
| 24 | const limitIdx = args.indexOf("--limit"); |
| 25 | const limit = limitIdx !== -1 ? args[limitIdx + 1] : "10"; |
| 26 | |
| 27 | const params = new URLSearchParams(); |
| 28 | if (status) params.set("status", status); |
| 29 | if (limit) params.set("limit", limit); |
| 30 | |
| 31 | const qs = params.toString() ? `?${params}` : ""; |
| 32 | |
| 33 | const s = spinner(); |
| 34 | s.start("Fetching pipeline runs"); |
| 35 | const { runs } = await hubRequest<{ runs: PipelineRun[] }>( |
| 36 | `/api/repos/${slug}/canopy/runs${qs}` |
| 37 | ); |
| 38 | s.stop(`${runs.length} run${runs.length !== 1 ? "s" : ""}`); |
| 39 | |
| 40 | if (runs.length === 0) { |
| 41 | log.info("No pipeline runs."); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | // Table header |
| 46 | console.log( |
| 47 | `${"ID".padEnd(6)}${"Pipeline".padEnd(22)}${"Status".padEnd(18)}${"Branch".padEnd(14)}${"Duration".padEnd(10)}Created` |
| 48 | ); |
| 49 | console.log("-".repeat(90)); |
| 50 | |
| 51 | for (const run of runs) { |
| 52 | const id = String(run.id).padEnd(6); |
| 53 | const name = (run.pipeline_name || "-").slice(0, 20).padEnd(22); |
| 54 | const st = colorStatus(run.status).padEnd(18 + (STATUS_COLORS[run.status] ? 9 : 0)); |
| 55 | const branch = (run.trigger_ref || "-").slice(0, 12).padEnd(14); |
| 56 | const dur = formatDuration(run.duration_ms).padEnd(10); |
| 57 | const time = formatTime(run.created_at); |
| 58 | console.log(`${id}${name}${st}${branch}${dur}${time}`); |
| 59 | } |
| 60 | } |
| 61 | |