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

A repository's page says 'Make it yours' once, as one action that forks it, for a visitor who is signed in #14545

Merged opened this

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

Merged

scout/forge:scout/14523-a-repository-s-page-says-make-it-yours-o → main 8 commits fork 3 threads · 3 unresolved

  • Merged. scout/14523-a-repository-s-page-says-make-it-yours-o was merged into main as 49d4c30.

Review

Sign in to review this change.
5 files changed +182 −8
docs/features/make-it-yours.md +5
@@ -9,6 +9,11 @@ account as an app of your own, following the original; code and history are copi
99 (`makeItYours` in `src/actions/make_it_yours.ts`). A receipt in plain words follows, with **Undo**. Signed out, the
1010 row sends you to sign in, then to a plan page where one tap confirms and lands on the same receipt.
1111
12 An app's own page offers the same one button (`src/web/studio/yours_button.ts`): signed in it posts straight to
13 `make-it-yours` (same-origin checked) and lands on your copy with the receipt line; signed out it goes through
14 sign-in and back; once you have a copy it reads **Open your copy**; a private app shows none, and it is never the page's primary button (**Open the app** is).
15 There is no separate fork action on the repository tool: the existing `make_it_yours` tool below is that action.
16
1217 ## Undo
1318
1419 `undoMakeItYours` removes the copy this tap made, only while it has no commits of its own and no pull was sent from
src/web/studio/make_it_yours.ts +1 −3
@@ -201,9 +201,7 @@ export async function makeItYoursSubmit(ctx: Ctx): Promise<Response> {
201201 url: ctx.url,
202202 forgeUrl: ctx.app.config.url,
203203 flash: {
204 message: made.value.existing
205 ? `This is your ${made.value.repo}.`
206 : `${made.value.repo} is yours. Its first deploy is under way.`,
204 message: receiptWords(made.value),
207205 },
208206 });
209207 }
src/web/studio/previews.ts +2 −5
@@ -10,8 +10,7 @@
1010 import { frontPulls } from "./front_pulls.ts";
1111 import { sshOn } from "../../settings/ssh.ts";
1212 import { appCacheSize } from "../../apps/build_cache.ts";
13 import { HELP_BUILD } from "../../core/contribute.ts";
14 import { lacks } from "../../core/roles.ts";
13 import { yoursButton } from "./yours_button.ts";
1514 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
1615 import { terminalFor } from "../../actions/apps.ts";
1716 import { envPull, previewEnv } from "../../apps/data.ts";
@@ -369,9 +368,7 @@ export function appPanels(
369368 <span class="spacer"></span>
370369 ${lookNow(ctx, repo, record)}
371370 ${openAppBtn(base, production, copy !== undefined)}
372 ${when(!writer, () =>
373 btn(ctx.auth.user !== undefined && lacks(db, ctx.auth.user, "make_it_yours") ? HELP_BUILD : "Make it yours", { href: `${base}/make-it-yours`, icon: "fork" }),
374 )}
371 ${writer ? null : yoursButton(ctx, repo, record)}
375372 ${when(production, (env) => envActions(base, env, writer))}
376373 </div>`;
377374 return {
src/web/studio/yours_button.ts added +31
@@ -0,0 +1,31 @@
1 /**
2 * The one "Make it yours" on an app's page (#14523), in its four states: a
3 * POST that forks it into the visitor's account (same-origin checked by
4 * `makeItYoursSubmit`); a link through sign-in and back when signed out;
5 * "Open your copy" once the visitor has one; nothing on a private app. A
6 * guest (not yet a member) keeps the help-build door the preview explains.
7 * Never primary: "Open the app" beside it is the page's one accent (principle 5).
8 */
9 import { HELP_BUILD } from "../../core/contribute.ts";
10 import type { AppRecord } from "../../core/apps.ts";
11 import { getForkOf, type Repo } from "../../core/repos.ts";
12 import { lacks } from "../../core/roles.ts";
13 import type { Ctx } from "../../http/context.ts";
14 import { repoBase, signInHref } from "../chrome.ts";
15 import { html, type Html } from "../html.ts";
16 import { btn } from "../ui.ts";
17
18 export function yoursButton(ctx: Ctx, repo: Repo, record: AppRecord): Html | undefined {
19 if (repo.visibility !== "public" || record.access !== "public") return undefined;
20 const base = repoBase(repo);
21 const user = ctx.auth.user;
outdated

Principle 5: one accent per page. This is a primary button next to other actions in the row; show that it is the only primary on the page, or make it secondary. No test covers the signed-out return path or the guest branch above.

22 if (user === undefined) return btn("Make it yours", { icon: "fork", href: signInHref(ctx) });
23 if (user.kind !== "user") return undefined;
24 const db = ctx.app.db;
25 if (lacks(db, user, "make_it_yours")) return btn(HELP_BUILD, { icon: "fork", href: `${base}/make-it-yours` });
26 const mine = getForkOf(db, user.id, repo.id);
27 if (mine !== undefined) return btn("Open your copy", { icon: "fork", href: repoBase(mine) });
outdated

Use getForkOf(db, user.id, repo.id) from core/repos.ts instead of this query plus getRepoById; the helper exists and this duplicates it.

28 return html`<form method="post" action="${base}/make-it-yours">
29 ${btn("Make it yours", { icon: "fork" })}
30 </form>`;
31 }
test/make-it-yours-button.test.ts added +143
@@ -0,0 +1,143 @@
1 /**
2 * #14523: a public app's page offers one "Make it yours", in four states, and refuses a
3 * cross-origin post.
4 */
5 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App } from "../src/app.ts";
10 import { updateApp, getAppByRepo } from "../src/core/apps.ts";
11 import { newApp } from "../src/actions/apps.ts";
12 import { createSession } from "../src/core/sessions.ts";
13 import { createUser, getUserById } from "../src/core/users.ts";
14 import { getRepo } from "../src/core/repos.ts";
15 import { serve, type Servers } from "../src/http/server.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17 import { promote } from "./helpers/members.ts";
18
19 quietLogs();
20
21 describe("make it yours on the repository page", () => {
22 let dataDir: string;
23 let app: App;
24 let servers: Servers;
25 let bobCookie: string;
26 let danaCookie: string;
27
28 const get = async (path: string, cookie?: string) =>
29 (await fetch(`${servers.url}${path}`, { headers: cookie ? { cookie } : {} })).text();
30 const post = (path: string, headers: Record<string, string>) =>
31 fetch(`${servers.url}${path}`, {
32 method: "POST",
33 headers: { cookie: bobCookie, "content-type": "application/x-www-form-urlencoded", ...headers },
34 body: "",
35 redirect: "manual",
36 });
37
38 beforeAll(async () => {
39 dataDir = await mkdtemp(join(tmpdir(), "forge-yours-button-"));
40 app = createApp({ dataDir, port: 0, mailer: async () => true });
41 servers = await serve(app);
42 const mk = async (username: string, first = false) => {
43 const r = await createUser(app.db, {
44 origin: "seed",
45 trusted: true,
46 username,
47 email: `${username}@example.com`,
48 password: "correct horse battery",
49 adminIfFirst: first,
50 });
51 if (!r.ok) throw new Error(String(r.error));
52 promote(app.db, r.value);
53 return getUserById(app.db, r.value.id)!;
54 };
55 const alice = await mk("alice", true);
56 const bob = await mk("bob");
57 for (const [name, visibility] of [["garden", "public"], ["vault", "private"]] as const) {
58 const made = await newApp(app, alice, { name, description: name, visibility });
59 if (!made.ok) throw new Error(String(made.error));
60 }
61 const dana = await createUser(app.db, {
62 origin: "seed",
63 trusted: true,
64 username: "dana",
65 email: "[email protected]",
66 password: "correct horse battery",
outdated

This test would pass without the change: bob is not a member of the private vault, so the page is a not-found and never contained the button. Render a private app the visitor can see (the owner's own page is a writer, so use a collaborator or an app with access set to non-public) and assert the button is absent there.

67 });
68 if (!dana.ok) throw new Error(String(dana.error));
69 danaCookie = `forge_session=${createSession(app.db, dana.value.id).token}`;
70 const hidden = await newApp(app, alice, { name: "closed", description: "closed", visibility: "public" });
71 if (!hidden.ok) throw new Error(String(hidden.error));
72 const repo = getRepo(app.db, "alice", "closed")!;
73 updateApp(app.db, getAppByRepo(app.db, repo.id)!.id, { access: "private" });
74 bobCookie = `forge_session=${createSession(app.db, bob.id).token}`;
75 }, 60_000);
76
77 afterAll(async () => {
78 servers.stop();
79 app.close();
80 await rm(dataDir, { recursive: true, force: true });
81 });
82
83 test("signed out: the button leads to sign-in and back", async () => {
84 const page = await get("/alice/garden");
85 expect(page).toContain("Make it yours");
86 expect(page).toContain("/signin?next=%2Falice%2Fgarden");
87 });
88
89 test("signed out: the sign-in it leads to carries the visitor back to the app page", async () => {
90 const res = await fetch(`${servers.url}/signin?next=%2Falice%2Fgarden`);
91 expect(res.status).toBe(200);
92 expect(await res.text()).toContain("%2Falice%2Fgarden");
93 });
94
95 test("a public repository whose app is not public shows no button to a visitor who can see the page", async () => {
96 const page = await get("/alice/closed", bobCookie);
97 expect(page).toContain("closed");
98 expect(page).not.toContain("Make it yours");
99 expect(page).not.toContain(`<form method="post" action="/alice/closed/make-it-yours"`);
100 });
101
102 test("a private repository is a not-found to a stranger, so no button", async () => {
103 expect(await get("/alice/vault", bobCookie)).not.toContain("Make it yours");
104 });
105
106 test("a guest is offered the help-build door, not the fork post", async () => {
107 const page = await get("/alice/garden", danaCookie);
108 // The button itself, in the app's head, not the contribute section's link.
109 const head = page.slice(page.indexOf('id="app-head"'), page.indexOf('id="contribute"'));
110 expect(head).toContain('href="/alice/garden/make-it-yours"');
111 expect(head).toContain("Help build this");
112 expect(page).not.toContain('<form method="post" action="/alice/garden/make-it-yours"');
113 expect(page).not.toContain("Open your copy");
114 });
115
116 test("the button is never the page's accent: the Make it yours control is not primary", async () => {
117 for (const cookie of [undefined, bobCookie]) {
118 const page = await get("/alice/garden", cookie);
119 const at = page.indexOf("Make it yours");
120 const tag = page.slice(page.lastIndexOf("<", at - 1), at);
121 expect(tag).not.toContain("primary");
122 }
123 });
124
125 test("a cross-origin post is refused and makes nothing", async () => {
126 const res = await post("/alice/garden/make-it-yours", { origin: "https://evil.example" });
127 expect(res.status).toBeGreaterThanOrEqual(400);
128 expect(getRepo(app.db, "bob", "garden")).toBeUndefined();
129 });
130
131 test("signed in: one button posts, lands on the copy with the receipt, then Open your copy", async () => {
132 const page = await get("/alice/garden", bobCookie);
133 expect(page).toContain('action="/alice/garden/make-it-yours"');
134 expect(page).not.toContain("Open your copy");
135 const res = await post("/alice/garden/make-it-yours", { origin: servers.url });
136 expect(res.status).toBe(303);
137 expect(res.headers.get("location")).toContain("/bob/garden");
138 expect(getRepo(app.db, "bob", "garden")).toBeDefined();
139 const after = await get("/alice/garden", bobCookie);
140 expect(after).toContain("Open your copy");
141 expect(after).not.toContain('action="/alice/garden/make-it-yours"');
142 });
143 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs