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 safe>{name}</code>
94 </dt>
95 <dd safe>{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" safe>{`![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 safe>{k}</code>
121 </dt>
122 <dd safe>{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 {isRunning ? (
149 <meta http-equiv="refresh" content="4" />
150 ) : null}
151 <div class="container">
152 <RepoHeader repo={repo} />
153 <RepoNav repo={repo} active="ci" user={user} />
154 <div class="list-header">
155 <h2 class="list-heading">Pipelines</h2>
156 {user?.isAdmin && (
157 <div class="ci-history-actions">
158 <form
159 method="POST"
160 action={`/${repo.name}/ci/purge-cache`}
161 class="inline-form"
162 >
163 <button
164 type="submit"
165 class="btn btn-secondary btn-sm"
166 title="Delete all Docker cache volumes for this repository"
167 >
168 Purge caches
169 </button>
170 </form>
171 <form
172 method="POST"
173 action={`/${repo.name}/ci/run`}
174 class="inline-form"
175 >
176 <button
177 type="submit"
178 class="btn btn-primary btn-sm"
179 disabled={
180 manualTriggerDisabledReason
181 ? true
182 : undefined
183 }
184 title={
185 manualTriggerDisabledReason ?? undefined
186 }
187 >
188 Run pipeline
189 </button>
190 </form>
191 </div>
192 )}
193 </div>
194 {runs.length === 0 ? (
195 <div class="empty-state">
196 <p>No pipeline runs yet.</p>
197 <p class="text-muted">
198 Push a <code>.hearthforge-ci.toml</code> to your
199 repository to get started.
200 </p>
201 </div>
202 ) : (
203 <ul class="issue-list">
204 {runs.map((run) => (
205 <li class="issue-item">
206 <div class="release-item-header">
207 <div class="release-item-main">
208 <a
209 href={`/${repo.name}/ci/${run.id}`}
210 class="release-item-title"
211 >
212 <CiStatusPill
213 status={run.status}
214 title={
215 run.status === "queued"
216 ? queueTitle(
217 run.queue_position,
218 )
219 : undefined
220 }
221 />
222 <span class="ci-run-id">
223 #{run.repo_run_id ?? run.id}
224 </span>
225 </a>
226 <div class="release-item-meta">
227 {!!run.commit_sha && (
228 <code class="ci-sha" safe>
229 {run.commit_sha.slice(0, 8)}
230 </code>
231 )}
232 {!!run.commit_branch && (
233 <span class="badge" safe>
234 {run.commit_branch}
235 </span>
236 )}
237 {!!run.commit_tag && (
238 <span class="badge" safe>
239 {run.commit_tag}
240 </span>
241 )}
242 <span class="text-muted" safe>
243 {run.trigger_source}
244 </span>
245 {!!run.triggered_by_username && (
246 <span class="text-muted" safe>
247 by {run.triggered_by_username}
248 </span>
249 )}
250 {run.artifact_count > 0 && (
251 <span>
252 {run.artifact_count}{" "}
253 artifact
254 {run.artifact_count !== 1
255 ? "s"
256 : ""}
257 </span>
258 )}
259 {!!run.started_at &&
260 !!run.finished_at && (
261 <span class="text-muted" safe>
262 {duration(
263 run.started_at,
264 run.finished_at,
265 )}
266 </span>
267 )}
268 </div>
269 </div>
270 <div class="release-item-date">
271 <time datetime={run.created_at} safe>
272 {formatDateTime(run.created_at)}
273 </time>
274 </div>
275 </div>
276 </li>
277 ))}
278 </ul>
279 )}
280 <Pagination {...pagination} />
281 <CiHelp repo={repo} />
282 </div>
283 </Layout>
284 );
285}
286