Skip to content

Commit cf0c753

Browse files
committed
feat: implement UpdateIssueComment test and snapshot
- Add unit test for UpdateIssueComment tool in pkg/github/issues_test.go to verify functionality and input validation. - Introduce a snapshot file for UpdateIssueComment tool to document expected behavior and input schema. - Include new PATCH endpoint constant for updating issue comments in pkg/github/helper_test.go.
1 parent d0f8aaf commit cf0c753

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"annotations": {
3+
"title": "Update issue comment"
4+
},
5+
"description": "Update an existing comment on an issue or pull request in a GitHub repository. Use the comment ID from issue_read with method get_comments, or from the comment object when adding a comment.",
6+
"inputSchema": {
7+
"properties": {
8+
"body": {
9+
"description": "New comment content",
10+
"type": "string"
11+
},
12+
"comment_id": {
13+
"description": "ID of the comment to update (from issue_read get_comments or add_issue_comment response)",
14+
"type": "number"
15+
},
16+
"owner": {
17+
"description": "Repository owner",
18+
"type": "string"
19+
},
20+
"repo": {
21+
"description": "Repository name",
22+
"type": "string"
23+
}
24+
},
25+
"required": [
26+
"owner",
27+
"repo",
28+
"comment_id",
29+
"body"
30+
],
31+
"type": "object"
32+
},
33+
"name": "update_issue_comment"
34+
}

pkg/github/helper_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const (
5858
GetReposIssuesCommentsByOwnerByRepoByIssueNumber = "GET /repos/{owner}/{repo}/issues/{issue_number}/comments"
5959
PostReposIssuesByOwnerByRepo = "POST /repos/{owner}/{repo}/issues"
6060
PostReposIssuesCommentsByOwnerByRepoByIssueNumber = "POST /repos/{owner}/{repo}/issues/{issue_number}/comments"
61+
PatchReposIssuesCommentsByOwnerByRepoByCommentID = "PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}"
6162
PatchReposIssuesByOwnerByRepoByIssueNumber = "PATCH /repos/{owner}/{repo}/issues/{issue_number}"
6263
GetReposIssuesSubIssuesByOwnerByRepoByIssueNumber = "GET /repos/{owner}/{repo}/issues/{issue_number}/sub_issues"
6364
PostReposIssuesSubIssuesByOwnerByRepoByIssueNumber = "POST /repos/{owner}/{repo}/issues/{issue_number}/sub_issues"

pkg/github/issues_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,109 @@ func Test_AddIssueComment(t *testing.T) {
469469
}
470470
}
471471

472+
func Test_UpdateIssueComment(t *testing.T) {
473+
// Verify tool definition once
474+
serverTool := UpdateIssueComment(translations.NullTranslationHelper)
475+
tool := serverTool.Tool
476+
require.NoError(t, toolsnaps.Test(tool.Name, tool))
477+
478+
assert.Equal(t, "update_issue_comment", tool.Name)
479+
assert.NotEmpty(t, tool.Description)
480+
481+
assert.Contains(t, tool.InputSchema.(*jsonschema.Schema).Properties, "owner")
482+
assert.Contains(t, tool.InputSchema.(*jsonschema.Schema).Properties, "repo")
483+
assert.Contains(t, tool.InputSchema.(*jsonschema.Schema).Properties, "comment_id")
484+
assert.Contains(t, tool.InputSchema.(*jsonschema.Schema).Properties, "body")
485+
assert.ElementsMatch(t, tool.InputSchema.(*jsonschema.Schema).Required, []string{"owner", "repo", "comment_id", "body"})
486+
487+
mockComment := &github.IssueComment{
488+
ID: github.Ptr(int64(456)),
489+
Body: github.Ptr("Updated comment body"),
490+
User: &github.User{
491+
Login: github.Ptr("testuser"),
492+
},
493+
HTMLURL: github.Ptr("https://github.com/owner/repo/issues/42#issuecomment-456"),
494+
}
495+
496+
tests := []struct {
497+
name string
498+
mockedClient *http.Client
499+
requestArgs map[string]any
500+
expectError bool
501+
expectedComment *github.IssueComment
502+
expectedErrMsg string
503+
}{
504+
{
505+
name: "successful comment update",
506+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
507+
PatchReposIssuesCommentsByOwnerByRepoByCommentID: mockResponse(t, http.StatusOK, mockComment),
508+
}),
509+
requestArgs: map[string]any{
510+
"owner": "owner",
511+
"repo": "repo",
512+
"comment_id": float64(456),
513+
"body": "Updated comment body",
514+
},
515+
expectError: false,
516+
expectedComment: mockComment,
517+
},
518+
{
519+
name: "comment update fails - missing body",
520+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
521+
PatchReposIssuesCommentsByOwnerByRepoByCommentID: mockResponse(t, http.StatusOK, mockComment),
522+
}),
523+
requestArgs: map[string]any{
524+
"owner": "owner",
525+
"repo": "repo",
526+
"comment_id": float64(456),
527+
"body": "",
528+
},
529+
expectError: false,
530+
expectedErrMsg: "missing required parameter: body",
531+
},
532+
}
533+
534+
for _, tc := range tests {
535+
t.Run(tc.name, func(t *testing.T) {
536+
client := github.NewClient(tc.mockedClient)
537+
deps := BaseDeps{
538+
Client: client,
539+
}
540+
handler := serverTool.Handler(deps)
541+
542+
request := createMCPRequest(tc.requestArgs)
543+
544+
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
545+
546+
if tc.expectError {
547+
require.Error(t, err)
548+
if tc.expectedErrMsg != "" {
549+
assert.Contains(t, err.Error(), tc.expectedErrMsg)
550+
}
551+
return
552+
}
553+
554+
if tc.expectedErrMsg != "" {
555+
require.NotNil(t, result)
556+
textContent := getTextResult(t, result)
557+
assert.Contains(t, textContent.Text, tc.expectedErrMsg)
558+
return
559+
}
560+
561+
require.NoError(t, err)
562+
563+
textContent := getTextResult(t, result)
564+
565+
var returnedComment github.IssueComment
566+
err = json.Unmarshal([]byte(textContent.Text), &returnedComment)
567+
require.NoError(t, err)
568+
assert.Equal(t, *tc.expectedComment.ID, *returnedComment.ID)
569+
assert.Equal(t, *tc.expectedComment.Body, *returnedComment.Body)
570+
assert.Equal(t, *tc.expectedComment.User.Login, *returnedComment.User.Login)
571+
})
572+
}
573+
}
574+
472575
func Test_SearchIssues(t *testing.T) {
473576
// Verify tool definition once
474577
serverTool := SearchIssues(translations.NullTranslationHelper)

0 commit comments

Comments
 (0)