CiHistory.tsx
Raw
1import type { RepositoryRow } from "../../db/index.ts";
2import { formatDateTime } from "../../lib/formatDate.ts";
3import type { SessionUser } from "../../middleware/session.ts";
4import { Layout } from "../layout.tsx";
5import { Pagination, type PaginationInfo } from "../Pagination.tsx";
6import { RepoHeader } from "../repos/RepoHeader.tsx";
7import { RepoNav } from "../repos/RepoNav.tsx";
8import { CiStatusPill } from "./CiStatusPill.tsx";
9
10interface 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
26interface CiHistoryProps {
27 user: SessionUser | null;
28 repo: RepositoryRow;
29 runs: RunSummary[];
30 pagination: PaginationInfo;
31 manualTriggerDisabledReason: string | null;
32}
33
34function 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
40function 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
51const 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
64function 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>[section]</code> is a step executed in file
81 order. Reserved tables: <code>[on]</code> (triggers) and{" "}
82 <code>[variables]</code> (user-overridable inputs).
83 </p>
84 <div class="ci-help-sections">
85 <div class="ci-help-section">
86 <h4 class="ci-help-section-title">
87 Predefined variables
88 </h4>
89 <dl class="ci-help-vars">
90 {CI_VARIABLES.map(([name, desc]) => (
91 <>
92 <dt>
93 <code>{name}</code>
94 </dt>
95 <dd>{desc}</dd>
96 </>
97 ))}
98 </dl>
99 </div>
100 <div class="ci-help-section">
101 <h4 class="ci-help-section-title">Status badge</h4>
102 <p class="ci-help-badge-desc">Embed in your README:</p>
103 <code class="ci-help-badge-code">{`![pipeline](/${repo.name}/ci/badge.svg)`}</code>
104 <h4
105 class="ci-help-section-title"
106 style="margin-top: var(--space-4)"
107 >
108 Artifact types
109 </h4>
110 <dl class="ci-help-vars">
111 {[
112 ["publish_file", "copy file as-is"],
113 ["publish_tar", ".tar archive"],
114 ["publish_gzip", ".tar.gz archive"],
115 ["publish_zstd", ".tar.zst archive"],
116 ["publish_zip", ".zip archive"],
117 ].map(([k, v]) => (
118 <>
119 <dt>
120 <code>{k}</code>
121 </dt>
122 <dd>{v}</dd>
123 </>
124 ))}
125 </dl>
126 </div>
127 </div>
128 </div>
129 </details>
130 );
131}
132
133export function CiHistory({
134 user,
135 repo,
136 runs,
137 pagination,
138 manualTriggerDisabledReason,
139}: CiHistoryProps) {
140 const isRunning = runs.some(
141 (r) =>
142 r.status === "pending" ||
143 r.status === "running" ||
144 r.status === "queued",
145 );
146 return (
147 <Layout user={user} title={`Pipelines — ${repo.name}`}>
148 {
149 (isRunning ? (
150 <meta http-equiv="refresh" content="4" />
151 ) : (
152 ""
153 )) as unknown as JSX.Element
154 }
155 <div class="container">
156 <RepoHeader repo={repo} />
157 <RepoNav repo={repo} active="ci" user={user} />
158 <div class="list-header">
159 <h2 class="list-heading">Pipelines</h2>
160 {user?.isAdmin && (
161 <div class="ci-history-actions">
162 <form
163 method="POST"
164 action={`/${repo.name}/ci/purge-cache`}
165 class="inline-form"
166 >
167 <button
168 type="submit"
169 class="btn btn-secondary btn-sm"
170 title="Delete all Docker cache volumes for this repository"
171 >
172 Purge caches
173 </button>
174 </form>
175 <form
176 method="POST"
177 action={`/${repo.name}/ci/run`}
178 class="inline-form"
179 >
180 <button
181 type="submit"
182 class="btn btn-primary btn-sm"
183 disabled={
184 manualTriggerDisabledReason
185 ? true
186 : undefined
187 }
188 title={
189 manualTriggerDisabledReason ?? undefined
190 }
191 >
192 Run pipeline
193 </button>
194 </form>
195 </div>
196 )}
197 </div>
198 {runs.length === 0 ? (
199 <div class="empty-state">
200 <p>No pipeline runs yet.</p>
201 <p class="text-muted">
202 Push a <code>.hearthforge-ci.toml</code> to your
203 repository to get started.
204 </p>
205 </div>
206 ) : (
207 <ul class="issue-list">
208 {runs.map((run) => (
209 <li class="issue-item">
210 <div class="release-item-header">
211 <div class="release-item-main">
212 <a
213 href={`/${repo.name}/ci/${run.id}`}
214 class="release-item-title"
215 >
216 <CiStatusPill
217 status={run.status}
218 title={
219 run.status === "queued"
220 ? queueTitle(
221 run.queue_position,
222 )
223 : undefined
224 }
225 />
226 <span class="ci-run-id">
227 #{run.repo_run_id ?? run.id}
228 </span>
229 </a>
230 <div class="release-item-meta">
231 {run.commit_sha && (
232 <code class="ci-sha">
233 {run.commit_sha.slice(0, 8)}
234 </code>
235 )}
236 {run.commit_branch && (
237 <span class="badge">
238 {run.commit_branch}
239 </span>
240 )}
241 {run.commit_tag && (
242 <span class="badge">
243 {run.commit_tag}
244 </span>
245 )}
246 <span class="text-muted">
247 {run.trigger_source}
248 </span>
249 {run.triggered_by_username && (
250 <span class="text-muted">
251 by{" "}
252 {run.triggered_by_username}
253 </span>
254 )}
255 {run.artifact_count > 0 && (
256 <span>
257 {run.artifact_count}{" "}
258 artifact
259 {run.artifact_count !== 1
260 ? "s"
261 : ""}
262 </span>
263 )}
264 {run.started_at &&
265 run.finished_at && (
266 <span class="text-muted">
267 {duration(
268 run.started_at,
269 run.finished_at,
270 )}
271 </span>
272 )}
273 </div>
274 </div>
275 <div class="release-item-date">
276 <time datetime={run.created_at}>
277 {formatDateTime(run.created_at)}
278 </time>
279 </div>
280 </div>
281 </li>
282 ))}
283 </ul>
284 )}
285 <Pagination {...pagination} />
286 <CiHelp repo={repo} />
287 </div>
288 </Layout>
289 );
290}
291