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

The /__forge/ door's "More pulls are open" line says how many and links to the repository's open pulls #6414

Closed opened this
Closed without merging

scout/forge:scout/6411-the-forge-door-s-more-pulls-are-open-lin → main 3 commits fork 2 threads · 2 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +83 −13
src/apps/door.ts +23 −5
@@ -24,11 +24,18 @@ export type DoorFacts = {
2424 ssh?: string | null;
2525 /** Open pulls a viewer may see, each with where to go: its preview or the forge. */
2626 pulls?: DoorPullLine[] | undefined;
27 /** How many open pulls the door does not draw; `capped` when there may be more. */
28 morePulls?: DoorMorePulls | undefined;
2729 };
2830
29 /** How many pulls the door lists; one more read than this means there are others. */
31 export type DoorMorePulls = { count: number; capped: boolean };
32
33 /** How many pulls the door lists; the rest are counted. */
3034 export const DOOR_PULL_LIMIT = 5;
3135
36 /** How many the door reads to count them; past this the count says "N+". */
37 export const DOOR_PULL_READ = 50;
38
3239 export type DoorPullLine = { number: number; title: string; check: string; href: string };
3340
3441 const STYLE =
@@ -92,12 +99,23 @@ function servingHtml(facts: DoorFacts): string {
9299 return `<p>Serving ${sha(now)} since ${stamp(now.at)}</p>${older === "" ? "" : `<h2>Before it</h2><ul>${older}</ul>`}`;
93100 }
94101
95 function pullsHtml(pulls: DoorPullLine[] | undefined): string {
102 function pullsHtml(facts: DoorFacts): string {
103 const pulls = facts.pulls;
outdated

The count needs only the number of pulls. proxy.ts builds a full line (envStatus lookup) for up to 50 pulls to get it. Return the count separately, or resolve env state only for the first five.

96104 if (pulls === undefined || pulls.length === 0) return "";
97 const items = pulls.slice(0, DOOR_PULL_LIMIT).map((p) =>
105 const items = pulls.map((p) =>
98106 link(p.href, `#${p.number} ${[...p.title].slice(0, 120).join("")} · check ${p.check}`),
99107 );
100 const more = pulls.length > DOOR_PULL_LIMIT ? "<p>More pulls are open on the forge.</p>" : "";
108 const rest = facts.morePulls?.count ?? 0;
109 let more = "";
110 if (rest > 0) {
111 const n = facts.morePulls?.capped === true ? `${rest}+` : `${rest}`;
112 const words = `${n} more open ${rest === 1 ? "pull" : "pulls"}`;
113 const repo = facts.where.repo;
114 more =
115 repo === undefined
116 ? `<p>${words}</p>`
117 : `<p><a href="${escapeAttr(new URL(`/${repo}/pulls`, facts.forgeUrl).href)}">${words}</a></p>`;
118 }
101119 return `<h2>Open pulls</h2><ul>${items.join("")}</ul>${more}`;
102120 }
103121
@@ -119,7 +137,7 @@ export function doorPage(facts: DoorFacts, head: boolean): Response {
119137 : `<p>Terminal:</p><pre><code>${escapeAttr(facts.ssh)}</code></pre>`;
120138 const html = `<!doctype html>
121139 <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>${escapeAttr(where.app)}, ${escapeAttr(doorWhere(where))}</title><style>${STYLE}</style></head>
122 <body><main><h1>${escapeAttr(where.app)}</h1><p>${whereLine(where, forgeUrl)}</p>${servingHtml(facts)}${pullsHtml(facts.pulls)}<ul>${links.join("")}</ul>${terminal}</main></body></html>
140 <body><main><h1>${escapeAttr(where.app)}</h1><p>${whereLine(where, forgeUrl)}</p>${servingHtml(facts)}${pullsHtml(facts)}<ul>${links.join("")}</ul>${terminal}</main></body></html>
123141 `;
124142 return new Response(head ? null : html, {
125143 headers: {
src/apps/proxy.ts +16 −6
@@ -39,7 +39,7 @@ import type { AppsRuntime, EnvStatus } from "./runtime.ts";
3939 import { crewNow, tellLink } from "./crew.ts";
4040 import { feedback, mintProof } from "./feedback.ts";
4141 import { clientBucket } from "../http/peer.ts";
42 import { DOOR_PULL_LIMIT, doorPage, type DoorPullLine } from "./door.ts";
42 import { DOOR_PULL_LIMIT, DOOR_PULL_READ, doorPage, type DoorMorePulls, type DoorPullLine } from "./door.ts";
4343 import { checkWord } from "./check_word.ts";
4444 import { openPulls } from "./door_pulls.ts";
4545 import { takePhases, timePhase } from "../core/route_timing.ts";
@@ -446,18 +446,20 @@ function doorSsh(
446446 /**
447447 * The door's open pulls: a preview that is running or asleep links to itself,
448448 * every other pull to its page on the forge. Empty for whoever may not read.
449 * Reads one more than the door draws, so it can say there are others.
449 * Reads enough to count the ones the door does not draw.
450450 */
451451 function doorPulls(
452452 runtime: AppsRuntime,
453453 target: AppTarget,
454454 who: GrantWho | undefined,
455 ): DoorPullLine[] {
455 ): { pulls: DoorPullLine[]; morePulls: DoorMorePulls } {
456456 const db = runtime.app.db;
457457 const repo = getRepoById(db, target.app.repoId);
458 if (repo === undefined) return [];
458 if (repo === undefined) return { pulls: [], morePulls: { count: 0, capped: false } };
459459 const viewer = who === undefined ? undefined : getUserById(db, who.id);
460 return openPulls(db, repo, viewer, DOOR_PULL_LIMIT + 1).map((p) => {
460 const read = openPulls(db, repo, viewer, DOOR_PULL_READ);
461 // Only the pulls the door draws get an env lookup; the rest are counted.
462 const pulls = read.slice(0, DOOR_PULL_LIMIT).map((p) => {
461463 const env = previewEnv(p.number);
462464 const state = runtime.envStatus(target.app, env);
463465 const live =
@@ -477,6 +479,13 @@ function doorPulls(
477479 ).href,
478480 };
479481 });
482 return {
483 pulls,
484 morePulls: {
485 count: Math.max(read.length - DOOR_PULL_LIMIT, 0),
486 capped: read.length >= DOOR_PULL_READ,
487 },
488 };
480489 }
481490
482491 /** Whether the person a grant names may enter this app now. */
@@ -733,7 +742,7 @@ export async function proxyApp(
733742 const serving = timePhase(request, "serving", () =>
734743 readServing(runtime.app.db, target.app.repoId, target.env),
735744 );
736 const pulls = timePhase(request, "pulls", () =>
745 const { pulls, morePulls } = timePhase(request, "pulls", () =>
737746 doorPulls(runtime, target, signed?.who),
738747 );
739748 const page = timePhase(request, "render", () =>
@@ -741,6 +750,7 @@ export async function proxyApp(
741750 {
742751 where,
743752 pulls,
753 morePulls,
744754 ...(serving === undefined ? {} : { serving }),
745755 forgeUrl: runtime.options.forgeUrl,
746756 ...(ssh === undefined ? {} : { ssh }),
test/apps-door.test.ts +2 −2
@@ -416,9 +416,9 @@ test("the door links a dormant preview to itself, and says so when more than fiv
416416 );
417417 let html = await (await door(`notes.${DOMAIN}`)).text();
418418 expect(html).toContain(`<a href="${runtime.urlFor(record, `pull-${dormant}`)}">#${dormant} quiet one`);
419 expect(html).not.toContain("More pulls are open");
419 expect(html).not.toContain("more open pull");
420420 for (let i = 0; i < 5; i++) mk(`extra ${i}`);
421421 html = await (await door(`notes.${DOMAIN}`)).text();
422 expect(html).toContain("More pulls are open on the forge.");
422 expect(html).toContain("3 more open pulls</a>");
423423 expect((html.match(/ · check /g) ?? []).length).toBe(5);
424424 });
test/door-more-pulls.test.ts added +42
@@ -0,0 +1,42 @@
1 import { expect, test } from "bun:test";
2 import { DOOR_PULL_LIMIT, DOOR_PULL_READ, doorPage, type DoorPullLine } from "../src/apps/door.ts";
3
4 const pulls = (n: number): DoorPullLine[] =>
5 Array.from({ length: n }, (_, i) => ({
6 number: i + 1,
7 title: `t${i + 1}`,
8 check: "passed",
9 href: `https://forge.test/trevato/notes/pulls/${i + 1}`,
10 }));
11
12 const page = async (n: number, repo: string | undefined): Promise<string> =>
13 doorPage(
14 {
15 where: { app: "notes", env: "production", ...(repo === undefined ? {} : { repo }) },
16 forgeUrl: "https://forge.test",
17 pulls: pulls(Math.min(n, DOOR_PULL_LIMIT)),
18 morePulls: { count: Math.max(n - DOOR_PULL_LIMIT, 0), capped: n >= DOOR_PULL_READ },
19 },
20 false,
21 ).text();
22
23 test("a reader sees how many more pulls are open, linked to the pulls list", async () => {
24 const html = await page(8, "trevato/notes");
25 expect(html).toContain('<a href="https://forge.test/trevato/notes/pulls">3 more open pulls</a>');
26 });
27
28 test("a guest sees the count and no link or repository name", async () => {
29 const html = await page(8, undefined);
30 expect(html).toContain("<p>3 more open pulls</p>");
31 expect(html).not.toMatch(/href="[^"]*\/pulls"/);
32 expect(html.split("3 more open pulls")[1]).not.toContain("trevato/notes");
33 });
34
35 test("with five or fewer the line is absent", async () => {
36 for (const n of [0, 3, 5]) expect(await page(n, "trevato/notes")).not.toContain("more open pull");
37 });
38
39 test("one more says pull, and a full read says N+", async () => {
40 expect(await page(6, "trevato/notes")).toContain("1 more open pull<");
41 expect(await page(50, "trevato/notes")).toContain("45+ more open pulls");
42 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs