Public: anyone can read this; the crew can change it.
/:owner/:name/waves lists a repository's waves newest first, and /waves/<version> renders one as a note #1532
scout/forge:scout/1507-owner-name-waves-lists-a-repository-s-wa → main 3 commits fork
5 files
changed +297
−0
README.md +5
| @@ -610,6 +610,11 @@ work here. | ||
| 610 | 610 | - **Automation** — a JSON API covering repos, issues, forks, and orgs (the |
| 611 | 611 | label-as-queue surface), a durable event outbox, and **webhooks** (HMAC-signed |
| 612 | 612 | delivery with retry). |
| 613 | - **Waves** — a file at `waves/<version>.md` on the default branch appears at | |
| 614 | `/<owner>/<name>/waves`, newest first, and `/<owner>/<name>/waves/<version>` | |
| 615 | renders one wave as a note. The format is in `src/story/wave.ts`. Private | |
| 616 | repositories keep their waves private. Not yet in the terminal. | |
| 617 | ||
| 613 | 618 | - **Removing an account** — `DELETE /api/v1/admin/accounts/:username` (admin |
| 614 | 619 | only). An account that is the last owner of an organization answers 409 with |
| 615 | 620 | the organizations named. Send `{"retireOrgs": true}` to preview: the answer |
scripts/gate.ts +1
| @@ -59,6 +59,7 @@ export const GATE: readonly string[] = [ | ||
| 59 | 59 | "test/review-from-green.test.ts", |
| 60 | 60 | "test/themes.test.ts", |
| 61 | 61 | "test/waves.test.ts", |
| 62 | "test/waves-page.test.ts", | |
| 62 | 63 | "test/crew-wrapup.test.ts", |
| 63 | 64 | "test/crew-limits.test.ts", |
| 64 | 65 | "test/crew-parks.test.ts", |
src/http/server.ts +3
| @@ -140,6 +140,7 @@ import { | ||
| 140 | 140 | branchDelete, |
| 141 | 141 | branchesPage, |
| 142 | 142 | } from "../web/pages/branches.ts"; |
| 143 | import { wavePage, wavesPage } from "../web/pages/waves.ts"; | |
| 143 | 144 | import { |
| 144 | 145 | contributeSubmit, |
| 145 | 146 | deleteFilePage, |
| @@ -1024,6 +1025,8 @@ export function buildRouter(): Router<Handler> { | ||
| 1024 | 1025 | router.add("GET", "/:owner/:name/forks", forksPage); |
| 1025 | 1026 | // The repository's activity, for a feed reader. |
| 1026 | 1027 | router.add("GET", "/:owner/:name/activity.atom", activityAtom); |
| 1028 | router.add("GET", "/:owner/:name/waves", wavesPage); | |
| 1029 | router.add("GET", "/:owner/:name/waves/:version", wavePage); | |
| 1027 | 1030 | router.add("GET", "/:owner/:name/branches", branchesPage); |
| 1028 | 1031 | router.add("POST", "/:owner/:name/branches", branchCreate); |
| 1029 | 1032 | router.add("POST", "/:owner/:name/branches/:branch/delete", branchDelete); |
src/web/pages/waves.ts added +149
| @@ -0,0 +1,149 @@ | ||
| 1 | /** | |
| 2 | * Waves, as pages: `/:owner/:name/waves` lists the notes in `waves/*.md` on | |
| 3 | * the default branch, newest first; `/:owner/:name/waves/<version>` renders | |
| 4 | * one. The file format is `story/wave.ts`; this reads it from git and shows | |
| 5 | * it. A file that does not parse is listed with its reason and breaks | |
| 6 | * nothing. Visibility is the repository's: `loadRepo` answers an outsider | |
| 7 | * with the same 404 as any other page of a private repository. | |
| 8 | */ | |
| 9 | ||
| 10 | import type { Repo } from "../../core/repos.ts"; | |
| 11 | import { blobForRender, listTree } from "../../git/read.ts"; | |
| 12 | import { repoDir } from "../../git/repos.ts"; | |
| 13 | import type { Ctx } from "../../http/context.ts"; | |
| 14 | import { parseWave, wavePath, type Wave } from "../../story/wave.ts"; | |
| 15 | import { crumbsFor, loadRepo, notFoundPage, repoBase, repoHeader } from "../chrome.ts"; | |
| 16 | import { html, type Html } from "../html.ts"; | |
| 17 | import { page } from "../layout.ts"; | |
| 18 | import { empty } from "../ui.ts"; | |
| 19 | ||
| 20 | const MAX_WAVE_BYTES = 64 * 1024; | |
| 21 | const MAX_WAVES = 500; | |
| 22 | const VERSION = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/; | |
| 23 | ||
| 24 | export type WaveEntry = | |
| 25 | | { ok: true; file: string; wave: Wave } | |
| 26 | | { ok: false; file: string; reason: string }; | |
| 27 | ||
| 28 | const core = (v: string): number[] => v.split("-")[0]!.split(".").map(Number); | |
| 29 | ||
| 30 | /** Newest first: by date, then by version number, then by name. */ | |
| 31 | export function compareWaves(a: Wave, b: Wave): number { | |
| 32 | if (a.date !== b.date) return a.date < b.date ? 1 : -1; | |
| 33 | const x = core(a.version); | |
| 34 | const y = core(b.version); | |
| 35 | for (let i = 0; i < 3; i++) if (x[i] !== y[i]) return y[i]! - x[i]!; | |
| 36 | return a.version < b.version ? 1 : a.version > b.version ? -1 : 0; | |
| 37 | } | |
| 38 | ||
| 39 | /** Valid waves newest first, then the files that did not parse, by name. */ | |
| 40 | export function orderWaves(entries: readonly WaveEntry[]): WaveEntry[] { | |
| 41 | const good = entries.filter((e): e is WaveEntry & { ok: true } => e.ok); | |
| 42 | const bad = entries.filter((e) => !e.ok); | |
| 43 | good.sort((a, b) => compareWaves(a.wave, b.wave)); | |
| 44 | bad.sort((a, b) => (a.file < b.file ? -1 : a.file > b.file ? 1 : 0)); | |
| 45 | return [...good, ...bad]; | |
| 46 | } | |
| 47 | ||
| 48 | async function readEntry(dir: string, rev: string, file: string): Promise<WaveEntry> { | |
| 49 | const path = `waves/${file}`; | |
| 50 | const blob = await blobForRender(dir, rev, path, MAX_WAVE_BYTES); | |
| 51 | if (blob.kind === "too_large") return { ok: false, file, reason: `${path}: file is too large` }; | |
| 52 | if (blob.kind !== "text") return { ok: false, file, reason: `${path}: not a text file` }; | |
| 53 | const parsed = parseWave(path, new TextDecoder().decode(blob.body)); | |
| 54 | if (!parsed.ok) return { ok: false, file, reason: parsed.error.message }; | |
| 55 | if (wavePath(parsed.value.version) !== path) | |
| 56 | return { ok: false, file, reason: `${path}: file name does not match version ${parsed.value.version}` }; | |
| 57 | return { ok: true, file, wave: parsed.value }; | |
| 58 | } | |
| 59 | ||
| 60 | async function readWaves(repo: Repo, ctx: Ctx): Promise<WaveEntry[]> { | |
| 61 | const dir = repoDir(ctx.app.reposRoot, repo.ownerName, repo.name); | |
| 62 | const tree = await listTree(dir, repo.defaultBranch, "waves"); | |
| 63 | if (!tree.ok) return []; | |
| 64 | const files = tree.value | |
| 65 | .filter((e) => e.type === "blob" && e.name.endsWith(".md")) | |
| 66 | .map((e) => e.name) | |
| 67 | .slice(0, MAX_WAVES); | |
| 68 | return orderWaves(await Promise.all(files.map((f) => readEntry(dir, repo.defaultBranch, f)))); | |
| 69 | } | |
| 70 | ||
| 71 | const waveHref = (repo: Repo, version: string): string => | |
| 72 | `${repoBase(repo)}/waves/${encodeURIComponent(version)}`; | |
| 73 | ||
| 74 | function row(repo: Repo, e: WaveEntry): Html { | |
| 75 | if (!e.ok) | |
| 76 | return html`<li class="row"><span class="row-main" | |
| 77 | ><span class="row-title mono">${e.file}</span | |
| 78 | ><span class="row-meta">Could not be read: ${e.reason}</span></span></li>`; | |
| 79 | const w = e.wave; | |
| 80 | return html`<li class="row"><span class="row-main" | |
| 81 | ><span class="row-title"><a href="${waveHref(repo, w.version)}">${w.version}</a> | |
| 82 | — ${w.headline}</span | |
| 83 | ><span class="row-meta">${w.date}</span></span></li>`; | |
| 84 | } | |
| 85 | ||
| 86 | // GET /:owner/:name/waves | |
| 87 | export async function wavesPage(ctx: Ctx): Promise<Response> { | |
| 88 | const repo = loadRepo(ctx); | |
| 89 | if (!repo) return notFoundPage(ctx); | |
| 90 | const entries = await readWaves(repo, ctx); | |
| 91 | const body = html`${repoHeader(ctx, repo, "code", { compact: true })} | |
| 92 | <div class="mt"> | |
| 93 | <h2>Waves</h2> | |
| 94 | ${entries.length === 0 | |
| 95 | ? empty({ | |
| 96 | icon: "search", | |
| 97 | title: "No waves yet", | |
| 98 | note: "A wave is a file at waves/<version>.md on the default branch.", | |
| 99 | }) | |
| 100 | : html`<ul class="list">${entries.map((e) => row(repo, e))}</ul>`} | |
| 101 | </div>`; | |
| 102 | return page({ | |
| 103 | ctx, | |
| 104 | title: `Waves · ${repo.name}`, | |
| 105 | crumbs: crumbsFor(repo), | |
| 106 | body, | |
| 107 | }); | |
| 108 | } | |
| 109 | ||
| 110 | // GET /:owner/:name/waves/:version | |
| 111 | export async function wavePage(ctx: Ctx): Promise<Response> { | |
| 112 | const repo = loadRepo(ctx); | |
| 113 | if (!repo) return notFoundPage(ctx); | |
| 114 | const version = ctx.params.version ?? ""; | |
| 115 | if (!VERSION.test(version)) return notFoundPage(ctx); | |
| 116 | const dir = repoDir(ctx.app.reposRoot, repo.ownerName, repo.name); | |
| 117 | const blobTree = await listTree(dir, repo.defaultBranch, "waves"); | |
| 118 | const file = `${version}.md`; | |
| 119 | if (!blobTree.ok || !blobTree.value.some((e) => e.type === "blob" && e.name === file)) | |
| 120 | return notFoundPage(ctx); | |
| 121 | const e = await readEntry(dir, repo.defaultBranch, file); | |
| 122 | const back = html`<p><a href="${repoBase(repo)}/waves">← All waves</a></p>`; | |
| 123 | const body = e.ok | |
| 124 | ? html`${repoHeader(ctx, repo, "code", { compact: true })} | |
| 125 | <article class="mt"> | |
| 126 | ${back} | |
| 127 | <h1>${e.wave.version} — ${e.wave.headline}</h1> | |
| 128 | <p class="muted">${e.wave.date}</p> | |
| 129 | <h2>Highlights</h2> | |
| 130 | <ul>${e.wave.highlights.map( | |
| 131 | (h) => html`<li><strong>${h.title}</strong> — ${h.why}</li>`, | |
| 132 | )}</ul> | |
| 133 | <h2>Pulls</h2> | |
| 134 | ${e.wave.pulls.length === 0 | |
| 135 | ? html`<p class="muted">No pulls listed.</p>` | |
| 136 | : html`<ul>${e.wave.pulls.map( | |
| 137 | (p) => html`<li><a href="${p.url}" rel="noopener noreferrer">${p.title}</a></li>`, | |
| 138 | )}</ul>`} | |
| 139 | </article>` | |
| 140 | : html`${repoHeader(ctx, repo, "code", { compact: true })} | |
| 141 | <article class="mt">${back}<h1>${version}</h1> | |
| 142 | <p>Could not be read: ${e.reason}</p></article>`; | |
| 143 | return page({ | |
| 144 | ctx, | |
| 145 | title: `${version} · Waves · ${repo.name}`, | |
| 146 | crumbs: crumbsFor(repo), | |
| 147 | body, | |
| 148 | }); | |
| 149 | } |
test/waves-page.test.ts added +139
| @@ -0,0 +1,139 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp, type App } from "../src/app.ts"; | |
| 6 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 7 | import { writeWave, type Wave } from "../src/story/wave.ts"; | |
| 8 | import { orderWaves } from "../src/web/pages/waves.ts"; | |
| 9 | ||
| 10 | const wave = (version: string, date: string, headline: string): Wave => ({ | |
| 11 | version, | |
| 12 | date, | |
| 13 | headline, | |
| 14 | highlights: [1, 2, 3].map((n) => ({ title: `T${n}`, why: `because ${n}` })), | |
| 15 | pulls: [{ title: "#1 First", url: "https://forge.example/a/b/pulls/1" }], | |
| 16 | }); | |
| 17 | const text = (w: Wave): string => { | |
| 18 | const r = writeWave(w); | |
| 19 | if (!r.ok) throw r.error; | |
| 20 | return r.value; | |
| 21 | }; | |
| 22 | ||
| 23 | describe("waves pages", () => { | |
| 24 | let dataDir: string; | |
| 25 | let app: App; | |
| 26 | let servers: Servers; | |
| 27 | let base: string; | |
| 28 | let alice = ""; | |
| 29 | let bob = ""; | |
| 30 | ||
| 31 | const call = (token: string | undefined, path: string, init?: RequestInit) => | |
| 32 | fetch(`${base}${path}`, { | |
| 33 | ...init, | |
| 34 | headers: { | |
| 35 | "content-type": "application/json", | |
| 36 | ...(token ? { authorization: `Bearer ${token}` } : {}), | |
| 37 | }, | |
| 38 | }); | |
| 39 | const signUp = async (username: string, password: string) => { | |
| 40 | await fetch(`${base}/api/v1/users`, { | |
| 41 | method: "POST", | |
| 42 | headers: { "content-type": "application/json" }, | |
| 43 | body: JSON.stringify({ username, email: `${username}@forge.test`, password }), | |
| 44 | }); | |
| 45 | const r = await fetch(`${base}/api/v1/tokens`, { | |
| 46 | method: "POST", | |
| 47 | headers: { "content-type": "application/json" }, | |
| 48 | body: JSON.stringify({ adminConfirm: true, username, password, name: "t", scope: "rw" }), | |
| 49 | }); | |
| 50 | return ((await r.json()) as { token: string }).token; | |
| 51 | }; | |
| 52 | const files = { | |
| 53 | "waves/0.7.1.md": text(wave("0.7.1", "2026-10-01", "Older")), | |
| 54 | "waves/0.7.3.md": text(wave("0.7.3", "2026-10-10", "<script>alert(1)</script> newest")), | |
| 55 | "waves/0.7.2.md": text(wave("0.7.2", "2026-10-05", "Middle")), | |
| 56 | "waves/0.7.9.md": "not a wave <b>", | |
| 57 | }; | |
| 58 | const seed = async (repo: string, visibility: string) => { | |
| 59 | await call(alice, "/api/v1/repos", { | |
| 60 | method: "POST", | |
| 61 | body: JSON.stringify({ name: repo, visibility }), | |
| 62 | }); | |
| 63 | await call(alice, `/api/v1/repos/alice/${repo}/contents`, { | |
| 64 | method: "POST", | |
| 65 | body: JSON.stringify({ | |
| 66 | branch: "main", | |
| 67 | message: "waves", | |
| 68 | files: Object.entries(files).map(([path, content]) => ({ path, content })), | |
| 69 | }), | |
| 70 | }); | |
| 71 | }; | |
| 72 | ||
| 73 | beforeAll(async () => { | |
| 74 | dataDir = await mkdtemp(join(tmpdir(), "forge-waves-")); | |
| 75 | app = createApp({ dataDir, port: 0 }); | |
| 76 | servers = await serve(app); | |
| 77 | base = servers.url; | |
| 78 | alice = await signUp("alice", "alice-waves-secret"); | |
| 79 | bob = await signUp("bob", "bob-waves-secret"); | |
| 80 | await seed("pub", "public"); | |
| 81 | await seed("priv", "private"); | |
| 82 | await call(alice, "/api/v1/repos", { | |
| 83 | method: "POST", | |
| 84 | body: JSON.stringify({ name: "none", visibility: "public" }), | |
| 85 | }); | |
| 86 | }); | |
| 87 | afterAll(async () => { | |
| 88 | servers.stop(); | |
| 89 | app.close(); | |
| 90 | await rm(dataDir, { recursive: true, force: true }); | |
| 91 | }); | |
| 92 | ||
| 93 | test("orderWaves puts newest first and unparsable files last", () => { | |
| 94 | const ok = (v: string, d: string) => ({ ok: true as const, file: `${v}.md`, wave: wave(v, d, "h") }); | |
| 95 | const bad = { ok: false as const, file: "a.md", reason: "r" }; | |
| 96 | const out = orderWaves([ok("0.7.1", "2026-10-01"), bad, ok("0.7.10", "2026-10-01"), ok("0.7.3", "2026-10-09")]); | |
| 97 | expect(out.map((e) => e.file)).toEqual(["0.7.3.md", "0.7.10.md", "0.7.1.md", "a.md"]); | |
| 98 | }); | |
| 99 | ||
| 100 | test("the list is newest first, escaped, and shows the bad file's reason", async () => { | |
| 101 | const r = await call(undefined, "/alice/pub/waves"); | |
| 102 | expect(r.status).toBe(200); | |
| 103 | const h = await r.text(); | |
| 104 | expect(h.indexOf("0.7.3")).toBeLessThan(h.indexOf("0.7.2")); | |
| 105 | expect(h.indexOf("0.7.2")).toBeLessThan(h.indexOf("0.7.1")); | |
| 106 | expect(h).not.toContain("<script>alert(1)</script>"); | |
| 107 | expect(h).toContain("<script>"); | |
| 108 | expect(h).toContain("0.7.9.md"); | |
| 109 | expect(h).toContain("missing front matter"); | |
| 110 | }); | |
| 111 | ||
| 112 | test("a repository without waves lists none", async () => { | |
| 113 | const r = await call(undefined, "/alice/none/waves"); | |
| 114 | expect(r.status).toBe(200); | |
| 115 | expect(await r.text()).toContain("No waves yet"); | |
| 116 | }); | |
| 117 | ||
| 118 | test("one wave renders its highlights and pulls; unknown is 404", async () => { | |
| 119 | const r = await call(undefined, "/alice/pub/waves/0.7.3"); | |
| 120 | expect(r.status).toBe(200); | |
| 121 | const h = await r.text(); | |
| 122 | expect(h).toContain("because 2"); | |
| 123 | expect(h).toContain('href="https://forge.example/a/b/pulls/1"'); | |
| 124 | expect(h).not.toContain("<script>alert(1)</script>"); | |
| 125 | expect((await call(undefined, "/alice/pub/waves/9.9.9")).status).toBe(404); | |
| 126 | expect((await call(undefined, "/alice/pub/waves/..%2Fx")).status).toBe(404); | |
| 127 | const bad = await call(undefined, "/alice/pub/waves/0.7.9"); | |
| 128 | expect(bad.status).toBe(200); | |
| 129 | expect(await bad.text()).toContain("missing front matter"); | |
| 130 | }); | |
| 131 | ||
| 132 | test("a private repository's waves are refused to an outsider", async () => { | |
| 133 | for (const p of ["/alice/priv/waves", "/alice/priv/waves/0.7.3"]) { | |
| 134 | expect((await call(undefined, p)).status).toBe(404); | |
| 135 | expect((await call(bob, p)).status).toBe(404); | |
| 136 | expect((await call(alice, p)).status).toBe(200); | |
| 137 | } | |
| 138 | }); | |
| 139 | }); |
new above
offline