Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,14 @@ The following sets of tools are available:
- `startSide`: For multi-line comments, the starting side of the diff that the comment applies to. LEFT indicates the previous state, RIGHT indicates the new state (string, optional)
- `subjectType`: The level at which the comment is targeted (string, required)

- **add_reply_to_pull_request_comment** - Add reply to a pull request comment
- **Required OAuth Scopes**: `repo`
- `body`: The text of the reply (string, required)
- `commentId`: The ID of the comment to reply to (string, required)
- `owner`: Repository owner (string, required)
- `pullNumber`: Pull request number (number, required)
- `repo`: Repository name (string, required)

- **create_pull_request** - Open new pull request
- **Required OAuth Scopes**: `repo`
- `base`: Branch to merge into (string, required)
Expand Down
39 changes: 39 additions & 0 deletions pkg/github/__toolsnaps__/add_reply_to_pull_request_comment.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"annotations": {
"title": "Add reply to pull request comment"
},
"description": "Add a reply to an existing pull request comment. This creates a new comment that is linked as a reply to the specified comment.",
"inputSchema": {
"properties": {
"body": {
"description": "The text of the reply",
"type": "string"
},
"commentId": {
"description": "The ID of the comment to reply to",
"type": "string"
},
"owner": {
"description": "Repository owner",
"type": "string"
},
"pullNumber": {
"description": "Pull request number",
"type": "string"
},
"repo": {
"description": "Repository name",
"type": "string"
}
},
"required": [
"owner",
"repo",
"pullNumber",
"commentId",
"body"
],
"type": "object"
},
"name": "add_reply_to_pull_request_comment"
}
4 changes: 2 additions & 2 deletions pkg/github/__toolsnaps__/assign_copilot_to_issue.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"type": "string"
},
"custom_instructions": {
"type": "string",
"description": "Optional custom instructions to guide the agent beyond the issue body. Use this to provide additional context, constraints, or guidance that is not captured in the issue description"
"description": "Optional custom instructions to guide the agent beyond the issue body. Use this to provide additional context, constraints, or guidance that is not captured in the issue description",
"type": "string"
},
"issue_number": {
"description": "Issue number",
Expand Down
1 change: 1 addition & 0 deletions pkg/github/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const (
PutReposPullsMergeByOwnerByRepoByPullNumber = "PUT /repos/{owner}/{repo}/pulls/{pull_number}/merge"
PutReposPullsUpdateBranchByOwnerByRepoByPullNumber = "PUT /repos/{owner}/{repo}/pulls/{pull_number}/update-branch"
PostReposPullsRequestedReviewersByOwnerByRepoByPullNumber = "POST /repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers"
PostReposPullsCommentsByOwnerByRepoByPullNumber = "POST /repos/{owner}/{repo}/pulls/{pull_number}/comments"

// Notifications endpoints
GetNotifications = "GET /notifications"
Expand Down
91 changes: 91 additions & 0 deletions pkg/github/pullrequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,97 @@ func UpdatePullRequest(t translations.TranslationHelperFunc) inventory.ServerToo
})
}

// AddReplyToPullRequestComment creates a tool to add a reply to an existing pull request comment.
func AddReplyToPullRequestComment(t translations.TranslationHelperFunc) inventory.ServerTool {
schema := &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"owner": {
Type: "string",
Description: "Repository owner",
},
"repo": {
Type: "string",
Description: "Repository name",
},
"pullNumber": {
Type: "string",
Description: "Pull request number",
},
"commentId": {
Type: "string",
Description: "The ID of the comment to reply to",
},
"body": {
Type: "string",
Description: "The text of the reply",
},
},
Required: []string{"owner", "repo", "pullNumber", "commentId", "body"},
}

return NewTool(
ToolsetMetadataPullRequests,
mcp.Tool{
Name: "add_reply_to_pull_request_comment",
Description: t("TOOL_ADD_REPLY_TO_PULL_REQUEST_COMMENT_DESCRIPTION", "Add a reply to an existing pull request comment. This creates a new comment that is linked as a reply to the specified comment."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_ADD_REPLY_TO_PULL_REQUEST_COMMENT_USER_TITLE", "Add reply to pull request comment"),
ReadOnlyHint: false,
},
InputSchema: schema,
},
[]scopes.Scope{scopes.Repo},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
owner, err := RequiredParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
pullNumber, err := RequiredInt(args, "pullNumber")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
commentID, err := RequiredInt(args, "commentId")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
body, err := RequiredParam[string](args, "body")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}

client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}

comment, resp, err := client.PullRequests.CreateCommentInReplyTo(ctx, owner, repo, pullNumber, body, int64(commentID))
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to add reply to pull request comment", resp, err), nil, nil
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusCreated {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil
}
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to add reply to pull request comment", resp, bodyBytes), nil, nil
}

r, err := json.Marshal(comment)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal response", err), nil, nil
}

