Skip to content

Commit 1df6161

Browse files
vandermeij-martijnTiberriver256
authored andcommitted
feat: add_pull_request_comment
1 parent 2b7fb3a commit 1df6161

File tree

9 files changed

+690
-0
lines changed

9 files changed

+690
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ The Azure DevOps MCP server provides a variety of tools for interacting with Azu
188188
- `create_pull_request`: Create a new pull request between branches in a repository
189189
- `list_pull_requests`: List and filter pull requests in a project or repository
190190
- `get_pull_request_comments`: Get comments and comment threads from a specific pull request
191+
- `add_pull_request_comment`: Add a comment to a pull request (reply to existing comments or create new threads)
191192

192193
For comprehensive documentation on all tools, see the [Tools Documentation](docs/tools/).
193194

docs/tools/pull-requests.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,206 @@ The `get_pull_request_comments` tool:
507507

508508
This implementation provides a robust way to retrieve and analyze pull request comments from Azure DevOps repositories.
509509

510+
## add_pull_request_comment
511+
512+
Adds a comment to a pull request, either as a reply to an existing comment or as a new thread.
513+
514+
### Description
515+
516+
The `add_pull_request_comment` tool allows you to create new comments in pull requests in Azure DevOps. You can either:
517+
1. Add a reply to an existing comment thread
518+
2. Create a new thread with a comment in the general discussion
519+
3. Create a new thread with a comment on a specific file at a specific line
520+
521+
This tool is useful for providing feedback on pull requests, engaging in code review discussions, and automating comment workflows.
522+
523+
### Parameters
524+
525+
```json
526+
{
527+
"projectId": "MyProject", // Required: The ID or name of the project
528+
"repositoryId": "MyRepo", // Required: The ID or name of the repository
529+
"pullRequestId": 42, // Required: The ID of the pull request
530+
"content": "This looks good, let's merge!", // Required: The content of the comment
531+
"threadId": 123, // Optional: The ID of the thread to add the comment to (for replying)
532+
"parentCommentId": 456, // Optional: The ID of the parent comment (for threaded replies)
533+
"filePath": "/src/app.ts", // Optional: The path of the file to comment on (for file comments)
534+
"lineNumber": 42, // Optional: The line number to comment on (for file comments)
535+
"status": "active" // Optional: The status to set for a new thread (active, fixed, wontFix, closed, pending)
536+
}
537+
```
538+
539+
| Parameter | Type | Required | Description |
540+
| --------- | ---- | -------- | ----------- |
541+
| `projectId` | string | Yes | The ID or name of the project containing the repository |
542+
| `repositoryId` | string | Yes | The ID or name of the repository containing the pull request |
543+
| `pullRequestId` | number | Yes | The ID of the pull request to comment on |
544+
| `content` | string | Yes | The text content of the comment |
545+
| `threadId` | number | No | The ID of an existing thread to add the comment to. Required when replying to an existing thread |
546+
| `parentCommentId` | number | No | ID of the parent comment when replying to a specific comment in a thread |
547+
| `filePath` | string | No | The path of the file to comment on (for creating a new thread on a file) |
548+
| `lineNumber` | number | No | The line number to comment on (for creating a new thread on a file) |
549+
| `status` | string | No | The status to set for a new thread: "active", "fixed", "wontFix", "closed", or "pending" |
550+
551+
### Response
552+
553+
When adding a comment to an existing thread, the tool returns an object containing:
554+
555+
- `comment`: The created comment object with details like ID, content, and author
556+
557+
When creating a new thread with a comment, the tool returns an object containing:
558+
559+
- `comment`: The created comment object
560+
- `thread`: The created thread object with details like ID, status, and context
561+
562+
Example response for replying to an existing thread:
563+
564+
```json
565+
{
566+
"comment": {
567+
"id": 101,
568+
"content": "I agree with the suggestion",
569+
"commentType": 1,
570+
"parentCommentId": 100,
571+
"author": {
572+
"displayName": "John Doe",
573+
"id": "user-guid",
574+
"uniqueName": "[email protected]"
575+
},
576+
"publishedDate": "2023-05-15T10:23:45Z"
577+
}
578+
}
579+
```
580+
581+
Example response for creating a new thread on a file:
582+
583+
```json
584+
{
585+
"comment": {
586+
"id": 200,
587+
"content": "This variable name should be more descriptive",
588+
"commentType": 1,
589+
"author": {
590+
"displayName": "John Doe",
591+
"id": "user-guid",
592+
"uniqueName": "[email protected]"
593+
},
594+
"publishedDate": "2023-05-15T10:30:12Z"
595+
},
596+
"thread": {
597+
"id": 50,
598+
"status": 1,
599+
"threadContext": {
600+
"filePath": "/src/app.ts",
601+
"rightFileStart": {
602+
"line": 42,
603+
"offset": 1
604+
},
605+
"rightFileEnd": {
606+
"line": 42,
607+
"offset": 1
608+
}
609+
},
610+
"comments": [
611+
{
612+
"id": 200,
613+
"content": "This variable name should be more descriptive",
614+
"commentType": 1,
615+
"author": {
616+
"displayName": "John Doe",
617+
"id": "user-guid",
618+
"uniqueName": "[email protected]"
619+
},
620+
"publishedDate": "2023-05-15T10:30:12Z"
621+
}
622+
]
623+
}
624+
}
625+
```
626+
627+
### Error Handling
628+
629+
The tool may throw the following errors:
630+
631+
- ValidationError: If required parameters are missing or invalid
632+
- AuthenticationError: If authentication fails
633+
- PermissionError: If the user doesn't have permission to comment on the pull request
634+
- ResourceNotFoundError: If the project, repository, pull request, or thread doesn't exist
635+
- GeneralError: For other unexpected errors
636+
637+
Error messages will include details about what went wrong and suggestions for resolution.
638+
639+
### Example Usage
640+
641+
```typescript
642+
// Reply to an existing thread in a pull request
643+
const reply = await mcpClient.callTool('add_pull_request_comment', {
644+
projectId: 'MyProject',
645+
repositoryId: 'MyRepo',
646+
pullRequestId: 42,
647+
threadId: 123,
648+
content: 'I agree with the suggestion, let me implement this change.'
649+
});
650+
console.log(`Created reply with ID ${reply.comment.id}`);
651+
652+
// Reply to a specific comment in a thread
653+
const threadedReply = await mcpClient.callTool('add_pull_request_comment', {
654+
projectId: 'MyProject',
655+
repositoryId: 'MyRepo',
656+
pullRequestId: 42,
657+
threadId: 123,
658+
parentCommentId: 456,
659+
content: 'Specifically addressing your point about error handling.'
660+
});
661+
console.log(`Created threaded reply with ID ${threadedReply.comment.id}`);
662+
663+
// Create a new general discussion thread in a pull request
664+
const newThread = await mcpClient.callTool('add_pull_request_comment', {
665+
projectId: 'MyProject',
666+
repositoryId: 'MyRepo',
667+
pullRequestId: 42,
668+
content: 'Overall this looks good, but let\'s discuss the error handling approach.'
669+
});
670+
console.log(`Created new thread with ID ${newThread.thread.id}`);
671+
672+
// Create a comment on a specific file and line
673+
const fileComment = await mcpClient.callTool('add_pull_request_comment', {
674+
projectId: 'MyProject',
675+
repositoryId: 'MyRepo',
676+
pullRequestId: 42,
677+
content: 'This variable name should be more descriptive.',
678+
filePath: '/src/app.ts',
679+
lineNumber: 42
680+
});
681+
console.log(`Created file comment with ID ${fileComment.comment.id} in thread ${fileComment.thread.id}`);
682+
683+
// Create a comment with thread status
684+
const statusComment = await mcpClient.callTool('add_pull_request_comment', {
685+
projectId: 'MyProject',
686+
repositoryId: 'MyRepo',
687+
pullRequestId: 42,
688+
content: 'There\'s an edge case not handled here.',
689+
filePath: '/src/app.ts',
690+
lineNumber: 87,
691+
status: 'active'
692+
});
693+
console.log(`Created active thread with ID ${statusComment.thread.id}`);
694+
```
695+
696+
### Implementation Details
697+
698+
The `add_pull_request_comment` tool:
699+
700+
1. Establishes a connection to Azure DevOps using the provided credentials
701+
2. Retrieves the Git API client
702+
3. Creates the comment object with the provided content
703+
4. Determines whether to add a comment to an existing thread or create a new thread:
704+
- For existing threads, it calls `createComment` to add a comment to the thread
705+
- For new threads, it creates a thread object and calls `createThread` to create a new thread with the comment
706+
5. For file comments, it adds file path and line information to the thread context
707+
6. Maps status strings to the appropriate CommentThreadStatus enum values
708+
7. Returns the created comment or thread information
709+
8. Handles errors and provides meaningful error messages
710+
711+
This implementation provides a flexible way to add comments to pull requests, supporting both regular discussion comments and code review feedback.
712+

0 commit comments

Comments
 (0)