Improve PR review diffs (#139)

* Improve PR review diffs

* Clean up

* Add logging
This commit is contained in:
Colin McDonnell
2026-01-21 00:50:19 +00:00
committed by pullfrog[bot]
parent 1edeaa0f4c
commit a3a1530da2
2 changed files with 215 additions and 253 deletions
+90 -98
View File
@@ -121223,43 +121223,56 @@ function CreatePullRequestReviewTool(ctx) {
// mcp/reviewComments.ts
import { writeFileSync as writeFileSync3 } from "node:fs";
import { join as join5 } from "node:path";
var REVIEW_THREADS_QUERY = `
query ($owner: String!, $repo: String!, $pullNumber: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pullNumber) {
reviewThreads(first: 100) {
var REPLY_TO_FRAGMENT = `
replyTo {
databaseId
body
author { login }
replyTo {
databaseId
body
author { login }
replyTo {
databaseId
body
author { login }
replyTo {
databaseId
body
author { login }
replyTo {
databaseId
body
author { login }
}
}
}
}
}
`;
var REVIEW_QUERY = `
query ($nodeId: ID!) {
node(id: $nodeId) {
... on PullRequestReview {
databaseId
author { login }
comments(first: 100) {
nodes {
diffSide
startDiffSide
comments(first: 100) {
nodes {
id
databaseId
body
path
line
startLine
url
author {
login
}
createdAt
updatedAt
pullRequestReview {
databaseId
}
replyTo {
databaseId
}
reactionGroups {
content
reactors(first: 10) {
nodes {
... on Actor {
login
}
}
}
databaseId
body
path
line
startLine
diffHunk
url
author { login }
createdAt
${REPLY_TO_FRAGMENT}
reactionGroups {
content
reactors(first: 10) {
nodes {
... on Actor { login }
}
}
}
@@ -121270,20 +121283,11 @@ query ($owner: String!, $repo: String!, $pullNumber: Int!) {
}
`;
var MAX_BODY_PREVIEW = 80;
var MAX_THREAD_DEPTH = 10;
function truncateBody(body) {
const oneLine = body.replace(/\n/g, " ").trim();
if (oneLine.length <= MAX_BODY_PREVIEW) return oneLine;
return oneLine.slice(0, MAX_BODY_PREVIEW - 3) + "...";
}
function getReplyChain(comment, commentMap, depth = 0) {
if (depth >= MAX_THREAD_DEPTH) return [];
const parentId = comment.replyTo?.databaseId;
if (!parentId) return [];
const parent = commentMap.get(parentId);
if (!parent) return [];
return [...getReplyChain(parent, commentMap, depth + 1), parent];
}
function escapeXml(text) {
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
}
@@ -121293,6 +121297,11 @@ function hasThumbsUpFrom(comment, username) {
if (!thumbsUp?.reactors?.nodes) return false;
return thumbsUp.reactors.nodes.some((r) => r?.login === username);
}
function flattenReplyToChain(replyTo) {
if (!replyTo) return [];
const parent = flattenReplyToChain(replyTo.replyTo ?? null);
return [...parent, { body: replyTo.body, author: replyTo.author?.login ?? "unknown" }];
}
var GetReviewComments = type({
pull_number: type.number.describe("The pull request number"),
review_id: type.number.describe("The review ID to get comments for"),
@@ -121304,90 +121313,72 @@ function GetReviewCommentsTool(ctx) {
description: "Get review comments for a pull request review, including thread context. When approved_by is provided, only returns comments that user approved with \u{1F44D}. Returns commentsPath pointing to a file with full comment details in XML format.",
parameters: GetReviewComments,
execute: execute(async ({ pull_number, review_id, approved_by }) => {
const response = await ctx.octokit.graphql(REVIEW_THREADS_QUERY, {
const { data: review } = await ctx.octokit.rest.pulls.getReview({
owner: ctx.repo.owner,
repo: ctx.repo.name,
pullNumber: pull_number
pull_number,
review_id
});
const pullRequest = response.repository?.pullRequest;
if (!pullRequest?.reviewThreads?.nodes) {
const reviewer = review.user?.login ?? "unknown";
const response = await ctx.octokit.graphql(REVIEW_QUERY, {
nodeId: review.node_id
});
const reviewComments = response.node?.comments?.nodes;
if (!reviewComments) {
return {
review_id,
pull_number,
reviewer: "unknown",
reviewer,
count: 0,
commentsPath: null,
message: "No review threads found"
message: "No comments found for this review"
};
}
const commentMap = /* @__PURE__ */ new Map();
for (const thread of pullRequest.reviewThreads.nodes) {
if (!thread?.comments?.nodes) continue;
for (const comment of thread.comments.nodes) {
if (comment) commentMap.set(comment.databaseId, comment);
}
}
const leafComments = [];
for (const thread of pullRequest.reviewThreads.nodes) {
if (!thread?.comments?.nodes) continue;
const threadComments = thread.comments.nodes.filter(
(c) => c !== null
);
if (threadComments.length === 0) continue;
for (const comment of threadComments) {
if (comment.pullRequestReview?.databaseId !== review_id) continue;
if (approved_by && !hasThumbsUpFrom(comment, approved_by)) continue;
const replyChain = getReplyChain(comment, commentMap);
leafComments.push({
comment,
thread: replyChain,
side: thread.diffSide
});
}
}
if (leafComments.length === 0) {
const allComments = reviewComments.filter((c) => c !== null);
const comments = approved_by ? allComments.filter((c) => hasThumbsUpFrom(c, approved_by)) : allComments;
if (comments.length === 0) {
return {
review_id,
pull_number,
reviewer: "unknown",
reviewer,
count: 0,
commentsPath: null,
message: approved_by ? `No comments with \u{1F44D} from ${approved_by}` : "No comments found for this review"
};
}
const reviewer = leafComments[0].comment.author?.login ?? "unknown";
const lines = [];
lines.push(
`<review_comments count="${leafComments.length}" reviewer="${escapeXml(reviewer)}">`
);
lines.push(`<review_comments count="${comments.length}" reviewer="${escapeXml(reviewer)}">`);
lines.push("");
lines.push("<summary>");
for (const leaf of leafComments) {
const line = leaf.comment.line ?? leaf.comment.startLine ?? 0;
const preview = escapeXml(truncateBody(leaf.comment.body));
for (const comment of comments) {
const line = comment.line ?? comment.startLine ?? 0;
const preview = escapeXml(truncateBody(comment.body));
lines.push(
` <comment id="${leaf.comment.databaseId}" file="${escapeXml(leaf.comment.path)}" line="${line}">${preview}</comment>`
` <comment id="${comment.databaseId}" file="${escapeXml(comment.path)}" line="${line}">${preview}</comment>`
);
}
lines.push("</summary>");
lines.push("");
for (const leaf of leafComments) {
const line = leaf.comment.line ?? leaf.comment.startLine ?? 0;
const author = leaf.comment.author?.login ?? "unknown";
for (const comment of comments) {
const line = comment.line ?? comment.startLine ?? 0;
const author = comment.author?.login ?? "unknown";
lines.push(
`<comment id="${leaf.comment.databaseId}" file="${escapeXml(leaf.comment.path)}" line="${line}" author="${escapeXml(author)}">`
`<comment id="${comment.databaseId}" file="${escapeXml(comment.path)}" line="${line}" author="${escapeXml(author)}">`
);
if (leaf.thread.length > 0) {
const thread = flattenReplyToChain(comment.replyTo);
if (thread.length > 0) {
lines.push(" <thread>");
for (const msg of leaf.thread) {
const msgAuthor = msg.author?.login ?? "unknown";
for (const msg of thread) {
lines.push(
` <message id="${msg.databaseId}" author="${escapeXml(msgAuthor)}">${escapeXml(msg.body)}</message>`
` <message author="${escapeXml(msg.author)}">${escapeXml(msg.body)}</message>`
);
}
lines.push(" </thread>");
}
lines.push(` <body>${escapeXml(leaf.comment.body)}</body>`);
lines.push(" <diff>");
lines.push(escapeXml(comment.diffHunk));
lines.push(" </diff>");
lines.push(` <body>${escapeXml(comment.body)}</body>`);
lines.push("</comment>");
lines.push("");
}
@@ -121400,13 +121391,13 @@ function GetReviewCommentsTool(ctx) {
const filename = approved_by ? `review-${review_id}-approved-by-${approved_by}.xml` : `review-${review_id}-comments.xml`;
const commentsPath = join5(tempDir, filename);
writeFileSync3(commentsPath, content);
log.debug(`wrote ${leafComments.length} comments to ${commentsPath}`);
log.debug(`content: ${content}`);
log.info(`wrote ${comments.length} comments to ${commentsPath}`);
log.box(content);
return {
review_id,
pull_number,
reviewer,
count: leafComments.length,
count: comments.length,
commentsPath
};
})
@@ -121430,6 +121421,7 @@ function ListPullRequestReviewsTool(ctx) {
pull_number,
reviews: reviews.map((review) => ({
id: review.id,
node_id: review.node_id,
body: review.body,
state: review.state,
user: review.user?.login,