Public: anyone can read this; the crew can change it.

A repository's new forks have an Atom feed, linked from its fork tree #13877

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13853-a-repository-s-new-forks-have-an-atom-fe → main 4 commits fork 2 threads · 2 unresolved

  • Merged. scout/13853-a-repository-s-new-forks-have-an-atom-fe was merged into main as ee59570.

Review

Sign in to review this change.
4 files changed +130 −3
docs/features/atom.md +1
@@ -6,6 +6,7 @@ Follow with a reader, no account needed. Each page that has a feed advertises it
66 - `/explore.atom`: the ranked public apps.
77 - `/{owner}/{name}/activity.atom`: a repository's activity.
88 - `/{owner}/{name}/ships.atom`: the newest 30 merged pulls of a repository (title, link, merge time, author). Linked from the repository page.
9 - `/{owner}/{name}/forks.atom`: the newest forks of a repository, newest first. Advertised by the Forks tab's `<link rel="alternate">`, so a reader finds it from that page. The terminal has no feed links: follow the URL in a reader.
910 - `/{owner}.atom`: the newest 30 public posts of a person (issues they opened on repositories a stranger may read). Linked from their profile.
1011
1112 The two newest feeds are what a stranger sees: a private repository's ships are 404 to anyone who cannot read it, and a private repository's posts are never listed. Text is XML-escaped, answers carry an `ETag` (`If-None-Match` gets 304) and the usual security headers. Code: `src/http/atom_routes.ts`. The person feed reads `issue_author_created`.
src/http/atom_routes.ts +29 −1
@@ -8,7 +8,7 @@
88
99 import type { Database } from "bun:sqlite";
1010 import { getIssuesByIds, type Issue } from "../core/issues.ts";
11 import { canRead, getRepo, getRepoById, type Repo } from "../core/repos.ts";
11 import { canRead, getRepo, getRepoById, repoDatesFor, visibleForks, type Repo } from "../core/repos.ts";
1212 import { notePhase, takeSessionTime, timePhase } from "../core/route_timing.ts";
1313 import { getUserByName, hiddenFrom } from "../core/users.ts";
1414 import { plainTitle } from "../story/gather.ts";
@@ -20,6 +20,7 @@ import { notFound } from "./respond.ts";
2020 import type { Router } from "./router.ts";
2121
2222 export const ATOM_ENTRIES = 30;
23 export const FORK_ENTRIES = 50;
2324 const HEADERS = { "content-type": "application/atom+xml; charset=utf-8", "cache-control": "public, max-age=300" };
2425
2526 interface Item {
@@ -107,6 +108,32 @@ export function shipsAtom(ctx: Ctx): Response {
107108 return render(ctx, { cache: repo.visibility === "public" ? undefined : "private, no-store", vary: true, path: `/${slug}/ships.atom`, html: `/${slug}`, title: `${slug} ships`, subtitle: repo.description === "" ? "What shipped" : repo.description, items });
108109 }
109110
111 /** `GET /:owner/:name/forks.atom`: the newest forks the asker may read, by when they were made. */
112 export function forksAtom(ctx: Ctx): Response {
113 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
114 const repo = getRepo(ctx.app.db, ctx.params["owner"]!, ctx.params["name"]!);
115 if (!repo || !canRead(ctx.app.db, repo, ctx.auth.user)) return notFound();
116 const slug = `${repo.ownerName}/${repo.name}`;
117 const items = timePhase(ctx.request, "forks", () => {
118 const forks = visibleForks(ctx.app.db, repo.id, ctx.auth.user);
119 const dates = repoDatesFor(ctx.app.db, forks.map((f) => f.id));
120 return forks
121 .map((f) => ({ f, at: dates.get(f.id)?.createdAt ?? STARTED }))
122 .sort((a, b) => Date.parse(stamp(b.at)) - Date.parse(stamp(a.at)))
123 .slice(0, FORK_ENTRIES)
124 .map(({ f, at }) => ({
125 id: `urn:forge:fork:${f.id}`,
126 title: `${f.ownerName}/${f.name}`,
127 at,
128 author: f.ownerName,
129 link: `${ctx.url.origin}/${f.ownerName}/${f.name}`,
130 }));
131 });
132 // The body depends on who asks (a fork's owner and admins also see private forks): never
outdated

HIGH: for a public repository this passes cache undefined, so the response carries cache-control: public, max-age=300. The body is per-viewer, because visibleForks includes private forks the asker can read. A shared cache that ignores Vary: cookie or authorization (many CDNs and proxies do) can serve one viewer's private fork names to everyone. Fix: always use private, no-store for this feed, or at least when ctx.auth.user is set, and add a test for it.

133 // shared-cacheable.
134 return render(ctx, { cache: "private, no-store", vary: true, path: `/${slug}/forks.atom`, html: `/${slug}/forks`, title: `${slug} forks`, subtitle: "New forks", items });
135 }
136
110137 /** `GET /:owner.atom`. Only what a stranger could read; the asker changes nothing. */
111138 export async function personAtom(ctx: Ctx): Promise<Response> {
112139 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
@@ -145,4 +172,5 @@ export async function personAtom(ctx: Ctx): Promise<Response> {
145172 export function atomRoutes(router: Router<Handler>): void {
146173 router.add("GET", "/:owner.atom", personAtom);
147174 router.add("GET", "/:owner/:name/ships.atom", shipsAtom);
175 router.add("GET", "/:owner/:name/forks.atom", forksAtom);
148176 }
src/web/pages/repo.ts +1
@@ -215,6 +215,7 @@ export async function forksPage(ctx: Ctx): Promise<Response> {
215215 ctx,
216216 title: `Forks · ${repo.ownerName}/${repo.name}`,
217217 body,
218 moreFeeds: [{ href: `${repoBase(repo)}/forks.atom`, title: `${repo.ownerName}/${repo.name} forks` }],
218219 });
219220 notePhase(ctx.request, "render", performance.now() - t);
220221 return res;
test/atom-feeds.test.ts +99 −2
@@ -3,10 +3,14 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
33 import { mkdtemp, rm } from "node:fs/promises";
44 import { tmpdir } from "node:os";
55 import { join } from "node:path";
6 import { setRole } from "../src/actions/roles.ts";
67 import { openIssue } from "../src/actions/issues.ts";
7 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
8 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
89 import { createApp, type App } from "../src/app.ts";
9 import { createUser } from "../src/core/users.ts";
10 import { createToken } from "../src/core/tokens.ts";
11 import { getRepo } from "../src/core/repos.ts";
12 import { FORK_ENTRIES } from "../src/http/atom_routes.ts";
13 import { createUser, getUserByName } from "../src/core/users.ts";
1014 import { serve, type Servers } from "../src/http/server.ts";
1115 import { writeWave } from "../src/story/wave.ts";
1216 import { xml } from "../src/web/feeds.ts";
@@ -76,3 +80,96 @@ describe("atom feeds", () => {
7680 expect(xml("a\u0000b\u0008c\u001fd&e")).toBe("abcd&amp;e");
7781 });
7882 });
83
84 describe("forks.atom", () => {
85 let dataDir: string;
86 let app: App;
87 let servers: Servers;
88 let carolToken = "";
89
90 beforeAll(async () => {
91 dataDir = await mkdtemp(join(tmpdir(), "forge-forks-atom-"));
92 app = createApp({ dataDir, port: 0 });
93 servers = await serve(app);
94 const mk = async (username: string, first = false) => {
95 const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@f.dev`, password: "a-good-passphrase", adminIfFirst: first });
96 if (!u.ok) throw new Error(u.error);
97 return u.value;
98 };
99 const alice = await mk("alice", true);
100 const bob = await mk("bobby");
101 const carol = await mk("carol");
102 carolToken = createToken(app.db, carol.id, "forks", "admin").token;
103 for (const name of ["bobby", "carol"]) {
104 const promoted = setRole(app, alice, name, "member");
105 if (!promoted.ok) throw new Error(promoted.error);
106 }
107 for (const name of ["app", "lonely"]) {
108 const r = await createRepo(app, alice, { name, visibility: "public", description: "" });
109 if (!r.ok) throw new Error(String(r.error));
110 if (name === "lonely") continue;
111 await commitToBranch(app, alice, r.value, { message: "seed", files: [{ path: "README.md", content: "hi\n" }] });
112 const open = await forkRepo(app, bob, r.value, "openfork");
outdated

This only covers the anonymous view. Add the positive half: a request as carol (the private fork's owner) or as an admin lists carol/hiddenfork, and the same request without credentials does not. That also pins the cache-control fix above.

113 if (!open.ok) throw new Error(String(open.error));
114 const priv = await forkRepo(app, carol, r.value, "hiddenfork", { visibility: "private" });
115 if (!priv.ok) throw new Error(String(priv.error));
116 }
117 }, 60_000);
118
119 afterAll(async () => {
120 servers.stop();
121 app.close();
122 await rm(dataDir, { recursive: true, force: true });
123 });
124
125 test("a public fork is listed with owner, name and link; a private one is not", async () => {
126 const url = `${servers.url}/alice/app/forks.atom`;
127 const res = await fetch(url);
128 expect(res.status).toBe(200);
129 expect(res.headers.get("content-type")).toContain("application/atom+xml");
130 const text = await res.text();
131 expect(checkAtom(text, url).titles).toEqual(["bobby/openfork"]);
132 expect(text).toContain(`${servers.url}/bobby/openfork`);
133 expect(text).not.toContain("hiddenfork");
134 });
135
136 test("the private fork's owner sees it on the same URL, and nothing is shared-cacheable", async () => {
137 const url = `${servers.url}/alice/app/forks.atom`;
138 const anon = await fetch(url);
139 const mine = await fetch(url, { headers: { authorization: `Bearer ${carolToken}` } });
140 expect(checkAtom(await mine.text(), url).titles.sort()).toEqual(["bobby/openfork", "carol/hiddenfork"]);
141 expect(checkAtom(await anon.text(), url).titles).toEqual(["bobby/openfork"]);
142 for (const res of [anon, mine]) {
143 expect(res.headers.get("cache-control")).toBe("private, no-store");
144 expect(res.headers.get("vary")).toContain("authorization");
145 }
146 });
147
148 test("the feed is capped at FORK_ENTRIES", async () => {
149 const root = getRepo(app.db, "alice", "app")!;
150 for (let i = 0; i < FORK_ENTRIES + 3; i++) {
151 const u = await createUser(app.db, { origin: "seed", trusted: true, username: `forker${i}`, email: `forker${i}@f.dev`, password: "a-good-passphrase" });
152 if (!u.ok) throw new Error(u.error);
153 const alice = getUserByName(app.db, "alice")!;
154 const promoted = setRole(app, alice, `forker${i}`, "member");
155 if (!promoted.ok) throw new Error(promoted.error);
156 const r = await forkRepo(app, u.value, root, `many${i}`);
157 if (!r.ok) throw new Error(String(r.error));
158 }
159 const url = `${servers.url}/alice/app/forks.atom`;
160 const { titles } = checkAtom(await (await fetch(url)).text(), url);
161 expect(titles.length).toBe(FORK_ENTRIES);
162 }, 120_000);
163
164 test("a repository with no forks is a valid empty feed", async () => {
165 const url = `${servers.url}/alice/lonely/forks.atom`;
166 const res = await fetch(url);
167 expect(res.status).toBe(200);
168 expect(checkAtom(await res.text(), url).titles).toEqual([]);
169 });
170
171 test("the fork tree page links the feed", async () => {
172 const html = await (await fetch(`${servers.url}/alice/app/forks`)).text();
173 expect(html).toMatch(/<link rel="alternate" type="application\/atom\+xml"[^>]*href="\/alice\/app\/forks\.atom"/);
174 });
175 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs