Public: anyone can read this; the crew can change it.
Every open pull's preview is one click from the app's studio page #29
scout/forge:scout/25-every-open-pull-s-preview-is-one-click-f → main 1 commit fork
2 files
changed +207
−3
src/web/pages/studio.ts +91 −2
| @@ -19,7 +19,13 @@ import { | ||
| 19 | 19 | } from "../../actions/issues.ts"; |
| 20 | 20 | import { mergePull } from "../../actions/pulls.ts"; |
| 21 | 21 | import { tellCrew } from "../../actions/crew.ts"; |
| 22 | import { appsRuntime, type EnvStatus } from "../../apps/runtime.ts"; | |
| 22 | import { envPull, previewEnv } from "../../apps/data.ts"; | |
| 23 | import { | |
| 24 | appsRuntime, | |
| 25 | type AppsRuntime, | |
| 26 | type EnvState, | |
| 27 | type EnvStatus, | |
| 28 | } from "../../apps/runtime.ts"; | |
| 23 | 29 | import { listCrewRoles, type Agent } from "../../core/agents.ts"; |
| 24 | 30 | import { appsForRepos, getAppByRepo, type AppRecord } from "../../core/apps.ts"; |
| 25 | 31 | import { |
| @@ -69,6 +75,7 @@ import { liveClientJs, liveMount } from "../live.ts"; | ||
| 69 | 75 | import { isMode, modeCookie } from "../mode.ts"; |
| 70 | 76 | import { |
| 71 | 77 | avatar, |
| 78 | badge, | |
| 72 | 79 | btn, |
| 73 | 80 | card, |
| 74 | 81 | dot, |
| @@ -431,6 +438,87 @@ const WORD_ICON: Record<Word, Parameters<typeof icon>[0]> = { | ||
| 431 | 438 | planned: "map", |
| 432 | 439 | }; |
| 433 | 440 | |
| 441 | // ---- previews: every open pull's preview, one click away ---- | |
| 442 | ||
| 443 | /** The three words the studio uses for a preview, whatever its check says. */ | |
| 444 | function previewBadge(state: EnvState | undefined): Html { | |
| 445 | if (state === "running") | |
| 446 | return badge("live", { tone: "success", icon: "play" }); | |
| 447 | if (state === "deploying") | |
| 448 | return badge("starting", { tone: "attn", icon: "loader" }); | |
| 449 | return badge("not up", { | |
| 450 | ...(state === "failed" ? { tone: "danger" as const } : {}), | |
| 451 | icon: "square", | |
| 452 | }); | |
| 453 | } | |
| 454 | ||
| 455 | /** | |
| 456 | * One open pull's preview, as one row: its host is the click, its state is | |
| 457 | * the badge, and a check still running or a check that failed says so right | |
| 458 | * there — the row never hides a preview that is serving. | |
| 459 | */ | |
| 460 | function previewRow( | |
| 461 | base: string, | |
| 462 | runtime: AppsRuntime | undefined, | |
| 463 | record: AppRecord, | |
| 464 | pull: Pull, | |
| 465 | env: EnvStatus | undefined, | |
| 466 | ): Html { | |
| 467 | const href = | |
| 468 | runtime === undefined | |
| 469 | ? undefined | |
| 470 | : `${base}/app/${previewEnv(pull.number)}/open`; | |
| 471 | const host = | |
| 472 | runtime === undefined | |
| 473 | ? undefined | |
| 474 | : runtime.urlFor(record, previewEnv(pull.number)).replace(/^https?:\/\//, ""); | |
| 475 | const bits: Html[] = [html`#${pull.number} ${pull.title}`]; | |
| 476 | if (env?.commitOid) bits.push(html`${env.commitOid.slice(0, 7)}`); | |
| 477 | if (env?.since) bits.push(html`up ${time(env.since)}`); | |
| 478 | const say = env?.error ?? env?.note; | |
| 479 | if (say) bits.push(html`${say}`); | |
| 480 | return row({ | |
| 481 | ...(href === undefined ? {} : { href, nav: true }), | |
| 482 | lead: icon("rocket"), | |
| 483 | title: | |
| 484 | host === undefined | |
| 485 | ? `#${pull.number} ${pull.title}` | |
| 486 | : html`<span class="mono">${host}</span>`, | |
| 487 | meta: bits.reduce((a, b) => html`${a} · ${b}`), | |
| 488 | trail: previewBadge(env?.state), | |
| 489 | }); | |
| 490 | } | |
| 491 | ||
| 492 | /** | |
| 493 | * Every open pull's preview, one row each, from the runtime's own `status()` | |
| 494 | * — no per-pull query, so the list costs nothing extra as pulls pile up. A | |
| 495 | * pull with no environment yet still gets a row, so the list matches the | |
| 496 | * open pulls exactly. | |
| 497 | */ | |
| 498 | function previewsPanel( | |
| 499 | base: string, | |
| 500 | runtime: AppsRuntime | undefined, | |
| 501 | record: AppRecord, | |
| 502 | openPulls: readonly Pull[], | |
| 503 | envs: readonly EnvStatus[], | |
| 504 | ): Html | null { | |
| 505 | if (openPulls.length === 0) return null; | |
| 506 | const byPull = new Map( | |
| 507 | envs | |
| 508 | .filter((e) => e.env !== "production") | |
| 509 | .map((e) => [envPull(e.env), e] as const), | |
| 510 | ); | |
| 511 | return listCard( | |
| 512 | openPulls.map((p) => | |
| 513 | previewRow(base, runtime, record, p, byPull.get(p.number)), | |
| 514 | ), | |
| 515 | { | |
| 516 | head: html`${icon("rocket")} | |
| 517 | <h2>Previews</h2>`, | |
| 518 | }, | |
| 519 | ); | |
| 520 | } | |
| 521 | ||
| 434 | 522 | // ---- the app in the studio ---- |
| 435 | 523 | |
| 436 | 524 | export async function studioApp( |
| @@ -477,6 +565,7 @@ export async function studioApp( | ||
| 477 | 565 | // ---- the app itself ---- |
| 478 | 566 | const runtime = appsRuntime(ctx.app); |
| 479 | 567 | const envs: EnvStatus[] = runtime === undefined ? [] : runtime.status(record); |
| 568 | const previews = previewsPanel(base, runtime, record, openPulls, envs); | |
| 480 | 569 | const production = envs.find((e) => e.env === "production"); |
| 481 | 570 | const url = runtime?.urlFor(record, "production"); |
| 482 | 571 | const state = production?.state ?? "stopped"; |
| @@ -781,7 +870,7 @@ export async function studioApp( | ||
| 781 | 870 | })} |
| 782 | 871 | ${head} |
| 783 | 872 | <div class="dash"> |
| 784 | <div class="dash-main stack">${tell} ${asks} ${now}</div> | |
| 873 | <div class="dash-main stack">${tell} ${asks} ${previews} ${now}</div> | |
| 785 | 874 | <aside class="dash-rail stack"> |
| 786 | 875 | ${crewCard} ${weekCard} ${ownCard} ${more} |
| 787 | 876 | </aside> |
test/studio.test.ts +116 −1
| @@ -9,8 +9,9 @@ import { join } from "node:path"; | ||
| 9 | 9 | import { createApp, type App } from "../src/app.ts"; |
| 10 | 10 | import { enableApp } from "../src/actions/apps.ts"; |
| 11 | 11 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; |
| 12 | import { openPull } from "../src/actions/pulls.ts"; | |
| 12 | import { mergePull, openPull } from "../src/actions/pulls.ts"; | |
| 13 | 13 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 14 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 14 | 15 | import { getIssue, listIssues } from "../src/core/issues.ts"; |
| 15 | 16 | import { getPull, type Pull } from "../src/core/pulls.ts"; |
| 16 | 17 | import type { Repo } from "../src/core/repos.ts"; |
| @@ -369,3 +370,117 @@ describe("the studio", () => { | ||
| 369 | 370 | expect(after).not.toContain('action="/studio/asks"'); |
| 370 | 371 | }); |
| 371 | 372 | }); |
| 373 | ||
| 374 | describe("every open pull's preview on the studio page", () => { | |
| 375 | let dataDir: string; | |
| 376 | let app: App; | |
| 377 | let servers: Servers; | |
| 378 | let runtime: AppsRuntime; | |
| 379 | let base: string; | |
| 380 | let cookie: string; | |
| 381 | let repo: Repo; | |
| 382 | ||
| 383 | const get = (path: string) => | |
| 384 | fetch(`${base}${path}`, { | |
| 385 | headers: { cookie: `${cookie}; forge_mode=studio` }, | |
| 386 | redirect: "manual", | |
| 387 | }); | |
| 388 | ||
| 389 | beforeAll(async () => { | |
| 390 | dataDir = await mkdtemp(join(tmpdir(), "forge-studio-previews-")); | |
| 391 | app = createApp({ dataDir, port: 0 }); | |
| 392 | servers = await serve(app); | |
| 393 | base = servers.url; | |
| 394 | runtime = new AppsRuntime(app, { | |
| 395 | domain: "studio-previews.test", | |
| 396 | scheme: "http", | |
| 397 | publicPort: servers.port, | |
| 398 | forgeUrl: servers.url, | |
| 399 | concurrency: 1, | |
| 400 | }); | |
| 401 | await runtime.start(); | |
| 402 | const a = await createUser(app.db, { | |
| 403 | username: "alice", | |
| 404 | email: "[email protected]", | |
| 405 | password: "a-good-passphrase", | |
| 406 | adminIfFirst: true, | |
| 407 | }); | |
| 408 | if (!a.ok) throw new Error(a.error); | |
| 409 | cookie = ( | |
| 410 | ( | |
| 411 | await fetch(`${base}/signin`, { | |
| 412 | method: "POST", | |
| 413 | body: new URLSearchParams({ | |
| 414 | username: "alice", | |
| 415 | password: "a-good-passphrase", | |
| 416 | }), | |
| 417 | redirect: "manual", | |
| 418 | }) | |
| 419 | ).headers.get("set-cookie") ?? "" | |
| 420 | ).split(";")[0]!; | |
| 421 | const made = await createRepo(app, a.value, { | |
| 422 | name: "widget", | |
| 423 | visibility: "public", | |
| 424 | description: "", | |
| 425 | }); | |
| 426 | if (!made.ok) throw new Error(String(made.error)); | |
| 427 | repo = made.value; | |
| 428 | const seeded = await commitToBranch(app, a.value, repo, { | |
| 429 | message: "Start", | |
| 430 | files: [ | |
| 431 | { | |
| 432 | path: "package.json", | |
| 433 | content: '{"name":"widget","scripts":{"start":"bun run server.ts"}}\n', | |
| 434 | }, | |
| 435 | { | |
| 436 | path: "server.ts", | |
| 437 | content: | |
| 438 | "Bun.serve({ port: Number(process.env.PORT), fetch: () => new Response('ok') });\n", | |
| 439 | }, | |
| 440 | ], | |
| 441 | }); | |
| 442 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 443 | const enabled = await enableApp(app, a.value, repo, { slug: "widget" }); | |
| 444 | if (!enabled.ok) throw new Error(String(enabled.error)); | |
| 445 | }, 30_000); | |
| 446 | ||
| 447 | afterAll(async () => { | |
| 448 | await runtime.stop(); | |
| 449 | servers.stop(); | |
| 450 | app.close(); | |
| 451 | await rm(dataDir, { recursive: true, force: true }); | |
| 452 | }); | |
| 453 | ||
| 454 | test("an open pull's preview host and open link render; none do once nothing is open", async () => { | |
| 455 | const alice = getUserByName(app.db, "alice")!; | |
| 456 | const before = await (await get("/alice/widget")).text(); | |
| 457 | expect(before).not.toContain("<h2>Previews</h2>"); | |
| 458 | ||
| 459 | const branch = await commitToBranch(app, alice, repo, { | |
| 460 | branch: "feature/x", | |
| 461 | base: repo.defaultBranch, | |
| 462 | message: "Add X", | |
| 463 | files: [{ path: "X.md", content: "x\n" }], | |
| 464 | }); | |
| 465 | if (!branch.ok) throw new Error(JSON.stringify(branch.error)); | |
| 466 | const opened = await openPull(app, alice, repo, { | |
| 467 | title: "Add X", | |
| 468 | body: "", | |
| 469 | baseBranch: repo.defaultBranch, | |
| 470 | headBranch: "feature/x", | |
| 471 | }); | |
| 472 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 473 | const pull = opened.value; | |
| 474 | ||
| 475 | const page = await (await get("/alice/widget")).text(); | |
| 476 | expect(page).toContain("<h2>Previews</h2>"); | |
| 477 | expect(page).toContain(`href="/alice/widget/app/pull-${pull.number}/open"`); | |
| 478 | expect(page).toContain(`#${pull.number} Add X`); | |
| 479 | ||
| 480 | const merged = await mergePull(app, alice, repo, pull, "squash"); | |
| 481 | expect(merged.ok).toBe(true); | |
| 482 | const after = await (await get("/alice/widget")).text(); | |
| 483 | expect(after).not.toContain("<h2>Previews</h2>"); | |
| 484 | expect(after).not.toContain(`pull-${pull.number}/open`); | |
| 485 | }, 30_000); | |
| 486 | }); |
new above
offline