-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPullRequestTools.cs
More file actions
267 lines (233 loc) · 12.6 KB
/
Copy pathPullRequestTools.cs
File metadata and controls
267 lines (233 loc) · 12.6 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
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 = "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);
}
}