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

Opening an app's environment through the API names its Server-Timing phases: session, repo, open #5385

Merged opened this

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

Merged

scout/forge:scout/5361-opening-an-app-s-environment-through-the → main 1 commit fork

  • Merged. scout/5361-opening-an-app-s-environment-through-the was merged into main as 2523e47.

Review

Sign in to review this change.
2 files changed +90 −2
src/http/api.ts +5 −2
@@ -6005,9 +6005,12 @@ export function apiRoutes(router: Router<Handler>): void {
60056005 // The URL to open an environment at. For a private app it carries a
60066006 // grant, so the answer is for the caller alone and is not cached.
60076007 router.add("GET", "/api/v1/repos/:owner/:name/app/:env/open", (ctx) => {
6008 const repo = loadRepo(ctx);
6008 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
6009 const repo = timePhase(ctx.request, "repo", () => loadRepo(ctx));
60096010 if (!repo) return notFound();
6010 const link = openEnvUrl(ctx.app, ctx.auth.user, repo, ctx.params.env ?? "");
6011 const link = timePhase(ctx.request, "open", () =>
6012 openEnvUrl(ctx.app, ctx.auth.user, repo, ctx.params.env ?? ""),
6013 );
60116014 if (!link.ok) return respondAccess(link.error, 422);
60126015 return json({ url: link.value });
60136016 });
test/api-app-open-timing.test.ts added +85
@@ -0,0 +1,85 @@
1 /** Opening an app's environment through the API names its Server-Timing phases (#5361). */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { AppsRuntime } from "../src/apps/runtime.ts";
8 import { createRepo } from "../src/actions/repos.ts";
9 import { createApp as insertApp } from "../src/core/apps.ts";
10 import { createSession } from "../src/core/sessions.ts";
11 import { createUser } from "../src/core/users.ts";
12 import { serve, type Servers } from "../src/http/server.ts";
13 import { quietLogs } from "./helpers/globals.ts";
14
15 quietLogs();
16
17 let dataDir: string;
18 let app: App;
19 let servers: Servers;
20 let runtime: AppsRuntime;
21 let cookie = "";
22
23 const cells = process.env["FORGE_CELLS"];
24
25 beforeAll(async () => {
26 process.env["FORGE_CELLS"] = "off";
27 dataDir = await mkdtemp(join(tmpdir(), "forge-api-app-open-timing-"));
28 app = createApp({ dataDir, port: 0 });
29 servers = await serve(app);
30 runtime = new AppsRuntime(app, {
31 domain: "apps.test",
32 scheme: "http",
33 publicPort: servers.port,
34 forgeUrl: servers.url,
35 });
36 await runtime.start();
37 const made = await createUser(app.db, {
38 origin: "seed",
39 trusted: true,
40 username: "alice",
41 email: "[email protected]",
42 password: "a-good-passphrase",
43 adminIfFirst: true,
44 });
45 if (!made.ok) throw new Error(String(made.error));
46 const alice = made.value;
47 cookie = `forge_session=${createSession(app.db, alice.id).token}`;
48 const repo = await createRepo(app, alice, { name: "r", visibility: "public" });
49 if (!repo.ok) throw new Error(String(repo.error));
50 insertApp(app.db, { repoId: repo.value.id, slug: "r", access: "public" });
51 });
52 afterAll(async () => {
53 await runtime.stop();
54 await servers.stop();
55 app.close();
56 if (cells === undefined) delete process.env["FORGE_CELLS"];
57 else process.env["FORGE_CELLS"] = cells;
58 await rm(dataDir, { recursive: true, force: true });
59 });
60
61 async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> {
62 const res = await fetch(`${servers.url}${path}`, { headers: { cookie } });
63 const body = await res.text();
64 const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!);
65 return { status: res.status, names: names.slice(names.indexOf("app") + 1), body };
66 }
67
68 test("opening production names session, repo, open in order, answer unchanged", async () => {
69 const got = await phases("/api/v1/repos/alice/r/app/production/open");
70 expect(got.status).toBe(200);
71 expect(got.names).toEqual(["session", "repo", "open"]);
72 expect(typeof JSON.parse(got.body).url).toBe("string");
73 });
74
75 test("an unknown environment is still a 422 naming session, repo, open", async () => {
76 const got = await phases("/api/v1/repos/alice/r/app/nope/open");
77 expect(got.status).toBe(422);
78 expect(got.names).toEqual(["session", "repo", "open"]);
79 });
80
81 test("a missing repository is a 404 naming session, repo", async () => {
82 const got = await phases("/api/v1/repos/alice/nope/app/production/open");
83 expect(got.status).toBe(404);
84 expect(got.names).toEqual(["session", "repo"]);
85 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs