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

Old reviewed-pull posts say the verdict too #16928

Merged opened this

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

Merged

scout/forge:scout/5853-old-reviewed-pull-posts-say-the-verdict → main 0 commits fork 2 threads · 2 unresolved

  • Merged. scout/5853-old-reviewed-pull-posts-say-the-verdict was merged into main as 34e804b.

Review

Sign in to review this change.
2 files changed +127 −3
src/feed/items.ts +45 −3
@@ -12,7 +12,7 @@ import type { Chips } from "../core/feed_chips.ts";
1212 import { listDecidedIn } from "../core/decisions.ts";
1313 import { audienceFilter } from "../core/event_audience.ts";
1414 import { listEventsVisibleTo, listTeamEventsFor, type ForgeEvent } from "../core/events.ts";
15 import { getReview } from "../core/reviews.ts";
15 import { getReview, isReviewState, listReviewsFor, type Review } from "../core/reviews.ts";
1616 import { commentAnchor, threadWindow, getIssuesByIds, getIssuesByNumbers, numberKey, type Issue } from "../core/issues.ts";
1717 import {
1818 listDetachedMirrors,
@@ -398,7 +398,17 @@ export function eventCandidates(
398398 [...issues.values()].filter((i) => i.kind === "pull"),
399399 ).map((p) => [p.id, p]),
400400 );
401 // Old reviewed events (no reviewId) find their review: one read for all (#5853).
402 const oldReviewed = new Set<string>();
403 for (const e of events)
404 if (e.kind === "pull.reviewed" && typeof e.payload["reviewId"] !== "string") {
405 const n = e.payload["number"];
406 const issue = typeof n === "number" && e.repoId !== null ? issues.get(numberKey(e.repoId, n)) : undefined;
407 if (issue) oldReviewed.add(issue.id);
408 }
409 const reviews = listReviewsFor(db, [...oldReviewed]);
401410 const subjects: Subjects = {
411 reviews: (pullId) => reviews.get(pullId) ?? [],
402412 issue: (repoId, number) => issues.get(numberKey(repoId, number)),
403413 pull: (repoId, number) => {
404414 const issue = issues.get(numberKey(repoId, number));
@@ -589,6 +599,8 @@ type Partial = Omit<Candidate, "actor">;
589599
590600 /** The conversations the window's events name, read once for all of them. */
591601 interface Subjects {
602 /** The verdicts on a pull, for events that predate `reviewId`. */
603 reviews(pullId: string): readonly Review[];
592604 issue(repoId: string, number: number): Issue | undefined;
593605 pull(repoId: string, number: number): Pull | undefined;
594606 }
@@ -596,6 +608,32 @@ interface Subjects {
596608 /** Asked-for-changes sits above plain news; an approval is news. */
597609 const CHANGES_URGENCY = 1;
598610
611 /**
612 * An event written before it carried `reviewId` finds its review: the same
613 * pull, the same author, the state it names, nearest in time (#5853).
614 */
outdated

One listReviews query per old event per feed build. Batch with listReviewsFor or cache per pull for the build.

615 function oldReview(
616 p: Record<string, unknown>,
617 common: Omit<Partial, "what">,
618 pull: Pull | undefined,
619 subjects: Subjects,
620 ): Review | undefined {
621 const state = p["state"];
622 if (!pull || typeof state !== "string" || !isReviewState(state)) return undefined;
623 const at = Date.parse(common.at);
624 let best: Review | undefined;
625 let gap = Infinity;
626 for (const r of subjects.reviews(pull.id)) {
627 if (r.authorId !== common.actorId || r.state !== state) continue;
628 const away = Math.abs(Date.parse(r.createdAt) - at);
629 if (away < gap) {
630 best = r;
631 gap = away;
632 }
633 }
634 return best;
635 }
636
599637 /**
600638 * The verdict and the reviewer's first sentence, from the review the event
601639 * names. A review that is gone, or one with no verdict, keeps the old words.
@@ -605,9 +643,13 @@ function reviewedPost(
605643 p: Record<string, unknown>,
606644 common: Omit<Partial, "what">,
607645 title: string,
646 pull: Pull | undefined,
647 subjects: Subjects,
608648 ): Partial {
609649 const review =
610 typeof p["reviewId"] === "string" ? getReview(db, p["reviewId"]) : undefined;
650 typeof p["reviewId"] === "string"
651 ? getReview(db, p["reviewId"])
652 : oldReview(p, common, pull, subjects);
611653 const on = title === "" ? "a pull request" : clip(title);
612654 if (review?.state === "approved")
613655 return { ...common, what: `approved ${on}` };
@@ -752,7 +794,7 @@ function fromEvent(
752794 return { ...common, kind: "pull", link: change, what: `merged a pull request${title ? `: ${clip(title)}` : ""}${words}` };
753795 }
754796 case "pull.reviewed":
755 return reviewedPost(db, p, { ...common, kind: "pull", link: change }, title);
797 return reviewedPost(db, p, { ...common, kind: "pull", link: change }, title, pull, subjects);
756798 case "look.taken": {
757799 const env = p["env"];
758800 const oid = p["commitOid"];
test/feed-reviewed.test.ts +82
@@ -148,3 +148,85 @@ test("a review that is gone, or has no verdict, keeps the old words", async () =
148148 expect(words).toContain("reviewed a pull request: Remove the banner");
149149 expect(words).toContain("reviewed a pull request: Explain the banner");
150150 });
151
152 test("an old event with a state but no reviewId finds its review: same author, state, nearest in time", async () => {

Add cases for nearest-in-time among same-author same-state reviews, a non-matching author or state falling back to the old words, and the approved state.

153 const old = await pull("Trim the header");
154 const first = await submitReview(app, alice, repo, old, { state: "changes_requested", body: "Too wide. Shrink it." });
155 if (!first.ok) throw new Error(first.error);
156 // Strip the id the way events written before #5823 lack it.
157 const rows = app.db
158 .query<{ id: string; payload: string }, []>("SELECT id, payload FROM event WHERE kind = 'pull.reviewed'")
159 .all()
160 .filter((r) => (JSON.parse(r.payload) as { reviewId?: string }).reviewId === first.value.id);
161 expect(rows.length).toBe(1);
162 const payload = JSON.parse(rows[0]!.payload) as Record<string, unknown>;
163 delete payload["reviewId"];
164 app.db.run("UPDATE event SET payload = ?1 WHERE id = ?2", [JSON.stringify(payload), rows[0]!.id]);
165 const words = candidatesFor(app.db, alice, Date.now()).map((c) => said(c.what));
166 expect(words).toContain("asked for changes on Trim the header: Too wide.");
167 });
168
169 /** Strip `reviewId` from the event that names this review, as events before #5823 lack it. */
170 const strip = (reviewId: string, at?: string): void => {
171 const row = app.db
172 .query<{ id: string; payload: string }, []>("SELECT id, payload FROM event WHERE kind = 'pull.reviewed'")
173 .all()
174 .find((r) => (JSON.parse(r.payload) as { reviewId?: string }).reviewId === reviewId)!;
175 const payload = JSON.parse(row.payload) as Record<string, unknown>;
176 delete payload["reviewId"];
177 app.db.run("UPDATE event SET payload = ?1 WHERE id = ?2", [JSON.stringify(payload), row.id]);
178 if (at) app.db.run("UPDATE event SET created_at = ?1 WHERE id = ?2", [at, row.id]);
179 };
180
181 const words = (): string[] => candidatesFor(app.db, alice, Date.now()).map((c) => said(c.what));
182
183 test("an old event takes the same-author same-state review nearest in time", async () => {
184 const p = await pull("Shrink the nav");
185 const early = await submitReview(app, alice, repo, p, { state: "changes_requested", body: "First take. Redo it." });
186 const late = await submitReview(app, alice, repo, p, { state: "changes_requested", body: "Second take. Still wrong." });
187 if (!early.ok || !late.ok) throw new Error("review");
188 const now = Date.now();
189 const iso = (ms: number): string => new Date(now + ms).toISOString();
190 const [t0, t0b, t1, t1b] = [iso(-60_000), iso(-59_000), iso(-30_000), iso(-29_000)];
191 app.db.run("UPDATE event SET created_at = ?1 WHERE kind = 'pull.opened' AND json_extract(payload, '$.number') = ?2", [iso(-120_000), p.number]);
192 app.db.run("UPDATE review SET created_at = ?2 WHERE id = ?1", [early.value.id, t0]);
193 app.db.run("UPDATE review SET created_at = ?2 WHERE id = ?1", [late.value.id, t1]);
194 // Both events are old; the fold shows one of them, and each must find its own.
195 strip(early.value.id, t0b);
196 strip(late.value.id, t1b);
197 expect(words()).toContain("asked for changes on Shrink the nav: Second take.");
198 expect(words()).not.toContain("asked for changes on Shrink the nav: First take.");
199 });
200
201 test("an old event never takes another author's or another state's review", async () => {
202 const p = await pull("Rename the tab");
203 const mine = await submitReview(app, alice, repo, p, { state: "approved" });
204 if (!mine.ok) throw new Error("review");
205 // Same pull and state, but by someone else: not alice's event's review.
206 app.db.run("UPDATE review SET author_id = ?1 WHERE id = ?2", [getUserByName(app.db, "bot")!.id, mine.value.id]);
207 strip(mine.value.id);
208 expect(words()).toContain("reviewed a pull request: Rename the tab");
209 expect(words()).not.toContain("approved Rename the tab");
210
211 const q = await pull("Move the tab");
212 const other = await submitReview(app, alice, repo, q, { state: "approved" });
213 if (!other.ok) throw new Error("review");
214 // The review's state differs from the one the event names.
215 app.db.run("UPDATE review SET state = 'commented' WHERE id = ?1", [other.value.id]);
216 const row = app.db
217 .query<{ id: string; payload: string }, []>("SELECT id, payload FROM event WHERE kind = 'pull.reviewed'")
218 .all()
219 .find((r) => (JSON.parse(r.payload) as { reviewId?: string }).reviewId === other.value.id)!;
220 const payload = JSON.parse(row.payload) as Record<string, unknown>;
221 delete payload["reviewId"];
222 app.db.run("UPDATE event SET payload = ?1 WHERE id = ?2", [JSON.stringify(payload), row.id]);
223 expect(words()).toContain("reviewed a pull request: Move the tab");
224 });
225
226 test("an old approved event says approved", async () => {
227 const p = await pull("Polish the logo");
228 const ok = await submitReview(app, alice, repo, p, { state: "approved" });
229 if (!ok.ok) throw new Error("review");
230 strip(ok.value.id);
231 expect(words()).toContain("approved Polish the logo");
232 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs