Skip to content

Commit 938e354

Browse files
committed
testing review threads
1 parent ec6afa7 commit 938e354

File tree

5 files changed

+141
-112
lines changed

5 files changed

+141
-112
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ Possible options:
930930
2. get_diff - Get the diff of a pull request.
931931
3. get_status - Get status of a head commit in a pull request. This reflects status of builds and checks.
932932
4. get_files - Get the list of files changed in a pull request. Use with pagination parameters to control the number of results returned.
933-
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.
933+
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.
934934
6. get_reviews - Get the reviews on a pull request. When asked for review comments, use get_review_comments method.
935935
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.
936936
(string, required)

pkg/github/__toolsnaps__/pull_request_read.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"inputSchema": {
88
"properties": {
99
"method": {
10-
"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",
10+
"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",
1111
"enum": [
1212
"get",
1313
"get_diff",

pkg/github/pullrequests.go

Lines changed: 96 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
// GetPullRequest creates a tool to get details of a specific pull request.
22-
func PullRequestRead(getClient GetClientFn, t translations.TranslationHelperFunc, flags FeatureFlags) (mcp.Tool, server.ToolHandlerFunc) {
22+
func PullRequestRead(getClient GetClientFn, getGQLClient GetGQLClientFn, t translations.TranslationHelperFunc, flags FeatureFlags) (mcp.Tool, server.ToolHandlerFunc) {
2323
return mcp.NewTool("pull_request_read",
2424
mcp.WithDescription(t("TOOL_PULL_REQUEST_READ_DESCRIPTION", "Get information on a specific pull request in GitHub repository.")),
2525
mcp.WithToolAnnotation(mcp.ToolAnnotation{
@@ -34,7 +34,7 @@ Possible options:
3434
2. get_diff - Get the diff of a pull request.
3535
3. get_status - Get status of a head commit in a pull request. This reflects status of builds and checks.
3636
4. get_files - Get the list of files changed in a pull request. Use with pagination parameters to control the number of results returned.
37-
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.
37+
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.
3838
6. get_reviews - Get the reviews on a pull request. When asked for review comments, use get_review_comments method.
3939
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.
4040
`),
@@ -94,7 +94,11 @@ Possible options:
9494
case "get_files":
9595
return GetPullRequestFiles(ctx, client, owner, repo, pullNumber, pagination)
9696
case "get_review_comments":
97-
return GetPullRequestReviewComments(ctx, client, owner, repo, pullNumber, pagination)
97+
gqlClient, err := getGQLClient(ctx)
98+
if err != nil {
99+
return mcp.NewToolResultError(fmt.Sprintf("failed to get GitHub GQL client: %v", err)), nil
100+
}
101+
return GetPullRequestReviewComments(ctx, gqlClient, owner, repo, pullNumber, pagination)
98102
case "get_reviews":
99103
return GetPullRequestReviews(ctx, client, owner, repo, pullNumber)
100104
case "get_comments":
@@ -249,33 +253,104 @@ func GetPullRequestFiles(ctx context.Context, client *github.Client, owner, repo
249253
return mcp.NewToolResultText(string(r)), nil
250254
}
251255

252-
func GetPullRequestReviewComments(ctx context.Context, client *github.Client, owner, repo string, pullNumber int, pagination PaginationParams) (*mcp.CallToolResult, error) {
253-
opts := &github.PullRequestListCommentsOptions{
254-
ListOptions: github.ListOptions{
255-
PerPage: pagination.PerPage,
256-
Page: pagination.Page,
257-
},
256+
// GraphQL types for review threads query
257+
type reviewThreadsQuery struct {
258+
Repository struct {
259+
PullRequest struct {
260+
ReviewThreads struct {
261+
Nodes []reviewThreadNode
262+
PageInfo pageInfoFragment
263+
TotalCount githubv4.Int
264+
} `graphql:"reviewThreads(first: $first, after: $after)"`
265+
} `graphql:"pullRequest(number: $prNum)"`
266+
} `graphql:"repository(owner: $owner, name: $repo)"`
267+
}
268+
269+
type reviewThreadNode struct {
270+
ID githubv4.ID
271+
IsResolved githubv4.Boolean
272+
IsOutdated githubv4.Boolean
273+
IsCollapsed githubv4.Boolean
274+
Comments struct {
275+
Nodes []reviewCommentNode
276+
TotalCount githubv4.Int
277+
} `graphql:"comments(first: $commentsPerThread)"`
278+
}
279+
280+
type reviewCommentNode struct {
281+
ID githubv4.ID
282+
Body githubv4.String
283+
Path githubv4.String
284+
Line *githubv4.Int
285+
Author struct {
286+
Login githubv4.String
258287
}
288+
CreatedAt githubv4.DateTime
289+
UpdatedAt githubv4.DateTime
290+
URL githubv4.URI
291+
}
292+
293+
type pageInfoFragment struct {
294+
HasNextPage githubv4.Boolean
295+
HasPreviousPage githubv4.Boolean
296+
StartCursor githubv4.String
297+
EndCursor githubv4.String
298+
}
259299

260-
comments, resp, err := client.PullRequests.ListComments(ctx, owner, repo, pullNumber, opts)
300+
func GetPullRequestReviewComments(ctx context.Context, gqlClient *githubv4.Client, owner, repo string, pullNumber int, pagination PaginationParams) (*mcp.CallToolResult, error) {
301+
// Convert pagination parameters to GraphQL format
302+
gqlParams, err := pagination.ToGraphQLParams()
261303
if err != nil {
262-
return ghErrors.NewGitHubAPIErrorResponse(ctx,
263-
"failed to get pull request review comments",
264-
resp,
304+
return mcp.NewToolResultError(fmt.Sprintf("invalid pagination parameters: %v", err)), nil
305+
}
306+
307+
// Default to 100 threads if not specified, max is 100 for GraphQL
308+
perPage := int32(100)
309+
if gqlParams.First != nil && *gqlParams.First > 0 {
310+
perPage = *gqlParams.First
311+
}
312+
313+
// Build variables for GraphQL query
314+
vars := map[string]interface{}{
315+
"owner": githubv4.String(owner),
316+
"repo": githubv4.String(repo),
317+
"prNum": githubv4.Int(pullNumber),
318+
"first": githubv4.Int(perPage),
319+
"commentsPerThread": githubv4.Int(50), // Max 50 comments per thread
320+
}
321+
322+
// Add cursor if provided
323+
if gqlParams.After != nil && *gqlParams.After != "" {
324+
vars["after"] = githubv4.String(*gqlParams.After)
325+
}
326+
// Note: when after is nil, we still need to include it in the vars map
327+
// for GraphQL query construction, even though it's nullable
328+
if _, exists := vars["after"]; !exists {
329+
vars["after"] = (*githubv4.String)(nil)
330+
}
331+
332+
// Execute GraphQL query
333+
var query reviewThreadsQuery
334+
if err := gqlClient.Query(ctx, &query, vars); err != nil {
335+
return ghErrors.NewGitHubGraphQLErrorResponse(ctx,
336+
"failed to get pull request review threads",
265337
err,
266338
), nil
267339
}
268-
defer func() { _ = resp.Body.Close() }()
269340

270-
if resp.StatusCode != http.StatusOK {
271-
body, err := io.ReadAll(resp.Body)
272-
if err != nil {
273-
return nil, fmt.Errorf("failed to read response body: %w", err)
274-
}
275-
return mcp.NewToolResultError(fmt.Sprintf("failed to get pull request review comments: %s", string(body))), nil
341+
// Build response with review threads and pagination info
342+
response := map[string]interface{}{
343+
"reviewThreads": query.Repository.PullRequest.ReviewThreads.Nodes,
344+
"pageInfo": map[string]interface{}{
345+
"hasNextPage": query.Repository.PullRequest.ReviewThreads.PageInfo.HasNextPage,
346+
"hasPreviousPage": query.Repository.PullRequest.ReviewThreads.PageInfo.HasPreviousPage,
347+
"startCursor": string(query.Repository.PullRequest.ReviewThreads.PageInfo.StartCursor),
348+
"endCursor": string(query.Repository.PullRequest.ReviewThreads.PageInfo.EndCursor),
349+
},
350+
"totalCount": int(query.Repository.PullRequest.ReviewThreads.TotalCount),
276351
}
277352

278-
r, err := json.Marshal(comments)
353+
r, err := json.Marshal(response)
279354
if err != nil {
280355
return nil, fmt.Errorf("failed to marshal response: %w", err)
281356
}

0 commit comments

Comments
 (0)