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 #1801
scout/forge:scout/1507-owner-name-waves-lists-a-repository-s-wa → main 7 commits fork 4 threads · 4 unresolved
6 files
changed +321
−0
README.md +5
| @@ -846,6 +846,11 @@ work here. | ||
| 846 | 846 | - **Automation** — a JSON API covering repos, issues, forks, and orgs (the |
| 847 | 847 | label-as-queue surface), a durable event outbox, and **webhooks** (HMAC-signed |
| 848 | 848 | delivery with retry). |
| 849 | - **Waves** — a file at `waves/<version>.md` on the default branch appears at | |
| 850 | `/<owner>/<name>/waves`, newest first, and `/<owner>/<name>/waves/<version>` | |
| 851 | renders one wave as a note. The format is in `src/story/wave.ts`. Private | |
| 852 | repositories keep their waves private. Not yet in the terminal (#1819). | |
| 853 | ||
| 849 | 854 | - **Removing an account** — `DELETE /api/v1/admin/accounts/:username` (admin |
| 850 | 855 | only). An account that is the last owner of an organization answers 409 with |
| 851 | 856 | the organizations named. Send `{"retireOrgs": true}` to preview: the answer |
scripts/gate.ts +1
| @@ -84,6 +84,7 @@ export const GATE: readonly string[] = [ | ||
| 84 | 84 | "test/review-from-green.test.ts", |
| 85 | 85 | "test/themes.test.ts", |
| 86 | 86 | "test/waves.test.ts", |
| 87 | "test/waves-page.test.ts", | |
| 87 | 88 | "test/cut-wave.test.ts", |
| 88 | 89 | "test/crew-wrapup.test.ts", |
| 89 | 90 | "test/crew-limits.test.ts", |
src/http/server.ts +3
| @@ -159,6 +159,7 @@ import { | ||
| 159 | 159 | branchDelete, |
| 160 | 160 | branchesPage, |
| 161 | 161 | } from "../web/pages/branches.ts"; |
| 162 | import { wavePage, wavesPage } from "../web/pages/waves.ts"; | |
| 162 | 163 | import { |
| 163 | 164 | contributeSubmit, |
| 164 | 165 | deleteFilePage, |
| @@ -1104,6 +1105,8 @@ export function buildRouter(): Router<Handler> { | ||
| 1104 | 1105 | router.add("GET", "/:owner/:name/forks", forksPage); |
| 1105 | 1106 | // The repository's activity, for a feed reader. |
| 1106 | 1107 | router.add("GET", "/:owner/:name/activity.atom", activityAtom); |
| 1108 | router.add("GET", "/:owner/:name/waves", wavesPage); | |
| 1109 | router.add("GET", "/:owner/:name/waves/:version", wavePage); | |
| 1107 | 1110 | router.add("GET", "/:owner/:name/branches", branchesPage); |
| 1108 | 1111 | router.add("POST", "/:owner/:name/branches", branchCreate); |
| 1109 | 1112 | router.add("POST", "/:owner/:name/branches/:branch/delete", branchDelete); |
src/web/pages/repo.ts +3
| @@ -991,6 +991,9 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 991 | 991 | branchCount: refs.branches.length, |
| 992 | 992 | })}${noApp} |
| 993 | 993 | ${frozenBlock(ctx, repo)} ${appStrip(ctx, repo)} |
| 994 | ${ref === repo.defaultBranch && tree.some((e) => e.type === "tree" && e.name === "waves") | |
| 995 | ? html`<p class="muted small"><a href="${repoBase(repo)}/waves">Waves</a></p>` | |
| 996 | : html``} | |
| 994 | 997 | ${following === undefined |
| 995 | 998 | ? forkBar(ctx, repo) |
| 996 | 999 | : followingCard(ctx, repo, following)} |
src/web/pages/waves.ts added +163
| @@ -0,0 +1,163 @@ | ||
| 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 | // Parsed lists by the oid of the repository's `waves/` tree: a tree oid names | |
| 61 | // its content, so an entry is never stale; only the size needs bounding. | |
| 62 | const CACHE_MAX = 64; | |
| 63 | const cache = new Map<string, WaveEntry[]>(); | |
| 64 | ||
| 65 | async function readWaves(repo: Repo, ctx: Ctx): Promise<WaveEntry[]> { | |
| 66 | const dir = repoDir(ctx.app.reposRoot, repo.ownerName, repo.name); | |
| 67 | const root = await listTree(dir, repo.defaultBranch, ""); | |
| 68 | const oid = root.ok ? root.value.find((e) => e.type === "tree" && e.name === "waves")?.oid : undefined; | |
| 69 | if (oid === undefined) return []; | |
| 70 | const key = `${dir}:${oid}`; | |
| 71 | const hit = cache.get(key); | |
| 72 | if (hit) return hit; | |
| 73 | const tree = await listTree(dir, repo.defaultBranch, "waves"); | |
| 74 | if (!tree.ok) return []; | |
Nit: this lists |
||
| 75 | const files = tree.value | |
| 76 | .filter((e) => e.type === "blob" && e.name.endsWith(".md")) | |
| 77 | .map((e) => e.name) | |
| 78 | .slice(0, MAX_WAVES); | |
| 79 | const entries = orderWaves(await Promise.all(files.map((f) => readEntry(dir, repo.defaultBranch, f)))); | |
| 80 | if (cache.size >= CACHE_MAX) cache.delete(cache.keys().next().value!); | |
| 81 | cache.set(key, entries); | |
| 82 | return entries; | |
| 83 | } | |
| 84 | ||
| 85 | const waveHref = (repo: Repo, version: string): string => | |
| 86 | `${repoBase(repo)}/waves/${encodeURIComponent(version)}`; | |
| 87 | ||
| 88 | function row(repo: Repo, e: WaveEntry): Html { | |
| 89 | if (!e.ok) | |
| 90 | return html`<li class="row"><span class="row-main" | |
| 91 | ><span class="row-title mono">${e.file}</span | |
| 92 | ><span class="row-meta">Could not be read: ${e.reason}</span></span></li>`; | |
| 93 | const w = e.wave; | |
| 94 | return html`<li class="row"><span class="row-main" | |
| 95 | ><span class="row-title"><a href="${waveHref(repo, w.version)}">${w.version}</a> | |
| 96 | — ${w.headline}</span | |
| 97 | ><span class="row-meta">${w.date}</span></span></li>`; | |
| 98 | } | |
| 99 | ||
| 100 | // GET /:owner/:name/waves | |
| 101 | export async function wavesPage(ctx: Ctx): Promise<Response> { | |
| 102 | const repo = loadRepo(ctx); | |
| 103 | if (!repo) return notFoundPage(ctx); | |
| 104 | const entries = await readWaves(repo, ctx); | |
| 105 | const body = html`${repoHeader(ctx, repo, "code", { compact: true })} | |
| 106 | <div class="mt"> | |
| 107 | <h2>Waves</h2> | |
| 108 | ${entries.length === 0 | |
| 109 | ? empty({ | |
| 110 | icon: "search", | |
| 111 | title: "No waves yet", | |
| 112 | note: "A wave is a file at waves/<version>.md on the default branch.", | |
| 113 | }) | |
| 114 | : html`<ul class="list">${entries.map((e) => row(repo, e))}</ul>`} | |
| 115 | </div>`; | |
| 116 | return page({ | |
| 117 | ctx, | |
| 118 | title: `Waves · ${repo.name}`, | |
| 119 | crumbs: crumbsFor(repo), | |
| 120 | body, | |
| 121 | }); | |
| 122 | } | |
| 123 | ||
| 124 | // GET /:owner/:name/waves/:version | |
| 125 | export async function wavePage(ctx: Ctx): Promise<Response> { | |
| 126 | const repo = loadRepo(ctx); | |
| 127 | if (!repo) return notFoundPage(ctx); | |
| 128 | const version = ctx.params.version ?? ""; | |
| 129 | if (!VERSION.test(version)) return notFoundPage(ctx); | |
| 130 | const dir = repoDir(ctx.app.reposRoot, repo.ownerName, repo.name); | |
| 131 | const blobTree = await listTree(dir, repo.defaultBranch, "waves"); | |
| 132 | const file = `${version}.md`; | |
| 133 | if (!blobTree.ok || !blobTree.value.some((e) => e.type === "blob" && e.name === file)) | |
| 134 | return notFoundPage(ctx); | |
| 135 | const e = await readEntry(dir, repo.defaultBranch, file); | |
| 136 | const back = html`<p><a href="${repoBase(repo)}/waves">← All waves</a></p>`; | |
| 137 | const body = e.ok | |
| 138 | ? html`${repoHeader(ctx, repo, "code", { compact: true })} | |
| 139 | <article class="mt"> | |
| 140 | ${back} | |
| 141 | <h1>${e.wave.version} — ${e.wave.headline}</h1> | |
| 142 | <p class="muted">${e.wave.date}</p> | |
| 143 | <h2>Highlights</h2> | |
| 144 | <ul>${e.wave.highlights.map( | |
| 145 | (h) => html`<li><strong>${h.title}</strong> — ${h.why}</li>`, | |
| 146 | )}</ul> | |
| 147 | <h2>Pulls</h2> | |
| 148 | ${e.wave.pulls.length === 0 | |
| 149 | ? html`<p class="muted">No pulls listed.</p>` | |
| 150 | : html`<ul>${e.wave.pulls.map( | |
| 151 | (p) => html`<li><a href="${p.url}" rel="noopener noreferrer">${p.title}</a></li>`, | |
| 152 | )}</ul>`} | |
| 153 | </article>` | |
| 154 | : html`${repoHeader(ctx, repo, "code", { compact: true })} | |
| 155 | <article class="mt">${back}<h1>${version}</h1> | |
| 156 | <p>Could not be read: ${e.reason}</p></article>`; | |
| 157 | return page({ | |
| 158 | ctx, | |
| 159 | title: `${version} · Waves · ${repo.name}`, | |
| 160 | crumbs: crumbsFor(repo), | |
| 161 | body, | |
| 162 | }); | |
| 163 | } |
test/waves-page.test.ts added +146
| @@ -0,0 +1,146 @@ | ||
| 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 | ||
| 140 | test("the repository page links to waves only when waves/ exists", async () => { | |
| 141 | const withWaves = await (await call(undefined, "/alice/pub")).text(); | |
| 142 | expect(withWaves).toContain('href="/alice/pub/waves"'); | |
| 143 | const without = await (await call(undefined, "/alice/none")).text(); | |
| 144 | expect(without).not.toContain("/alice/none/waves"); | |
|
||
| 145 | }); | |
| 146 | }); |
new above
offline
Every anonymous GET reads up to 500 blobs and parses them (about 32 MB at worst), with nothing cached. Keep the parsed list per tree oid of
waves/(fromlistTree, orrev-parse <branch>:waves) and reuse it while that oid stays the same.