Skip to content

Commit 5a4338c

Browse files
authored
adding review comments grouped as threads (#1554)
* adding review comments grouped as threads * minor fix * minor edit * update docs * fix docs * increase limit to 100 * fixtext
1 parent a48e306 commit 5a4338c

File tree

5 files changed

+357
-145
lines changed

5 files changed

+357
-145
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ The following sets of tools are available:
991991
2. get_diff - Get the diff of a pull request.
992992
3. get_status - Get status of a head commit in a pull request. This reflects status of builds and checks.
993993
4. get_files - Get the list of files changed in a pull request. Use with pagination parameters to control the number of results returned.
994-
5. get_review_comments - Get the review comments on a pull request. They are comments made on a portion of the unified diff during a pull request review. Use with pagination parameters to control the number of results returned.
994+
5. get_review_comments - Get review threads on a pull request. Each thread contains logically grouped review comments made on the same code location during pull request reviews. Returns threads with metadata (isResolved, isOutdated, isCollapsed) and their associated comments. Use cursor-based pagination (perPage, after) to control results.
995995
6. get_reviews - Get the reviews on a pull request. When asked for review comments, use get_review_comments method.
996996
7. get_comments - Get comments on a pull request. Use this if user doesn't specifically want review comments. Use with pagination parameters to control the number of results returned.
997997
(string, required)

pkg/github/__toolsnaps__/pull_request_read.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"properties": {
1616
"method": {
1717
"type": "string",
18-
"description": "Action to specify what pull request data needs to be retrieved from GitHub. \nPossible options: \n 1. get - Get details of a specific pull request.\n 2. get_diff - Get the diff of a pull request.\n 3. get_status - Get status of a head commit in a pull request. This reflects status of builds and checks.\n 4. get_files - Get the list of files changed in a pull request. Use with pagination parameters to control the number of results returned.\n 5. get_review_comments - Get the review comments on a pull request. They are comments made on a portion of the unified diff during a pull request review. Use with pagination parameters to control the number of results returned.\n 6. get_reviews - Get the reviews on a pull request. When asked for review comments, use get_review_comments method.\n 7. get_comments - Get comments on a pull request. Use this if user doesn't specifically want review comments. Use with pagination parameters to control the number of results returned.\n",
18+
"description": "Action to specify what pull request data needs to be retrieved from GitHub. \nPossible options: \n 1. get - Get details of a specific pull request.\n 2. get_diff - Get the diff of a pull request.\n 3. get_status - Get status of a head commit in a pull request. This reflects status of builds and checks.\n 4. get_files - Get the list of files changed in a pull request. Use with pagination parameters to control the number of results returned.\n 5. get_review_comments - Get review threads on a pull request. Each thread contains logically grouped review comments made on the same code location during pull request reviews. Returns threads with metadata (isResolved, isOutdated, isCollapsed) and their associated comments. Use cursor-based pagination (perPage, after) to control results.\n 6. get_reviews - Get the reviews on a pull request. When asked for review comments, use get_review_comments method.\n 7. get_comments - Get comments on a pull request. Use this if user doesn't specifically want review comments. Use with pagination parameters to control the number of results returned.\n",
1919
"enum": [
2020
"get",
2121
"get_diff",

pkg/github/pullrequests.go

Lines changed: 115 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
// PullRequestRead creates a tool to get details of a specific pull request.
24-
func PullRequestRead(getClient GetClientFn, cache *lockdown.RepoAccessCache, t translations.TranslationHelperFunc, flags FeatureFlags) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) {
24+
func PullRequestRead(getClient GetClientFn, getGQLClient GetGQLClientFn, cache *lockdown.RepoAccessCache, t translations.TranslationHelperFunc, flags FeatureFlags) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) {
2525
schema := &jsonschema.Schema{
2626
Type: "object",
2727
Properties: map[string]*jsonschema.Schema{
@@ -33,7 +33,7 @@ Possible options:
3333
2. get_diff - Get the diff of a pull request.
3434
3. get_status - Get status of a head commit in a pull request. This reflects status of builds and checks.
3535
4. get_files - Get the list of files changed in a pull request. Use with pagination parameters to control the number of results returned.
36-
5. get_review_comments - Get the review comments on a pull request. They are comments made on a portion of the unified diff during a pull request review. Use with pagination parameters to control the number of results returned.
36+
5. get_review_comments - Get review threads on a pull request. Each thread contains logically grouped review comments made on the same code location during pull request reviews. Returns threads with metadata (isResolved, isOutdated, isCollapsed) and their associated comments. Use cursor-based pagination (perPage, after) to control results.
3737
6. get_reviews - Get the reviews on a pull request. When asked for review comments, use get_review_comments method.
3838
7. get_comments - Get comments on a pull request. Use this if user doesn't specifically want review comments. Use with pagination parameters to control the number of results returned.
3939
`,
@@ -107,7 +107,15 @@ Possible options:
107107
result, err := GetPullRequestFiles(ctx, client, owner, repo, pullNumber, pagination)
108108
return result, nil, err
109109
case "get_review_comments":
110-
result, err := GetPullRequestReviewComments(ctx, client, cache, owner, repo, pullNumber, pagination, flags)
110+
gqlClient, err := getGQLClient(ctx)
111+
if err != nil {
112+
return utils.NewToolResultErrorFromErr("failed to get GitHub GQL client", err), nil, nil
113+
}
114+
cursorPagination, err := OptionalCursorPaginationParams(args)
115+
if err != nil {
116+
return utils.NewToolResultError(err.Error()), nil, nil
117+
}
118+
result, err := GetPullRequestReviewComments(ctx, gqlClient, cache, owner, repo, pullNumber, cursorPagination, flags)
111119
return result, nil, err
112120
case "get_reviews":
113121
result, err := GetPullRequestReviews(ctx, client, cache, owner, repo, pullNumber, flags)
@@ -282,54 +290,124 @@ func GetPullRequestFiles(ctx context.Context, client *github.Client, owner, repo
282290
return utils.NewToolResultText(string(r)), nil
283291
}
284292

285-
func GetPullRequestReviewComments(ctx context.Context, client *github.Client, cache *lockdown.RepoAccessCache, owner, repo string, pullNumber int, pagination PaginationParams, ff FeatureFlags) (*mcp.CallToolResult, error) {
286-
opts := &github.PullRequestListCommentsOptions{
287-
ListOptions: github.ListOptions{
288-
PerPage: pagination.PerPage,
289-
Page: pagination.Page,
290-
},
293+
// GraphQL types for review threads query
294+
type reviewThreadsQuery struct {
295+
Repository struct {
296+
PullRequest struct {
297+
ReviewThreads struct {
298+
Nodes []reviewThreadNode
299+
PageInfo pageInfoFragment
300+
TotalCount githubv4.Int
301+
} `graphql:"reviewThreads(first: $first, after: $after)"`
302+
} `graphql:"pullRequest(number: $prNum)"`
303+
} `graphql:"repository(owner: $owner, name: $repo)"`
304+
}
305+
306+
type reviewThreadNode struct {
307+
ID githubv4.ID
308+
IsResolved githubv4.Boolean
309+
IsOutdated githubv4.Boolean
310+
IsCollapsed githubv4.Boolean
311+
Comments struct {
312+
Nodes []reviewCommentNode
313+
TotalCount githubv4.Int
314+
} `graphql:"comments(first: $commentsPerThread)"`
315+
}
316+
317+
type reviewCommentNode struct {
318+
ID githubv4.ID
319+
Body githubv4.String
320+
Path githubv4.String
321+
Line *githubv4.Int
322+
Author struct {
323+
Login githubv4.String
291324
}
325+
CreatedAt githubv4.DateTime
326+
UpdatedAt githubv4.DateTime
327+
URL githubv4.URI
328+
}
292329

293-
comments, resp, err := client.PullRequests.ListComments(ctx, owner, repo, pullNumber, opts)
330+
type pageInfoFragment struct {
331+
HasNextPage githubv4.Boolean
332+
HasPreviousPage githubv4.Boolean
333+
StartCursor githubv4.String
334+
EndCursor githubv4.String
335+
}
336+
337+
func GetPullRequestReviewComments(ctx context.Context, gqlClient *githubv4.Client, cache *lockdown.RepoAccessCache, owner, repo string, pullNumber int, pagination CursorPaginationParams, ff FeatureFlags) (*mcp.CallToolResult, error) {
338+
// Convert pagination parameters to GraphQL format
339+
gqlParams, err := pagination.ToGraphQLParams()
294340
if err != nil {
295-
return ghErrors.NewGitHubAPIErrorResponse(ctx,
296-
"failed to get pull request review comments",
297-
resp,
298-
err,
299-
), nil
341+
return utils.NewToolResultError(fmt.Sprintf("invalid pagination parameters: %v", err)), nil
300342
}
301-
defer func() { _ = resp.Body.Close() }()
302343

303-
if resp.StatusCode != http.StatusOK {
304-
body, err := io.ReadAll(resp.Body)
305-
if err != nil {
306-
return nil, fmt.Errorf("failed to read response body: %w", err)
307-
}
308-
return utils.NewToolResultError(fmt.Sprintf("failed to get pull request review comments: %s", string(body))), nil
344+
// Build variables for GraphQL query
345+
vars := map[string]any{
346+
"owner": githubv4.String(owner),
347+
"repo": githubv4.String(repo),
348+
"prNum": githubv4.Int(int32(pullNumber)), //nolint:gosec // pullNumber is controlled by user input validation
349+
"first": githubv4.Int(*gqlParams.First),
350+
"commentsPerThread": githubv4.Int(100),
351+
}
352+
353+
// Add cursor if provided
354+
if gqlParams.After != nil {
355+
vars["after"] = githubv4.String(*gqlParams.After)
356+
} else {
357+
vars["after"] = (*githubv4.String)(nil)
309358
}
310359

360+
// Execute GraphQL query
361+
var query reviewThreadsQuery
362+
if err := gqlClient.Query(ctx, &query, vars); err != nil {
363+
return ghErrors.NewGitHubGraphQLErrorResponse(ctx,
364+
"failed to get pull request review threads",
365+
err,
366+
), nil
367+
}
368+
369+
// Lockdown mode filtering
311370
if ff.LockdownMode {
312371
if cache == nil {
313372
return nil, fmt.Errorf("lockdown cache is not configured")
314373
}
315-
filteredComments := make([]*github.PullRequestComment, 0, len(comments))
316-
for _, comment := range comments {
317-
user := comment.GetUser()
318-
if user == nil {
319-
continue
320-
}
321-
isSafeContent, err := cache.IsSafeContent(ctx, user.GetLogin(), owner, repo)
322-
if err != nil {
323-
return utils.NewToolResultError(fmt.Sprintf("failed to check lockdown mode: %v", err)), nil
324-
}
325-
if isSafeContent {
326-
filteredComments = append(filteredComments, comment)
374+
375+
// Iterate through threads and filter comments
376+
for i := range query.Repository.PullRequest.ReviewThreads.Nodes {
377+
thread := &query.Repository.PullRequest.ReviewThreads.Nodes[i]
378+
filteredComments := make([]reviewCommentNode, 0, len(thread.Comments.Nodes))
379+
380+
for _, comment := range thread.Comments.Nodes {
381+
login := string(comment.Author.Login)
382+
if login != "" {
383+
isSafeContent, err := cache.IsSafeContent(ctx, login, owner, repo)
384+
if err != nil {
385+
return nil, fmt.Errorf("failed to check lockdown mode: %w", err)
386+
}
387+
if isSafeContent {
388+
filteredComments = append(filteredComments, comment)
389+
}
390+
}
327391
}
392+
393+
thread.Comments.Nodes = filteredComments
394+
thread.Comments.TotalCount = githubv4.Int(int32(len(filteredComments))) //nolint:gosec // comment count is bounded by API limits
328395
}
329-
comments = filteredComments
330396
}
331397

332-
r, err := json.Marshal(comments)
398+
// Build response with review threads and pagination info
399+
response := map[string]any{
400+
"reviewThreads": query.Repository.PullRequest.ReviewThreads.Nodes,
401+
"pageInfo": map[string]any{
402+
"hasNextPage": query.Repository.PullRequest.ReviewThreads.PageInfo.HasNextPage,
403+
"hasPreviousPage": query.Repository.PullRequest.ReviewThreads.PageInfo.HasPreviousPage,
404+
"startCursor": string(query.Repository.PullRequest.ReviewThreads.PageInfo.StartCursor),
405+
"endCursor": string(query.Repository.PullRequest.ReviewThreads.PageInfo.EndCursor),
406+
},
407+
"totalCount": int(query.Repository.PullRequest.ReviewThreads.TotalCount),
408+
}
409+
410+
r, err := json.Marshal(response)
333411
if err != nil {
334412
return nil, fmt.Errorf("failed to marshal response: %w", err)
335413
}
@@ -697,7 +775,7 @@ func UpdatePullRequest(getClient GetClientFn, getGQLClient GetGQLClientFn, t tra
697775
} `graphql:"repository(owner: $owner, name: $repo)"`
698776
}
699777

700-
err = gqlClient.Query(ctx, &prQuery, map[string]interface{}{
778+
err = gqlClient.Query(ctx, &prQuery, map[string]any{
701779
"owner": githubv4.String(owner),
702780
"repo": githubv4.String(repo),
703781
"prNum": githubv4.Int(pullNumber), // #nosec G115 - pull request numbers are always small positive integers

0 commit comments

Comments
 (0)