Skip to content

Cursor plugin specification and official plugins

TypeScript

fix(watch-pr): paginate review threads instead of reading only the first 100 (#484)

yi111ff8f83da30af
diff --git a/pstack/skills/poteto-mode/scripts/watch-pr/fakes.test-helper.ts b/pstack/skills/poteto-mode/scripts/watch-pr/fakes.test-helper.ts
index 2312522c..84ccf596 100644
--- a/pstack/skills/poteto-mode/scripts/watch-pr/fakes.test-helper.ts
+++ b/pstack/skills/poteto-mode/scripts/watch-pr/fakes.test-helper.ts
@@ -7,7 +7,9 @@ import type {
PrContext,
PullRequestFacts,
Repository,
+ RawReviewThread,
ReviewThread,
+ ReviewThreadPage,
RollupPage,
} from "./types.ts";
import { parsePrNumber } from "./types.ts";
@@ -17,12 +19,17 @@ export interface FakeReaderOptions {
readonly fastPath?: ChecksFastPath;
readonly rollupPages?: readonly RollupPage[];
readonly threads?: readonly ReviewThread[];
+ readonly threadPages?: readonly ReviewThreadPage[];
readonly commitRollups?: readonly CommitRollup[];
readonly openPullRequests?: readonly OpenPullRequest[];
readonly origin?: Repository | null;
readonly current?: PrContext;
}
+function toRawThread(thread: ReviewThread): RawReviewThread {
+ return { id: thread.id, firstComment: thread.firstComment, resolved: false };
+}
+
export function passingCheck(name = "ci"): Check {
return {
kind: "passed",
@@ -78,6 +85,7 @@ export function fakeReader(
isDraft: false,
};
let page = 0;
+ let threadPage = 0;
return {
calls,
async originRepo() {
@@ -106,9 +114,12 @@ export function fakeReader(
calls.push(`checkRollupPage:${after ?? "null"}`);
return options.rollupPages?.[page++] ?? { checks: [], endCursor: null };
},
- async reviewThreads() {
- calls.push("reviewThreads");
- return options.threads ?? [];
+ async reviewThreadPage(_requested, after) {
+ calls.push(`reviewThreadPage:${after ?? "null"}`);
+ return options.threadPages?.[threadPage++] ?? {
+ threads: (options.threads ?? []).map(toRawThread),
+ endCursor: null,
+ };
},
async commitRollups() {
calls.push("commitRollups");
diff --git a/pstack/skills/poteto-mode/scripts/watch-pr/github.test.ts b/pstack/skills/poteto-mode/scripts/watch-pr/github.test.ts
index 87f4048f..4ef39d2a 100644
--- a/pstack/skills/poteto-mode/scripts/watch-pr/github.test.ts
+++ b/pstack/skills/poteto-mode/scripts/watch-pr/github.test.ts
@@ -2,12 +2,14 @@ import { describe, expect, it } from "bun:test";
import {
ChecksUnavailable,
WatcherQueryError,
+ buildReviewThreads,
mapRollupNode,
orderStack,
parsePullRequest,
- parseReviewThreads,
+ parseReviewThreadPage,
resolveChecks,
resolveContext,
+ resolveReviewThreads,
} from "./github.ts";
import {
fakeReader,
@@ -198,6 +200,7 @@ it("annotates Bugbot threads with distinct review-pass counts", () => {
repository: {
pullRequest: {
reviewThreads: {
+ pageInfo: { hasNextPage: false, endCursor: null },
nodes: [
{
id: "one",
@@ -250,12 +253,107 @@ it("annotates Bugbot threads with distinct review-pass counts", () => {
},
},
};
- const threads = parseReviewThreads(response);
+ const threads = buildReviewThreads(parseReviewThreadPage(response).threads);
expect(threads).toHaveLength(2);
expect(threads.map((thread) => thread.isBugbot)).toEqual([true, true]);
expect(threads.map((thread) => thread.bugbotReviewPasses)).toEqual([3, 3]);
});
+describe("review thread pagination", () => {
+ const thread = (id: string, resolved = false) => ({
+ id,
+ firstComment: {
+ authorLogin: "someone",
+ body: "please fix",
+ path: "a.ts",
+ line: 1,
+ createdAt: "now",
+ },
+ resolved,
+ });
+
+ it("keeps walking pages until the cursor is exhausted", async () => {
+ const reader = fakeReader({
+ threadPages: [
+ { threads: [thread("one")], endCursor: "next" },
+ { threads: [thread("two")], endCursor: "last" },
+ { threads: [thread("three")], endCursor: null },
+ ],
+ });
+ const threads = await resolveReviewThreads(reader, context);
+ expect(threads.map((item) => item.id)).toEqual(["one", "two", "three"]);
+ expect(reader.calls).toEqual([
+ "reviewThreadPage:null",
+ "reviewThreadPage:next",
+ "reviewThreadPage:last",
+ ]);
+ });
+
+ it("sees an unresolved thread that sits past the first page", async () => {
+ const reader = fakeReader({
+ threadPages: [
+ { threads: [thread("early", true)], endCursor: "next" },
+ { threads: [thread("late")], endCursor: null },
+ ],
+ });
+ const threads = await resolveReviewThreads(reader, context);
+ expect(threads.map((item) => item.id)).toEqual(["late"]);
+ });
+
+ it("stops after a single page when the cursor is exhausted", async () => {
+ const reader = fakeReader({
+ threadPages: [{ threads: [thread("only")], endCursor: null }],
+ });
+ expect((await resolveReviewThreads(reader, context)).length).toBe(1);
+ expect(reader.calls).toEqual(["reviewThreadPage:null"]);
+ });
+
+ it("counts Bugbot review passes across every page", async () => {
+ const bugbot = (id: string, runId: string) => ({
+ id,
+ firstComment: {
+ authorLogin: "bugbot",
+ body: `RUN_ID: ${runId}`,
+ path: "a.ts",
+ line: 1,
+ createdAt: "now",
+ },
+ resolved: false,
+ });
+ const reader = fakeReader({
+ threadPages: [
+ { threads: [bugbot("one", "run-1")], endCursor: "next" },
+ { threads: [bugbot("two", "run-2")], endCursor: null },
+ ],
+ });
+ const threads = await resolveReviewThreads(reader, context);
+ expect(threads.map((item) => item.bugbotReviewPasses)).toEqual([2, 2]);
+ });
+
+ it("drops resolved threads from later pages too", async () => {
+ const reader = fakeReader({
+ threadPages: [
+ { threads: [thread("one")], endCursor: "next" },
+ { threads: [thread("two", true)], endCursor: null },
+ ],
+ });
+ expect(
+ (await resolveReviewThreads(reader, context)).map((item) => item.id)
+ ).toEqual(["one"]);
+ });
+
+ it("rejects a page that omits pageInfo instead of assuming one page", async () => {
+ const response = {
+ data: {
+ repository: {
+ pullRequest: { reviewThreads: { nodes: [] } },
+ },
+ },
+ };
+ expect(() => parseReviewThreadPage(response)).toThrow(WatcherQueryError);
+ });
+});
+
describe("context and stack discovery", () => {
it("returns a fully explicit context without any reader call", async () => {
const reader = fakeReader();
diff --git a/pstack/skills/poteto-mode/scripts/watch-pr/github.ts b/pstack/skills/poteto-mode/scripts/watch-pr/github.ts
index 79bc06fb..cfc3764a 100644
--- a/pstack/skills/poteto-mode/scripts/watch-pr/github.ts
+++ b/pstack/skills/poteto-mode/scripts/watch-pr/github.ts
@@ -2,7 +2,7 @@ import { spawn } from "node:child_process";
import type * as T from "./types.ts";
import { nonEmpty, parsePrNumber } from "./types.ts";
export const REVIEW_THREADS_QUERY =
- "\nquery ReviewThreads($owner: String!, $repo: String!, $pr: Int!) {\n repository(owner: $owner, name: $repo) {\n pullRequest(number: $pr) {\n reviewThreads(first: 100) {\n nodes {\n id\n isResolved\n comments(first: 10) {\n nodes {\n body\n createdAt\n path\n line\n author { login }\n }\n }\n }\n }\n }\n }\n}\n";
+ "\nquery ReviewThreads($owner: String!, $repo: String!, $pr: Int!, $after: String) {\n repository(owner: $owner, name: $repo) {\n pullRequest(number: $pr) {\n reviewThreads(first: 100, after: $after) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n isResolved\n comments(first: 10) {\n nodes {\n body\n createdAt\n path\n line\n author { login }\n }\n }\n }\n }\n }\n }\n}\n";
export const PR_COMMIT_STATUS_QUERY =
"\nquery PrCommitStatuses($owner: String!, $repo: String!, $pr: Int!) {\n repository(owner: $owner, name: $repo) {\n pullRequest(number: $pr) {\n commits(last: 50) {\n nodes {\n commit {\n oid\n statusCheckRollup {\n state\n }\n }\n }\n }\n }\n }\n}\n";
export const PR_CHECK_ROLLUP_QUERY =
@@ -357,16 +357,13 @@ function passKey(comment: T.ReviewComment | null): string | null {
}
return null;
}
-export function parseReviewThreads(value: unknown): readonly T.ReviewThread[] {
- const nodes = list(
- at(value, ["data", "repository", "pullRequest", "reviewThreads", "nodes"]),
- "reviewThreads.nodes"
+export function parseReviewThreadPage(value: unknown): T.ReviewThreadPage {
+ const reviewThreads = record(
+ at(value, ["data", "repository", "pullRequest", "reviewThreads"]),
+ "reviewThreads"
);
- const threads: {
- readonly id: string;
- readonly firstComment: T.ReviewComment | null;
- readonly resolved: boolean;
- }[] = [];
+ const nodes = list(reviewThreads.nodes, "reviewThreads.nodes");
+ const threads: T.RawReviewThread[] = [];
for (const node of nodes) {
const thread = record(node, "review thread");
if (typeof thread.isResolved !== "boolean")
@@ -381,6 +378,18 @@ export function parseReviewThreads(value: unknown): readonly T.ReviewThread[] {
resolved: thread.isResolved,
});
}
+ const page = record(reviewThreads.pageInfo, "reviewThreads.pageInfo");
+ if (typeof page.hasNextPage !== "boolean")
+ missing("reviewThreads.pageInfo.hasNextPage", page.hasNextPage);
+ const cursor = optionalString(
+ page.endCursor,
+ "reviewThreads.pageInfo.endCursor"
+ );
+ return { threads, endCursor: page.hasNextPage && cursor ? cursor : null };
+}
+export function buildReviewThreads(
+ threads: readonly T.RawReviewThread[]
+): readonly T.ReviewThread[] {
const keys = new Set<string>();
let keyless = false;
for (const thread of threads) {
@@ -568,12 +577,13 @@ export class GhGitHubReader implements T.GitHubReader {
);
return { checks, endCursor: page.hasNextPage && cursor ? cursor : null };
}
- async reviewThreads(
- context: T.PrContext
- ): Promise<readonly T.ReviewThread[]> {
- return parseReviewThreads(
- await runJson(graphqlArgs(REVIEW_THREADS_QUERY, context))
- );
+ async reviewThreadPage(
+ context: T.PrContext,
+ after: string | null
+ ): Promise<T.ReviewThreadPage> {
+ const argv = graphqlArgs(REVIEW_THREADS_QUERY, context);
+ if (after !== null) argv.push("-f", `after=${after}`);
+ return parseReviewThreadPage(await runJson(argv));
}
async commitRollups(
context: T.PrContext
@@ -623,6 +633,19 @@ export async function resolveChecks(
: "fast path and GraphQL rollup were empty";
throw new ChecksUnavailable(`could not read PR checks: ${suffix}`);
}
+export async function resolveReviewThreads(
+ reader: T.GitHubReader,
+ context: T.PrContext
+): Promise<readonly T.ReviewThread[]> {
+ const threads: T.RawReviewThread[] = [];
+ let after: string | null = null;
+ do {
+ const page = await reader.reviewThreadPage(context, after);
+ threads.push(...page.threads);
+ after = page.endCursor;
+ } while (after !== null);
+ return buildReviewThreads(threads);
+}
export async function resolveContext(args: {
readonly reader: T.GitHubReader;
readonly owner: string | null;
diff --git a/pstack/skills/poteto-mode/scripts/watch-pr/policy.test.ts b/pstack/skills/poteto-mode/scripts/watch-pr/policy.test.ts
index 0620dded..6dae16da 100644
--- a/pstack/skills/poteto-mode/scripts/watch-pr/policy.test.ts
+++ b/pstack/skills/poteto-mode/scripts/watch-pr/policy.test.ts
@@ -106,7 +106,7 @@ describe("snapshot query planning", () => {
expect(snapshot.ci.kind).toBe("ci-pending");
expect(reader.calls).toEqual([
"pullRequest",
- "reviewThreads",
+ "reviewThreadPage:null",
"checksFastPath",
]);
});
diff --git a/pstack/skills/poteto-mode/scripts/watch-pr/policy.ts b/pstack/skills/poteto-mode/scripts/watch-pr/policy.ts
index 4665be37..cabadb9b 100644
--- a/pstack/skills/poteto-mode/scripts/watch-pr/policy.ts
+++ b/pstack/skills/poteto-mode/scripts/watch-pr/policy.ts
@@ -1,4 +1,8 @@
-import { WatcherQueryError, resolveChecks } from "./github.ts";
+import {
+ WatcherQueryError,
+ resolveChecks,
+ resolveReviewThreads,
+} from "./github.ts";
import type * as T from "./types.ts";
import { nonEmpty } from "./types.ts";
export function assessGitHubMerge(args: {
@@ -63,7 +67,7 @@ export async function readSnapshot(args: {
return { kind: "merged", context: args.context, facts };
if (facts.state === "CLOSED")
return { kind: "closed", context: args.context, facts };
- const threads = await args.reader.reviewThreads(args.context);
+ const threads = await resolveReviewThreads(args.reader, args.context);
const checks = await resolveChecks(args.reader, args.context);
const failed = nonEmpty(
checks.checks.filter(
diff --git a/pstack/skills/poteto-mode/scripts/watch-pr/types.ts b/pstack/skills/poteto-mode/scripts/watch-pr/types.ts
index b67144e0..6c94e0a6 100644
--- a/pstack/skills/poteto-mode/scripts/watch-pr/types.ts
+++ b/pstack/skills/poteto-mode/scripts/watch-pr/types.ts
@@ -379,6 +379,15 @@ export interface RollupPage {
readonly checks: readonly Check[];
readonly endCursor: string | null;
}
+export interface RawReviewThread {
+ readonly id: string;
+ readonly firstComment: ReviewComment | null;
+ readonly resolved: boolean;
+}
+export interface ReviewThreadPage {
+ readonly threads: readonly RawReviewThread[];
+ readonly endCursor: string | null;
+}
export interface GitHubReader {
originRepo(): Promise<Repository | null>;
currentPr(pr: PrNumber | null): Promise<PrContext>;
@@ -389,7 +398,10 @@ export interface GitHubReader {
context: PrContext,
after: string | null
): Promise<RollupPage>;
- reviewThreads(context: PrContext): Promise<readonly ReviewThread[]>;
+ reviewThreadPage(
+ context: PrContext,
+ after: string | null
+ ): Promise<ReviewThreadPage>;
commitRollups(context: PrContext): Promise<readonly CommitRollup[]>;
}
export interface PollingOptions {