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

Bring an app whole from another forge: paste its link, and it runs here #8909

Parked opened this in answer to
Closed without merging

scout/forge:scout/8904-bring-an-app-whole-from-another-forge-pa → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
10 files changed +625 −2
docs/features/bring.md added +15
@@ -0,0 +1,15 @@
1 # Bring an app from another forge (`/bring`)
2
3 Paste the link to an app on a forge you have connected (`/settings/origins`), and it
4 comes across: the code and history are mirrored, it is registered as a **private** app
5 and deploys at once, and you watch the first deploy on its app page. "Bring an app" is in
6 the `+` menu and the palette.
7
8 - **Link back stays.** The copy is a mirror: it keeps its origin, so improvements flow
9 to the author.
10 - **Data, variables and secrets never come.** The page lists the names in the app's
11 `.env.example` (names only, never values) and takes them in one form. Empty fields set nothing.
12 - **Left out:** a forge you have not connected (public, anonymous import) answers with a
13 link to connect it; there is no anonymous fetch yet.
14 - Code: `src/origins/bring.ts` (link, host, needs), `src/actions/bring.ts`,
15 `src/web/pages/bring.ts`. Test: `test/bring.test.ts`.
src/actions/bring.ts added +138
@@ -0,0 +1,138 @@
1 /**
2 * Bring an app whole (#8904): paste the link to an app on another forge and
3 * it is mirrored here, registered as an app and deployed, with its link back
4 * kept (a mirror knows its origin, so improvements flow to the author).
5 *
6 * What comes: the code and its history, and how it builds and runs (its own
7 * `forge.json`). What never comes by itself: data, variables and secrets.
8 * The app lands private; what it asks for (the names in its `.env.example`)
9 * is listed and asked for in one form.
10 *
11 * The page and nothing else calls this today; it is the same two steps an
12 * agent already has (`mirror` an origin repository, then enable the app).
13 */
14
15 import type { App } from "../app.ts";
16 import { getAppByRepo, slugFor, getAppBySlug, SLUG_MAX, type AppRecord } from "../core/apps.ts";
17 import { listAppEnvNames } from "../core/app_env.ts";
18 import { listOrigins } from "../core/origins.ts";
19 import { getRepoById, type Repo } from "../core/repos.ts";
20 import { isActive, type User } from "../core/users.ts";
21 import { readerFor } from "../git/read.ts";
22 import { repoDir } from "../git/repos.ts";
23 import { err, ok, type Result } from "../kernel/result.ts";
24 import { startMirror } from "../origins/mirror.ts";
25 import type { MirrorJob } from "../origins/jobs.ts";
26 import { needsOf, originOnHost, parseBringLink } from "../origins/bring.ts";
27 import { requireVerified } from "./verify.ts";
28 import { appLimitWords } from "./app_limit.ts";
29 import { enableApp, setAppEnvVar } from "./apps.ts";
30
31 export type BringError =
32 | { readonly kind: "unauthorized" }
33 | { readonly kind: "unverified" }
34 | { readonly kind: "invalid"; readonly message: string }
35 /** No connected account on that forge: sign in there first. */
36 | { readonly kind: "connect"; readonly host: string };
37
38 const LINK_WORDS = {
39 empty: "Paste the link to an app.",
40 not_a_link: "That is not a link to a repository (https://host/owner/name).",
41 not_a_repository: "That link does not name a repository: it needs the owner and the name.",
42 } as const;
43
44 /** Start bringing the app at `link`; the job is the first deploy's first half. */
45 export function startBring(
46 app: App,
47 actor: User | undefined,
48 link: string,
49 ): Result<{ job: MirrorJob; originId: string }, BringError> {
50 if (actor === undefined || !isActive(actor) || actor.kind === "agent")
51 return err({ kind: "unauthorized" });
52 const proven = requireVerified(app, actor);
53 if (!proven.ok) return err({ kind: "unverified" });
54 const parsed = parseBringLink(link);
55 if (!parsed.ok) return err({ kind: "invalid", message: LINK_WORDS[parsed.error] });
56 const origin = originOnHost(listOrigins(app.db, actor.id), parsed.value.host);
57 if (origin === undefined) return err({ kind: "connect", host: parsed.value.host });
58 const job = startMirror(app, actor, origin, parsed.value.fullName);
59 return ok({ job, originId: origin.id });
60 }
61
62 export interface Brought {
63 readonly repo: Repo;
64 readonly app: AppRecord;
65 /** Names the app asks for, with whether each is already set. */
66 readonly needs: readonly { readonly name: string; readonly set: boolean }[];
67 }
68
69 function freeSlug(app: App, name: string, owner: string): string {
70 const base = slugFor(`${name}-${owner}`).slice(0, SLUG_MAX - 3).replace(/-+$/, "");
71 for (let n = 1; n < 100; n++) {
72 const candidate = n === 1 ? base : `${base}-${n}`;
73 if (getAppBySlug(app.db, candidate) === undefined) return candidate;
74 }
75 return `${base}-${Date.now().toString(36).slice(-4)}`;
76 }
77
78 /**
79 * Once the mirror is whole: register it as an app (private, once; asking
80 * again lands on the same app) and read what it asks for. Only the job's
81 * owner gets here.
82 */
83 export async function bringLanded(
84 app: App,
85 actor: User,
86 job: MirrorJob,
87 ): Promise<Result<Brought, { kind: "invalid"; message: string }>> {
88 if (job.repo === undefined) return err({ kind: "invalid", message: "the copy is not made yet" });
89 const repo = getRepoById(app.db, job.repo.id);
90 if (repo === undefined || repo.ownerId !== actor.id)
91 return err({ kind: "invalid", message: "the copy is not made yet" });
92 let record = getAppByRepo(app.db, repo.id);
93 if (record === undefined) {
94 // Private first: the code may be public, running it for the world is a choice.
95 const enabled = enableApp(app, actor, repo, {
96 slug: freeSlug(app, repo.name, actor.username),
97 access: "private",
98 });
99 if (!enabled.ok)
100 return err({
101 kind: "invalid",
102 message:
103 enabled.error === "app_limit"
104 ? appLimitWords(app, actor.id)
105 : `the app could not be registered: ${enabled.error}`,
106 });
107 record = enabled.value;
108 }
109 const example = await readerFor(repoDir(app.reposRoot, repo.ownerName, repo.name)).object(
110 "HEAD:.env.example",
111 );
112 const names = example.ok && example.value.type === "blob"
113 ? needsOf(new TextDecoder().decode(example.value.body.slice(0, 65_536)))
114 : [];
115 const have = new Set(listAppEnvNames(app.db, record.id).map((e) => e.name));
116 return ok({
117 repo,
118 app: record,
119 needs: names.map((name) => ({ name, set: have.has(name) })),
120 });
121 }
122
123 /** The one form: set each non-empty value, for names the app asked for only. */
124 export async function setBroughtEnv(
125 app: App,
126 actor: User,
127 brought: Brought,
128 values: ReadonlyMap<string, string>,
129 ): Promise<number> {
130 let count = 0;
131 for (const { name } of brought.needs) {
132 const value = values.get(name);
133 if (value === undefined || value === "") continue;
134 const set = await setAppEnvVar(app, actor, brought.repo, name, value);
135 if (set.ok) count++;
136 }
137 return count;
138 }
src/core/reserved.ts +1
@@ -68,6 +68,7 @@ export const SYSTEM_NAMES: readonly string[] = [
6868 "deploy-to",
6969 "pipeline",
7070 "events",
71 "bring",
7172 "explore",
7273 "family",
7374 "favicon.ico",
src/http/server.ts +5
@@ -409,6 +409,7 @@ import {
409409 browseRaw,
410410 browseTree,
411411 } from "../web/pages/browse.ts";
412 import { bringEnvSubmit, bringJobPage, bringPage, bringSubmit } from "../web/pages/bring.ts";
412413 import { pushThrough } from "../origins/push-through.ts";
413414 import { CAME_FROM_ELSEWHERE, forbiddenPage, notFoundPage, SENT_FROM_ELSEWHERE } from "../web/chrome.ts";
414415 import { targetForShortCode } from "../origins/oauth.ts";
@@ -764,6 +765,10 @@ export function buildRouter(): Router<Handler> {
764765 // is registered before /:owner/:name can claim the shape.
765766 router.add("GET", "/-/confirm", confirmPage);
766767 router.add("GET", "/new", newRepoPage);
768 router.add("GET", "/bring", bringPage);
769 router.add("POST", "/bring", bringSubmit);
770 router.add("GET", "/bring/:id/:job", bringJobPage);
771 router.add("POST", "/bring/:id/:job/env", bringEnvSubmit);
767772 router.add("GET", "/new/issue", newIssueAnywhere);
768773 router.add("GET", "/repos", myReposPage);
769774 router.add("GET", "/now/:stage", nowPage);
src/origins/bring.ts added +71
@@ -0,0 +1,71 @@
1 /**
2 * Bring an app whole: the pure half. A pasted link becomes the forge it is on
3 * and the repository it names; a connected origin is picked by host; and the
4 * variables an app asks for are read, by name only, from the `.env.example`
5 * it ships. Nothing here touches the network, the database or a value.
6 */
7
8 import type { Origin } from "./types.ts";
9
10 export interface BringLink {
11 /** `host[:port]` of the forge the link points at. */
12 readonly host: string;
13 /** `owner/name` on that forge. */
14 readonly fullName: string;
15 }
16
17 export type BringLinkError = "empty" | "not_a_link" | "not_a_repository";
18
19 const SEGMENT = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
20 const MAX_LINK = 300;
21
22 /**
23 * `https://codeberg.org/ana/notes`, `…/ana/notes.git` and deeper pages
24 * (`…/tree/main`, `…/pulls/3`) all name the same repository. Only http(s)
25 * links, with no credentials in them, are links.
26 */
27 export function parseBringLink(
28 text: string,
29 ): { ok: true; value: BringLink } | { ok: false; error: BringLinkError } {
30 const raw = text.trim();
31 if (raw === "") return { ok: false, error: "empty" };
32 if (raw.length > MAX_LINK) return { ok: false, error: "not_a_link" };
33 const url = URL.parse(/^[a-z][a-z0-9+.-]*:\/\//i.test(raw) ? raw : `https://${raw}`);
34 if (
35 url === null ||
36 (url.protocol !== "https:" && url.protocol !== "http:") ||
37 url.username !== "" ||
38 url.password !== "" ||
39 url.hostname === ""
40 )
41 return { ok: false, error: "not_a_link" };
42 const [owner, rawName] = url.pathname.split("/").filter((s) => s !== "");
43 const name = rawName?.replace(/\.git$/, "");
44 if (owner === undefined || name === undefined || !SEGMENT.test(owner) || !SEGMENT.test(name))
45 return { ok: false, error: "not_a_repository" };
46 return { ok: true, value: { host: url.host.toLowerCase(), fullName: `${owner}/${name}` } };
47 }
48
49 /** The account's connected origin on the link's host, if it has one. */
50 export function originOnHost(origins: readonly Origin[], host: string): Origin | undefined {
51 return origins.find((o) => !o.needsReauth && URL.parse(o.webUrl)?.host.toLowerCase() === host);
52 }
53
54 const NAME = /^[A-Z][A-Z0-9_]{0,63}$/;
55 const MAX_NEEDS = 40;
56
57 /**
58 * The variable names in a `.env.example`: `NAME=` lines, `export NAME=`
59 * allowed, comments and anything else ignored. Values are dropped at once —
60 * an example's values are the author's, never ours to carry.
61 */
62 export function needsOf(example: string): string[] {
63 const names: string[] = [];
64 for (const line of example.split(/\r?\n/)) {
65 const m = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(line);
66 const name = m?.[1];
67 if (name !== undefined && NAME.test(name) && !names.includes(name)) names.push(name);
68 if (names.length >= MAX_NEEDS) break;
69 }
70 return names;
71 }
src/web/client.ts +1 −1
@@ -151,7 +151,7 @@ export const mirrorJs = /* js */ `
151151 paint(job.stage, job.detail);
152152 if (job.stage === 'done' && job.repo) {
153153 source.close();
154 location.href = '/' + job.repo.ownerName + '/' + job.repo.name;
154 location.href = list.getAttribute('data-landing') || '/' + job.repo.ownerName + '/' + job.repo.name;
155155 } else if (job.stage === 'failed') {
156156 // The page for a failed job carries the reason and a way to retry,
157157 // and does not stream — so this loads it once, and stops.
src/web/layout.ts +3
@@ -229,6 +229,7 @@ function newMenu(ctx: Ctx): Html {
229229 ...newIssueItem(ctx),
230230 { label: "New app", href: "/apps/new", icon: "rocket" },
231231 { label: "New repository", href: "/new", icon: "repo" },
232 { label: "Bring an app", href: "/bring", icon: "download" },
232233 { label: "Migrate", href: "/migrate", icon: "download" },
233234 { label: "New organization", href: "/organizations/new", icon: "users" },
234235 ...forgeDoor(ctx),
@@ -261,6 +262,7 @@ function userMenu(
261262 ...newIssueItem(ctx),
262263 { label: "New app", href: "/apps/new", icon: "rocket" },
263264 { label: "New repository", href: "/new", icon: "repo" },
265 { label: "Bring an app", href: "/bring", icon: "download" },
264266 { label: "Migrate", href: "/migrate", icon: "download" },
265267 ...forgeDoor(ctx),
266268 { separator: true, label: "" },
@@ -330,6 +332,7 @@ const STATIC_PALETTE: PaletteItem[] = [
330332 { t: "Front page", h: "/front", c: "Go to" },
331333 { t: "New app", h: "/apps/new", c: "Create" },
332334 { t: "New repository", h: "/new", c: "Create" },
335 { t: "Bring an app from another forge", h: "/bring", c: "Create" },
333336 { t: "Migrate a repository", h: "/migrate", c: "Create" },
334337 { t: "New organization", h: "/organizations/new", c: "Create" },
335338 { t: "Notes", h: "/notes", c: "Go to" },
src/web/pages/bring.ts added +196
@@ -0,0 +1,196 @@
1 /**
2 * `/bring`: paste the link to an app on another forge, and it runs here.
3 *
4 * Three small pages, one per step, each working without JavaScript: the link
5 * form, the import while it happens (the same checklist a mirror shows), and
6 * the landing, which says what the app asks for and takes it in one form.
7 * The first deploy is then watched on the app's own page.
8 */
9
10 import { bringLanded, setBroughtEnv, startBring, type BringError } from "../../actions/bring.ts";
11 import { mirrorJobFor } from "../../actions/origins.ts";
12 import { notFoundPage } from "../chrome.ts";
13 import { readForm } from "../form.ts";
14 import { html, type Html } from "../html.ts";
15 import { page, redirect } from "../layout.ts";
16 import { mirrorJs } from "../client.ts";
17 import { btn, card, field, icon } from "../ui.ts";
18 import type { Ctx } from "../../http/context.ts";
19 import { accountOnly } from "./origins.ts";
20 import { stageList } from "./browse.ts";
21
22 const NOT_COPIED = ["data", "variables", "secrets"];
23
24 function form(ctx: Ctx, value: string, problem?: Html, status = 200): Response {
25 return page(
26 {
27 ctx,
28 title: "Bring an app",
29 body: html`<div class="page-head"><h1>Bring an app</h1></div>
30 <div class="narrow-wide">
31 ${card(
32 html`<p class="muted small">
33 Paste the link to an app on another forge. It comes across with
34 how it builds and runs, and goes live here at its own address.
35 Its ${NOT_COPIED.join(", ")} stay behind: you are asked for
36 what it needs next.
37 </p>
38 ${problem ?? null}
39 <form method="post" action="/bring" class="stack">
40 ${field({
41 label: "Link to the app",
42 name: "link",
43 type: "url",
44 value,
45 required: true,
46 autofocus: true,
47 maxlength: 300,
48 placeholder: "https://codeberg.org/ana/notes",
49 spellcheck: false,
50 })}
51 <div class="bar">${btn("Bring it here", { variant: "primary", icon: "download" })}</div>
52 </form>`,
53 { head: html`${icon("download")}<h2>Bring it here</h2>` },
54 )}
55 </div>`,
56 },
57 status,
58 );
59 }
60
61 const toast = (message: Html | string): Html =>
62 html`<div class="toast" data-tone="danger" role="alert">${icon("alert")}<div class="toast-body">${message}</div></div>`;
63
64 function refusal(ctx: Ctx, link: string, e: BringError): Response {
65 switch (e.kind) {
66 case "connect":
67 return form(
68 ctx,
69 link,
70 toast(
71 html`Sign in to <span class="mono">${e.host}</span> first:
72 <a href="/settings/origins">connect your account there</a>, then
73 paste the link again.`,
74 ),
75 400,
76 );
77 case "invalid":
78 return form(ctx, link, toast(e.message), 400);
79 case "unverified":
80 return form(ctx, link, toast("Verify your email first: an app runs code."), 403);
81 case "unauthorized":
82 return form(ctx, link, toast("Only a person's own account brings an app."), 403);
83 }
84 }
85
86 /** GET /bring */
87 export function bringPage(ctx: Ctx): Response {
88 const denied = accountOnly(ctx);
89 if (denied) return denied;
90 return form(ctx, ctx.url.searchParams.get("link")?.slice(0, 300) ?? "");
91 }
92
93 /** POST /bring */
94 export async function bringSubmit(ctx: Ctx): Promise<Response> {
95 const denied = accountOnly(ctx);
96 if (denied) return denied;
97 const data = await readForm(ctx, "admin");
98 if (data instanceof Response) return data;
99 const raw = data.get("link");
100 const link = typeof raw === "string" ? raw : "";
101 const started = startBring(ctx.app, ctx.auth.user, link);
102 if (!started.ok) return refusal(ctx, link.slice(0, 300), started.error);
103 return redirect(`/bring/${started.value.originId}/${started.value.job.id}`);
104 }
105
106 /** GET /bring/:id/:job */
107 export async function bringJobPage(ctx: Ctx): Promise<Response> {
108 const denied = accountOnly(ctx);
109 if (denied) return denied;
110 const originId = ctx.params.id ?? "";
111 const found = mirrorJobFor(ctx.app, ctx.auth.user, originId, ctx.params.job ?? "");
112 if (!found.ok) return notFoundPage(ctx);
113 const job = found.value;
114 const here = `/bring/${originId}/${job.id}`;
115
116 if (job.stage === "done") {
117 const brought = await bringLanded(ctx.app, ctx.auth.user!, job);
118 if (!brought.ok) return notFoundPage(ctx);
119 const { repo, needs } = brought.value;
120 const open = `/${repo.ownerName}/${repo.name}/app`;
121 const asking = needs.filter((n) => !n.set);
122 return page({
123 ctx,
124 title: `Brought ${job.fullName}`,
125 body: html`<div class="page-head"><h1>${job.fullName} is here</h1></div>
126 <div class="narrow-wide">
127 ${card(
128 html`<p>
129 It is deploying now as a private app, linked back to
130 <span class="mono">${job.fullName}</span> so your improvements
131 can flow to its author. Watch the first deploy on its page.
132 </p>
133 <div class="bar">${btn("Watch the first deploy", { href: open, variant: "primary", icon: "rocket" })}</div>`,
134 { head: html`${icon("rocket")}<h2>Live here</h2>` },
135 )}
136 ${asking.length === 0
137 ? null
138 : card(
139 html`<p class="muted small">
140 The app lists these in its <span class="mono">.env.example</span>.
141 Nothing came with it: give what you want it to have, leave the
142 rest empty. Values are sealed and the app redeploys.
143 </p>
144 <form method="post" action="${here}/env" class="stack">
145 ${asking.map((n) =>
146 field({ label: n.name, name: `v_${n.name}`, type: "password", mono: true, autocomplete: "off", spellcheck: false }),
147 )}
148 <div class="bar">${btn("Save and continue", { variant: "primary", icon: "key" })}</div>
149 </form>`,
150 { head: html`${icon("key")}<h2>What it needs</h2>` },
151 )}
152 </div>`,
153 });
154 }
155
156 const running = job.stage !== "failed";
157 const stream = `/api/v1/user/origins/${originId}/mirror/${job.id}/stream`;
158 return page({
159 ctx,
160 title: `Bringing ${job.fullName}`,
161 body: html`<div class="page-head"><h1>Bringing ${job.fullName}</h1></div>
162 <div class="narrow-wide">
163 ${card(stageList(job, running ? stream : "", here), {
164 head: html`${icon("download")}<h2>${job.fullName}</h2>`,
165 })}
166 ${job.stage === "failed"
167 ? html`${toast(job.error ?? "The import failed.")}
168 <div class="bar mt">${btn("Try another link", { href: "/bring", variant: "outline" })}</div>`
169 : html`<p class="muted small mt">
170 This keeps going if you close the page; come back to this link.
171 <noscript>Without JavaScript, <a href="${here}">reload</a> to see where it is.</noscript>
172 </p>`}
173 </div>`,
174 scriptJs: mirrorJs,
175 });
176 }
177
178 /** POST /bring/:id/:job/env */
179 export async function bringEnvSubmit(ctx: Ctx): Promise<Response> {
180 const denied = accountOnly(ctx);
181 if (denied) return denied;
182 const data = await readForm(ctx, "admin");
183 if (data instanceof Response) return data;
184 const found = mirrorJobFor(ctx.app, ctx.auth.user, ctx.params.id ?? "", ctx.params.job ?? "");
185 if (!found.ok) return notFoundPage(ctx);
186 const brought = await bringLanded(ctx.app, ctx.auth.user!, found.value);
187 if (!brought.ok) return notFoundPage(ctx);
188 const values = new Map<string, string>();
189 for (const { name } of brought.value.needs) {
190 const v = data.get(`v_${name}`);
191 if (typeof v === "string") values.set(name, v);
192 }
193 await setBroughtEnv(ctx.app, ctx.auth.user!, brought.value, values);
194 const { repo } = brought.value;
195 return redirect(`/${repo.ownerName}/${repo.name}/app`);
196 }
src/web/pages/browse.ts +2 −1
@@ -1011,13 +1011,14 @@ const STAGES: { name: MirrorStage; label: string }[] = [
10111011 const ORDER = STAGES.map((s) => s.name);
10121012
10131013 /** The checklist. Its shape is what `mirrorJs` updates in place. */
1014 export function stageList(job: MirrorJob, streamUrl: string): Html {
1014 export function stageList(job: MirrorJob, streamUrl: string, landing = ""): Html {
10151015 return html`<ol
10161016 class="list stages"
10171017 aria-live="polite"
10181018 id="stages"
10191019 data-stream="${streamUrl}"
10201020 data-stage="${job.stage}"
1021 ${landing === "" ? null : html`data-landing="${landing}"`}
10211022 data-repo="${job.repo === undefined
10221023 ? ""
10231024 : `/${job.repo.ownerName}/${job.repo.name}`}"
test/bring.test.ts added +193
@@ -0,0 +1,193 @@
1 import { afterAll, beforeAll, describe, expect, setDefaultTimeout, 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 { getAppByRepo } from "../src/core/apps.ts";
7 import { listAppEnvNames } from "../src/core/app_env.ts";
8 import { createOrigin } from "../src/core/origins.ts";
9 import { getRepo } from "../src/core/repos.ts";
10 import { getUserByName } from "../src/core/users.ts";
11 import { serve, type Servers } from "../src/http/server.ts";
12 import { settled } from "../src/origins/jobs.ts";
13 import { needsOf, originOnHost, parseBringLink } from "../src/origins/bring.ts";
14 import { syncsSettled } from "../src/origins/sync.ts";
15 import { SYSTEM_NAMES } from "../src/core/reserved.ts";
16 import { newFakeState, serveFakeForge, type FakeForge } from "./fake-origin.ts";
17 import { proveAll } from "./helpers/verified.ts";
18 import { seedAdmin } from "./helpers/admin.ts";
19
20 setDefaultTimeout(60_000);
21
22 describe("the link and the needs", () => {
23 test("a link names its host and repository, however deep or dressed", () => {
24 for (const text of [
25 "https://codeberg.org/ana/notes",
26 " https://Codeberg.org/ana/notes.git ",
27 "https://codeberg.org/ana/notes/pulls/3?x=1#y",
28 "codeberg.org/ana/notes",
29 ])
30 expect(parseBringLink(text)).toEqual({
31 ok: true,
32 value: { host: "codeberg.org", fullName: "ana/notes" },
33 });
34 });
35
36 test("anything else is refused with a reason", () => {
37 expect(parseBringLink("")).toEqual({ ok: false, error: "empty" });
38 expect(parseBringLink("javascript:alert(1)")).toEqual({ ok: false, error: "not_a_link" });
39 expect(parseBringLink("ftp://x.org/a/b")).toEqual({ ok: false, error: "not_a_link" });
40 expect(parseBringLink("https://u:[email protected]/a/b")).toEqual({ ok: false, error: "not_a_link" });
41 expect(parseBringLink("https://x.org/ana")).toEqual({ ok: false, error: "not_a_repository" });
42 expect(parseBringLink("https://x.org/a/..")).toEqual({ ok: false, error: "not_a_repository" });
43 expect(parseBringLink("https://x.org/a/b".padEnd(400, "c"))).toEqual({ ok: false, error: "not_a_link" });
44 });
45
46 test("an origin is picked by host, and a spent one is not", () => {
47 const o = (webUrl: string, needsReauth: boolean) => ({ webUrl, needsReauth }) as never;
48 expect(originOnHost([o("https://a.org", false), o("https://b.org", false)], "b.org")).toBeDefined();
49 expect(originOnHost([o("https://b.org", true)], "b.org")).toBeUndefined();
50 expect(originOnHost([], "b.org")).toBeUndefined();
51 });
52
53 test("needs are names only, from NAME= lines", () => {
54 expect(
55 needsOf("# keys\nAPI_KEY=abc123\nexport DB_URL = x\nlower=1\nAPI_KEY=again\n\nPORT=3000\n"),
56 ).toEqual(["API_KEY", "DB_URL", "PORT"]);
57 });
58
59 test("bring is a page, so no account may take the name", () => {
60 expect(SYSTEM_NAMES).toContain("bring");
61 });
62 });
63
64 const LOGIN = "octo";
65 const TOKEN = "octo-secret-7089";
66 const PASSWORD = "alice-secret-7089";
67 const basic = (user: string, secret: string): string =>
68 `Basic ${Buffer.from(`${user}:${secret}`).toString("base64")}`;
69
70 describe("bringing an app whole", () => {
71 let dirs: string[] = [];
72 let app: App;
73 let gitApp: App;
74 let servers: Servers;
75 let gitServers: Servers;
76 let fake: FakeForge;
77 const alice = () => getUserByName(app.db, "alice")!;
78 const post = (path: string, body: Record<string, string>) =>
79 fetch(`${servers.url}${path}`, {
80 method: "POST",
81 redirect: "manual",
82 headers: { authorization: basic("alice", PASSWORD), "content-type": "application/x-www-form-urlencoded" },
83 body: new URLSearchParams(body).toString(),
84 });
85
86 beforeAll(async () => {
87 dirs = [await mkdtemp(join(tmpdir(), "forge-bring-")), await mkdtemp(join(tmpdir(), "forge-bring-git-"))];
88 gitApp = createApp({ dataDir: dirs[1]!, port: 0 });
89 gitServers = await serve(gitApp);
90 await seedAdmin(gitApp, { username: LOGIN, email: "[email protected]", password: TOKEN });
91 proveAll(gitApp);
92 const auth = { "content-type": "application/json", authorization: basic(LOGIN, TOKEN) };
93 await fetch(`${gitServers.url}/api/v1/repos`, {
94 method: "POST",
95 headers: auth,
96 body: JSON.stringify({ name: "widgets", visibility: "public" }),
97 });
98 const made = await fetch(`${gitServers.url}/api/v1/repos/octo/widgets/contents`, {
99 method: "POST",
100 headers: auth,
101 body: JSON.stringify({
102 branch: "main",
103 message: "an app",
104 files: [
105 { path: "forge.json", content: '{"app":{"run":"bun run index.ts"}}\n' },
106 { path: ".env.example", content: "API_KEY=changeme\nGREETING=hello\n" },
107 { path: ".env", content: "API_KEY=real-secret\n" },
108 ],
109 }),
110 });
111 expect(made.status).toBeLessThan(300);
112 const state = newFakeState("forgejo", LOGIN, TOKEN);
113 state.repos.push({
114 id: 41,
115 fullName: "octo/widgets",
116 defaultBranch: "main",
117 cloneUrl: `${gitServers.url}/octo/widgets.git`,
118 });
119 fake = serveFakeForge(state);
120 state.repos[0]!.htmlUrl = `${fake.url}/octo/widgets`;
121 app = createApp({ dataDir: dirs[0]!, port: 0 });
122 servers = await serve(app);
123 await seedAdmin(app, { username: "alice", email: "[email protected]", password: PASSWORD });
124 proveAll(app);
125 }, 120_000);
126
127 afterAll(async () => {
128 await syncsSettled();
129 fake.stop();
130 servers.stop();
131 gitServers.stop();
132 app.close();
133 gitApp.close();
134 for (const d of dirs) await rm(d, { recursive: true, force: true, maxRetries: 5 });
135 }, 120_000);
136
137 test("the page asks for a link", async () => {
138 const res = await fetch(`${servers.url}/bring`, { headers: { authorization: basic("alice", PASSWORD) } });
139 expect(res.status).toBe(200);
140 expect(await res.text()).toContain('name="link"');
141 });
142
143 test("a bad link and an unconnected forge are refused in words, starting nothing", async () => {
144 const bad = await post("/bring", { link: "nonsense" });
145 expect(bad.status).toBe(400);
146 const none = await post("/bring", { link: `${fake.url}/octo/widgets` });
147 expect(none.status).toBe(400);
148 expect(await none.text()).toContain("/settings/origins");
149 expect(getRepo(app.db, "alice", "widgets")).toBeUndefined();
150 });
151
152 test("a link comes across, private, linked back, asking only by name", async () => {
153 const made = createOrigin(app.db, app.config.dataDir, {
154 userId: alice().id,
155 kind: "forgejo",
156 apiUrl: fake.apiUrl,
157 webUrl: fake.url,
158 login: LOGIN,
159 token: TOKEN,
160 });
161 if (!made.ok) throw new Error(made.error);
162
163 const started = await post("/bring", { link: `${fake.url}/octo/widgets.git` });
164 expect(started.status).toBe(303);
165 const where = started.headers.get("location")!;
166 const jobId = where.split("/").pop()!;
167 expect(where).toBe(`/bring/${made.value.id}/${jobId}`);
168 expect((await settled(jobId))?.stage).toBe("done");
169
170 const landed = await fetch(`${servers.url}${where}`, { headers: { authorization: basic("alice", PASSWORD) } });
171 expect(landed.status).toBe(200);
172 const text = await landed.text();
173 expect(text).toContain("/alice/widgets/app");
174 expect(text).toContain("API_KEY");
175 expect(text).toContain("GREETING");
176 expect(text).not.toContain("real-secret");
177
178 const repo = getRepo(app.db, "alice", "widgets")!;
179 expect(getAppByRepo(app.db, repo.id)?.access).toBe("private");
180 const link = app.db
181 .query<{ origin_full_name: string }, [string]>("SELECT origin_full_name FROM repo WHERE id = ?1")
182 .get(repo.id);
183 expect(link?.origin_full_name).toBe("octo/widgets");
184 // Nothing came by default: not the example's values, not the author's .env.
185 expect(listAppEnvNames(app.db, getAppByRepo(app.db, repo.id)!.id)).toEqual([]);
186
187 // One form sets what was filled, only for names the app asked for.
188 const saved = await post(`${where}/env`, { v_API_KEY: "mine", v_GREETING: "", v_EVIL: "x" });
189 expect(saved.status).toBe(303);
190 expect(saved.headers.get("location")).toBe("/alice/widgets/app");
191 expect(listAppEnvNames(app.db, getAppByRepo(app.db, repo.id)!.id).map((e) => e.name)).toEqual(["API_KEY"]);
192 });
193 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs