Skip to content

Commit 45cb759

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 1bb9ff8 commit 45cb759

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
@@ -57,6 +57,7 @@ const (
5757
GetReposIssuesCommentsByOwnerByRepoByIssueNumber = "GET /repos/{owner}/{repo}/issues/{issue_number}/comments"
5858
PostReposIssuesByOwnerByRepo = "POST /repos/{owner}/{repo}/issues"
5959
PostReposIssuesCommentsByOwnerByRepoByIssueNumber = "POST /repos/{owner}/{repo}/issues/{issue_number}/comments"
60+
PatchReposIssuesCommentsByOwnerByRepoByCommentID = "PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}"
6061
PatchReposIssuesByOwnerByRepoByIssueNumber = "PATCH /repos/{owner}/{repo}/issues/{issue_number}"
6162
GetReposIssuesSubIssuesByOwnerByRepoByIssueNumber = "GET /repos/{owner}/{repo}/issues/{issue_number}/sub_issues"
6263
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
@@ -470,6 +470,109 @@ func Test_AddIssueComment(t *testing.T) {
470470
}
471471
}
472472

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

0 commit comments

Comments
 (0)