CiHistory.tsx
| 1 | import type { RepositoryRow } from "../../db/index.ts"; |
| 2 | import { formatDateTime } from "../../lib/formatDate.ts"; |
| 3 | import type { SessionUser } from "../../middleware/session.ts"; |
| 4 | import { Layout } from "../layout.tsx"; |
| 5 | import { Pagination, type PaginationInfo } from "../Pagination.tsx"; |
| 6 | import { RepoHeader } from "../repos/RepoHeader.tsx"; |
| 7 | import { RepoNav } from "../repos/RepoNav.tsx"; |
| 8 | import { CiStatusPill } from "./CiStatusPill.tsx"; |
| 9 | |
| 10 | interface RunSummary { |
| 11 | id: number; |
| 12 | repo_run_id: number | null; |
| 13 | status: string; |
| 14 | trigger_source: string; |
| 15 | commit_sha: string | null; |
| 16 | commit_branch: string | null; |
| 17 | commit_tag: string | null; |
| 18 | started_at: string | null; |
| 19 | finished_at: string | null; |
| 20 | created_at: string; |
| 21 | triggered_by_username: string | null; |
| 22 | artifact_count: number; |
| 23 | queue_position: number | null; |
| 24 | } |
| 25 | |
| 26 | interface CiHistoryProps { |
| 27 | user: SessionUser | null; |
| 28 | repo: RepositoryRow; |
| 29 | runs: RunSummary[]; |
| 30 | pagination: PaginationInfo; |
| 31 | manualTriggerDisabledReason: string | null; |
| 32 | } |
| 33 | |
| 34 | function queueTitle(position: number | null): string { |
| 35 | if (position === null) return "Waiting in the build queue"; |
| 36 | if (position === 1) return "Waiting in the build queue — next up"; |
| 37 | return `Waiting in the build queue — ${position - 1} run${position - 1 === 1 ? "" : "s"} ahead`; |
| 38 | } |
| 39 | |
| 40 | function duration(start: string | null, end: string | null): string { |
| 41 | if (!start || !end) return ""; |
| 42 | const ms = new Date(end).getTime() - new Date(start).getTime(); |
| 43 | if (ms < 0) return ""; |
| 44 | const s = Math.floor(ms / 1000); |
| 45 | if (s < 60) return `${s}s`; |
| 46 | const m = Math.floor(s / 60); |
| 47 | const rem = s % 60; |
| 48 | return rem > 0 ? `${m}m ${rem}s` : `${m}m`; |
| 49 | } |
| 50 | |
| 51 | const CI_VARIABLES = [ |
| 52 | ["CI", "always true"], |
| 53 | ["CI_PIPELINE_ID", "numeric run ID"], |
| 54 | ["CI_COMMIT_SHA", "full commit hash"], |
| 55 | ["CI_COMMIT_SHORT_SHA", "first 8 chars"], |
| 56 | ["CI_COMMIT_BRANCH", "branch name (empty for tags)"], |
| 57 | ["CI_COMMIT_TAG", "tag name (empty for branches)"], |
| 58 | ["CI_COMMIT_REF_NAME", "branch or tag name"], |
| 59 | ["CI_TRIGGER_SOURCE", "push · tag · manual"], |
| 60 | ["CI_REPO_NAME", "repository name"], |
| 61 | ["CI_SERVER_URL", "HearthForge base URL"], |
| 62 | ]; |
| 63 | |
| 64 | function CiHelp({ repo }: { repo: RepositoryRow }) { |
| 65 | return ( |
| 66 | <details class="form-card ci-help"> |
| 67 | <summary class="ci-help-summary"> |
| 68 | <span>How to define a pipeline file</span> |
| 69 | <a |
| 70 | href="/assets/hearthforge-ci-template.toml" |
| 71 | download=".hearthforge-ci.toml" |
| 72 | class="btn btn-sm btn-secondary ci-help-download" |
| 73 | > |
| 74 | Download template |
| 75 | </a> |
| 76 | </summary> |
| 77 | <div class="ci-help-body"> |
| 78 | <p class="ci-help-desc"> |
| 79 | Add <code>.hearthforge-ci.toml</code> to your repository |
| 80 | root. Each <code>[[steps]]</code> entry is a step, executed |
| 81 | in file order, and needs a <code>name</code>. Other tables:{" "} |
| 82 | <code>[on]</code> (triggers), <code>[variables]</code>{" "} |
| 83 | (user-overridable inputs) and <code>[[copy]]</code> (files |
| 84 | taken from another image, like <code>COPY --from</code>). |
| 85 | </p> |
| 86 | <div class="ci-help-sections"> |
| 87 | <div class="ci-help-section"> |
| 88 | <h4 class="ci-help-section-title"> |
| 89 | Predefined variables |
| 90 | </h4> |
| 91 | <dl class="ci-help-vars"> |
| 92 | {CI_VARIABLES.map(([name, desc]) => ( |
| 93 | <> |
| 94 | <dt> |
| 95 | <code safe>{name}</code> |
| 96 | </dt> |
| 97 | <dd safe>{desc}</dd> |
| 98 | </> |
| 99 | ))} |
| 100 | </dl> |
| 101 | </div> |
| 102 | <div class="ci-help-section"> |
| 103 | <h4 class="ci-help-section-title">Status badge</h4> |
| 104 | <p class="ci-help-badge-desc">Embed in your README:</p> |
| 105 | <code |
| 106 | class="ci-help-badge-code" |
| 107 | safe |
| 108 | >{``}</code> |
| 109 | <h4 |
| 110 | class="ci-help-section-title" |
| 111 | style="margin-top: var(--space-4)" |
| 112 | > |
| 113 | Step options |
| 114 | </h4> |
| 115 | <dl class="ci-help-vars"> |
| 116 | {[ |
| 117 | ["run_sh", "shell command to run"], |
| 118 | ["run_if", "skip the step unless this exits 0"], |
| 119 | [ |
| 120 | "always", |
| 121 | "run even after an earlier step failed", |
| 122 | ], |
| 123 | [ |
| 124 | "warn_on_fail", |
| 125 | "step warns instead of failing; the run reports warning", |
| 126 | ], |
| 127 | ["clear", "reset the clone before the step"], |
| 128 | ["timeout", "per-step timeout in seconds"], |
| 129 | ].map(([k, v]) => ( |
| 130 | <> |
| 131 | <dt> |
| 132 | <code safe>{k}</code> |
| 133 | </dt> |
| 134 | <dd safe>{v}</dd> |
| 135 | </> |
| 136 | ))} |
| 137 | </dl> |
| 138 | <h4 |
| 139 | class="ci-help-section-title" |
| 140 | style="margin-top: var(--space-4)" |
| 141 | > |
| 142 | Artifact types |
| 143 | </h4> |
| 144 | <dl class="ci-help-vars"> |
| 145 | {[ |
| 146 | ["publish_file", "copy file as-is"], |
| 147 | ["publish_tar", ".tar archive"], |
| 148 | ["publish_gzip", ".tar.gz archive"], |
| 149 | ["publish_zstd", ".tar.zst archive"], |
| 150 | ["publish_zip", ".zip archive"], |
| 151 | ].map(([k, v]) => ( |
| 152 | <> |
| 153 | <dt> |
| 154 | <code safe>{k}</code> |
| 155 | </dt> |
| 156 | <dd safe>{v}</dd> |
| 157 | </> |
| 158 | ))} |
| 159 | </dl> |
| 160 | </div> |
| 161 | </div> |
| 162 | </div> |
| 163 | </details> |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | export function CiHistory({ |
| 168 | user, |
| 169 | repo, |
| 170 | runs, |
| 171 | pagination, |
| 172 | manualTriggerDisabledReason, |
| 173 | }: CiHistoryProps) { |
| 174 | const isRunning = runs.some( |
| 175 | (r) => |
| 176 | r.status === "pending" || |
| 177 | r.status === "running" || |
| 178 | r.status === "queued", |
| 179 | ); |
| 180 | return ( |
| 181 | <Layout user={user} title={`Pipelines — ${repo.name}`}> |
| 182 | {isRunning ? <meta http-equiv="refresh" content="4" /> : null} |
| 183 | <div class="container"> |
| 184 | <RepoHeader repo={repo} /> |
| 185 | <RepoNav repo={repo} active="ci" user={user} /> |
| 186 | <div class="list-header"> |
| 187 | <h2 class="list-heading">Pipelines</h2> |
| 188 | {user?.isAdmin && ( |
| 189 | <div class="list-header-actions"> |
| 190 | <form |
| 191 | method="POST" |
| 192 | action={`/${repo.name}/ci/purge-cache`} |
| 193 | class="inline-form" |
| 194 | > |
| 195 | <button |
| 196 | type="submit" |
| 197 | class="btn btn-secondary btn-sm" |
| 198 | title="Delete all Docker cache volumes for this repository" |
| 199 | > |
| 200 | Purge caches |
| 201 | </button> |
| 202 | </form> |
| 203 | <form |
| 204 | method="POST" |
| 205 | action={`/${repo.name}/ci/run`} |
| 206 | class="inline-form" |
| 207 | > |
| 208 | <button |
| 209 | type="submit" |
| 210 | class="btn btn-primary btn-sm" |
| 211 | disabled={ |
| 212 | manualTriggerDisabledReason |
| 213 | ? true |
| 214 | : undefined |
| 215 | } |
| 216 | title={ |
| 217 | manualTriggerDisabledReason ?? undefined |
| 218 | } |
| 219 | > |
| 220 | Run pipeline |
| 221 | </button> |
| 222 | </form> |
| 223 | </div> |
| 224 | )} |
| 225 | </div> |
| 226 | {runs.length === 0 ? ( |
| 227 | <div class="empty-state"> |
| 228 | <p>No pipeline runs yet.</p> |
| 229 | <p class="text-muted"> |
| 230 | Push a <code>.hearthforge-ci.toml</code> to your |
| 231 | repository to get started. |
| 232 | </p> |
| 233 | </div> |
| 234 | ) : ( |
| 235 | <ul class="issue-list"> |
| 236 | {runs.map((run) => ( |
| 237 | <li class="issue-item"> |
| 238 | <div class="release-item-header"> |
| 239 | <div class="release-item-main"> |
| 240 | <a |
| 241 | href={`/${repo.name}/ci/${run.id}`} |
| 242 | class="release-item-title" |
| 243 | > |
| 244 | <CiStatusPill |
| 245 | status={run.status} |
| 246 | title={ |
| 247 | run.status === "queued" |
| 248 | ? queueTitle( |
| 249 | run.queue_position, |
| 250 | ) |
| 251 | : undefined |
| 252 | } |
| 253 | /> |
| 254 | <span class="ci-run-id"> |
| 255 | #{run.repo_run_id ?? run.id} |
| 256 | </span> |
| 257 | </a> |
| 258 | <div class="release-item-meta"> |
| 259 | {!!run.commit_sha && ( |
| 260 | <code class="ci-sha" safe> |
| 261 | {run.commit_sha.slice(0, 8)} |
| 262 | </code> |
| 263 | )} |
| 264 | {!!run.commit_branch && ( |
| 265 | <span class="badge" safe> |
| 266 | {run.commit_branch} |
| 267 | </span> |
| 268 | )} |
| 269 | {!!run.commit_tag && ( |
| 270 | <span class="badge" safe> |
| 271 | {run.commit_tag} |
| 272 | </span> |
| 273 | )} |
| 274 | <span class="text-muted" safe> |
| 275 | {run.trigger_source} |
| 276 | </span> |
| 277 | {!!run.triggered_by_username && ( |
| 278 | <span class="text-muted" safe> |
| 279 | by{" "} |
| 280 | {run.triggered_by_username} |
| 281 | </span> |
| 282 | )} |
| 283 | {run.artifact_count > 0 && ( |
| 284 | <span> |
| 285 | {run.artifact_count}{" "} |
| 286 | artifact |
| 287 | {run.artifact_count !== 1 |
| 288 | ? "s" |
| 289 | : ""} |
| 290 | </span> |
| 291 | )} |
| 292 | {!!run.started_at && |
| 293 | !!run.finished_at && ( |
| 294 | <span |
| 295 | class="text-muted" |
| 296 | safe |
| 297 | > |
| 298 | {duration( |
| 299 | run.started_at, |
| 300 | run.finished_at, |
| 301 | )} |
| 302 | </span> |
| 303 | )} |
| 304 | </div> |
| 305 | </div> |
| 306 | <div class="release-item-date"> |
| 307 | <time datetime={run.created_at} safe> |
| 308 | {formatDateTime(run.created_at)} |
| 309 | </time> |
| 310 | </div> |
| 311 | </div> |
| 312 | </li> |
| 313 | ))} |
| 314 | </ul> |
| 315 | )} |
| 316 | <Pagination {...pagination} /> |
| 317 | <CiHelp repo={repo} /> |
| 318 | </div> |
| 319 | </Layout> |
| 320 | ); |
| 321 | } |
| 322 |