return utils.NewToolResultText(string(r)), nil, nil
})
}

// ListPullRequests creates a tool to list and filter repository pull requests.
func ListPullRequests(t translations.TranslationHelperFunc) inventory.ServerTool {
schema := &jsonschema.Schema{
Expand Down
164 changes: 164 additions & 0 deletions pkg/github/pullrequests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3227,3 +3227,167 @@ func getLatestPendingReviewQuery(p getLatestPendingReviewQueryParams) githubv4mo
),
)
}

func TestAddReplyToPullRequestComment(t *testing.T) {
t.Parallel()

// Verify tool definition once
serverTool := AddReplyToPullRequestComment(translations.NullTranslationHelper)
tool := serverTool.Tool
require.NoError(t, toolsnaps.Test(tool.Name, tool))

assert.Equal(t, "add_reply_to_pull_request_comment", tool.Name)
assert.NotEmpty(t, tool.Description)
schema := tool.InputSchema.(*jsonschema.Schema)
assert.Contains(t, schema.Properties, "owner")
assert.Contains(t, schema.Properties, "repo")
assert.Contains(t, schema.Properties, "pullNumber")
assert.Contains(t, schema.Properties, "commentId")
assert.Contains(t, schema.Properties, "body")
assert.ElementsMatch(t, schema.Required, []string{"owner", "repo", "pullNumber", "commentId", "body"})

// Setup mock reply comment for success case
mockReplyComment := &github.PullRequestComment{
ID: github.Ptr(int64(456)),
Body: github.Ptr("This is a reply to the comment"),
InReplyTo: github.Ptr(int64(123)),
HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42#discussion_r456"),
User: &github.User{
Login: github.Ptr("responder"),
},
CreatedAt: &github.Timestamp{Time: time.Now()},
UpdatedAt: &github.Timestamp{Time: time.Now()},
}

tests := []struct {
name string
mockedClient *http.Client
requestArgs map[string]interface{}
expectToolError bool
expectedToolErrMsg string
}{
{
name: "successful reply to pull request comment",
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"pullNumber": float64(42),
"commentId": float64(123),
"body": "This is a reply to the comment",
},
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
PostReposPullsCommentsByOwnerByRepoByPullNumber: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusCreated)
responseData, _ := json.Marshal(mockReplyComment)
_, _ = w.Write(responseData)
},
}),
},
{
name: "missing required parameter owner",
requestArgs: map[string]interface{}{
"repo": "repo",
"pullNumber": float64(42),
"commentId": float64(123),
"body": "This is a reply to the comment",
},
expectToolError: true,
expectedToolErrMsg: "missing required parameter: owner",
},
{
name: "missing required parameter repo",
requestArgs: map[string]interface{}{
"owner": "owner",
"pullNumber": float64(42),
"commentId": float64(123),
"body": "This is a reply to the comment",
},
expectToolError: true,
expectedToolErrMsg: "missing required parameter: repo",
},
{
name: "missing required parameter pullNumber",
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"commentId": float64(123),
"body": "This is a reply to the comment",
},
expectToolError: true,
expectedToolErrMsg: "missing required parameter: pullNumber",
},
{
name: "missing required parameter commentId",
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"pullNumber": float64(42),
"body": "This is a reply to the comment",
},
expectToolError: true,
expectedToolErrMsg: "missing required parameter: commentId",
},
{
name: "missing required parameter body",
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"pullNumber": float64(42),
"commentId": float64(123),
},
expectToolError: true,
expectedToolErrMsg: "missing required parameter: body",
},
{
name: "API error when adding reply",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
PostReposPullsCommentsByOwnerByRepoByPullNumber: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
},
}),
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"pullNumber": float64(42),
"commentId": float64(123),
"body": "This is a reply to the comment",
},
expectToolError: true,
expectedToolErrMsg: "failed to add reply to pull request comment",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

// Setup client with mock
client := github.NewClient(tc.mockedClient)
serverTool := AddReplyToPullRequestComment(translations.NullTranslationHelper)
deps := BaseDeps{
Client: client,
}
handler := serverTool.Handler(deps)

// Create call request
request := createMCPRequest(tc.requestArgs)

// Call handler
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
require.NoError(t, err)

if tc.expectToolError {
require.True(t, result.IsError)
errorContent := getErrorResult(t, result)
assert.Contains(t, errorContent.Text, tc.expectedToolErrMsg)
return
}

// Parse the result and verify it's not an error
require.False(t, result.IsError)
textContent := getTextResult(t, result)
assert.Contains(t, textContent.Text, "This is a reply to the comment")
})
}
}
1 change: 1 addition & 0 deletions pkg/github/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func AllTools(t translations.TranslationHelperFunc) []inventory.ServerTool {
RequestCopilotReview(t),
PullRequestReviewWrite(t),
AddCommentToPendingReview(t),
AddReplyToPullRequestComment(t),

// Code security tools
GetCodeScanningAlert(t),
Expand Down