| 9d879c0 | | | 1 | import type { Metadata } from "next"; |
| 9d879c0 | | | 2 | import { redirect } from "next/navigation"; |
| 9d879c0 | | | 3 | import { groveApiUrl } from "@/lib/utils"; |
| 9d879c0 | | | 4 | import { |
| 9d879c0 | | | 5 | formatTriggerType, |
| 9d879c0 | | | 6 | getInvocationRunSlug, |
| 9d879c0 | | | 7 | getSeedRunIdFromInvocationSlug, |
| 9d879c0 | | | 8 | getInvocationStatus, |
| 9d879c0 | | | 9 | groupByInvocation, |
| 9d879c0 | | | 10 | } from "@/lib/canopy-invocations"; |
| fe3b509 | | | 11 | import { InvocationDetail } from "./invocation-detail"; |
| 5bcd5db | | | 12 | import { CanopyLiveRefresh } from "@/app/components/canopy-live-refresh"; |
| 9d879c0 | | | 13 | |
| 9d879c0 | | | 14 | interface Props { |
| 9d879c0 | | | 15 | params: Promise<{ owner: string; repo: string; runSlug: string }>; |
| 9d879c0 | | | 16 | } |
| 9d879c0 | | | 17 | |
| 9d879c0 | | | 18 | interface Run { |
| 9d879c0 | | | 19 | id: number; |
| 9d879c0 | | | 20 | pipeline_name: string; |
| 9d879c0 | | | 21 | status: string; |
| 9d879c0 | | | 22 | trigger_type?: string | null; |
| 9d879c0 | | | 23 | commit_id: string | null; |
| 9d879c0 | | | 24 | commit_message: string | null; |
| 9d879c0 | | | 25 | trigger_ref?: string | null; |
| 9d879c0 | | | 26 | started_at?: string | null; |
| 9d879c0 | | | 27 | duration_ms?: number | null; |
| 9d879c0 | | | 28 | created_at: string; |
| 9d879c0 | | | 29 | repo_name: string; |
| 9d879c0 | | | 30 | owner_name: string; |
| 9d879c0 | | | 31 | } |
| 9d879c0 | | | 32 | |
| 9d879c0 | | | 33 | export async function generateMetadata({ params }: Props): Promise<Metadata> { |
| 86450dc | | | 34 | const { repo } = await params; |
| 86450dc | | | 35 | return { title: `Run · ${repo}` }; |
| 9d879c0 | | | 36 | } |
| 9d879c0 | | | 37 | |
| 9d879c0 | | | 38 | async function getRuns(owner: string, repo: string): Promise<Run[]> { |
| 9d879c0 | | | 39 | try { |
| 9d879c0 | | | 40 | const res = await fetch( |
| 9d879c0 | | | 41 | `${groveApiUrl}/api/repos/${owner}/${repo}/canopy/runs?limit=200`, |
| 9d879c0 | | | 42 | { cache: "no-store" } |
| 9d879c0 | | | 43 | ); |
| 9d879c0 | | | 44 | if (!res.ok) return []; |
| 9d879c0 | | | 45 | const data = await res.json(); |
| 9d879c0 | | | 46 | return data.runs ?? []; |
| 9d879c0 | | | 47 | } catch { |
| 9d879c0 | | | 48 | return []; |
| 9d879c0 | | | 49 | } |
| 9d879c0 | | | 50 | } |
| 9d879c0 | | | 51 | |
| 9d879c0 | | | 52 | export default async function CanopyInvocationPage({ params }: Props) { |
| 9d879c0 | | | 53 | const { owner, repo, runSlug } = await params; |
| 9d879c0 | | | 54 | const normalizedRunSlug = runSlug.toLowerCase(); |
| 9d879c0 | | | 55 | const seedId = getSeedRunIdFromInvocationSlug(normalizedRunSlug); |
| 9d879c0 | | | 56 | const runs = await getRuns(owner, repo); |
| 9d879c0 | | | 57 | const invocations = groupByInvocation(runs); |
| 9d879c0 | | | 58 | const invocation = |
| 9d879c0 | | | 59 | (seedId !== null |
| 9d879c0 | | | 60 | ? invocations.find((group) => group.runs.some((run) => run.id === seedId)) |
| 9d879c0 | | | 61 | : null) ?? |
| 9d879c0 | | | 62 | invocations.find((group) => { |
| 9d879c0 | | | 63 | if (group.commitId) { |
| 9d879c0 | | | 64 | return group.commitId.toLowerCase().startsWith(normalizedRunSlug); |
| 9d879c0 | | | 65 | } |
| 8a2c7d4 | | | 66 | return getInvocationRunSlug(group, invocations) === normalizedRunSlug; |
| 9d879c0 | | | 67 | }); |
| 9d879c0 | | | 68 | |
| 9d879c0 | | | 69 | if (!invocation) { |
| 9d879c0 | | | 70 | return ( |
| fe3b509 | | | 71 | <div className="px-4 sm:px-6 py-6"> |
| 9d879c0 | | | 72 | <p className="text-sm" style={{ color: "var(--text-faint)" }}> |
| 9d879c0 | | | 73 | Build invocation not found. |
| 9d879c0 | | | 74 | </p> |
| 9d879c0 | | | 75 | </div> |
| 9d879c0 | | | 76 | ); |
| 9d879c0 | | | 77 | } |
| 9d879c0 | | | 78 | |
| 9d879c0 | | | 79 | const title = |
| 9d879c0 | | | 80 | invocation.commitMessage || |
| 9d879c0 | | | 81 | (invocation.commitId |
| 9d879c0 | | | 82 | ? invocation.commitId.substring(0, 8) |
| 9d879c0 | | | 83 | : `${formatTriggerType(invocation.triggerType)} build`); |
| 9d879c0 | | | 84 | const status = getInvocationStatus(invocation.runs); |
| 9d879c0 | | | 85 | const newestRun = invocation.runs[invocation.runs.length - 1]; |
| 8a2c7d4 | | | 86 | const canonicalSlug = getInvocationRunSlug(invocation, invocations); |
| 9d879c0 | | | 87 | if (normalizedRunSlug !== canonicalSlug) { |
| 9d879c0 | | | 88 | redirect(`/${owner}/${repo}/runs/${canonicalSlug}`); |
| 9d879c0 | | | 89 | } |
| 9d879c0 | | | 90 | |
| 9d879c0 | | | 91 | return ( |
| 5bcd5db | | | 92 | <> |
| 5bcd5db | | | 93 | <CanopyLiveRefresh scope="repo" owner={owner} repo={repo} /> |
| 5bcd5db | | | 94 | <InvocationDetail |
| 5bcd5db | | | 95 | owner={owner} |
| 5bcd5db | | | 96 | repo={repo} |
| 5bcd5db | | | 97 | runs={invocation.runs} |
| 5bcd5db | | | 98 | title={title} |
| 5bcd5db | | | 99 | status={status} |
| 5bcd5db | | | 100 | triggerType={invocation.triggerType ?? null} |
| 5bcd5db | | | 101 | commitId={invocation.commitId} |
| 5bcd5db | | | 102 | newestCreatedAt={newestRun.created_at} |
| 5bcd5db | | | 103 | /> |
| 5bcd5db | | | 104 | </> |
| 9d879c0 | | | 105 | ); |
| 9d879c0 | | | 106 | } |