Public: anyone can read this; the crew can change it.
A reviewed-pull post says the verdict, approved or changes asked, and the reviewer's first sentence #5841
scout/forge:scout/5823-a-reviewed-pull-post-says-the-verdict-ap → main 5 commits fork 3 threads · 3 unresolved
3 files
changed +205
−4
src/actions/pulls.ts +14 −3
| @@ -555,15 +555,20 @@ export async function submitReview( | ||
| 555 | 555 | pull.id, |
| 556 | 556 | [sent.value], |
| 557 | 557 | ); |
| 558 | const stored = getReviewByRemoteId(app.db, pull.id, sent.value.remoteId); | |
| 558 | 559 | app.bus.append({ |
| 559 | 560 | kind: "pull.reviewed", |
| 560 | 561 | repoId: repo.id, |
| 561 | 562 | actorId: user.id, |
| 562 | payload: { number: pull.number, state: input.state, via: "origin" }, | |
| 563 | payload: { | |
| 564 | number: pull.number, | |
| 565 | state: input.state, | |
| 566 | via: "origin", | |
| 567 | ...(stored ? { reviewId: stored.id } : {}), | |
| 568 | }, | |
| 563 | 569 | }); |
| 564 | 570 | // Line comments stay here, as `addReview`'s always have on a mirror. |
| 565 | 571 | for (const line of anchored.value) postLine(app, pull, user, line); |
| 566 | const stored = getReviewByRemoteId(app.db, pull.id, sent.value.remoteId); | |
| 567 | 572 | return stored ? ok(stored) : err("origin_refused"); |
| 568 | 573 | } |
| 569 | 574 | |
| @@ -608,6 +613,7 @@ export async function submitReview( | ||
| 608 | 613 | number: pull.number, |
| 609 | 614 | state: input.state, |
| 610 | 615 | body: (input.body ?? "").trim(), |
| 616 | reviewId: recorded.value.id, | |
| 611 | 617 | }, |
| 612 | 618 | }); |
| 613 | 619 | return recorded; |
| @@ -749,7 +755,12 @@ export async function approveHere( | ||
| 749 | 755 | kind: "pull.reviewed", |
| 750 | 756 | repoId: repo.id, |
| 751 | 757 | actorId: user.id, |
| 752 | payload: { number: pull.number, state: "approved", via: "here" }, | |
| 758 | payload: { | |
| 759 | number: pull.number, | |
| 760 | state: "approved", | |
| 761 | via: "here", | |
| 762 | reviewId: recorded.value.id, | |
| 763 | }, | |
| 753 | 764 | }); |
| 754 | 765 | return recorded; |
| 755 | 766 | } |
src/feed/items.ts +41 −1
| @@ -11,6 +11,7 @@ import type { Chips } from "../core/feed_chips.ts"; | ||
| 11 | 11 | import { listDecidedIn } from "../core/decisions.ts"; |
| 12 | 12 | import { audienceFilter } from "../core/event_audience.ts"; |
| 13 | 13 | import { listEventsVisibleTo, type ForgeEvent } from "../core/events.ts"; |
| 14 | import { getReview } from "../core/reviews.ts"; | |
| 14 | 15 | import { getIssuesByNumbers, numberKey, type Issue } from "../core/issues.ts"; |
| 15 | 16 | import { |
| 16 | 17 | listDetachedMirrors, |
| @@ -385,6 +386,45 @@ interface Subjects { | ||
| 385 | 386 | pull(repoId: string, number: number): Pull | undefined; |
| 386 | 387 | } |
| 387 | 388 | |
| 389 | /** Asked-for-changes sits above plain news; an approval is news. */ | |
| 390 | const CHANGES_URGENCY = 1; | |
New ranking tier = new feature work, outside the FLOW ONLY direction. Park until flow alarms are clear. |
||
| 391 | ||
| 392 | /** | |
| 393 | * The verdict and the reviewer's first sentence, from the review the event | |
| 394 | * names. A review that is gone, or one with no verdict, keeps the old words. | |
| 395 | */ | |
| 396 | function reviewedPost( | |
| 397 | db: Database, | |
| 398 | p: Record<string, unknown>, | |
| 399 | common: Omit<Partial, "what">, | |
| 400 | title: string, | |
| 401 | ): Partial { | |
| 402 | const review = | |
| 403 | typeof p["reviewId"] === "string" ? getReview(db, p["reviewId"]) : undefined; | |
| 404 | const on = title === "" ? "a pull request" : clip(title); | |
| 405 | if (review?.state === "approved") | |
| 406 | return { ...common, what: `approved ${on}` }; | |
| 407 | if (review?.state === "changes_requested") { | |
| 408 | const first = firstSentence(review.body); | |
| 409 | return { | |
| 410 | ...common, | |
| 411 | urgency: CHANGES_URGENCY, | |
| 412 | what: `asked for changes on ${on}${first === "" ? "" : `: ${first}`}`, | |
| 413 | }; | |
| 414 | } | |
| 415 | return { | |
| 416 | ...common, | |
| 417 | what: `reviewed a pull request${title ? `: ${clip(title)}` : ""}`, | |
| 418 | }; | |
| 419 | } | |
| 420 | ||
| 421 | /** Up to the first sentence's end, at most 100 characters. */ | |
| 422 | function firstSentence(body: string): string { | |
| 423 | const text = body.trim().replace(/\s+/g, " "); | |
| 424 | const end = text.search(/[.!?](\s|$)/); | |
| 425 | return clip(end === -1 ? text : text.slice(0, end + 1), 100); | |
| 426 | } | |
| 427 | ||
| 388 | 428 | function fromEvent( |
| 389 | 429 | db: Database, |
| 390 | 430 | e: ForgeEvent, |
| @@ -438,7 +478,7 @@ function fromEvent( | ||
| 438 | 478 | case "pull.merged": |
| 439 | 479 | return { ...common, kind: "pull", link: change, what: `merged a pull request${title ? `: ${clip(title)}` : ""}${toldWords}` }; |
| 440 | 480 | case "pull.reviewed": |
| 441 | return { ...common, kind: "pull", link: change, what: `reviewed a pull request${title ? `: ${clip(title)}` : ""}` }; | |
| 481 | return reviewedPost(db, p, { ...common, kind: "pull", link: change }, title); | |
| 442 | 482 | case "look.taken": |
| 443 | 483 | return { ...common, kind: "look", link: base(repo), what: "a new picture of it is ready" }; |
| 444 | 484 | case "app.serving": |
test/feed-reviewed.test.ts added +150
| @@ -0,0 +1,150 @@ | ||
| 1 | /** A reviewed-pull post says the verdict and the reviewer's first sentence (#5823). */ | |
| 2 | ||
| 3 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 4 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { join } from "node:path"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 9 | import { openPull, submitReview } from "../src/actions/pulls.ts"; | |
| 10 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 11 | import type { Pull } from "../src/core/pulls.ts"; | |
| 12 | import type { Repo } from "../src/core/repos.ts"; | |
| 13 | import { createToken } from "../src/core/tokens.ts"; | |
| 14 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { candidatesFor } from "../src/feed/items.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | const PASSWORD = "a-long-reviewed-passphrase"; | |
| 22 | const APPROVED = "approved Tidy the footer"; | |
| 23 | const CHANGES = "asked for changes on Widen the board: The migration drops a column people use."; | |
| 24 | ||
| 25 | let dataDir = ""; | |
| 26 | let app: App; | |
| 27 | let servers: Servers; | |
| 28 | let alice: User; | |
| 29 | let cookie = ""; | |
| 30 | let token = ""; | |
| 31 | let repo: Repo; | |
| 32 | let n = 0; | |
| 33 | ||
| 34 | const pull = async (title: string): Promise<Pull> => { | |
| 35 | n++; | |
| 36 | await commitToBranch(app, alice, repo, { | |
| 37 | branch: `bot/${n}`, | |
| 38 | base: "main", | |
| 39 | message: title, | |
| 40 | files: [{ path: `F${n}.md`, content: `${n}\n` }], | |
| 41 | }); | |
| 42 | const opened = await openPull(app, getUserByName(app.db, "bot")!, repo, { | |
| 43 | title, | |
| 44 | body: "", | |
| 45 | baseBranch: "main", | |
| 46 | headBranch: `bot/${n}`, | |
| 47 | }); | |
| 48 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 49 | return opened.value; | |
| 50 | }; | |
| 51 | ||
| 52 | /** The fold appends " · opened …, 1 review" (#5808); the verdict is what comes before it. */ | |
| 53 | const said = (what: string): string => what.split(" · ")[0]!; | |
| 54 | ||
| 55 | const mcpFeed = async (): Promise<string[]> => { | |
| 56 | const res = await fetch(`${servers.url}/mcp`, { | |
| 57 | method: "POST", | |
| 58 | headers: { | |
| 59 | "content-type": "application/json", | |
| 60 | accept: "application/json, text/event-stream", | |
| 61 | authorization: `Bearer ${token}`, | |
| 62 | }, | |
| 63 | body: JSON.stringify({ | |
| 64 | jsonrpc: "2.0", | |
| 65 | id: 1, | |
| 66 | method: "tools/call", | |
| 67 | params: { name: "feed", arguments: {} }, | |
| 68 | }), | |
| 69 | }); | |
| 70 | const body = (await res.json()) as { result: { content: { text: string }[] } }; | |
| 71 | const text = body.result.content[0]!.text; | |
| 72 | return (JSON.parse(text) as { items: { what: string }[] }).items.map((i) => said(i.what)); | |
| 73 | }; | |
| 74 | ||
| 75 | beforeAll(async () => { | |
| 76 | dataDir = await mkdtemp(join(tmpdir(), "forge-feed-reviewed-")); | |
| 77 | app = createApp({ dataDir, port: 0 }); | |
| 78 | servers = await serve(app); | |
| 79 | const a = await createUser(app.db, { | |
| 80 | origin: "seed", | |
| 81 | trusted: true, | |
| 82 | username: "alice", | |
| 83 | email: "[email protected]", | |
| 84 | password: PASSWORD, | |
| 85 | adminIfFirst: true, | |
| 86 | }); | |
| 87 | if (!a.ok) throw new Error(a.error); | |
| 88 | alice = a.value; | |
| 89 | const res = await fetch(`${servers.url}/signin`, { | |
| 90 | method: "POST", | |
| 91 | body: new URLSearchParams({ username: "alice", password: PASSWORD }), | |
| 92 | redirect: "manual", | |
| 93 | }); | |
| 94 | cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 95 | token = createToken(app.db, alice.id, "ro", "ro").token; | |
| 96 | await createAgent(app, alice, { username: "bot" }); | |
| 97 | const made = await createRepo(app, alice, { name: "site", visibility: "public" }); | |
| 98 | if (!made.ok) throw new Error(String(made.error)); | |
| 99 | repo = made.value; | |
| 100 | await commitToBranch(app, alice, repo, { | |
| 101 | message: "seed", | |
| 102 | files: [{ path: "README.md", content: "# site\n" }], | |
| 103 | }); | |
| 104 | addToCrew(app, alice, repo, "bot"); | |
| 105 | }, 30_000); | |
| 106 | ||
| 107 | afterAll(async () => { | |
| 108 | await servers.stop(); | |
| 109 | await rm(dataDir, { recursive: true, force: true }); | |
| 110 | }); | |
| 111 | ||
| 112 | test("each verdict is said in the post, asked-for-changes first, on / and in the MCP feed", async () => { | |
| 113 | const ok = await pull("Tidy the footer"); | |
| 114 | const bad = await pull("Widen the board"); | |
| 115 | // The approval is the later event, so recency alone would put it first. | |
| 116 | const asked = await submitReview(app, alice, repo, bad, { | |
| 117 | state: "changes_requested", | |
| 118 | body: "The migration drops a column people use. Please keep it.", | |
| 119 | }); | |
| 120 | expect(asked.ok).toBe(true); | |
| 121 | const approved = await submitReview(app, alice, repo, ok, { state: "approved" }); | |
| 122 | expect(approved.ok).toBe(true); | |
| 123 | ||
| 124 | const posts = candidatesFor(app.db, alice, Date.now()) | |
| 125 | .map((c) => said(c.what)) | |
| 126 | .filter((w) => w.startsWith("approved ") || w.startsWith("asked for changes")); | |
| 127 | expect(posts.sort()).toEqual([APPROVED, CHANGES].sort()); | |
| 128 | ||
outdated
|
||
| 129 | const home = await fetch(`${servers.url}/`, { headers: { cookie } }); | |
| 130 | const html = (await home.text()).replace(/\s+/g, " "); | |
| 131 | const at = (s: string): number => html.indexOf(s); | |
| 132 | expect(at(APPROVED)).toBeGreaterThan(-1); | |
| 133 | expect(at(CHANGES)).toBeGreaterThan(-1); | |
| 134 | expect(at(CHANGES)).toBeLessThan(at(APPROVED)); | |
| 135 | ||
| 136 | const rows = (await mcpFeed()).filter((w) => w === APPROVED || w === CHANGES); | |
| 137 | expect(rows).toEqual([CHANGES, APPROVED]); | |
| 138 | }); | |
| 139 | ||
| 140 | test("a review that is gone, or has no verdict, keeps the old words", async () => { | |
| 141 | const gone = await pull("Remove the banner"); | |
| 142 | const given = await submitReview(app, alice, repo, gone, { state: "approved" }); | |
| 143 | if (!given.ok) throw new Error(given.error); | |
| 144 | app.db.run("DELETE FROM review WHERE id = ?1", [given.value.id]); | |
| 145 | const note = await pull("Explain the banner"); | |
| 146 | expect((await submitReview(app, alice, repo, note, { state: "commented", body: "Hm." })).ok).toBe(true); | |
| 147 | const words = candidatesFor(app.db, alice, Date.now()).map((c) => said(c.what)); | |
| 148 | expect(words).toContain("reviewed a pull request: Remove the banner"); | |
| 149 | expect(words).toContain("reviewed a pull request: Explain the banner"); | |
| 150 | }); |
new above
offline
Indented two spaces instead of six, unlike its neighbours. Align it with
body:above.