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

An app's page lists its forks as a plain list with each fork's owner and how long ago it last changed #13509

Merged opened this

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

Merged

scout/forge:scout/13459-an-app-s-page-lists-its-forks-as-a-plain → main 4 commits fork 1 thread · 1 unresolved

  • Merged. scout/13459-an-app-s-page-lists-its-forks-as-a-plain was merged into main as 274c390.

Review

Sign in to review this change.
4 files changed +241 −1
docs/features/fork-tree.md +6
@@ -58,3 +58,9 @@ Each fork in the forks page's tree and in the terminal's tree and forks screens
5858 ## Offers from forks
5959
6060 A repository's home lists the open pulls its forks have offered into it: title, fork, age, each linking to the pull. A fork the viewer cannot read offers nothing in the list. The forks terminal screen shows the same rows (`forkOffers`, `src/core/fork_offers.ts`), twenty at most.
61
62 ## On the app page
63
64 An app's page shows a **Forks (N)** card in its side rail: up to ten forks, the most recently changed first, each
65 with its owner, its name and "changed 3d ago" (`src/web/studio/forks_card.ts`). Only forks the viewer may read are
66 listed or counted, so the number never hints at a private fork. "All" opens the forks page.
src/web/studio/app.ts +2 −1
@@ -57,6 +57,7 @@ import { appMilestones } from "./milestone_lines.ts";
5757 import { nowCard, nowRegion } from "./now.ts";
5858 import { repoAsks } from "./asks.ts";
5959 import { notesCard } from "./notes.ts";
60 import { forksCard } from "./forks_card.ts";
6061 import { migrationRegion } from "./migration.ts";
6162 import { adapterCard } from "./adapter.ts";
6263 import { jobsLink } from "./jobs.ts";
@@ -565,7 +566,7 @@ export async function studioApp(
565566 ${notesCard(ctx, repo, record)}
566567 </div>
567568 <aside class="stack">
568 ${adapter} ${schedules} ${crewCard} ${weekCard} ${moneyCard} ${ownCard} ${more}
569 ${adapter} ${schedules} ${forksCard(ctx, repo)} ${crewCard} ${weekCard} ${moneyCard} ${ownCard} ${more}

This wiring has no test that fails without it. Add a rendered-page test (heading with N, rows, private fork absent, card absent when empty).

Nothing fails if this call is removed. Add a page-level test that fetches this app page and asserts the app-forks card and its Forks (N) heading, so the wiring is covered.

569570 </aside>
570571 </div>
571572 ${liveMount({ repo: base, app: true })}`;
src/web/studio/forks_card.ts added +78
@@ -0,0 +1,78 @@
1 /**
2 * Who already made it theirs: the forks of an app, on its page. Ten at most,
3 * the most recently changed first, each with its owner, its name and how long
4 * ago it last changed (the newest push, else its creation). Only forks the
5 * viewer may read are listed or counted, so the number never leaks a private
6 * fork. The full, ranked list is one step deeper, on the forks page.
7 */
8
9 import { visibleForks, type Repo } from "../../core/repos.ts";
10 import type { User } from "../../core/users.ts";
11 import type { Database } from "bun:sqlite";
12 import type { Ctx } from "../../http/context.ts";
13 import { html, type Html } from "../html.ts";
14 import { icon, listCard, row, timeAgo } from "../ui.ts";
15 import { repoBase } from "../chrome.ts";
16
17 /** How many forks the app page lists. */
18 export const FORKS_SHOWN = 10;
19
20 /** One listed fork. */
21 export interface ForkLine {
22 readonly owner: string;
23 readonly name: string;
24 readonly changedAt: string;
25 }
26
27 /** The forks the viewer may see: how many, and the newest-changed ten. */
28 export function forksOfApp(
29 db: Database,
30 repo: Repo,
31 viewer: User | undefined,
32 ): { readonly total: number; readonly shown: readonly ForkLine[] } {
33 // Visibility is a per-viewer rule, so it filters first; the ordering and
34 // the limit then run in one query over those ids, never a sort in JS.
35 const forks = visibleForks(db, repo.id, viewer);
36 if (forks.length === 0) return { total: 0, shown: [] };
37 const byId = new Map(forks.map((f) => [f.id, f]));
38 const rows = db
39 .query<{ id: string; changed: string }, [string, number]>(
40 `SELECT repo.id,
41 COALESCE((SELECT MAX(event.created_at) FROM event
42 WHERE event.repo_id = repo.id AND event.kind = 'push'),
43 repo.created_at) AS changed
44 FROM repo WHERE repo.id IN (SELECT value FROM json_each(?1))
45 ORDER BY changed DESC, repo.id LIMIT ?2`,
46 )
47 .all(JSON.stringify(forks.map((f) => f.id)), FORKS_SHOWN);
48 const shown = rows.map((r) => {
49 const f = byId.get(r.id)!;
50 return { owner: f.ownerName, name: f.name, changedAt: r.changed };
51 });
52 return { total: forks.length, shown };
53 }
54
55 /** The "Forks (N)" card; nothing at all when the viewer sees no fork. */
56 export function forksCard(ctx: Ctx, repo: Repo): Html | null {
57 const { total, shown } = forksOfApp(ctx.app.db, repo, ctx.auth.user);
58 if (total === 0) return null;
59 const base = repoBase(repo);
60 return listCard(
61 shown.map((f) =>
62 row({
63 href: `/${encodeURIComponent(f.owner)}/${encodeURIComponent(f.name)}`,
64 title: html`${f.owner} / ${f.name}`,
65 meta: f.changedAt === "" ? html`` : html`changed ${timeAgo(f.changedAt)}`,
66 data: { fork: `${f.owner}/${f.name}` },
67 }),
68 ),
69 {
70 head: html`${icon("fork")}
71 <h2>Forks (${total})</h2>
72 <span class="spacer"></span>
73 <a class="small" href="${base}/forks">All</a>`,
74 id: "app-forks",
75 extra: "about",
76 },
77 );
78 }
test/app-forks-card.test.ts added +155
@@ -0,0 +1,155 @@
1 /**
2 * An app's page lists its forks (#13459): "Forks (N)", up to ten, the most
3 * recently changed first, each with owner, name and when it changed. A private
4 * fork the viewer cannot read is neither listed nor counted.
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createRepo, forkRepo, updateRepoSettings } from "../src/actions/repos.ts";
11 import { createApp, type App } from "../src/app.ts";
12 import { createApp as createAppRecord } from "../src/core/apps.ts";
13 import { createSession } from "../src/core/sessions.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import { type Repo } from "../src/core/repos.ts";
16 import { createUser, type User } from "../src/core/users.ts";
17 import type { Ctx } from "../src/http/context.ts";
18 import { FORKS_SHOWN, forksCard, forksOfApp } from "../src/web/studio/forks_card.ts";
19 import { promote } from "./helpers/members.ts";
20
21 describe("an app's forks", () => {
22 let dataDir: string;
23 let app: App;
24 let lib: Repo;
25 let forkless: Repo;
26 let servers: Servers;
27 const users: Record<string, User> = {};
28
29 beforeAll(async () => {
30 dataDir = await mkdtemp(join(tmpdir(), "forge-app-forks-"));
31 app = createApp({ dataDir, port: 0 });
32 for (const [name, first] of [["alice", true], ["bob", false], ["carol", false], ["dan", false]] as const) {
33 const made = await createUser(app.db, {
34 origin: "seed", trusted: true, username: name, email: `${name}@forge.local`,
35 password: "a-good-passphrase", adminIfFirst: first,
36 });
37 if (!made.ok) throw new Error(made.error);
38 users[name] = made.value;
39 }
40 promote(app.db, users["bob"]!, users["carol"]!, users["dan"]!);
41 const made = await createRepo(app, users["alice"]!, { name: "lib", visibility: "public" });
42 if (!made.ok) throw new Error(String(made.error));
43 lib = made.value;
44 const bare = await createRepo(app, users["alice"]!, { name: "bare", visibility: "public" });
45 if (!bare.ok) throw new Error(String(bare.error));
46 forkless = bare.value;
47 for (const [r, slug] of [[lib, "lib"], [forkless, "bare"]] as const) {
48 const made = createAppRecord(app.db, { repoId: r.id, slug, access: "public" });
49 if (!made.ok) throw new Error(made.error);
50 // One merged pull: an app with history shows its side rail, a never-run one does not.
51 app.db.query("INSERT INTO issue (id, repo_id, number, title, author_id) VALUES (?1, ?2, 1, 'Start', ?3)").run(`i-${slug}`, r.id, users["alice"]!.id);
52 app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) VALUES (?1, 'main', 'start', '', '', ?2)").run(`i-${slug}`, new Date().toISOString());
53 }
54 servers = await serve(app);
55 for (const [who, at] of [["bob", "2026-01-01T00:00:00.000Z"], ["carol", "2026-03-01T00:00:00.000Z"], ["dan", "2026-02-01T00:00:00.000Z"]] as const) {
56 const forked = await forkRepo(app, users[who]!, lib);
57 if (!forked.ok) throw new Error(String(forked.error));
58 app.db.query("UPDATE repo SET created_at = ?1 WHERE id = ?2").run(at, forked.value.id);
59 if (who === "dan") {
60 const closed = await updateRepoSettings(app, users[who]!, forked.value, { visibility: "private" });
61 if (!closed.ok) throw new Error(String(closed.error));
62 }
63 }
64 }, 60_000);
65
66 afterAll(async () => {
67 servers.stop();
68 app.close();
69 await rm(dataDir, { recursive: true, force: true });
70 });
71
72 test("lists the forks newest first and counts them", () => {
73 const { total, shown } = forksOfApp(app.db, lib, undefined);
74 expect(total).toBe(2);
75 expect(shown.map((f) => f.owner)).toEqual(["carol", "bob"]);
76 expect(shown[0]!.name).toBe("lib");
77 });
78
79 test("a private fork is neither listed nor counted for a stranger or no one, but its owner sees it", () => {
80 for (const viewer of [undefined, users["bob"], users["carol"]]) {
81 const { total, shown } = forksOfApp(app.db, lib, viewer);
82 expect(total).toBe(2);
83 expect(shown.some((f) => f.owner === "dan")).toBe(false);
84 }
85 const dans = forksOfApp(app.db, lib, users["dan"]);
86 expect(dans.total).toBe(3);
87 expect(dans.shown.map((f) => f.owner)).toEqual(["carol", "dan", "bob"]);
88 });
89
90 const card = (viewer: User | undefined, repo: Repo = lib) =>
91 forksCard({ app, auth: { user: viewer } } as unknown as Ctx, repo)?.toString() ?? null;
92
93 test("the rendered card has the heading with N, a row per fork, and no private fork", () => {
94 for (const viewer of [undefined, users["bob"]]) {
95 const out = card(viewer)!;
96 expect(out).toContain("Forks (2)");
97 expect(out).toContain("carol / lib");
98 expect(out).toContain("bob / lib");
99 expect(out).toContain("changed");
100 expect(out.match(/data-fork=/g)!.length).toBe(2);
101 expect(out).not.toContain("dan");
102 }
103 expect(card(users["dan"])).toContain("dan / lib");
104 });
105
106 test("the card is absent when the viewer sees no fork", () => {
107 expect(card(undefined, forkless)).toBeNull();
108 });
109
110 const page = async (path: string, viewer?: User) => {
111 const headers: Record<string, string> = viewer
112 ? { cookie: `forge_session=${createSession(app.db, viewer.id).token}` }
113 : {};
114 const res = await fetch(`${servers.url}${path}`, { headers });
115 expect(res.status).toBe(200);
116 return await res.text();
117 };
118
119 test("the app page carries the card: Forks (N), a row per visible fork, no private fork", async () => {
120 for (const viewer of [undefined, users["bob"]]) {
121 const out = await page("/alice/lib", viewer);
122 expect(out).toContain('id="app-forks"');
123 expect(out).toContain("Forks (2)");
124 expect(out.match(/data-fork="/g)!.length).toBe(2);
125 expect(out).toContain('data-fork="carol/lib"');
126 expect(out).toContain('data-fork="bob/lib"');
127 expect(out).not.toContain("dan/lib");
128 expect(out).not.toContain("dan / lib");
129 }
130 expect(await page("/alice/lib", users["dan"])).toContain("Forks (3)");
131 });
132
133 test("an app with no fork has no forks card on its page", async () => {
134 const out = await page("/alice/bare");
135 expect(out).not.toContain('id="app-forks"');
136 expect(out).not.toContain("Forks (");
137 });
138
139 test("never lists more than ten, and still counts them all", async () => {
140 for (let i = 0; i < 10; i++) {
141 const made = await createUser(app.db, {
142 origin: "seed", trusted: true, username: `extra${i}`, email: `extra${i}@forge.local`,
143 password: "a-good-passphrase", adminIfFirst: false,
144 });
145 if (!made.ok) throw new Error(made.error);
146 promote(app.db, made.value);
147 const forked = await forkRepo(app, made.value, lib);
148 if (!forked.ok) throw new Error(String(forked.error));
149 }
150 const { total, shown } = forksOfApp(app.db, lib, undefined);
151 expect(total).toBe(12);
152 expect(shown.length).toBe(FORKS_SHOWN);
153 expect(shown[0]!.changedAt >= shown[9]!.changedAt).toBe(true);
154 }, 60_000);
155 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs