diff --git a/pkg/github/pullrequests.go b/pkg/github/pullrequests.go index e64ae03e4..d8f3b7136 100644 --- a/pkg/github/pullrequests.go +++ b/pkg/github/pullrequests.go @@ -1518,6 +1518,14 @@ func AddCommentToPendingReview(getGQLClient GetGQLClientFn, t translations.Trans return mcp.NewToolResultError(err.Error()), nil } + if addPullRequestReviewThreadMutation.AddPullRequestReviewThread.Thread.ID == nil { + return mcp.NewToolResultError(`Failed to add comment to pending review. Possible reasons: + - The line number doesn't exist in the pull request diff + - The file path is incorrect + - The side (LEFT/RIGHT) is invalid for the specified line +`), nil + } + // Return nothing interesting, just indicate success for the time being. // In future, we may want to return the review ID, but for the moment, we're not leaking // API implementation details to the LLM. diff --git a/pkg/github/pullrequests_test.go b/pkg/github/pullrequests_test.go index 347bce672..b29c743a3 100644 --- a/pkg/github/pullrequests_test.go +++ b/pkg/github/pullrequests_test.go @@ -2555,10 +2555,75 @@ func TestAddPullRequestReviewCommentToPendingReview(t *testing.T) { PullRequestReviewID: githubv4.NewID("PR_kwDODKw3uc6WYN1T"), }, nil, - githubv4mock.DataResponse(map[string]any{}), + githubv4mock.DataResponse(map[string]any{ + "addPullRequestReviewThread": map[string]any{ + "thread": map[string]any{ + "id": "MDEyOlB1bGxSZXF1ZXN0UmV2aWV3VGhyZWFkMTIzNDU2", + }, + }, + }), ), ), }, + { + name: "thread ID is nil - invalid line number", + requestArgs: map[string]any{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "path": "file.go", + "body": "Comment on non-existent line", + "subjectType": "LINE", + "line": float64(999), + "side": "RIGHT", + }, + mockedClient: githubv4mock.NewMockedHTTPClient( + viewerQuery("williammartin"), + getLatestPendingReviewQuery(getLatestPendingReviewQueryParams{ + author: "williammartin", + owner: "owner", + repo: "repo", + prNum: 42, + + reviews: []getLatestPendingReviewQueryReview{ + { + id: "PR_kwDODKw3uc6WYN1T", + state: "PENDING", + url: "https://github.com/owner/repo/pull/42", + }, + }, + }), + githubv4mock.NewMutationMatcher( + struct { + AddPullRequestReviewThread struct { + Thread struct { + ID githubv4.ID + } + } `graphql:"addPullRequestReviewThread(input: $input)"` + }{}, + githubv4.AddPullRequestReviewThreadInput{ + Path: githubv4.String("file.go"), + Body: githubv4.String("Comment on non-existent line"), + SubjectType: githubv4mock.Ptr(githubv4.PullRequestReviewThreadSubjectTypeLine), + Line: githubv4.NewInt(999), + Side: githubv4mock.Ptr(githubv4.DiffSideRight), + StartLine: nil, + StartSide: nil, + PullRequestReviewID: githubv4.NewID("PR_kwDODKw3uc6WYN1T"), + }, + nil, + githubv4mock.DataResponse(map[string]any{ + "addPullRequestReviewThread": map[string]any{ + "thread": map[string]any{ + "id": nil, + }, + }, + }), + ), + ), + expectToolError: true, + expectedToolErrMsg: "Failed to add comment to pending review", + }, } for _, tc := range tests {