-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubRepoConnector.cs
More file actions
313 lines (282 loc) · 12.7 KB
/
GitHubRepoConnector.cs
File metadata and controls
313 lines (282 loc) · 12.7 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
// Copyright (c) DEMA Consulting
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System.Text.RegularExpressions;
namespace DemaConsulting.BuildMark;
/// <summary>
/// GitHub repository connector implementation.
/// </summary>
public partial class GitHubRepoConnector : RepoConnectorBase
{
private static readonly Dictionary<string, string> LabelTypeMap = new()
{
{ "bug", "bug" },
{ "defect", "bug" },
{ "feature", "feature" },
{ "enhancement", "feature" },
{ "documentation", "documentation" },
{ "performance", "performance" },
{ "security", "security" }
};
/// <summary>
/// Validates and sanitizes a tag name to prevent command injection.
/// </summary>
/// <param name="tag">Tag name to validate.</param>
/// <returns>Sanitized tag name.</returns>
/// <exception cref="ArgumentException">Thrown if tag name is invalid.</exception>
private static string ValidateTag(string tag)
{
// Ensure tag name matches allowed pattern to prevent injection attacks
if (!TagNameRegex().IsMatch(tag))
{
throw new ArgumentException($"Invalid tag name: {tag}", nameof(tag));
}
return tag;
}
/// <summary>
/// Validates and sanitizes an issue or PR ID to prevent command injection.
/// </summary>
/// <param name="id">ID to validate.</param>
/// <param name="paramName">Parameter name for exception message.</param>
/// <returns>Sanitized ID.</returns>
/// <exception cref="ArgumentException">Thrown if ID is invalid.</exception>
private static string ValidateId(string id, string paramName)
{
// Ensure ID is numeric to prevent injection attacks
if (!NumericIdRegex().IsMatch(id))
{
throw new ArgumentException($"Invalid ID: {id}", paramName);
}
return id;
}
/// <summary>
/// Gets the history of tags leading to the current branch.
/// </summary>
/// <returns>List of tags in chronological order.</returns>
public override async Task<List<Version>> GetTagHistoryAsync()
{
// Get all tags merged into current branch, sorted by creation date
// Arguments: --sort=creatordate (chronological order), --merged HEAD (reachable from HEAD)
// Output format: one tag name per line
var output = await RunCommandAsync("git", "tag --sort=creatordate --merged HEAD");
// Split output into individual tag names
var tagNames = output
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Trim())
.ToList();
// Parse and filter to valid version tags only
return tagNames
.Select(Version.TryCreate)
.Where(t => t != null)
.Cast<Version>()
.ToList();
}
/// <summary>
/// Checks if a git tag exists in the repository.
/// </summary>
/// <param name="tag">Tag name to check.</param>
/// <returns>True if the tag exists, false otherwise.</returns>
private async Task<bool> TagExistsAsync(string tag)
{
try
{
// Try to resolve the tag to a commit hash
// If tag doesn't exist, RunCommandAsync will throw InvalidOperationException
await RunCommandAsync("git", $"rev-parse --verify {ValidateTag(tag)}");
return true;
}
catch (InvalidOperationException)
{
// Tag doesn't exist
return false;
}
}
/// <summary>
/// Gets the list of pull request IDs between two versions.
/// </summary>
/// <param name="from">Starting version (null for start of history).</param>
/// <param name="to">Ending version (null for current state).</param>
/// <returns>List of pull request IDs.</returns>
public override async Task<List<string>> GetPullRequestsBetweenTagsAsync(Version? from, Version? to)
{
// Get commits using GitHub API instead of git log
// This approach doesn't require fetch-depth: 0 in CI and works with shallow clones
string commitHashesOutput;
if (from == null && to == null)
{
// No versions specified, get all commits using paginated API
commitHashesOutput = await RunCommandAsync("gh", "api repos/:owner/:repo/commits --paginate --jq .[].sha");
}
else if (from == null)
{
// Only end version specified - get commits up to 'to' tag/HEAD
// Check if the tag exists; if not, use HEAD
var toExists = to != null && await TagExistsAsync(to.Tag);
var toRef = toExists ? ValidateTag(to!.Tag) : "HEAD";
// Get all commits up to toRef
commitHashesOutput = await RunCommandAsync("gh", $"api repos/:owner/:repo/commits?sha={toRef} --paginate --jq .[].sha");
}
else if (to == null)
{
// Only start version specified - compare from tag to HEAD
var fromTag = ValidateTag(from.Tag);
commitHashesOutput = await RunCommandAsync("gh", $"api repos/:owner/:repo/compare/{fromTag}...HEAD --jq .commits[].sha");
}
else
{
// Both versions specified - compare from tag to to tag/HEAD
var fromTag = ValidateTag(from.Tag);
var toExists = await TagExistsAsync(to.Tag);
var toRef = toExists ? ValidateTag(to.Tag) : "HEAD";
commitHashesOutput = await RunCommandAsync("gh", $"api repos/:owner/:repo/compare/{fromTag}...{toRef} --jq .commits[].sha");
}
// Pipe commit hashes to gh pr list to batch search for PRs
// This is much faster than querying each commit individually
// The commit hashes from the first command are piped as stdin to the second command
string prSearchOutput;
try
{
// Search for PRs by piping commit hashes to gh pr list
prSearchOutput = await RunCommandAsync("gh", "pr list --state all --json number --jq .[].number", commitHashesOutput);
}
catch (InvalidOperationException)
{
// Fallback to empty result if batch query fails
prSearchOutput = string.Empty;
}
var pullRequestsFromApi = prSearchOutput
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
.Select(n => n.Trim())
.Where(n => !string.IsNullOrEmpty(n))
.Distinct()
.ToList();
return pullRequestsFromApi;
}
/// <summary>
/// Gets the issue IDs associated with a pull request.
/// </summary>
/// <param name="pullRequestId">Pull request ID.</param>
/// <returns>List of issue IDs.</returns>
public override async Task<List<string>> GetIssuesForPullRequestAsync(string pullRequestId)
{
// Use GitHub API to get issues that are actually linked to close when PR merges
// This is more reliable than parsing PR body text which could contain any #numbers
// Arguments: --json closingIssuesReferences (get linked issues), --jq to extract numbers
// Output: issue numbers (one per line)
var validatedId = ValidateId(pullRequestId, nameof(pullRequestId));
var output = await RunCommandAsync("gh", $"pr view {validatedId} --json closingIssuesReferences --jq .closingIssuesReferences[].number");
// Parse output to get issue numbers
var issues = output
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
.Select(n => n.Trim())
.Where(n => !string.IsNullOrEmpty(n))
.ToList();
return issues;
}
/// <summary>
/// Gets the title of an issue.
/// </summary>
/// <param name="issueId">Issue ID.</param>
/// <returns>Issue title.</returns>
public override async Task<string> GetIssueTitleAsync(string issueId)
{
// Validate and fetch issue title using GitHub CLI
// Arguments: --json title (get title field), --jq .title (extract title value)
// Output: issue title as plain text
var validatedId = ValidateId(issueId, nameof(issueId));
return await RunCommandAsync("gh", $"issue view {validatedId} --json title --jq .title");
}
/// <summary>
/// Gets the type of an issue (bug, feature, etc.).
/// </summary>
/// <param name="issueId">Issue ID.</param>
/// <returns>Issue type.</returns>
public override async Task<string> GetIssueTypeAsync(string issueId)
{
// Validate and fetch issue labels using GitHub CLI
// Arguments: --json labels (get labels array), --jq .labels[].name (extract label names)
// Output: one label name per line
var validatedId = ValidateId(issueId, nameof(issueId));
var output = await RunCommandAsync("gh", $"issue view {validatedId} --json labels --jq .labels[].name");
var labels = output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
// Map labels to standardized issue types
var matchingType = labels
.Select(label => label.ToLowerInvariant())
.SelectMany(lowerLabel => LabelTypeMap
.Where(kvp => lowerLabel.Contains(kvp.Key))
.Select(kvp => kvp.Value))
.FirstOrDefault();
// Return matching type or default when no recognized label found
return matchingType ?? "other";
}
/// <summary>
/// Gets the git hash for a tag.
/// </summary>
/// <param name="tag">Tag name (null for current state).</param>
/// <returns>Git hash.</returns>
public override async Task<string> GetHashForTagAsync(string? tag)
{
// Get commit hash for tag or HEAD using git rev-parse
// Arguments: tag name or "HEAD" for current commit
// Output: full 40-character commit SHA
var refName = tag == null ? "HEAD" : ValidateTag(tag);
return await RunCommandAsync("git", $"rev-parse {refName}");
}
/// <summary>
/// Gets the URL for an issue.
/// </summary>
/// <param name="issueId">Issue ID.</param>
/// <returns>Issue URL.</returns>
public override async Task<string> GetIssueUrlAsync(string issueId)
{
// Validate and fetch issue URL using GitHub CLI
// Arguments: --json url (get url field), --jq .url (extract url value)
// Output: full HTTPS URL to the issue
var validatedId = ValidateId(issueId, nameof(issueId));
return await RunCommandAsync("gh", $"issue view {validatedId} --json url --jq .url");
}
/// <summary>
/// Gets the list of open issue IDs.
/// </summary>
/// <returns>List of open issue IDs.</returns>
public override async Task<List<string>> GetOpenIssuesAsync()
{
// Fetch all open issue numbers using GitHub CLI
// Arguments: --state open (open issues only), --json number (get number field), --jq .[].number (extract numbers from array)
// Output: one issue number per line
var output = await RunCommandAsync("gh", "issue list --state open --json number --jq .[].number");
// Parse output into list of issue IDs
return output
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
.Select(n => n.Trim())
.ToList();
}
/// <summary>
/// Regular expression to match valid tag names (alphanumeric, dots, hyphens, underscores, slashes).
/// </summary>
/// <returns>Compiled regular expression.</returns>
[GeneratedRegex(@"^[a-zA-Z0-9._/-]+$", RegexOptions.Compiled)]
private static partial Regex TagNameRegex();
/// <summary>
/// Regular expression to match numeric IDs.
/// </summary>
/// <returns>Compiled regular expression.</returns>
[GeneratedRegex(@"^\d+$", RegexOptions.Compiled)]
private static partial Regex NumericIdRegex();
}