| da0f651 | | | 1 | import type { Metadata } from "next"; |
| fe3b509 | | | 2 | import Link from "next/link"; |
| da0f651 | | | 3 | import { groveApiUrl } from "@/lib/utils"; |
| fe3b509 | | | 4 | import { Badge } from "@/app/components/ui/badge"; |
| fe3b509 | | | 5 | import { TimeAgo } from "@/app/components/time-ago"; |
| fe3b509 | | | 6 | import { |
| fe3b509 | | | 7 | formatTriggerType, |
| fe3b509 | | | 8 | getInvocationRunSlug, |
| fe3b509 | | | 9 | getInvocationStatus, |
| fe3b509 | | | 10 | groupByInvocation, |
| fe3b509 | | | 11 | } from "@/lib/canopy-invocations"; |
| 5bcd5db | | | 12 | import { CanopyLiveRefresh } from "@/app/components/canopy-live-refresh"; |
| da0f651 | | | 13 | |
| da0f651 | | | 14 | interface Props { |
| da0f651 | | | 15 | params: Promise<{ owner: string; repo: string }>; |
| da0f651 | | | 16 | } |
| da0f651 | | | 17 | |
| da0f651 | | | 18 | export async function generateMetadata({ params }: Props): Promise<Metadata> { |
| 86450dc | | | 19 | const { repo } = await params; |
| 86450dc | | | 20 | return { title: `Canopy · ${repo}` }; |
| da0f651 | | | 21 | } |
| da0f651 | | | 22 | |
| fe3b509 | | | 23 | interface Run { |
| fe3b509 | | | 24 | id: number; |
| fe3b509 | | | 25 | pipeline_name: string; |
| fe3b509 | | | 26 | status: string; |
| fe3b509 | | | 27 | trigger_type?: string | null; |
| fe3b509 | | | 28 | commit_id: string | null; |
| fe3b509 | | | 29 | commit_message: string | null; |
| fe3b509 | | | 30 | trigger_ref?: string | null; |
| fe3b509 | | | 31 | started_at?: string | null; |
| fe3b509 | | | 32 | duration_ms?: number | null; |
| fe3b509 | | | 33 | created_at: string; |
| fe3b509 | | | 34 | repo_name: string; |
| fe3b509 | | | 35 | owner_name: string; |
| fe3b509 | | | 36 | } |
| fe3b509 | | | 37 | |
| fe3b509 | | | 38 | const statusLabels: Record<string, string> = { |
| fe3b509 | | | 39 | pending: "Pending", |
| fe3b509 | | | 40 | running: "Running", |
| fe3b509 | | | 41 | passed: "Passed", |
| fe3b509 | | | 42 | failed: "Failed", |
| fe3b509 | | | 43 | cancelled: "Cancelled", |
| fe3b509 | | | 44 | }; |
| fe3b509 | | | 45 | |
| fe3b509 | | | 46 | async function getRepoRuns(owner: string, repo: string): Promise<Run[]> { |
| da0f651 | | | 47 | try { |
| da0f651 | | | 48 | const res = await fetch( |
| fe3b509 | | | 49 | `${groveApiUrl}/api/canopy/recent-runs?owner=${encodeURIComponent(owner)}&limit=50`, |
| fe3b509 | | | 50 | { cache: "no-store" }, |
| da0f651 | | | 51 | ); |
| fe3b509 | | | 52 | if (!res.ok) return []; |
| fe3b509 | | | 53 | const data = await res.json(); |
| fe3b509 | | | 54 | const runs: Run[] = data.runs ?? []; |
| fe3b509 | | | 55 | return runs.filter((r) => r.repo_name === repo); |
| da0f651 | | | 56 | } catch { |
| fe3b509 | | | 57 | return []; |
| da0f651 | | | 58 | } |
| da0f651 | | | 59 | } |
| da0f651 | | | 60 | |
| da0f651 | | | 61 | export default async function CanopyRepoPage({ params }: Props) { |
| da0f651 | | | 62 | const { owner, repo } = await params; |
| fe3b509 | | | 63 | const runs = await getRepoRuns(owner, repo); |
| a011f1e | | | 64 | |
| fe3b509 | | | 65 | if (runs.length === 0) { |
| da0f651 | | | 66 | return ( |
| da0f651 | | | 67 | <div className="max-w-3xl mx-auto px-4 py-6"> |
| da0f651 | | | 68 | <p |
| da0f651 | | | 69 | className="text-sm py-8 text-center" |
| da0f651 | | | 70 | style={{ color: "var(--text-faint)" }} |
| da0f651 | | | 71 | > |
| 087adca | | | 72 | No builds yet. Add a{" "} |
| da0f651 | | | 73 | <code className="text-xs">.canopy/*.yml</code> file to get started. |
| da0f651 | | | 74 | </p> |
| da0f651 | | | 75 | </div> |
| da0f651 | | | 76 | ); |
| da0f651 | | | 77 | } |
| da0f651 | | | 78 | |
| fe3b509 | | | 79 | const invocations = groupByInvocation(runs); |
| fe3b509 | | | 80 | |
| da0f651 | | | 81 | return ( |
| da0f651 | | | 82 | <div className="max-w-3xl mx-auto px-4 py-6"> |
| 5bcd5db | | | 83 | <CanopyLiveRefresh scope="repo" owner={owner} repo={repo} /> |
| fe3b509 | | | 84 | <div> |
| fe3b509 | | | 85 | {invocations.map((invocation) => { |
| fe3b509 | | | 86 | const title = |
| fe3b509 | | | 87 | invocation.commitMessage || |
| fe3b509 | | | 88 | (invocation.commitId |
| fe3b509 | | | 89 | ? invocation.commitId.substring(0, 8) |
| fe3b509 | | | 90 | : `${formatTriggerType(invocation.triggerType)} build`); |
| fe3b509 | | | 91 | |
| fe3b509 | | | 92 | const summaryStatus = getInvocationStatus(invocation.runs); |
| fe3b509 | | | 93 | const newestRun = invocation.runs[invocation.runs.length - 1]; |
| fe3b509 | | | 94 | const invocationHref = `/${owner}/${repo}/runs/${getInvocationRunSlug(invocation, invocations)}`; |
| fe3b509 | | | 95 | |
| fe3b509 | | | 96 | return ( |
| fe3b509 | | | 97 | <Link |
| fe3b509 | | | 98 | key={invocation.key} |
| fe3b509 | | | 99 | href={invocationHref} |
| fe3b509 | | | 100 | className="flex items-start gap-2 sm:gap-3 py-2.5 px-1 hover-row" |
| fe3b509 | | | 101 | style={{ borderBottom: "1px solid var(--divide)", textDecoration: "none", color: "inherit" }} |
| fe3b509 | | | 102 | > |
| fe3b509 | | | 103 | <span className="shrink-0 mt-0.5 sm:hidden"> |
| fe3b509 | | | 104 | <Badge variant={summaryStatus} compact> |
| fe3b509 | | | 105 | {(statusLabels[summaryStatus] ?? summaryStatus).charAt(0)} |
| fe3b509 | | | 106 | </Badge> |
| fe3b509 | | | 107 | </span> |
| fe3b509 | | | 108 | <span className="shrink-0 mt-0.5 hidden sm:block"> |
| fe3b509 | | | 109 | <Badge |
| fe3b509 | | | 110 | variant={summaryStatus} |
| fe3b509 | | | 111 | style={{ minWidth: "4rem", textAlign: "center" }} |
| fe3b509 | | | 112 | > |
| fe3b509 | | | 113 | {statusLabels[summaryStatus] ?? summaryStatus} |
| fe3b509 | | | 114 | </Badge> |
| fe3b509 | | | 115 | </span> |
| fe3b509 | | | 116 | <div className="min-w-0 flex-1"> |
| fe3b509 | | | 117 | <div className="text-sm truncate" style={{ color: "var(--text-primary)" }}> |
| fe3b509 | | | 118 | {title} |
| fe3b509 | | | 119 | </div> |
| fe3b509 | | | 120 | <div className="flex items-center gap-2 mt-0.5 text-xs" style={{ color: "var(--text-faint)" }}> |
| fe3b509 | | | 121 | <span>{formatTriggerType(invocation.triggerType)}</span> |
| fe3b509 | | | 122 | {invocation.commitId && ( |
| fe3b509 | | | 123 | <> |
| fe3b509 | | | 124 | <span>·</span> |
| fe3b509 | | | 125 | <span className="font-mono">{invocation.commitId.substring(0, 8)}</span> |
| fe3b509 | | | 126 | </> |
| fe3b509 | | | 127 | )} |
| fe3b509 | | | 128 | <span>·</span> |
| fe3b509 | | | 129 | <span> |
| fe3b509 | | | 130 | {invocation.runs.length} workflow{invocation.runs.length === 1 ? "" : "s"} |
| fe3b509 | | | 131 | </span> |
| fe3b509 | | | 132 | <span className="ml-auto shrink-0"> |
| fe3b509 | | | 133 | <TimeAgo date={newestRun.created_at} /> |
| fe3b509 | | | 134 | </span> |
| fe3b509 | | | 135 | </div> |
| fe3b509 | | | 136 | </div> |
| fe3b509 | | | 137 | </Link> |
| fe3b509 | | | 138 | ); |
| fe3b509 | | | 139 | })} |
| fe3b509 | | | 140 | </div> |
| da0f651 | | | 141 | </div> |
| da0f651 | | | 142 | ); |
| da0f651 | | | 143 | } |