-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPullRequestTools.cs
More file actions
364 lines (316 loc) · 17.4 KB
/
PullRequestTools.cs
File metadata and controls
364 lines (316 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System.ComponentModel;
using System.Text.Json;
using ModelContextProtocol.Server;
using Viamus.Azure.Devops.Mcp.Server.Services;
namespace Viamus.Azure.Devops.Mcp.Server.Tools;
/// <summary>
/// MCP tools for Azure DevOps Pull Request operations.
/// </summary>
[McpServerToolType]
public sealed class PullRequestTools
{
private readonly IAzureDevOpsService _azureDevOpsService;
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public PullRequestTools(IAzureDevOpsService azureDevOpsService)
{
_azureDevOpsService = azureDevOpsService;
}
[McpServerTool(Name = "get_pull_requests")]
[Description("Gets pull requests for a Git repository with optional filters. Returns PR details including title, source/target branches, status, reviewers, and merge status.")]
public async Task<string> GetPullRequests(
[Description("The repository name or ID")] string repositoryNameOrId,
[Description("The project name (optional if default project is configured)")] string? project = null,
[Description("Filter by status: 'active', 'completed', 'abandoned', or 'all' (default: all)")] string? status = null,
[Description("Filter by creator's unique name or GUID")] string? creatorId = null,
[Description("Filter by reviewer's unique name or GUID")] string? reviewerId = null,
[Description("Filter by source branch (e.g., 'refs/heads/feature-branch')")] string? sourceRefName = null,
[Description("Filter by target branch (e.g., 'refs/heads/main')")] string? targetRefName = null,
[Description("Maximum number of results to return (default: 50)")] int top = 50,
[Description("Number of results to skip for pagination (default: 0)")] int skip = 0,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(repositoryNameOrId))
{
return JsonSerializer.Serialize(new { error = "Repository name or ID is required" }, JsonOptions);
}
var pullRequests = await _azureDevOpsService.GetPullRequestsAsync(
repositoryNameOrId, project, status, creatorId, reviewerId,
sourceRefName, targetRefName, top, skip, cancellationToken);
return JsonSerializer.Serialize(new
{
repository = repositoryNameOrId,
count = pullRequests.Count,
pullRequests
}, JsonOptions);
}
[McpServerTool(Name = "get_pull_request")]
[Description("Gets details of a specific pull request by ID within a repository. Returns full PR information including description, reviewers with their votes, and merge status.")]
public async Task<string> GetPullRequest(
[Description("The repository name or ID")] string repositoryNameOrId,
[Description("The pull request ID")] int pullRequestId,
[Description("The project name (optional if default project is configured)")] string? project = null,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(repositoryNameOrId))
{
return JsonSerializer.Serialize(new { error = "Repository name or ID is required" }, JsonOptions);
}
if (pullRequestId <= 0)
{
return JsonSerializer.Serialize(new { error = "Pull request ID must be a positive integer" }, JsonOptions);
}
var pullRequest = await _azureDevOpsService.GetPullRequestByIdAsync(
repositoryNameOrId, pullRequestId, project, cancellationToken);
if (pullRequest is null)
{
return JsonSerializer.Serialize(new { error = $"Pull request {pullRequestId} not found in repository '{repositoryNameOrId}'" }, JsonOptions);
}
return JsonSerializer.Serialize(pullRequest, JsonOptions);
}
[McpServerTool(Name = "get_pull_request_by_id")]
[Description("Gets details of a pull request by ID only, without needing to specify the repository. This is a project-level lookup that finds the PR across all repositories. Returns full PR information including description, reviewers with their votes, merge status, and repository details.")]
public async Task<string> GetPullRequestById(
[Description("The pull request ID")] int pullRequestId,
[Description("The project name (optional if default project is configured)")] string? project = null,
CancellationToken cancellationToken = default)
{
if (pullRequestId <= 0)
{
return JsonSerializer.Serialize(new { error = "Pull request ID must be a positive integer" }, JsonOptions);
}
var pullRequest = await _azureDevOpsService.GetPullRequestByIdOnlyAsync(
pullRequestId, project, cancellationToken);
if (pullRequest is null)
{
return JsonSerializer.Serialize(new { error = $"Pull request {pullRequestId} not found in project" }, JsonOptions);
}
return JsonSerializer.Serialize(pullRequest, JsonOptions);
}
[McpServerTool(Name = "get_pull_request_threads")]
[Description("Gets comment threads for a pull request. Returns all discussion threads including inline comments on files with their status and replies.")]
public async Task<string> GetPullRequestThreads(
[Description("The repository name or ID")] string repositoryNameOrId,
[Description("The pull request ID")] int pullRequestId,
[Description("The project name (optional if default project is configured)")] string? project = null,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(repositoryNameOrId))
{
return JsonSerializer.Serialize(new { error = "Repository name or ID is required" }, JsonOptions);
}
if (pullRequestId <= 0)
{
return JsonSerializer.Serialize(new { error = "Pull request ID must be a positive integer" }, JsonOptions);
}
var threads = await _azureDevOpsService.GetPullRequestThreadsAsync(
repositoryNameOrId, pullRequestId, project, cancellationToken);
return JsonSerializer.Serialize(new
{
repository = repositoryNameOrId,
pullRequestId,
count = threads.Count,
threads
}, JsonOptions);
}
[McpServerTool(Name = "create_pull_request_thread")]
[Description("Creates a new comment thread on a pull request. Omit filePath and lineNumber for a general PR discussion, or provide them to create an inline file comment.")]
public async Task<string> CreatePullRequestThread(
[Description("The repository name or ID")] string repositoryNameOrId,
[Description("The pull request ID")] int pullRequestId,
[Description("The initial comment text (Markdown supported)")] string content,
[Description("Optional file path for an inline thread, e.g. '/src/App.cs'")] string? filePath = null,
[Description("Optional line number for an inline thread on the right/new file")] int? lineNumber = null,
[Description("Optional ending line number for an inline thread range on the right/new file")] int? endLineNumber = null,
[Description("The project name (optional if default project is configured)")] string? project = null,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(repositoryNameOrId))
{
return JsonSerializer.Serialize(new { error = "Repository name or ID is required" }, JsonOptions);
}
if (pullRequestId <= 0)
{
return JsonSerializer.Serialize(new { error = "Pull request ID must be a positive integer" }, JsonOptions);
}
if (string.IsNullOrWhiteSpace(content))
{
return JsonSerializer.Serialize(new { error = "Comment content cannot be empty" }, JsonOptions);
}
if (!string.IsNullOrWhiteSpace(filePath) && (!lineNumber.HasValue || lineNumber <= 0))
{
return JsonSerializer.Serialize(new { error = "Line number must be a positive integer when filePath is provided" }, JsonOptions);
}
if (lineNumber.HasValue && string.IsNullOrWhiteSpace(filePath))
{
return JsonSerializer.Serialize(new { error = "File path is required when lineNumber is provided" }, JsonOptions);
}
if (endLineNumber.HasValue && (!lineNumber.HasValue || endLineNumber < lineNumber))
{
return JsonSerializer.Serialize(new { error = "End line number must be greater than or equal to lineNumber" }, JsonOptions);
}
var thread = await _azureDevOpsService.CreatePullRequestThreadAsync(
repositoryNameOrId, pullRequestId, content,
filePath, lineNumber, endLineNumber, project, cancellationToken);
return JsonSerializer.Serialize(new
{
success = true,
message = $"Thread {thread.Id} created on pull request {pullRequestId}",
thread
}, JsonOptions);
}
[McpServerTool(Name = "add_pull_request_thread_comment")]
[Description("Adds a comment to an existing comment thread on a pull request. Use this to reply to discussions returned by get_pull_request_threads. Pass parentCommentId to reply to a specific comment within the thread; omit it to add a top-level comment.")]
public async Task<string> AddPullRequestThreadComment(
[Description("The repository name or ID")] string repositoryNameOrId,
[Description("The pull request ID")] int pullRequestId,
[Description("The thread ID (from get_pull_request_threads)")] int threadId,
[Description("The comment text (Markdown supported)")] string content,
[Description("Optional parent comment ID when replying to a specific comment in the thread")] int? parentCommentId = null,
[Description("The project name (optional if default project is configured)")] string? project = null,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(repositoryNameOrId))
{
return JsonSerializer.Serialize(new { error = "Repository name or ID is required" }, JsonOptions);
}
if (pullRequestId <= 0)
{
return JsonSerializer.Serialize(new { error = "Pull request ID must be a positive integer" }, JsonOptions);
}
if (threadId <= 0)
{
return JsonSerializer.Serialize(new { error = "Thread ID must be a positive integer" }, JsonOptions);
}
if (string.IsNullOrWhiteSpace(content))
{
return JsonSerializer.Serialize(new { error = "Comment content cannot be empty" }, JsonOptions);
}
var comment = await _azureDevOpsService.AddPullRequestThreadCommentAsync(
repositoryNameOrId, pullRequestId, threadId, content,
parentCommentId, project, cancellationToken);
return JsonSerializer.Serialize(new
{
success = true,
message = $"Comment added to thread {threadId} on pull request {pullRequestId}",
comment
}, JsonOptions);
}
[McpServerTool(Name = "search_pull_requests")]
[Description("Searches pull requests by text in title or description. Useful for finding PRs related to specific features or bugs.")]
public async Task<string> SearchPullRequests(
[Description("The repository name or ID")] string repositoryNameOrId,
[Description("Text to search for in PR title or description")] string searchText,
[Description("The project name (optional if default project is configured)")] string? project = null,
[Description("Filter by status: 'active', 'completed', 'abandoned', or 'all' (default: all)")] string? status = null,
[Description("Maximum number of results to return (default: 50)")] int top = 50,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(repositoryNameOrId))
{
return JsonSerializer.Serialize(new { error = "Repository name or ID is required" }, JsonOptions);
}
if (string.IsNullOrWhiteSpace(searchText))
{
return JsonSerializer.Serialize(new { error = "Search text is required" }, JsonOptions);
}
var pullRequests = await _azureDevOpsService.SearchPullRequestsAsync(
repositoryNameOrId, searchText, project, status, top, cancellationToken);
return JsonSerializer.Serialize(new
{
repository = repositoryNameOrId,
searchText,
count = pullRequests.Count,
pullRequests
}, JsonOptions);
}
[McpServerTool(Name = "create_pull_request")]
[Description("Creates a new pull request in a Git repository. Supports setting title, description, source/target branches, draft status, reviewers, and linked work items.")]
public async Task<string> CreatePullRequest(
[Description("The repository name or ID")] string repositoryNameOrId,
[Description("The source branch (e.g., 'refs/heads/feature-branch')")] string sourceRefName,
[Description("The target branch (e.g., 'refs/heads/main')")] string targetRefName,
[Description("The pull request title")] string title,
[Description("The pull request description")] string? description = null,
[Description("Whether to create as a draft pull request (default: false)")] bool isDraft = false,
[Description("The project name (optional if default project is configured)")] string? project = null,
[Description("Semicolon-separated reviewer GUIDs (e.g., 'guid1;guid2')")] string? reviewerIds = null,
[Description("Semicolon-separated work item IDs to link (e.g., '123;456')")] string? workItemIds = null,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(repositoryNameOrId))
{
return JsonSerializer.Serialize(new { error = "Repository name or ID is required" }, JsonOptions);
}
if (string.IsNullOrWhiteSpace(sourceRefName))
{
return JsonSerializer.Serialize(new { error = "Source branch is required" }, JsonOptions);
}
if (string.IsNullOrWhiteSpace(targetRefName))
{
return JsonSerializer.Serialize(new { error = "Target branch is required" }, JsonOptions);
}
if (string.IsNullOrWhiteSpace(title))
{
return JsonSerializer.Serialize(new { error = "Title is required" }, JsonOptions);
}
var parsedReviewerIds = string.IsNullOrWhiteSpace(reviewerIds)
? null
: reviewerIds.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var parsedWorkItemIds = string.IsNullOrWhiteSpace(workItemIds)
? null
: workItemIds.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Where(id => int.TryParse(id, out _))
.Select(int.Parse);
var pullRequest = await _azureDevOpsService.CreatePullRequestAsync(
repositoryNameOrId, sourceRefName, targetRefName, title,
description, isDraft, project, parsedReviewerIds, parsedWorkItemIds,
cancellationToken);
return JsonSerializer.Serialize(new
{
success = true,
message = $"Pull request {pullRequest.PullRequestId} created successfully",
pullRequest
}, JsonOptions);
}
[McpServerTool(Name = "query_pull_requests")]
[Description("Advanced query for pull requests with multiple combined filters. Allows filtering by status, branches, dates, creator, and reviewer simultaneously.")]
public async Task<string> QueryPullRequests(
[Description("The repository name or ID")] string repositoryNameOrId,
[Description("The project name (optional if default project is configured)")] string? project = null,
[Description("Filter by status: 'active', 'completed', 'abandoned', or 'all'")] string? status = null,
[Description("Filter by creator's unique name or GUID")] string? creatorId = null,
[Description("Filter by reviewer's unique name or GUID")] string? reviewerId = null,
[Description("Filter by source branch (e.g., 'refs/heads/feature-branch')")] string? sourceRefName = null,
[Description("Filter by target branch (e.g., 'refs/heads/main')")] string? targetRefName = null,
[Description("Maximum number of results to return (default: 50)")] int top = 50,
[Description("Number of results to skip for pagination (default: 0)")] int skip = 0,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(repositoryNameOrId))
{
return JsonSerializer.Serialize(new { error = "Repository name or ID is required" }, JsonOptions);
}
var pullRequests = await _azureDevOpsService.GetPullRequestsAsync(
repositoryNameOrId, project, status, creatorId, reviewerId,
sourceRefName, targetRefName, top, skip, cancellationToken);
return JsonSerializer.Serialize(new
{
repository = repositoryNameOrId,
filters = new
{
status = status ?? "all",
creatorId,
reviewerId,
sourceRefName,
targetRefName
},
pagination = new { top, skip },
count = pullRequests.Count,
pullRequests
}, JsonOptions);
}
}