Public: anyone can read this; the crew can change it.
A preview's forge toolbar names the pull it previews and links to it #13148
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13128-a-preview-s-forge-toolbar-names-the-pull → main 5 commits fork
7 files
changed +85
−31
src/apps/proxy.ts +9 −5
| @@ -440,9 +440,9 @@ function sealedPull(runtime: AppsRuntime, target: AppTarget): Issue | undefined | ||
| 440 | 440 | /** |
| 441 | 441 | * The response with the phases the request named, as `Server-Timing`: the |
| 442 | 442 | * one writer for the proxy, on the forge's own `/__forge/*` pages and, for |
| 443 | * a reader who may see them (`relayAnswer`), on a relayed app answer. It appends, so an app's own entries stay first; | |
| 444 | * on an app answer the forge's phases are `forge-` named (`relayPhase`) so | |
| 445 | * none can collide with the app's or with the server's `route`. | |
| 443 | * a reader who may see them (`relayAnswer`), on a relayed app answer. It appends, so an app's own | |
| 444 | * entries stay first; on an app answer the forge's phases are `forge-` named (`relayPhase`) so none | |
| 445 | * can collide with the app's or with the server's `route`. | |
| 446 | 446 | */ |
| 447 | 447 | function withPhases(request: Request, res: Response): Response { |
| 448 | 448 | const phases = takePhases(request); |
| @@ -579,13 +579,15 @@ export function pillWhere( | ||
| 579 | 579 | const number = /^pull-(\d+)$/.exec(target.env)?.[1]; |
| 580 | 580 | if (number === undefined) return where; |
| 581 | 581 | where.pull = Number(number); |
| 582 | // What a waiting guest's preview runs on is said to whoever looks at it: it is about the page in front of them. | |
| 582 | // What a waiting guest's preview runs on is said to whoever looks at it: it is about the page in | |
| 583 | // front of them. | |
| 583 | 584 | const row = getPull(db, target.app.repoId, where.pull); |
| 584 | 585 | if (row !== undefined && guestWaits(db, row)) where.waits = true; |
| 585 | 586 | const pull = getIssue(db, target.app.repoId, where.pull); |
| 586 | 587 | if (pull === undefined) return where; |
| 587 | 588 | if (!reads || !mayView(db, repo, pull, user)) return where; |
| 588 | 589 | where.title = pull.title; |
| 590 | if (row !== undefined && row.headBranch !== "") where.branch = row.headBranch; | |
| 589 | 591 | const head = getPull(db, target.app.repoId, where.pull); |
| 590 | 592 | if (head !== undefined && head.headOid !== "") where.check = pillCheck(db, pull.id, head.headOid); |
| 591 | 593 | // How current the preview is: the row's head against the commit it runs. A |
| @@ -671,7 +673,9 @@ function sideFor( | ||
| 671 | 673 | return sideForViewer(runtime, target.app, target.env, getUserById(runtime.app.db, who.id)); |
| 672 | 674 | } |
| 673 | 675 | |
| 674 | /** The repository, when `viewer` is an active reader of it: the only one the other side is told to. */ | |
| 676 | /** | |
| 677 | * The repository, when `viewer` is an active reader of it: the only one the other side is told to. | |
| 678 | */ | |
| 675 | 679 | export function sideReader(db: App["db"], record: AppRecord, viewer: User | undefined): Repo | undefined { |
| 676 | 680 | if (viewer === undefined || !isActive(viewer)) return undefined; |
| 677 | 681 | const repo = getRepoById(db, record.repoId); |
src/apps/toolbar.ts +40 −14
| @@ -24,10 +24,15 @@ export const FRESHNESS_WORDS = { | ||
| 24 | 24 | behind: "Shows an older push than the pull's newest", |
| 25 | 25 | } as const; |
| 26 | 26 | |
| 27 | /** What the pill says on a guest pull's preview nobody has approved yet; sent with the tag, so the script carries no copy. */ | |
| 27 | /** | |
| 28 | * What the pill says on a guest pull's preview nobody has approved yet; sent with the tag, so the | |
| 29 | * script carries no copy. | |
| 30 | */ | |
| 28 | 31 | export const PREVIEW_WAITS = "This preview uses empty data until a writer approves the pull"; |
| 29 | 32 | |
| 30 | /** The client, as plain source: no `eval`, no inline handlers. Its comments are for readers here. */ | |
| 33 | /** | |
| 34 | * The client, as plain source: no `eval`, no inline handlers. Its comments are for readers here. | |
| 35 | */ | |
| 31 | 36 | const toolbarSource = `(function () { |
| 32 | 37 | if (window.__forgeToolbar) return; |
| 33 | 38 | window.__forgeToolbar = true; |
| @@ -92,21 +97,22 @@ const toolbarSource = `(function () { | ||
| 92 | 97 | if (d.env === "production") line("production"); |
| 93 | 98 | var LC = "#8ab4ff"; |
| 94 | 99 | var LS = "color:" + LC + ";text-decoration:none;display:flex;align-items:center;min-height:44px"; |
| 95 | var pullHref = ""; | |
| 100 | var pullHref = "", pv = d.env !== "production" && d.pull; | |
| 96 | 101 | // The pull this preview shows, one tap away: built from the script's own origin, never from page text. |
| 97 | if (d.env !== "production" && /^\\d{1,9}$/.test(d.pull || "") && /^[\\w.-]+\\/[\\w.-]+$/.test(d.repo || "")) { | |
| 102 | if (pv && /^\\d{1,9}$/.test(d.pull) && /^[\\w.-]+\\/[\\w.-]+$/.test(d.repo || "")) { | |
| 98 | 103 | try { pullHref = new URL("/" + d.repo + "/pulls/" + d.pull, fo).href; } catch (e) {} |
| 99 | 104 | } |
| 100 | if (d.env === "production") {} | |
| 101 | else if (pullHref) { | |
| 105 | if (pullHref) { | |
| 102 | 106 | var pl = document.createElement("a"); |
| 103 | pl.textContent = "preview \\u00b7 #" + d.pull; | |
| 107 | pl.textContent = "Pull #" + d.pull; | |
| 104 | 108 | pl.href = pullHref; |
| 105 | 109 | pl.rel = "noopener"; |
| 106 | 110 | pl.style.cssText = LS; |
| 107 | 111 | panel.appendChild(pl); |
| 108 | } else if (d.pull) line("preview \\u00b7 #" + d.pull); | |
| 112 | } else if (pv) line("Pull #" + d.pull); | |
| 113 | if (pv) line("Environment: preview"); | |
| 109 | 114 | if (d.title) line(d.title); |
| 115 | if (pv && d.branch) line("Branch: " + d.branch); | |
| 110 | 116 | var CW = ${JSON.stringify(CHECK_WORDS)}; |
| 111 | 117 | if (d.env !== "production" && d.check && Object.prototype.hasOwnProperty.call(CW, d.check)) line(CW[d.check]); |
| 112 | 118 | // Whether this is the pull as it stands now; an open, current preview says nothing. |
| @@ -605,7 +611,10 @@ const toolbarSource = `(function () { | ||
| 605 | 611 | })(); |
| 606 | 612 | `; |
| 607 | 613 | |
| 608 | /** What is served: the source without its whole-line comments, so the notes cost the pill none of its 24 KB (#10869). */ | |
| 614 | /** | |
| 615 | * What is served: the source without its whole-line comments, so the notes cost the pill none of | |
| 616 | * its 24 KB (#10869). | |
| 617 | */ | |
| 609 | 618 | export const toolbarJs = toolbarSource.replace(/^[ \t]*\/\/[^\n]*\n/gm, ""); |
| 610 | 619 | |
| 611 | 620 | const hash = sha256(toolbarJs); |
| @@ -617,7 +626,9 @@ export const TOOLBAR_NAME = name; | ||
| 617 | 626 | |
| 618 | 627 | /** The same script on an app's own host, where a strict `script-src 'self'` allows it. */ |
| 619 | 628 | export const TOOLBAR_ASSET_PATH = `/__forge/${name}.js`; |
| 620 | /** Every path under this prefix on an app host is the forge's, whether or not it names the script. */ | |
| 629 | /** | |
| 630 | * Every path under this prefix on an app host is the forge's, whether or not it names the script. | |
| 631 | */ | |
| 621 | 632 | export const TOOLBAR_ASSET_PREFIX = "/__forge/toolbar"; |
| 622 | 633 | |
| 623 | 634 | /** The forge's answer for the script: immutable, because its name is its hash. */ |
| @@ -670,17 +681,25 @@ export type PillWhere = { | ||
| 670 | 681 | env: string; |
| 671 | 682 | pull?: number; |
| 672 | 683 | title?: string; |
| 684 | /** The pull's head branch, for a reader of the pull only. */ | |
| 685 | branch?: string; | |
| 673 | 686 | /** Whether the pull is open, merged or closed; for a reader of the pull only. */ |
| 674 | 687 | state?: "open" | "merged" | "closed"; |
| 675 | 688 | /** The preview runs a commit that is not the pull's head (and none is deploying). */ |
| 676 | 689 | behind?: boolean; |
| 677 | 690 | /** A newer commit is being deployed now. */ |
| 678 | 691 | deploying?: boolean; |
| 679 | /** The pull is a guest's nobody has approved yet: its preview runs on empty data, without the app's variables. */ | |
| 692 | /** | |
| 693 | * The pull is a guest's nobody has approved yet: its preview runs on empty data, without the | |
| 694 | * app's variables. | |
| 695 | */ | |
| 680 | 696 | waits?: boolean; |
| 681 | 697 | /** The newest check of the pull's head, a key of `CHECK_WORDS`. */ |
| 682 | 698 | check?: PillCheck; |
| 683 | /** The commit the environment is serving and the instant it went live, from the forge's own record. */ | |
| 699 | /** | |
| 700 | * The commit the environment is serving and the instant it went live, from the forge's own | |
| 701 | * record. | |
| 702 | */ | |
| 684 | 703 | serving?: { oid: string; at: string }; |
| 685 | 704 | }; |
| 686 | 705 | |
| @@ -729,6 +748,7 @@ export function toolbarTag( | ||
| 729 | 748 | ["env", where.env], |
| 730 | 749 | ["pull", where.pull === undefined ? undefined : String(where.pull)], |
| 731 | 750 | ["title", where.title], |
| 751 | ["branch", where.branch], | |
| 732 | 752 | ["state", where.state], |
| 733 | 753 | ["check", where.check], |
| 734 | 754 | ["commit", where.serving?.oid], |
| @@ -758,7 +778,10 @@ function lastBodyClose(buffer: Buffer): number { | ||
| 758 | 778 | return -1; |
| 759 | 779 | } |
| 760 | 780 | |
| 761 | /** The opening of the tag the proxy writes, with the hashed name: what a page that has the toolbar contains. */ | |
| 781 | /** | |
| 782 | * The opening of the tag the proxy writes, with the hashed name: what a page that has the toolbar | |
| 783 | * contains. | |
| 784 | */ | |
| 762 | 785 | const TAG_OPEN = Buffer.from(`<script src="${TOOLBAR_ASSET_PATH}"`); |
| 763 | 786 | |
| 764 | 787 | /** |
| @@ -770,7 +793,10 @@ export function carriesToolbar(bytes: Uint8Array): boolean { | ||
| 770 | 793 | return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).includes(TAG_OPEN); |
| 771 | 794 | } |
| 772 | 795 | |
| 773 | /** `bytes` with the pill's tag before the last `</body>`, or at the end; a page that has one is returned as it is. */ | |
| 796 | /** | |
| 797 | * `bytes` with the pill's tag before the last `</body>`, or at the end; a page that has one is | |
| 798 | * returned as it is. | |
| 799 | */ | |
| 774 | 800 | export function withToolbar( |
| 775 | 801 | bytes: Uint8Array, |
| 776 | 802 | forgeUrl: string, |
test/apps-pill-freshness.test.ts +2 −2
| @@ -269,14 +269,14 @@ describe("the pill's script draws the freshness line", () => { | ||
| 269 | 269 | ]; |
| 270 | 270 | for (const [extra, said] of cases) { |
| 271 | 271 | const lines = panelLines({ ...base, ...extra }); |
| 272 | expect(lines.slice(0, 4)).toEqual(["site", "preview \u00b7 #7", "Add a thing", said]); | |
| 272 | expect(lines.slice(0, 5)).toEqual(["site", "Pull #7", "Environment: preview", "Add a thing", said]); | |
| 273 | 273 | expect(lines.filter((l) => words.includes(l as never))).toEqual([said]); |
| 274 | 274 | } |
| 275 | 275 | }); |
| 276 | 276 | |
| 277 | 277 | test("the line follows the pull when there is no title", () => { |
| 278 | 278 | const lines = panelLines({ ...base, title: "", state: "merged" }); |
| 279 | expect(lines.slice(0, 3)).toEqual(["site", "preview \u00b7 #7", FRESHNESS_WORDS.merged]); | |
| 279 | expect(lines.slice(0, 4)).toEqual(["site", "Pull #7", "Environment: preview", FRESHNESS_WORDS.merged]); | |
| 280 | 280 | }); |
| 281 | 281 | |
| 282 | 282 | test("merged and closed win over deploying and behind; deploying wins over behind", () => { |
test/apps-pill-where.test.ts +6
| @@ -63,6 +63,10 @@ describe("the pill's data", () => { | ||
| 63 | 63 | }); |
| 64 | 64 | if (!pull.ok) throw new Error(pull.error); |
| 65 | 65 | pulls[name] = pull.value.number; |
| 66 | app.db.run( | |
| 67 | "INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', ?2, '', '')", | |
| 68 | [pull.value.id, `scout/${name}-branch`], | |
| 69 | ); | |
| 66 | 70 | } |
| 67 | 71 | }, 120_000); |
| 68 | 72 | |
| @@ -87,12 +91,14 @@ describe("the pill's data", () => { | ||
| 87 | 91 | expect(where.pull).toBe(pulls.open!); |
| 88 | 92 | expect(where.env).toBe(`pull-${pulls.open}`); |
| 89 | 93 | expect(where.title).toContain("onerror"); |
| 94 | expect(where.branch).toBe("scout/open-branch"); | |
| 90 | 95 | }); |
| 91 | 96 | |
| 92 | 97 | test("the title is withheld from a viewer who may not read the pull", () => { |
| 93 | 98 | const anon = pillWhere(runtime, preview("closed"), undefined); |
| 94 | 99 | expect(anon.pull).toBe(pulls.closed!); |
| 95 | 100 | expect(anon.title).toBeUndefined(); |
| 101 | expect(anon.branch).toBeUndefined(); | |
| 96 | 102 | const owner = pillWhere(runtime, preview("closed"), { id: aliceId } as never); |
| 97 | 103 | expect(owner.title).toContain("closed"); |
| 98 | 104 | }); |
test/apps-toolbar.test.ts +19 −4
| @@ -17,7 +17,7 @@ import { getAppByRepo } from "../src/core/apps.ts"; | ||
| 17 | 17 | import { listRuns } from "../src/core/runs.ts"; |
| 18 | 18 | import { AppsRuntime, type EnvStatus } from "../src/apps/runtime.ts"; |
| 19 | 19 | import { parseManifest } from "../src/apps/manifest.ts"; |
| 20 | import { REWRITE_MAX, TOOLBAR_ASSET_PATH, TOOLBAR_PATH, readPage, toolbarJs, withToolbar } from "../src/apps/toolbar.ts"; | |
| 20 | import { REWRITE_MAX, TOOLBAR_ASSET_PATH, TOOLBAR_PATH, readPage, toolbarJs, toolbarTag, withToolbar } from "../src/apps/toolbar.ts"; | |
| 21 | 21 | import { createUser } from "../src/core/users.ts"; |
| 22 | 22 | import { serve, type Servers } from "../src/http/server.ts"; |
| 23 | 23 | import { quietLogs } from "./helpers/globals.ts"; |
| @@ -476,23 +476,38 @@ describe("the pill's keyboard and touch behaviour", () => { | ||
| 476 | 476 | |
| 477 | 477 | |
| 478 | 478 | test("on a preview with a repo the pull line links to the pull; otherwise it is plain text", () => { |
| 479 | const a = tree({ app: "notes", repo: "alice/notes", pull: "12" }).lines.find((l) => l.textContent === "preview \u00b7 #12")!; | |
| 479 | const a = tree({ app: "notes", repo: "alice/notes", pull: "12" }).lines.find((l) => l.textContent === "Pull #12")!; | |
| 480 | 480 | expect(a.tag).toBe("a"); |
| 481 | 481 | expect(a.href).toBe("https://forge.test/alice/notes/pulls/12"); |
| 482 | 482 | expect(a.style.cssText).toContain("min-height:44px"); |
| 483 | 483 | const prod = tree({ app: "notes", repo: "alice/notes", env: "production", pull: "12" }); |
| 484 | expect(prod.lines.some((l) => l.textContent.startsWith("preview \u00b7"))).toBe(false); | |
| 484 | expect(prod.lines.some((l) => l.textContent.startsWith("Pull #"))).toBe(false); | |
| 485 | 485 | expect(prod.lines.some((l) => l.textContent === "production")).toBe(true); |
| 486 | 486 | // An unknown environment with no pull draws no environment line at all. |
| 487 | 487 | const unknown = tree({ app: "notes", repo: "alice/notes", env: "staging" }); |
| 488 | 488 | expect(unknown.lines.some((l) => l.textContent === "production" || l.textContent.startsWith("preview"))).toBe(false); |
| 489 | 489 | for (const bad of [{ pull: "12x" }, { pull: "../1" }, { pull: "1234567890" }, { repo: "" }]) { |
| 490 | 490 | const t = tree({ app: "notes", repo: "alice/notes", pull: "12", ...bad }); |
| 491 | const l = t.lines.find((x) => x.textContent.startsWith("preview \u00b7"))!; | |
| 491 | const l = t.lines.find((x) => x.textContent.startsWith("Pull #"))!; | |
| 492 | 492 | expect(l.tag === "a").toBe(false); |
| 493 | 493 | } |
| 494 | 494 | }); |
| 495 | 495 | |
| 496 | test("a preview names its environment and branch in words; production shows neither", () => { | |
| 497 | const t = tree({ app: "notes", repo: "alice/notes", env: "pull-12", pull: "12", branch: "scout/x" }); | |
| 498 | expect(t.lines.some((l) => l.textContent === "Environment: preview")).toBe(true); | |
| 499 | expect(t.lines.some((l) => l.textContent === "Branch: scout/x")).toBe(true); | |
| 500 | const prod = tree({ app: "notes", env: "production", branch: "scout/x" }); | |
| 501 | expect(prod.lines.some((l) => /^(Branch|Environment|Pull)/.test(l.textContent))).toBe(false); | |
| 502 | }); | |
| 503 | ||
| 504 | test("the tag escapes the branch and title", () => { | |
| 505 | const tag = toolbarTag("https://forge.test", { app: "a", env: "pull-1", pull: 1, title: '"><b>', branch: '"><script>' }); | |
| 506 | expect(tag).not.toContain("<b>"); | |
| 507 | expect(tag.match(/<script/g)!.length).toBe(1); | |
| 508 | expect(tag).toContain('data-branch=""><script>"'); | |
| 509 | }); | |
| 510 | ||
| 496 | 511 | test("the panel is a labelled region, opens with focus inside, and Escape returns focus to the pill", () => { |
| 497 | 512 | const { doc, pill, panel } = tree({ app: "notes", env: "production" }); |
| 498 | 513 | expect(panel.attrs["aria-label"]).toBeTruthy(); |
test/lost-journey.test.ts +6 −4
| @@ -176,7 +176,9 @@ describe("lost journey: where you are and three exits", () => { | ||
| 176 | 176 | const walk = (e: El): El[] => [e, ...e.children.flatMap(walk)]; |
| 177 | 177 | const unescape = (v: string) => |
| 178 | 178 | v.replace(/"/g, '"').replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&"); |
| 179 | /** Boot the toolbar exactly as the browser would for the served tag, and let the crew answer land. */ | |
| 179 | /** | |
| 180 | * Boot the toolbar exactly as the browser would for the served tag, and let the crew answer land. | |
| 181 | */ | |
| 180 | 182 | const boot = async (html: string, crew: object, href: string) => { |
| 181 | 183 | const t = tagOf(html)!; |
| 182 | 184 | const dataset: Record<string, string> = {}; |
| @@ -229,7 +231,7 @@ describe("lost journey: where you are and three exits", () => { | ||
| 229 | 231 | expect(focusable(home)).toBe(true); |
| 230 | 232 | expect(home.href).toBe(`${origin}/`); |
| 231 | 233 | |
| 232 | const pullLink = run.named(`preview \u00b7 #${pull}`)!; | |
| 234 | const pullLink = run.named(`Pull #${pull}`)!; | |
| 233 | 235 | expect(focusable(pullLink)).toBe(true); |
| 234 | 236 | expect(pullLink.href).toBe(`${origin}/alice/public/pulls/${pull}`); |
| 235 | 237 | |
| @@ -259,7 +261,7 @@ describe("lost journey: where you are and three exits", () => { | ||
| 259 | 261 | const html = await (await ask(host, "/", cookie)).text(); |
| 260 | 262 | const run = await boot(html, await crewOf(host, cookie), `http://${host}/`); |
| 261 | 263 | expect(run.named("production")).toBeDefined(); |
| 262 | expect(run.built.some((e) => String(e.textContent).startsWith("preview \u00b7"))).toBe(false); | |
| 264 | expect(run.built.some((e) => String(e.textContent).startsWith("Pull #"))).toBe(false); | |
| 263 | 265 | expect(run.named("Home")!.href).toBe(`${new URL(servers.url).origin}/`); |
| 264 | 266 | }); |
| 265 | 267 | |
| @@ -272,7 +274,7 @@ describe("lost journey: where you are and three exits", () => { | ||
| 272 | 274 | expect(tag[2]).toContain(` data-env="pull-${pull}"`); |
| 273 | 275 | expect(tag[2]).toContain(` data-pull="${pull}"`); |
| 274 | 276 | const run = await boot(html, await crewOf(host), `http://${host}/`); |
| 275 | expect(run.named(`preview \u00b7 #${pull}`)!.href).toBe(`${new URL(servers.url).origin}/alice/public/pulls/${pull}`); | |
| 277 | expect(run.named(`Pull #${pull}`)!.href).toBe(`${new URL(servers.url).origin}/alice/public/pulls/${pull}`); | |
| 276 | 278 | expect(run.named("Home")).toBeDefined(); |
| 277 | 279 | expect(run.named("Tell the crew")).toBeUndefined(); |
| 278 | 280 | // Comment goes to the forge's own page, which asks the stranger to sign in there. |
test/toolbar-injection.test.ts +3 −2
| @@ -75,7 +75,8 @@ test("a nonce from the app's own policy rides on the tag; a policy without one a | ||
| 75 | 75 | expect(toolbarTag(FORGE)).toContain('src="/__forge/'); |
| 76 | 76 | }); |
| 77 | 77 | |
| 78 | // A source check only; the layout proof is the proxy tests in apps-toolbar.test.ts (data untouched, one tag). | |
| 78 | // A source check only; the layout proof is the proxy tests in apps-toolbar.test.ts (data untouched, | |
| 79 | // one tag). | |
| 79 | 80 | test("the pill overlays the page: fixed, out of flow, in a shadow root", () => { |
| 80 | 81 | expect(toolbarJs).toContain("position:fixed"); |
| 81 | 82 | expect(toolbarJs).toContain("attachShadow"); |
| @@ -85,6 +86,6 @@ test("the pill overlays the page: fixed, out of flow, in a shadow root", () => { | ||
| 85 | 86 | }); |
| 86 | 87 | |
| 87 | 88 | test("on a preview the pill links to the pull's forge page in plain words", () => { |
| 88 | expect(toolbarJs).toContain('"preview \\u00b7 #"'); | |
| 89 | expect(toolbarJs).toContain('"Pull #"'); | |
| 89 | 90 | expect(toolbarJs).toContain('"/pulls/"'); |
| 90 | 91 | }); |
new above
offline