feat(FN-5181): paginate PR review snapshots

Adds paginated PR review snapshots to the dashboard GitHub module with corresponding test coverage across the unit and route layers.

Fusion-Task-Id: FN-5181
This commit is contained in:
Fusion (runfusion.ai)
2026-05-20 01:20:02 -07:00
committed by gsxdsm
parent ab0a253004
commit d34cf8c9aa
3 changed files with 412 additions and 82 deletions

View File

@@ -1581,14 +1581,138 @@ describe("GitHubClient", () => {
});
});
describe("FN-5181 PR review pagination", () => {
it("FN-5181 paginates GraphQL review details across comment and review pages", async () => {
mockIsGhAvailable.mockReturnValue(false);
const clientWithToken = new GitHubClient("ghp_token");
const fetchSpy = vi.spyOn(global, "fetch" as any)
.mockResolvedValueOnce({
ok: true,
status: 200,
statusText: "OK",
json: async () => ({
data: {
repository: {
pullRequest: {
reviewDecision: "CHANGES_REQUESTED",
comments: {
nodes: [
{ id: "C_1", body: "first comment", createdAt: "2024-01-01T00:00:00Z", updatedAt: "2024-01-01T00:00:01Z", url: "https://example.com/c1", author: { login: "alice" } },
],
pageInfo: { hasNextPage: true, endCursor: "comment-cursor-1" },
},
reviews: {
nodes: [
{ id: "R_1", state: "COMMENTED", body: "first review", submittedAt: "2024-01-01T00:00:02Z", url: "https://example.com/r1", author: { login: "reviewer-1" } },
],
pageInfo: { hasNextPage: true, endCursor: "review-cursor-1" },
},
},
},
},
}),
} as any)
.mockResolvedValueOnce({
ok: true,
status: 200,
statusText: "OK",
json: async () => ({
data: {
repository: {
pullRequest: {
reviewDecision: "CHANGES_REQUESTED",
comments: {
nodes: [
{ id: "C_2", body: "second comment", createdAt: "2024-01-01T00:00:03Z", updatedAt: "2024-01-01T00:00:04Z", url: "https://example.com/c2", author: { login: "bob" } },
],
pageInfo: { hasNextPage: false, endCursor: "comment-cursor-2" },
},
reviews: {
nodes: [
{ id: "R_2", state: "APPROVED", body: "second review", submittedAt: "2024-01-01T00:00:05Z", url: "https://example.com/r2", author: { login: "reviewer-2" } },
],
pageInfo: { hasNextPage: false, endCursor: "review-cursor-2" },
},
},
},
},
}),
} as any);
const details = await (clientWithToken as any).getPrReviewDetailsWithApi("owner", "repo", 1);
expect(details.reviewDecision).toBe("CHANGES_REQUESTED");
expect(details.comments.map((comment: any) => comment.id)).toEqual(["C_1", "C_2"]);
expect(details.reviews.map((review: any) => review.id)).toEqual(["R_1", "R_2"]);
expect(fetchSpy).toHaveBeenCalledTimes(2);
const firstBody = JSON.parse(String(fetchSpy.mock.calls[0]?.[1]?.body));
const secondBody = JSON.parse(String(fetchSpy.mock.calls[1]?.[1]?.body));
expect(firstBody.variables).toEqual(expect.objectContaining({ commentsAfter: null, reviewsAfter: null, fetchComments: true, fetchReviews: true }));
expect(secondBody.variables).toEqual(expect.objectContaining({ commentsAfter: "comment-cursor-1", reviewsAfter: "review-cursor-1", fetchComments: true, fetchReviews: true }));
fetchSpy.mockRestore();
});
it("FN-5181 paginates gh review details across issue comments, inline comments, and review pages", async () => {
const issueCommentPageOne = Array.from({ length: 100 }, (_, index) => ({
id: `issue-${index + 1}`,
body: `issue comment ${index + 1}`,
author: { login: `issue-author-${index + 1}` },
createdAt: new Date(Date.UTC(2024, 0, 1, 0, 0, index)).toISOString(),
updatedAt: new Date(Date.UTC(2024, 0, 1, 0, 1, index)).toISOString(),
url: `https://example.com/issue-${index + 1}`,
}));
const reviewPageOne = Array.from({ length: 100 }, (_, index) => ({
id: `review-${index + 1}`,
state: "COMMENTED",
body: `review ${index + 1}`,
submittedAt: new Date(Date.UTC(2024, 0, 1, 0, 2, index)).toISOString(),
url: `https://example.com/review-${index + 1}`,
author: { login: `reviewer-${index + 1}` },
}));
mockRunGhJsonAsync
.mockResolvedValueOnce({ reviewDecision: "APPROVED" })
.mockResolvedValueOnce(issueCommentPageOne)
.mockResolvedValueOnce([
{ id: "issue-101", body: "issue comment 101", author: { login: "bob" }, createdAt: "2024-01-01T00:10:01Z", updatedAt: "2024-01-01T00:10:02Z", url: "https://example.com/issue-101" },
])
.mockResolvedValueOnce([
{ id: 301, body: "inline comment", user: { login: "carol" }, created_at: "2024-01-01T00:10:03Z", updated_at: "2024-01-01T00:10:04Z", html_url: "https://example.com/inline-301" },
])
.mockResolvedValueOnce(reviewPageOne)
.mockResolvedValueOnce([
{ id: "review-101", state: "APPROVED", body: "review 101", submittedAt: "2024-01-01T00:10:05Z", url: "https://example.com/review-101", author: { login: "erin" } },
]);
const details = await (client as any).getPrReviewDetailsWithGh("owner", "repo", 1);
expect(details.reviewDecision).toBe("APPROVED");
expect(details.comments).toHaveLength(102);
expect(details.comments[0]?.id).toBe("issue-1");
expect(details.comments.at(-1)?.id).toBe("301");
expect(details.comments.at(-1)?.author.login).toBe("carol");
expect(details.reviews).toHaveLength(101);
expect(details.reviews[0]?.id).toBe("review-1");
expect(details.reviews.at(-1)?.id).toBe("review-101");
expect(mockRunGhJsonAsync).toHaveBeenNthCalledWith(2, ["api", "repos/owner/repo/issues/1/comments?per_page=100&page=1"]);
expect(mockRunGhJsonAsync).toHaveBeenNthCalledWith(3, ["api", "repos/owner/repo/issues/1/comments?per_page=100&page=2"]);
expect(mockRunGhJsonAsync).toHaveBeenNthCalledWith(4, ["api", "repos/owner/repo/pulls/1/comments?per_page=100&page=1"]);
expect(mockRunGhJsonAsync).toHaveBeenNthCalledWith(5, ["api", "repos/owner/repo/pulls/1/reviews?per_page=100&page=1"]);
expect(mockRunGhJsonAsync).toHaveBeenNthCalledWith(6, ["api", "repos/owner/repo/pulls/1/reviews?per_page=100&page=2"]);
});
});
describe("getPrReviewSnapshot", () => {
it("normalizes reviews/comments into review-state items and summary", async () => {
mockRunGhJsonAsync
.mockResolvedValueOnce({
reviewDecision: "CHANGES_REQUESTED",
reviews: [{ id: "r1", state: "CHANGES_REQUESTED", body: "please fix", submittedAt: "2024-01-01T00:00:00Z", author: { login: "octocat" }, url: "https://github.com/owner/repo/pull/1#review-r1" }],
comments: [{ id: "c1", body: "nit", createdAt: "2024-01-01T00:00:00Z", updatedAt: "2024-01-01T00:00:01Z", author: { login: "reviewer" }, url: "https://github.com/owner/repo/pull/1#issuecomment-c1" }],
})
.mockResolvedValueOnce({ reviewDecision: "CHANGES_REQUESTED" })
.mockResolvedValueOnce([
{ id: "c1", body: "nit", createdAt: "2024-01-01T00:00:00Z", updatedAt: "2024-01-01T00:00:01Z", author: { login: "reviewer" }, url: "https://github.com/owner/repo/pull/1#issuecomment-c1" },
])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([
{ id: "r1", state: "CHANGES_REQUESTED", body: "please fix", submittedAt: "2024-01-01T00:00:00Z", author: { login: "octocat" }, url: "https://github.com/owner/repo/pull/1#review-r1" },
])
.mockResolvedValueOnce({
number: 1,
url: "https://github.com/owner/repo/pull/1",

View File

@@ -69,7 +69,7 @@ function createStore(task: Task): TaskStore {
} as unknown as TaskStore;
}
describe("PR reviews routes", () => {
describe("FN-5181 PR reviews routes", () => {
beforeEach(() => {
vi.spyOn(githubRateLimiter, "canMakeRequest").mockReturnValue(true);
});
@@ -98,6 +98,35 @@ describe("PR reviews routes", () => {
expect(response.body.comments).toHaveLength(1);
});
it("FN-5181 returns every review item from the snapshot when pagination exceeds 100 comments", async () => {
const task = createTask();
const store = createStore(task);
const items = Array.from({ length: 205 }, (_, index) => ({
id: `gh-comment-${index + 1}`,
githubCommentId: index + 1,
body: `comment ${index + 1}`,
author: { login: `reviewer-${index + 1}` },
state: "COMMENTED",
createdAt: new Date(Date.UTC(2024, 0, 1, 0, 0, index)).toISOString(),
}));
vi.spyOn(GitHubClient.prototype, "getPrReviewSnapshot").mockResolvedValue({
decision: "COMMENTED",
checks: [],
items,
prInfo: task.prInfo!,
commentCount: items.length,
summary: { reviewDecision: "COMMENTED", reviewers: [], blockingReasons: [], checks: [] },
} as never);
const app = createServer(store);
const response = await performGet(app, "/api/tasks/FN-001/pr/reviews");
expect(response.status).toBe(200);
expect(response.body.snapshot.items).toHaveLength(205);
expect(response.body.snapshot.items[0]?.id).toBe("gh-comment-1");
expect(response.body.snapshot.items.at(-1)?.id).toBe("gh-comment-205");
});
it("moves in-review task to todo once on changes-requested refresh", async () => {
const task = createTask();
const store = createStore(task);

View File

@@ -126,6 +126,9 @@ export interface PrComment {
html_url: string;
}
const PR_REVIEW_PAGE_SIZE = 100;
const MAX_PR_REVIEW_PAGES = 10;
export type ReviewDecision = "APPROVED" | "CHANGES_REQUESTED" | "REVIEW_REQUIRED" | null;
export type PrCheckState =
| "success"
@@ -271,6 +274,57 @@ interface PrReviewDetails {
reviews: GhReviewJson[];
}
interface GraphQlPageInfo {
hasNextPage?: boolean | null;
endCursor?: string | null;
}
interface GraphQlPrCommentNode {
id: string;
body: string;
createdAt: string;
updatedAt: string;
url: string;
author?: { login?: string | null } | null;
}
interface GraphQlPrReviewNode {
id: string;
state: string;
body?: string | null;
submittedAt?: string | null;
url?: string | null;
author?: { login?: string | null } | null;
}
interface RestPullRequestComment {
id: number;
body?: string | null;
user?: { login?: string | null } | null;
created_at?: string;
updated_at?: string;
html_url?: string;
}
interface GraphQlPrReviewDetailsPayload {
data?: {
repository?: {
pullRequest?: {
reviewDecision?: ReviewDecision;
comments?: {
nodes?: Array<GraphQlPrCommentNode | null>;
pageInfo?: GraphQlPageInfo | null;
} | null;
reviews?: {
nodes?: Array<GraphQlPrReviewNode | null>;
pageInfo?: GraphQlPageInfo | null;
} | null;
} | null;
} | null;
};
errors?: Array<{ message: string }>;
}
interface GhPrListJson {
number: number;
url: string;
@@ -990,105 +1044,228 @@ export class GitHubClient {
}
private async getPrReviewDetailsWithGh(owner: string, repo: string, number: number): Promise<PrReviewDetails> {
const pr = await runGhJsonAsync<GhPrViewJson>([
const pr = await runGhJsonAsync<Pick<GhPrViewJson, "reviewDecision">>([
"pr",
"view",
String(number),
"--repo",
`${owner}/${repo}`,
"--json",
"reviewDecision,reviews,comments",
"reviewDecision",
]);
const issueComments = await this.fetchGhApiPages<GhPrViewJson["comments"][number]>(
`repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/issues/${number}/comments`,
owner,
repo,
number,
"issue-comments",
);
const pullComments = await this.fetchGhApiPages<RestPullRequestComment>(
`repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${number}/comments`,
owner,
repo,
number,
"pull-comments",
);
const reviews = await this.fetchGhApiPages<GhReviewJson>(
`repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${number}/reviews`,
owner,
repo,
number,
"reviews",
);
const comments = [
...(issueComments ?? []).map((comment) => ({
id: comment.id,
body: comment.body,
author: { login: comment.author?.login ?? "reviewer" },
createdAt: comment.createdAt,
updatedAt: comment.updatedAt,
url: comment.url,
})),
...(pullComments ?? []).map((comment) => ({
id: String(comment.id),
body: comment.body ?? "",
author: { login: comment.user?.login ?? "reviewer" },
createdAt: comment.created_at ?? new Date().toISOString(),
updatedAt: comment.updated_at ?? comment.created_at ?? new Date().toISOString(),
url: comment.html_url ?? "",
})),
].sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
return {
reviewDecision: pr.reviewDecision ?? null,
comments: pr.comments ?? [],
reviews: pr.reviews ?? [],
comments,
reviews: (reviews ?? []).map((review) => ({
id: review.id,
state: review.state,
body: review.body,
submittedAt: review.submittedAt,
url: review.url,
author: { login: review.author?.login ?? "reviewer" },
})),
};
}
private async fetchGhApiPages<T>(
path: string,
owner: string,
repo: string,
number: number,
label: string,
): Promise<T[]> {
const items: T[] = [];
for (let page = 1; page <= MAX_PR_REVIEW_PAGES; page += 1) {
const separator = path.includes("?") ? "&" : "?";
const pagePath = `${path}${separator}per_page=${PR_REVIEW_PAGE_SIZE}&page=${page}`;
const pageItems = await runGhJsonAsync<T[]>(["api", pagePath]);
items.push(...pageItems);
if (pageItems.length < PR_REVIEW_PAGE_SIZE) {
return items;
}
}
process.stderr.write(
`[github] PR review pagination cap hit for ${owner}/${repo}#${number} (${label}) after ${MAX_PR_REVIEW_PAGES} pages\n`,
);
return items;
}
private async getPrReviewDetailsWithApi(owner: string, repo: string, number: number): Promise<PrReviewDetails> {
const response = await fetch(`${this.baseUrl}/graphql`, {
method: "POST",
headers: this.buildHeaders(),
body: JSON.stringify({
query: `query PullRequestReviewDetails($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $number) {
reviewDecision
comments(first: 100) {
nodes {
id
body
createdAt
updatedAt
url
author { login }
const comments: PrReviewDetails["comments"] = [];
const reviews: PrReviewDetails["reviews"] = [];
let reviewDecision: ReviewDecision = null;
let commentsAfter: string | null = null;
let reviewsAfter: string | null = null;
let fetchComments = true;
let fetchReviews = true;
for (let page = 1; page <= MAX_PR_REVIEW_PAGES; page += 1) {
const response = await fetch(`${this.baseUrl}/graphql`, {
method: "POST",
headers: this.buildHeaders(),
body: JSON.stringify({
query: `query PullRequestReviewDetails(
$owner: String!
$repo: String!
$number: Int!
$commentsAfter: String
$reviewsAfter: String
$fetchComments: Boolean!
$fetchReviews: Boolean!
) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $number) {
reviewDecision
comments(first: ${PR_REVIEW_PAGE_SIZE}, after: $commentsAfter) @include(if: $fetchComments) {
nodes {
id
body
createdAt
updatedAt
url
author { login }
}
pageInfo {
hasNextPage
endCursor
}
}
}
reviews(first: 100) {
nodes {
id
state
body
submittedAt
url
author { login }
reviews(first: ${PR_REVIEW_PAGE_SIZE}, after: $reviewsAfter) @include(if: $fetchReviews) {
nodes {
id
state
body
submittedAt
url
author { login }
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
}`,
variables: { owner, repo, number },
}),
});
}`,
variables: {
owner,
repo,
number,
commentsAfter,
reviewsAfter,
fetchComments,
fetchReviews,
},
}),
});
const payload = await response.json() as {
data?: {
repository?: {
pullRequest?: {
reviewDecision?: ReviewDecision;
comments?: { nodes?: Array<{ id: string; body: string; createdAt: string; updatedAt: string; url: string; author?: { login?: string | null } | null } | null> };
reviews?: { nodes?: Array<{ id: string; state: string; body?: string | null; submittedAt?: string | null; url?: string | null; author?: { login?: string | null } | null } | null> };
};
const payload = await response.json() as GraphQlPrReviewDetailsPayload;
if (!response.ok || payload.errors?.length) {
const message = payload.errors?.[0]?.message || response.statusText;
throw new Error(`GitHub API error: ${response.status} ${message}`);
}
const pr = payload.data?.repository?.pullRequest;
if (!pr) {
throw new Error(`PR #${number} not found in ${owner}/${repo}`);
}
reviewDecision = pr.reviewDecision ?? null;
if (fetchComments) {
comments.push(...(pr.comments?.nodes ?? []).flatMap((comment) => {
if (!comment) return [];
return [{
id: comment.id,
body: comment.body,
createdAt: comment.createdAt,
updatedAt: comment.updatedAt,
url: comment.url,
author: { login: comment.author?.login ?? "reviewer" },
}];
}));
fetchComments = Boolean(pr.comments?.pageInfo?.hasNextPage);
commentsAfter = pr.comments?.pageInfo?.endCursor ?? null;
}
if (fetchReviews) {
reviews.push(...(pr.reviews?.nodes ?? []).flatMap((review) => {
if (!review) return [];
return [{
id: review.id,
state: review.state,
body: review.body,
submittedAt: review.submittedAt,
url: review.url,
author: { login: review.author?.login ?? "reviewer" },
}];
}));
fetchReviews = Boolean(pr.reviews?.pageInfo?.hasNextPage);
reviewsAfter = pr.reviews?.pageInfo?.endCursor ?? null;
}
if (!fetchComments && !fetchReviews) {
return {
reviewDecision,
comments,
reviews,
};
};
errors?: Array<{ message: string }>;
};
if (!response.ok || payload.errors?.length) {
const message = payload.errors?.[0]?.message || response.statusText;
throw new Error(`GitHub API error: ${response.status} ${message}`);
}
}
const pr = payload.data?.repository?.pullRequest;
if (!pr) {
throw new Error(`PR #${number} not found in ${owner}/${repo}`);
}
process.stderr.write(
`[github] PR review pagination cap hit for ${owner}/${repo}#${number} (graphql) after ${MAX_PR_REVIEW_PAGES} pages\n`,
);
return {
reviewDecision: pr.reviewDecision ?? null,
comments: (pr.comments?.nodes ?? []).flatMap((comment) => {
if (!comment) return [];
return [{
id: comment.id,
body: comment.body,
createdAt: comment.createdAt,
updatedAt: comment.updatedAt,
url: comment.url,
author: { login: comment.author?.login ?? "reviewer" },
}];
}),
reviews: (pr.reviews?.nodes ?? []).flatMap((review) => {
if (!review) return [];
return [{
id: review.id,
state: review.state,
body: review.body,
submittedAt: review.submittedAt,
url: review.url,
author: { login: review.author?.login ?? "reviewer" },
}];
}),
reviewDecision,
comments,
reviews,
};
}