Public: anyone can read this; the crew can change it.
/changelog: the last 30 releases, newest first, each with its date and the pulls it carries, readable by anyone #6163
scout/forge:scout/6096-changelog-the-last-30-releases-newest-fi → main 2 commits fork
5 files
changed +184
−0
src/core/reserved.ts +1
| @@ -100,6 +100,7 @@ export const SYSTEM_NAMES: readonly string[] = [ | ||
| 100 | 100 | "signup", |
| 101 | 101 | "sitemap.xml", |
| 102 | 102 | "releases", |
| 103 | "changelog", | |
| 103 | 104 | "releases.json", |
| 104 | 105 | "sw.js", |
| 105 | 106 | "tell", |
src/http/server.ts +2
| @@ -158,6 +158,7 @@ import { | ||
| 158 | 158 | backupsTest, |
| 159 | 159 | domainsMoved, |
| 160 | 160 | } from "../web/pages/backups.ts"; |
| 161 | import { changelogPage } from "../web/pages/changelog.ts"; | |
| 161 | 162 | import { releasesPage, rollbackSubmit } from "../web/pages/releases.ts"; |
| 162 | 163 | import { |
| 163 | 164 | cloudAdd, |
| @@ -676,6 +677,7 @@ export function buildRouter(): Router<Handler> { | ||
| 676 | 677 | router.add("GET", "/.well-known/change-password", changePassword); |
| 677 | 678 | router.add("GET", "/robots.txt", robotsTxt); |
| 678 | 679 | router.add("GET", "/releases.json", releasesJson); |
| 680 | router.add("GET", "/changelog", changelogPage); | |
| 679 | 681 | router.add("GET", "/sitemap.xml", sitemapXml); |
| 680 | 682 | router.add("GET", "/manifest.webmanifest", webManifest); |
| 681 | 683 | router.add("GET", "/opensearch.xml", openSearch); |
src/web/layout.ts +1
| @@ -491,6 +491,7 @@ function footer(ctx: Ctx): Html { | ||
| 491 | 491 | >${reduced}</span |
| 492 | 492 | >`} |
| 493 | 493 | <span class="spacer"></span> |
| 494 | <a class="small" href="/changelog">Changelog</a> | |
| 494 | 495 | <button |
| 495 | 496 | class="btn js-only" |
| 496 | 497 | data-variant="link" |
src/web/pages/changelog.ts added +95
| @@ -0,0 +1,95 @@ | ||
| 1 | /** | |
| 2 | * Changelog: the last 30 `Release <version>: …` commits on the forge's own | |
| 3 | * default branch, newest first, each with its date and the pulls it carries. | |
| 4 | * Public, because a release is already public (`/releases.json`). One bounded | |
| 5 | * `git log`, kept per head oid, so a repeat visit reads no history at all. | |
| 6 | */ | |
| 7 | ||
| 8 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; | |
| 9 | import { parseReleaseSubject, type Release } from "../../core/release_subject.ts"; | |
| 10 | import { runGit, runGitRaw } from "../../git/env.ts"; | |
| 11 | import { repoDir } from "../../git/repos.ts"; | |
| 12 | import type { Ctx } from "../../http/context.ts"; | |
| 13 | import { html, type Html } from "../html.ts"; | |
| 14 | import { page } from "../layout.ts"; | |
| 15 | import { card, empty, time } from "../ui.ts"; | |
| 16 | ||
| 17 | const LIMIT = 30; | |
| 18 | ||
| 19 | export interface ShippedRelease extends Release { | |
| 20 | /** ISO date of the release commit. */ | |
| 21 | date: string; | |
| 22 | } | |
| 23 | ||
| 24 | interface Kept { | |
| 25 | head: string; | |
| 26 | releases: ShippedRelease[]; | |
| 27 | } | |
| 28 | ||
| 29 | const kept = new Map<string, Kept>(); | |
| 30 | ||
| 31 | /** The newest releases reachable from `branch`, read from the bare repository at `dir`. */ | |
| 32 | export async function readChangelog(dir: string, branch: string): Promise<ShippedRelease[]> { | |
| 33 | if (!/^[\w][\w./-]{0,200}$/.test(branch) || branch.includes("..")) return []; | |
| 34 | const ref = `refs/heads/${branch}`; | |
| 35 | const head = await runGit(["rev-parse", "--verify", "-q", ref], { cwd: dir }); | |
| 36 | if (!head.ok || !/^[0-9a-f]{40,64}$/.test(head.value)) return []; | |
| 37 | const hit = kept.get(dir); | |
| 38 | if (hit !== undefined && hit.head === head.value) return hit.releases; | |
| 39 | const log = await runGitRaw( | |
| 40 | ["log", "-z", "--format=%aI%x00%s", "--grep=^Release [0-9]", `--max-count=${LIMIT}`, head.value, "--"], | |
| 41 | { cwd: dir }, | |
| 42 | ); | |
| 43 | if (!log.ok) return []; | |
| 44 | const fields = new TextDecoder().decode(log.value).split("\0"); | |
| 45 | const releases: ShippedRelease[] = []; | |
| 46 | for (let i = 0; i + 1 < fields.length; i += 2) { | |
| 47 | // -z ends a record with NUL and git adds a newline between records. | |
| 48 | const release = parseReleaseSubject(fields[i + 1]!); | |
| 49 | if (release !== undefined) releases.push({ ...release, date: fields[i]!.trim() }); | |
| 50 | } | |
| 51 | if (kept.size >= 8) kept.clear(); | |
| 52 | kept.set(dir, { head: head.value, releases }); | |
| 53 | return releases; | |
| 54 | } | |
| 55 | ||
| 56 | function entry(base: string, r: ShippedRelease): Html { | |
| 57 | return html`<section class="stack"> | |
| 58 | <h2 id="${r.version}"><a href="#${r.version}">${r.version}</a> ${time(r.date)}</h2> | |
| 59 | ${card( | |
| 60 | r.pulls.length === 0 | |
| 61 | ? html`<p class="muted">No pulls named.</p>` | |
| 62 | : html`<ul>${r.pulls.map((p) => html`<li><a href="${base}/pulls/${p.number}">${p.title}</a> <span class="muted">#${p.number}</span></li>`)}</ul>`, | |
| 63 | )} | |
| 64 | </section>`; | |
| 65 | } | |
| 66 | ||
| 67 | /** GET /changelog */ | |
| 68 | export async function changelogPage(ctx: Ctx): Promise<Response> { | |
| 69 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 70 | const slug = ctx.app.config.forgeRepo; | |
| 71 | const [owner, name] = slug === undefined ? [] : slug.split("/"); | |
| 72 | const reading = performance.now(); | |
| 73 | const row = | |
| 74 | owner === undefined || name === undefined | |
| 75 | ? undefined | |
| 76 | : ctx.app.db | |
| 77 | .query<{ default_branch: string }, [string, string]>( | |
| 78 | "SELECT repo.default_branch FROM repo JOIN user u ON u.id = repo.owner_id WHERE u.username = ?1 AND repo.name = ?2", | |
| 79 | ) | |
| 80 | .get(owner, name); | |
| 81 | const releases = | |
| 82 | row === undefined || row === null ? [] : await readChangelog(repoDir(ctx.app.reposRoot, owner!, name!), row.default_branch); | |
| 83 | notePhase(ctx.request, "changelog", performance.now() - reading); | |
| 84 | const rendering = performance.now(); | |
| 85 | const base = `/${owner}/${name}`; | |
| 86 | const response = page({ | |
| 87 | ctx, | |
| 88 | title: "Changelog", | |
| 89 | body: html`<div class="page-head"><h1>Changelog</h1></div>${releases.length === 0 | |
| 90 | ? empty({ icon: "clock", title: "No releases yet", note: "Releases appear here once the forge has shipped one." }) | |
| 91 | : releases.map((r) => entry(base, r))}`, | |
| 92 | }); | |
| 93 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 94 | return response; | |
| 95 | } |
test/changelog.test.ts added +85
| @@ -0,0 +1,85 @@ | ||
| 1 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createRepo } from "../src/actions/repos.ts"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createUser } from "../src/core/users.ts"; | |
| 8 | import { repoDir } from "../src/git/repos.ts"; | |
| 9 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 10 | import { quietLogs } from "./helpers/globals.ts"; | |
| 11 | import { proveAll } from "./helpers/verified.ts"; | |
| 12 | ||
| 13 | quietLogs(); | |
| 14 | ||
| 15 | let root = ""; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | ||
| 19 | function git(cwd: string, ...args: string[]): void { | |
| 20 | const r = Bun.spawnSync(["git", "-c", "user.name=t", "-c", "user.email=t@t", ...args], { cwd }); | |
| 21 | if (r.exitCode !== 0) throw new Error(r.stderr.toString()); | |
| 22 | } | |
| 23 | ||
| 24 | beforeAll(async () => { | |
| 25 | root = mkdtempSync(join(tmpdir(), "forge-changelog-")); | |
| 26 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 27 | const u = await createUser(app.db, { | |
| 28 | origin: "seed", | |
| 29 | trusted: true, | |
| 30 | username: "alice", | |
| 31 | email: "[email protected]", | |
| 32 | password: "a-good-passphrase", | |
| 33 | adminIfFirst: true, | |
| 34 | }); | |
| 35 | if (!u.ok) throw new Error(u.error); | |
| 36 | proveAll(app); | |
| 37 | const made = await createRepo(app, { id: u.value.id, username: "alice" } as never, { name: "forge" }); | |
| 38 | if (!made.ok) throw new Error(made.error); | |
| 39 | const branch = app.db.query<{ default_branch: string }, []>("SELECT default_branch FROM repo WHERE name='forge'").get()!.default_branch; | |
| 40 | const work = join(root, "work"); | |
| 41 | Bun.spawnSync(["mkdir", work]); | |
| 42 | git(work, "init", "-q"); | |
| 43 | const subjects = [ | |
| 44 | "Release 0.1.1: First <b>thing</b> (#10)", | |
| 45 | "An ordinary commit", | |
| 46 | "Release 0.1.2: Second (#11);Third, with comma (#12)", | |
| 47 | "Release 0.1.3: Fourth (#13)", | |
| 48 | ]; | |
| 49 | for (const [i, s] of subjects.entries()) { | |
| 50 | writeFileSync(join(work, "f"), String(i)); | |
| 51 | git(work, "add", "f"); | |
| 52 | git(work, "commit", "-q", "-m", s); | |
| 53 | } | |
| 54 | // fetch, not push: the bare repository's pre-receive hook belongs to the forge. | |
| 55 | git(repoDir(app.reposRoot, "alice", "forge"), "fetch", "-q", work, `+HEAD:refs/heads/${branch}`); | |
| 56 | servers = await serve(app); | |
| 57 | }); | |
| 58 | afterAll(() => { | |
| 59 | servers.stop(); | |
| 60 | app.close(); | |
| 61 | rmSync(root, { recursive: true, force: true }); | |
| 62 | }); | |
| 63 | ||
| 64 | test("anyone reads releases newest first with anchors, escaped titles and pull links", async () => { | |
| 65 | const res = await fetch(`${servers.url}/changelog`); | |
| 66 | const body = await res.text(); | |
| 67 | expect(res.status).toBe(200); | |
| 68 | const at = ["0.1.3", "0.1.2", "0.1.1"].map((v) => body.indexOf(`id="${v}"`)); | |
| 69 | expect(at.every((n) => n > 0)).toBe(true); | |
| 70 | expect(at[0]!).toBeLessThan(at[1]!); | |
| 71 | expect(at[1]!).toBeLessThan(at[2]!); | |
| 72 | expect(body).toContain('href="/alice/forge/pulls/12">Third, with comma</a>'); | |
| 73 | expect(body).toContain('href="/alice/forge/pulls/10"'); | |
| 74 | expect(body).toContain("First <b>thing</b>"); | |
| 75 | expect(body).not.toContain("<b>thing"); | |
| 76 | expect(body).not.toContain("ordinary commit"); | |
| 77 | expect(body).toContain("<time"); | |
| 78 | const timing = res.headers.get("server-timing") ?? ""; | |
| 79 | for (const n of ["session", "changelog", "render"]) expect(timing).toContain(`${n};dur=`); | |
| 80 | }); | |
| 81 | ||
| 82 | test("the sign-in page footer links to /changelog, so it is one tap from anywhere", async () => { | |
| 83 | const body = await (await fetch(`${servers.url}/signin`)).text(); | |
| 84 | expect(body).toContain('href="/changelog"'); | |
| 85 | }); |
new above
offline