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

Pages that name a thing describe it in one sentence from its live state, and a private one says only its name and 'private' #1417

Closed opened this
Closed without merging

scout/forge:scout/1412-pages-that-name-a-thing-describe-it-in-o → main 4 commits fork 3 threads · 3 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
8 files changed +344 −1
scripts/gate.ts +1
@@ -34,6 +34,7 @@ export const GATE: readonly string[] = [
3434 "test/attach-look.test.ts",
3535 "test/og-card.test.ts",
3636 "test/health.test.ts",
37 "test/unfurl.test.ts",
3738 "test/client-address.test.ts",
3839 "test/client-list.test.ts",
3940 "test/contents-api.test.ts",
src/web/pages/org.ts +11 −1
@@ -37,6 +37,7 @@ import { field, readForm, type Form } from "../form.ts";
3737 import { html, when, type Html } from "../html.ts";
3838 import { icon } from "../icons.ts";
3939 import { flashTo, page } from "../layout.ts";
40 import { personLine } from "../unfurl.ts";
4041 import { secretsSection } from "./secrets.ts";
4142 import { agentsOnForge, grantsSection, saveGrants } from "./subscriptions.ts";
4243 import { shelfRows } from "../studio/shelf.ts";
@@ -362,7 +363,16 @@ export async function ownerProfile(ctx: Ctx): Promise<Response> {
362363 )}
363364 ${isOrg ? orgSecrets(ctx, owner.username) : null}`;
364365
365 return page({ ctx, title: owner.username, body });
366 return page({
367 ctx,
368 title: owner.username,
369 description: personLine(
370 owner.username,
371 listReposByOwner(ctx.app.db, owner.username, undefined, { limit: 101 })
372 .length,
373 ),
374 body,
375 });
366376 }
367377
368378 function orgSecrets(ctx: Ctx, name: string): Html | null {
src/web/studio/app.ts +10
@@ -32,6 +32,8 @@ import { crumbsFor, repoBase, repoHeader, repoPalette } from "../chrome.ts";
3232 import { appTailJs, liveRegionsJs } from "../client.ts";
3333 import { html, when, type Html } from "../html.ts";
3434 import { page } from "../layout.ts";
35 import { appLine } from "../unfurl.ts";
36 import { envsFor } from "../../apps/runtime.ts";
3537 import { liveClientJs, liveMount } from "../live.ts";
3638 import { followingCard, followingOf } from "../pages/following.ts";
3739 import {
@@ -471,6 +473,14 @@ export async function studioApp(
471473 return page({
472474 ctx,
473475 title: `${repo.name}`,
476 description: appLine(

envsFor runs on every render, even when the repo is private and appLine returns "Private." anyway. Skip the lookup for non-public repos.

477 repo,
478 repo.visibility === "public"
479 ? envsFor(ctx.app, ctx.auth.user, record).find(
480 (e) => e.env === "production",
481 )
482 : undefined,
483 ),
474484 crumbs: crumbsFor(repo),
475485 palette: repoPalette(ctx, repo),
476486 body,
src/web/studio/change.ts +2
@@ -40,6 +40,7 @@ import {
4040 import { html, when, type Html } from "../html.ts";
4141 import type { IconName as Glyph } from "../icons.ts";
4242 import { fragment, page, redirect } from "../layout.ts";
43 import { threadLine } from "../unfurl.ts";
4344 import { renderMarkdown } from "../markdown.ts";
4445 import { liveClientJs, liveMount } from "../live.ts";
4546 import {
@@ -583,6 +584,7 @@ async function changeConversation(
583584 return page({
584585 ctx,
585586 title: `${pull.title} · ${repo.name}`,
587 description: threadLine(repo, pull),
586588 crumbs: crumbsFor(repo),
587589 palette: repoPalette(ctx, repo),
588590 body,
src/web/studio/run.ts +2
@@ -23,6 +23,7 @@ import { crumbsFor, repoBase, repoHeader, repoPalette } from "../chrome.ts";
2323 import { studioRunJs } from "../client.ts";
2424 import { html, when, type Html } from "../html.ts";
2525 import { page } from "../layout.ts";
26 import { runLine } from "../unfurl.ts";
2627 import { btn, sha } from "../ui.ts";
2728 import { previewCard } from "../pages/apps.ts";
2829 import { costText, durationText, runTitle, runTokens } from "../pages/runs.ts";
@@ -292,6 +293,7 @@ export async function runView(
292293 return page({
293294 ctx,
294295 title: `${title} · ${repo.name}`,
296 description: runLine(repo, run),
295297 crumbs: crumbsFor(repo),
296298 palette: repoPalette(ctx, repo),
297299 body,
src/web/studio/tell.ts +2
@@ -27,6 +27,7 @@ import type { Ctx } from "../../http/context.ts";
2727 import { crumbsFor, repoBase, repoHeader, repoPalette } from "../chrome.ts";
2828 import { html, when, type Html } from "../html.ts";
2929 import { page } from "../layout.ts";
30 import { threadLine } from "../unfurl.ts";
3031 import { liveClientJs, liveMount } from "../live.ts";
3132 import {
3233 avatar,
@@ -392,6 +393,7 @@ async function studioTell(
392393 return page({
393394 ctx,
394395 title: `${issue.title} · ${repo.name}`,
396 description: threadLine(repo, issue),
395397 crumbs: crumbsFor(repo),
396398 palette: repoPalette(ctx, repo),
397399 body,
src/web/unfurl.ts added +66
@@ -0,0 +1,66 @@
1 /**
2 * The one sentence a link unfurls as, from a thing's live state. A private
3 * repository's pages say their name and "private" and nothing from inside:
4 * the tags are read by whoever fetches the URL, and a cached unfurl outlives
5 * the session that rendered it. A sealed issue is private inside a public
6 * repository and is held to the same rule.
7 */
8 import type { Issue } from "../core/issues.ts";
9 import type { Pull } from "../core/pulls.ts";
10 import type { Repo } from "../core/repos.ts";
11 import type { Run } from "../core/runs.ts";
12 import type { EnvStatus } from "../apps/runtime.ts";
13
14 const PRIVATE = "Private.";
15
16 const firstLine = (text: string): string =>
17 text.split(/\r?\n/).find((l) => l.trim() !== "")?.trim() ?? "";
18
19 const hidden = (repo: Repo): boolean => repo.visibility !== "public";

The sealed-issue rule is not covered by test/unfurl.test.ts. Add one: a sealed issue in a public repo unfurls as "Private.".

20
21 /** "Open · first line of the body", or the title when the body is empty. */
22 export function threadLine(repo: Repo, thread: Issue | Pull): string {
23 if (hidden(repo) || thread.sealed) return PRIVATE;
24 const merged = "mergedAt" in thread && thread.mergedAt !== null;
25 const state = merged ? "Merged" : thread.state === "open" ? "Open" : "Closed";
26 const line = firstLine(thread.body) || thread.title;
27 return `${state} · ${line}`;
28 }
29
30 /** "Succeeded · <summary> (commit abc1234)". */

runLine and appLine have no test. Cover the verdict and state mapping, and the private case.

31 export function runLine(repo: Repo, run: Run): string {
32 if (hidden(repo)) return PRIVATE;
33 const verdict = {
34 queued: "Queued",
35 running: "Running",
36 ok: "Passed",
37 failed: "Failed",
38 cancelled: "Cancelled",
39 }[run.state];
40 const summary = firstLine(run.summary);
41 return `${verdict}${summary === "" ? "" : ` · ${summary}`} (commit ${run.commitOid.slice(0, 7)})`;
42 }
43
44 /** "Live" or "Asleep" for an app's production, from the environment's state. */
45 export function appLine(repo: Repo, production: EnvStatus | undefined): string {
46 if (hidden(repo)) return PRIVATE;
47 const state =
48 production === undefined
49 ? "Not deployed yet"
50 : production.state === "running"
51 ? "Live"
52 : production.state === "asleep" || production.dormant !== undefined
53 ? "Asleep"
54 : production.state === "deploying"
55 ? "Deploying"
56 : production.state === "failed"
57 ? "Failing"
58 : "Stopped";
59 return `${repo.ownerName}/${repo.name} app · ${state}`;
60 }
61
62 /** "N public repositories", counted as a stranger sees them; capped at 100+. */
63 export function personLine(username: string, publicRepos: number): string {
64 const n = publicRepos > 100 ? "100+" : String(publicRepos);
65 return `${username} · ${n} public ${publicRepos === 1 ? "repository" : "repositories"}`;
66 }
test/unfurl.test.ts added +250
@@ -0,0 +1,250 @@
1 /**
2 * A link unfurls as one sentence from the thing's live state, and a private
3 * thing unfurls as "private" only: the tags are read by whoever fetches the
4 * URL, so the body must never reach them, even for the owner's own session.
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 { createApp, type App } from "../src/app.ts";
11 import { openIssue } from "../src/actions/issues.ts";
12 import { openPull } from "../src/actions/pulls.ts";
13 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
14 import type { Repo } from "../src/core/repos.ts";
15 import { createApp as createAppRecord } from "../src/core/apps.ts";
16 import { createRun, type Run } from "../src/core/runs.ts";
17 import type { EnvStatus } from "../src/apps/runtime.ts";
18 import { appLine, runLine } from "../src/web/unfurl.ts";
19 import { createUser, type User } from "../src/core/users.ts";
20 import { serve, type Servers } from "../src/http/server.ts";
21 import { quietLogs } from "./helpers/globals.ts";
22
23 quietLogs();
24
25 const PASSWORD = "an-unfurl-passphrase";
26 const SECRET = "the-private-body-line";
27
28 const ok = <T>(
29 r: { ok: true; value: T } | { ok: false; error: unknown },
30 ): T => {
31 if (!r.ok) throw new Error(JSON.stringify(r.error));
32 return r.value;
33 };
34
35 const og = (page: string, property: string): string =>
36 new RegExp(`<meta (?:property|name)="${property}" content="([^"]*)"`).exec(
37 page,
38 )?.[1] ?? "";
39
40 describe("link descriptions", () => {
41 let dataDir = "";
42 let app: App;
43 let servers: Servers;
44 let alice: User;
45 let cookie = "";
46 let open: Repo;
47 let closed: Repo;
48 let openRun: Run;
49 let closedRun: Run;
50
51 const get = async (path: string, signedIn = true): Promise<string> =>
52 await (
53 await fetch(`${servers.url}${path}`, {
54 headers: signedIn ? { cookie } : {},
55 })
56 ).text();
57
58 beforeAll(async () => {
59 dataDir = await mkdtemp(join(tmpdir(), "forge-unfurl-"));
60 app = createApp({ dataDir, port: 0 });
61 servers = await serve(app);
62 alice = ok(
63 await createUser(app.db, {
64 username: "alice",
65 email: "[email protected]",
66 password: PASSWORD,
67 adminIfFirst: true,
68 trusted: true,
69 }),
70 );
71 const res = await fetch(`${servers.url}/signin`, {
72 method: "POST",
73 body: new URLSearchParams({ username: "alice", password: PASSWORD }),
74 redirect: "manual",
75 });
76 cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!;
77 open = ok(await createRepo(app, alice, { name: "open", visibility: "public" }));
78 closed = ok(
79 await createRepo(app, alice, { name: "closed", visibility: "private" }),
80 );
81 for (const repo of [open, closed]) {
82 ok(
83 await commitToBranch(app, alice, repo, {
84 message: "Start",
85 files: [{ path: "README.md", content: "# hi\n" }],
86 }),
87 );
88 }
89 ok(
90 openIssue(app, alice, open, {
91 title: "Public bug",
92 body: "Crashes on start.\nSecond line.",
93 }),
94 );
95 ok(openIssue(app, alice, closed, { title: "Hidden bug", body: SECRET }));
96 ok(
97 openIssue(app, alice, open, {
98 title: "Sealed bug",
99 body: SECRET,
100 sealed: true,
101 }),
102 );
103 const mk = (repo: Repo): Run =>
104 createRun(app.db, {
105 repoId: repo.id,
106 kind: "deploy",
107 env: "production",
108 commitOid: "abcdef1234567890",
109 summary: SECRET,
110 });
111 for (const repo of [open, closed])
112 ok(
113 createAppRecord(app.db, {
114 repoId: repo.id,
115 slug: `${repo.name}-app`,
116 access: "public",
117 }),
118 );
119 openRun = mk(open);
120 closedRun = mk(closed);
121 ok(
122 await commitToBranch(app, alice, closed, {
123 branch: "feat",
124 base: "main",
125 message: "Feat",
126 files: [{ path: "a.txt", content: "a\n" }],
127 }),
128 );
129 ok(
130 await openPull(app, alice, closed, {
131 title: "Hidden pull",
132 body: SECRET,
133 baseBranch: "main",
134 headBranch: "feat",
135 }),
136 );
137 });
138
139 afterAll(async () => {
140 servers.stop();
141 app.close();
142 await rm(dataDir, { recursive: true, force: true });
143 });
144
145 test("a private pull unfurls as private, never its body", async () => {
146 const page = await get("/alice/closed/pulls/2");
147 expect(page).toContain("Hidden pull");
148 expect(page).not.toContain(`content="${SECRET}`);
149 expect(og(page, "og:description")).toBe("Private.");
150 expect(og(page, "og:description")).not.toContain(SECRET);
151 });
152
153 test("a private repository's issue unfurls as private, never its body", async () => {
154 const page = await get("/alice/closed/issues/1");
155 expect(og(page, "og:description")).toBe("Private.");
156 expect(page).not.toMatch(new RegExp(`<meta[^>]*${SECRET}`));
157 });
158
159 test("an anonymous reader of a private repository gets neither", async () => {
160 const page = await get("/alice/closed/issues/1", false);
161 expect(page).not.toContain(SECRET);
162 });
163
164 test("a public issue unfurls as its state and first line", async () => {
165 const page = await get("/alice/open/issues/1", false);
166 expect(og(page, "og:description")).toBe("Open · Crashes on start.");
167 expect(og(page, "description")).toBe("Open · Crashes on start.");
168 });
169
170 test("a sealed issue in a public repository unfurls as private, for its owner too", async () => {
171 const page = await get("/alice/open/issues/2");
172 expect(page).toContain("Sealed bug");
173 expect(og(page, "og:description")).toBe("Private.");
174 expect(page).not.toMatch(new RegExp(`<meta[^>]*${SECRET}`));
175 });
176
177 test("a private repository's run page unfurls as private", async () => {
178 const page = await get(`/alice/closed/runs/${closedRun.id}`);
179 expect(og(page, "og:description")).toBe("Private.");
180 expect(page).not.toMatch(new RegExp(`<meta[^>]*${SECRET}`));
181 });
182
183 test("a public run page unfurls as its verdict, summary and commit", async () => {
184 const page = await get(`/alice/open/runs/${openRun.id}`, false);
185 expect(og(page, "og:description")).toBe(
186 `Queued · ${SECRET} (commit abcdef1)`,
187 );
188 });
189
190 test("a private repository's app page unfurls as private", async () => {
191 const page = await get("/alice/closed");
192 expect(og(page, "og:description")).toBe("Private.");
193 });
194
195 test("a public app page unfurls as its name and state", async () => {
196 const page = await get("/alice/open", false);
197 expect(og(page, "og:description")).toBe("alice/open app · Not deployed yet");
198 });
199
200 test("a person's page counts their public repositories only", async () => {
201 const page = await get("/alice", false);
202 expect(og(page, "og:description")).toBe("alice · 1 public repository");
203 });
204 });
205
206 describe("run and app lines", () => {
207 const repo = (visibility: string) =>
208 ({ visibility, ownerName: "alice", name: "r" }) as unknown as Repo;
209 const run = (state: string, summary = "") =>
210 ({ state, summary, commitOid: "0123456789" }) as unknown as Run;
211 const env = (state: string, dormant?: object) =>
212 ({ env: "production", state, dormant }) as unknown as EnvStatus;
213
214 test("a run's verdict follows its state", () => {
215 const verdicts = {
216 queued: "Queued",
217 running: "Running",
218 ok: "Passed",
219 failed: "Failed",
220 cancelled: "Cancelled",
221 };
222 for (const [state, word] of Object.entries(verdicts))
223 expect(runLine(repo("public"), run(state))).toBe(`${word} (commit 0123456)`);
224 expect(runLine(repo("public"), run("ok", "\n All green\nmore"))).toBe(
225 "Passed · All green (commit 0123456)",
226 );
227 });
228
229 test("a run in a private or internal repository says only Private.", () => {
230 for (const v of ["private", "internal"])
231 expect(runLine(repo(v), run("ok", "secret"))).toBe("Private.");
232 });
233
234 test("an app is Live, Asleep, or in the state it is in", () => {
235 const line = (e?: EnvStatus) => appLine(repo("public"), e);
236 expect(line(env("running"))).toBe("alice/r app · Live");
237 expect(line(env("asleep"))).toBe("alice/r app · Asleep");
238 expect(line(env("stopped", { since: "x", idleSince: "y" }))).toBe(
239 "alice/r app · Asleep",
240 );
241 expect(line(env("deploying"))).toBe("alice/r app · Deploying");
242 expect(line(env("failed"))).toBe("alice/r app · Failing");
243 expect(line(env("stopped"))).toBe("alice/r app · Stopped");
244 expect(line(undefined)).toBe("alice/r app · Not deployed yet");
245 });
246
247 test("a private app says only Private.", () => {
248 expect(appLine(repo("private"), env("running"))).toBe("Private.");
249 });
250 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs