-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubRepoConnector.cs
More file actions
758 lines (668 loc) · 29.9 KB
/
GitHubRepoConnector.cs
File metadata and controls
758 lines (668 loc) · 29.9 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// 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 Octokit;
namespace DemaConsulting.BuildMark.RepoConnectors;
/// <summary>
/// GitHub repository connector implementation using Octokit.Net.
/// </summary>
public class GitHubRepoConnector : RepoConnectorBase
{
/// <summary>
/// Mapping of label keywords to their normalized item types.
/// </summary>
private static readonly Dictionary<string, string> LabelTypeMap = new()
{
{ "bug", "bug" },
{ "defect", "bug" },
{ "feature", "feature" },
{ "enhancement", "feature" },
{ "documentation", "documentation" },
{ "performance", "performance" },
{ "security", "security" }
};
/// <summary>
/// Gets build information for a release.
/// </summary>
/// <param name="version">Optional target version. If not provided, uses the most recent tag if it matches current commit.</param>
/// <returns>BuildInformation record with all collected data.</returns>
/// <exception cref="InvalidOperationException">Thrown if version cannot be determined.</exception>
public override async Task<BuildInformation> GetBuildInformationAsync(Version? version = null)
{
// Get repository metadata using git commands
var repoUrl = await RunCommandAsync("git", "remote get-url origin");
var branch = await RunCommandAsync("git", "rev-parse --abbrev-ref HEAD");
var currentCommitHash = await RunCommandAsync("git", "rev-parse HEAD");
// Parse owner and repo from URL
var (owner, repo) = ParseGitHubUrl(repoUrl);
// Get GitHub token
var token = await GetGitHubTokenAsync();
// Create Octokit client
var client = new GitHubClient(new Octokit.ProductHeaderValue("BuildMark"))
{
Credentials = new Credentials(token)
};
// Fetch all data from GitHub
var gitHubData = await FetchGitHubDataAsync(client, owner, repo, branch.Trim());
// Build lookup dictionaries and mappings
var lookupData = BuildLookupData(gitHubData);
// Determine the target version and hash
var (toVersion, toHash) = DetermineTargetVersion(version, currentCommitHash.Trim(), lookupData);
// Determine the starting release for comparing changes
var (fromVersion, fromHash) = DetermineBaselineVersion(toVersion, lookupData);
// Get commits in range
var commitsInRange = GetCommitsInRange(gitHubData.Commits, fromHash, toHash);
// Collect changes from PRs
var (bugs, nonBugChanges, allChangeIds) = await CollectChangesFromPullRequestsAsync(
commitsInRange,
lookupData,
owner,
repo,
token);
// Collect known issues
var knownIssues = CollectKnownIssues(gitHubData.Issues, allChangeIds);
// Sort all lists by Index to ensure chronological order
nonBugChanges.Sort((a, b) => a.Index.CompareTo(b.Index));
bugs.Sort((a, b) => a.Index.CompareTo(b.Index));
knownIssues.Sort((a, b) => a.Index.CompareTo(b.Index));
// Build version tags from version and hash info
var currentTag = new VersionTag(toVersion, toHash);
var baselineTag = fromVersion != null && fromHash != null
? new VersionTag(fromVersion, fromHash)
: null;
// Generate full changelog link for GitHub
var changelogLink = GenerateGitHubChangelogLink(owner, repo, fromVersion?.Tag, toVersion.Tag, lookupData.BranchTagNames);
// Create and return build information with all collected data
return new BuildInformation(
baselineTag,
currentTag,
nonBugChanges,
bugs,
knownIssues,
changelogLink);
}
/// <summary>
/// Container for GitHub data fetched from the API.
/// </summary>
internal sealed record GitHubData(
IReadOnlyList<GitHubCommit> Commits,
IReadOnlyList<Release> Releases,
IReadOnlyList<RepositoryTag> Tags,
IReadOnlyList<PullRequest> PullRequests,
IReadOnlyList<Issue> Issues);
/// <summary>
/// Container for lookup data structures built from GitHub data.
/// </summary>
internal sealed record LookupData(
Dictionary<int, Issue> IssueById,
Dictionary<string, PullRequest> CommitHashToPr,
List<Release> BranchReleases,
Dictionary<string, RepositoryTag> TagsByName,
Dictionary<string, Release> TagToRelease,
List<Version> ReleaseVersions,
HashSet<string> BranchTagNames);
/// <summary>
/// Fetches all required data from GitHub API in parallel.
/// </summary>
/// <param name="client">GitHub client.</param>
/// <param name="owner">Repository owner.</param>
/// <param name="repo">Repository name.</param>
/// <param name="branch">Branch name.</param>
/// <returns>Container with all fetched GitHub data.</returns>
private static async Task<GitHubData> FetchGitHubDataAsync(GitHubClient client, string owner, string repo, string branch)
{
// Fetch all data from GitHub in parallel
var commitsTask = GetAllCommitsAsync(client, owner, repo, branch);
var releasesTask = client.Repository.Release.GetAll(owner, repo);
var tagsTask = client.Repository.GetAllTags(owner, repo);
var pullRequestsTask = client.PullRequest.GetAllForRepository(owner, repo, new PullRequestRequest { State = ItemStateFilter.All });
var issuesTask = client.Issue.GetAllForRepository(owner, repo, new RepositoryIssueRequest { State = ItemStateFilter.All });
// Wait for all parallel fetches to complete
await Task.WhenAll(commitsTask, releasesTask, tagsTask, pullRequestsTask, issuesTask);
// Return collected data as a container
return new GitHubData(
await commitsTask,
await releasesTask,
await tagsTask,
await pullRequestsTask,
await issuesTask);
}
/// <summary>
/// Builds lookup data structures from GitHub data.
/// </summary>
/// <param name="data">GitHub data.</param>
/// <returns>Container with all lookup data structures.</returns>
internal static LookupData BuildLookupData(GitHubData data)
{
// Build a mapping from issue number to issue for efficient lookup.
// This is used to look up issue details when we find linked issue IDs.
var issueById = data.Issues.ToDictionary(i => i.Number, i => i);
// Build a mapping from commit SHA to pull request.
// This is used to associate commits with their pull requests for change tracking.
// For merged PRs, use MergeCommitSha; for open PRs, use head SHA.
var commitHashToPr = data.PullRequests
.Where(p => (p.Merged && p.MergeCommitSha != null) || (!p.Merged && p.Head?.Sha != null))
.ToDictionary(p => p.Merged ? p.MergeCommitSha! : p.Head.Sha, p => p);
// Build a set of commit SHAs in the current branch.
// This is used for efficient filtering of branch-related tags.
var branchCommitShas = new HashSet<string>(data.Commits.Select(c => c.Sha));
// Build a set of tags filtered to those on the current branch.
// This is used for efficient filtering of branch-related releases.
var branchTagNames = new HashSet<string>(
data.Tags.Where(t => branchCommitShas.Contains(t.Commit.Sha))
.Select(t => t.Name));
// Build an ordered list of releases on the current branch.
// This is used to select the prior release version for identifying changes in the build.
var branchReleases = data.Releases
.Where(r => !string.IsNullOrEmpty(r.TagName) && branchTagNames.Contains(r.TagName))
.ToList();
// Build a mapping from tag name to tag object for quick lookup.
// This is used to get commit SHAs for release tags.
var tagsByName = data.Tags.ToDictionary(t => t.Name, t => t);
// Build a mapping from tag name to release for version lookup.
// This is used to match version objects back to their releases.
var tagToRelease = branchReleases.ToDictionary(r => r.TagName!, r => r);
// Parse release tags into Version objects, maintaining release order (newest to oldest).
// This is used to determine version history and find previous releases.
var releaseVersions = branchReleases
.Select(r => Version.TryCreate(r.TagName!))
.Where(v => v != null)
.Cast<Version>()
.ToList();
return new LookupData(
issueById,
commitHashToPr,
branchReleases,
tagsByName,
tagToRelease,
releaseVersions,
branchTagNames);
}
/// <summary>
/// Determines the target version and hash for the build.
/// </summary>
/// <param name="version">Optional target version provided by caller.</param>
/// <param name="currentCommitHash">Current commit hash.</param>
/// <param name="lookupData">Lookup data structures.</param>
/// <returns>Tuple of (toVersion, toHash).</returns>
/// <exception cref="InvalidOperationException">Thrown if version cannot be determined.</exception>
internal static (Version toVersion, string toHash) DetermineTargetVersion(
Version? version,
string currentCommitHash,
LookupData lookupData)
{
// Use provided version if specified
var toVersion = version;
var toHash = currentCommitHash;
// Return early if version was explicitly provided
if (toVersion != null)
{
return (toVersion, toHash);
}
// Validate that repository has releases
if (lookupData.ReleaseVersions.Count == 0)
{
throw new InvalidOperationException(
"No releases found in repository and no version specified. " +
"Please provide a version parameter.");
}
// Use the most recent release (first in list since releases are newest to oldest)
var latestRelease = lookupData.BranchReleases[0];
var latestReleaseVersion = lookupData.ReleaseVersions[0];
var latestTagCommit = lookupData.TagsByName[latestRelease.TagName!];
// Check if current commit matches latest release tag
if (latestTagCommit.Commit.Sha == toHash)
{
// Current commit matches latest release tag, use it as target
return (latestReleaseVersion, toHash);
}
// Current commit doesn't match any release tag, cannot determine version
throw new InvalidOperationException(
"Target version not specified and current commit does not match any release tag. " +
"Please provide a version parameter.");
}
/// <summary>
/// Determines the baseline version for comparing changes.
/// </summary>
/// <param name="toVersion">Target version.</param>
/// <param name="lookupData">Lookup data structures.</param>
/// <returns>Tuple of (fromVersion, fromHash).</returns>
internal static (Version? fromVersion, string? fromHash) DetermineBaselineVersion(
Version toVersion,
LookupData lookupData)
{
// Return null baseline if no releases exist
if (lookupData.ReleaseVersions.Count == 0)
{
return (null, null);
}
// Find the position of target version in release history
var toIndex = FindVersionIndex(lookupData.ReleaseVersions, toVersion.FullVersion);
// Determine baseline version based on whether target is pre-release
var fromVersion = toVersion.IsPreRelease
? DetermineBaselineForPreRelease(toIndex, lookupData.ReleaseVersions)
: DetermineBaselineForRelease(toIndex, lookupData.ReleaseVersions);
// Get commit hash for baseline version if one was found
if (fromVersion != null &&
lookupData.TagToRelease.TryGetValue(fromVersion.Tag, out var fromRelease) &&
lookupData.TagsByName.TryGetValue(fromRelease.TagName!, out var fromTagCommit))
{
return (fromVersion, fromTagCommit.Commit.Sha);
}
// Return baseline version with null hash if commit not found
return (fromVersion, null);
}
/// <summary>
/// Determines the baseline version for a pre-release.
/// </summary>
/// <param name="toIndex">Index of target version in release history.</param>
/// <param name="releaseVersions">List of release versions.</param>
/// <returns>Baseline version or null.</returns>
private static Version? DetermineBaselineForPreRelease(int toIndex, List<Version> releaseVersions)
{
// Pre-release versions use the immediately previous (older) release as baseline
if (toIndex >= 0 && toIndex < releaseVersions.Count - 1)
{
// Target version exists in history, use next older release (higher index)
return releaseVersions[toIndex + 1];
}
// Target version not in history, use most recent release as baseline
if (toIndex == -1 && releaseVersions.Count > 0)
{
return releaseVersions[0];
}
// If toIndex is last in list, this is the oldest release, no baseline
return null;
}
/// <summary>
/// Determines the baseline version for a release (non-pre-release).
/// </summary>
/// <param name="toIndex">Index of target version in release history.</param>
/// <param name="releaseVersions">List of release versions.</param>
/// <returns>Baseline version or null.</returns>
private static Version? DetermineBaselineForRelease(int toIndex, List<Version> releaseVersions)
{
// Release versions skip pre-releases and use previous non-pre-release as baseline
var startIndex = DetermineSearchStartIndex(toIndex, releaseVersions.Count);
// Search forward through older releases (incrementing index) for previous non-pre-release version
if (startIndex >= 0)
{
for (var i = startIndex; i < releaseVersions.Count; i++)
{
if (!releaseVersions[i].IsPreRelease)
{
return releaseVersions[i];
}
}
}
return null;
}
/// <summary>
/// Determines the starting index for searching for previous releases.
/// </summary>
/// <param name="toIndex">Index of target version in release history.</param>
/// <param name="releaseCount">Total number of releases.</param>
/// <returns>Starting index for search, or -1 if no search needed.</returns>
private static int DetermineSearchStartIndex(int toIndex, int releaseCount)
{
// Target version exists in history, start search from next older release
if (toIndex >= 0 && toIndex < releaseCount - 1)
{
return toIndex + 1;
}
// Target version not in history, start from most recent release
if (toIndex == -1 && releaseCount > 0)
{
// Target version not in history, start from most recent release
// The target is newer than all existing releases, so use the most recent as baseline
return 0;
}
// Target is oldest release or no releases exist, no previous release exists
return -1;
}
/// <summary>
/// Collects changes from pull requests in the commit range.
/// </summary>
/// <param name="commitsInRange">Commits in range.</param>
/// <param name="lookupData">Lookup data structures.</param>
/// <param name="owner">Repository owner.</param>
/// <param name="repo">Repository name.</param>
/// <param name="token">GitHub token.</param>
/// <returns>Tuple of (bugs, nonBugChanges, allChangeIds).</returns>
private static async Task<(List<ItemInfo> bugs, List<ItemInfo> nonBugChanges, HashSet<string> allChangeIds)>
CollectChangesFromPullRequestsAsync(
List<GitHubCommit> commitsInRange,
LookupData lookupData,
string owner,
string repo,
string token)
{
// Initialize collections for tracking changes
var allChangeIds = new HashSet<string>();
var bugs = new List<ItemInfo>();
var nonBugChanges = new List<ItemInfo>();
// Create GraphQL client for finding linked issues (reused across multiple PR queries)
using var graphqlClient = new GitHubGraphQLClient(token);
// Process each commit that has an associated PR
foreach (var pr in commitsInRange
.Where(c => lookupData.CommitHashToPr.ContainsKey(c.Sha))
.Select(c => lookupData.CommitHashToPr[c.Sha]))
{
// Find issue IDs that are linked to this PR using GitHub GraphQL API
// All PRs are also issues, so we need to find the "real" issues (non-PR issues) that link to this PR
var linkedIssueIds = await graphqlClient.FindIssueIdsLinkedToPullRequestAsync(owner, repo, pr.Number);
// Process PR based on whether it has linked issues
if (linkedIssueIds.Count > 0)
{
ProcessLinkedIssues(linkedIssueIds, lookupData.IssueById, pr, allChangeIds, bugs, nonBugChanges);
}
else
{
ProcessPullRequestWithoutIssues(pr, allChangeIds, bugs, nonBugChanges);
}
}
// Return categorized changes
return (bugs, nonBugChanges, allChangeIds);
}
/// <summary>
/// Processes issues linked to a pull request.
/// </summary>
/// <param name="linkedIssueIds">List of linked issue IDs.</param>
/// <param name="issueById">Dictionary mapping issue IDs to issues.</param>
/// <param name="pr">Pull request.</param>
/// <param name="allChangeIds">Set of all change IDs (modified in place).</param>
/// <param name="bugs">List of bug changes (modified in place).</param>
/// <param name="nonBugChanges">List of non-bug changes (modified in place).</param>
private static void ProcessLinkedIssues(
IReadOnlyList<int> linkedIssueIds,
Dictionary<int, Issue> issueById,
PullRequest pr,
HashSet<string> allChangeIds,
List<ItemInfo> bugs,
List<ItemInfo> nonBugChanges)
{
// PR has linked issues - add them (deduplicated)
foreach (var issueId in linkedIssueIds)
{
// Check if issue already processed
var issueIdStr = issueId.ToString();
if (allChangeIds.Contains(issueIdStr))
{
continue;
}
// Look up the issue in the master issues list
if (issueById.TryGetValue(issueId, out var issue))
{
// Mark issue as processed and create item info
allChangeIds.Add(issueIdStr);
var itemInfo = CreateItemInfoFromIssue(issue, pr.Number);
// Categorize by type
if (itemInfo.Type == "bug")
{
bugs.Add(itemInfo);
}
else
{
nonBugChanges.Add(itemInfo);
}
}
}
}
/// <summary>
/// Processes a pull request without linked issues.
/// </summary>
/// <param name="pr">Pull request.</param>
/// <param name="allChangeIds">Set of all change IDs (modified in place).</param>
/// <param name="bugs">List of bug changes (modified in place).</param>
/// <param name="nonBugChanges">List of non-bug changes (modified in place).</param>
private static void ProcessPullRequestWithoutIssues(
PullRequest pr,
HashSet<string> allChangeIds,
List<ItemInfo> bugs,
List<ItemInfo> nonBugChanges)
{
// PR didn't close any issues - add the PR itself
var prId = $"#{pr.Number}";
if (allChangeIds.Add(prId))
{
// Create item info from PR
var itemInfo = CreateItemInfoFromPullRequest(pr);
// Categorize by type
if (itemInfo.Type == "bug")
{
bugs.Add(itemInfo);
}
else
{
nonBugChanges.Add(itemInfo);
}
}
}
/// <summary>
/// Collects known issues (open bugs not fixed in this build).
/// </summary>
/// <param name="issues">All issues from GitHub.</param>
/// <param name="allChangeIds">Set of all change IDs already processed.</param>
/// <returns>List of known issues.</returns>
private static List<ItemInfo> CollectKnownIssues(IReadOnlyList<Issue> issues, HashSet<string> allChangeIds)
{
return issues
.Where(i => i.State == ItemState.Open)
.Select(issue => (issue, issueId: issue.Number.ToString()))
.Where(tuple => !allChangeIds.Contains(tuple.issueId))
.Select(tuple => CreateItemInfoFromIssue(tuple.issue, tuple.issue.Number))
.Where(itemInfo => itemInfo.Type == "bug")
.ToList();
}
/// <summary>
/// Gets all commits for a branch using pagination.
/// </summary>
/// <param name="client">GitHub client.</param>
/// <param name="owner">Repository owner.</param>
/// <param name="repo">Repository name.</param>
/// <param name="branch">Branch name.</param>
/// <returns>List of all commits.</returns>
private static async Task<IReadOnlyList<GitHubCommit>> GetAllCommitsAsync(GitHubClient client, string owner, string repo, string branch)
{
// Create request for branch commits
var request = new CommitRequest { Sha = branch };
// Fetch and return all commits for the branch
return await client.Repository.Commit.GetAll(owner, repo, request);
}
/// <summary>
/// Gets commits in the range from fromHash (exclusive) to toHash (inclusive).
/// </summary>
/// <param name="commits">All commits.</param>
/// <param name="fromHash">Starting commit hash (exclusive - not included in results; null for start of history).</param>
/// <param name="toHash">Ending commit hash (inclusive - included in results).</param>
/// <returns>List of commits in range, excluding fromHash but including toHash.</returns>
internal static List<GitHubCommit> GetCommitsInRange(IReadOnlyList<GitHubCommit> commits, string? fromHash, string toHash)
{
// Initialize collection and state tracking
var result = new List<GitHubCommit>();
var foundTo = false;
// Iterate through commits from newest to oldest
foreach (var commit in commits)
{
// Mark when we've found the target commit
if (commit.Sha == toHash)
{
foundTo = true;
}
// Collect commits in range, excluding the fromHash commit itself
if (foundTo && commit.Sha != fromHash)
{
result.Add(commit);
}
// Stop when we reach the starting commit
if (commit.Sha == fromHash)
{
break;
}
}
// Return collected commits
return result;
}
/// <summary>
/// Creates an ItemInfo from an issue.
/// </summary>
/// <param name="issue">GitHub issue.</param>
/// <param name="index">Index for sorting.</param>
/// <returns>ItemInfo instance.</returns>
internal static ItemInfo CreateItemInfoFromIssue(Issue issue, int index)
{
// Determine item type from issue labels
var type = GetTypeFromLabels(issue.Labels);
// Create and return item info with issue details
return new ItemInfo(
issue.Number.ToString(),
issue.Title,
issue.HtmlUrl,
type,
index);
}
/// <summary>
/// Creates an ItemInfo from a pull request.
/// </summary>
/// <param name="pr">GitHub pull request.</param>
/// <returns>ItemInfo instance.</returns>
internal static ItemInfo CreateItemInfoFromPullRequest(PullRequest pr)
{
// Determine item type from PR labels
var type = GetTypeFromLabels(pr.Labels);
// Create and return item info with PR details
return new ItemInfo(
$"#{pr.Number}",
pr.Title,
pr.HtmlUrl,
type,
pr.Number);
}
/// <summary>
/// Determines item type from labels.
/// </summary>
/// <param name="labels">List of labels.</param>
/// <returns>Item type string.</returns>
internal static string GetTypeFromLabels(IReadOnlyList<Label> labels)
{
// Find first matching label type by checking label names against the type map
var matchingType = labels
.Select(label => label.Name.ToLowerInvariant())
.SelectMany(lowerLabel => LabelTypeMap
.Where(kvp => lowerLabel.Contains(kvp.Key))
.Select(kvp => kvp.Value))
.FirstOrDefault();
// Return matched type or default to "other"
return matchingType ?? "other";
}
/// <summary>
/// Gets GitHub token from environment or gh CLI.
/// </summary>
/// <returns>GitHub token.</returns>
private async Task<string> GetGitHubTokenAsync()
{
// Try to get token from environment variable first
var token = Environment.GetEnvironmentVariable("GH_TOKEN");
if (!string.IsNullOrEmpty(token))
{
return token;
}
// Fall back to gh auth token
try
{
return await RunCommandAsync("gh", "auth token");
}
catch (InvalidOperationException)
{
throw new InvalidOperationException("Unable to get GitHub token. Set GH_TOKEN environment variable or authenticate with 'gh auth login'.");
}
}
/// <summary>
/// Parses GitHub owner and repo from a git remote URL.
/// </summary>
/// <param name="url">Git remote URL.</param>
/// <returns>Tuple of (owner, repo).</returns>
/// <exception cref="ArgumentException">Thrown if URL format is invalid.</exception>
internal static (string owner, string repo) ParseGitHubUrl(string url)
{
// Normalize URL by trimming whitespace
url = url.Trim();
// Handle SSH URLs: git@github.com:owner/repo.git
if (url.StartsWith("git@github.com:", StringComparison.OrdinalIgnoreCase))
{
var path = url["git@github.com:".Length..];
return ParseOwnerRepo(path);
}
// Handle HTTPS URLs: https://github.com/owner/repo.git
if (url.StartsWith("https://github.com/", StringComparison.OrdinalIgnoreCase))
{
var path = url["https://github.com/".Length..];
return ParseOwnerRepo(path);
}
throw new ArgumentException($"Unsupported GitHub URL format: {url}", nameof(url));
}
/// <summary>
/// Parses owner and repo from a path segment.
/// </summary>
/// <param name="path">Path segment (e.g., "owner/repo.git").</param>
/// <returns>Tuple of (owner, repo).</returns>
/// <exception cref="ArgumentException">Thrown if path format is invalid.</exception>
internal static (string owner, string repo) ParseOwnerRepo(string path)
{
// Remove .git suffix if present
if (path.EndsWith(".git", StringComparison.OrdinalIgnoreCase))
{
path = path[..^4];
}
// Split path into owner and repo components
var parts = path.Split('/');
if (parts.Length != 2)
{
throw new ArgumentException($"Invalid GitHub path format: {path}");
}
// Return parsed owner and repo
return (parts[0], parts[1]);
}
/// <summary>
/// Generates a GitHub compare link for the full changelog.
/// </summary>
/// <param name="owner">Repository owner.</param>
/// <param name="repo">Repository name.</param>
/// <param name="oldTag">Old tag name (null if from beginning).</param>
/// <param name="newTag">New tag name.</param>
/// <param name="branchTagNames">Set of tag names on the current branch.</param>
/// <returns>WebLink to GitHub compare page, or null if no baseline tag or if tags not found in branch.</returns>
internal static WebLink? GenerateGitHubChangelogLink(string owner, string repo, string? oldTag, string newTag, HashSet<string> branchTagNames)
{
// Cannot generate comparison link without a baseline tag
if (oldTag == null)
{
return null;
}
// Suppress changelog link if either tag is not in the branch
if (!branchTagNames.Contains(oldTag) || !branchTagNames.Contains(newTag))
{
return null;
}
// Build comparison label and URL
var comparisonLabel = $"{oldTag}...{newTag}";
var comparisonUrl = $"https://github.com/{owner}/{repo}/compare/{comparisonLabel}";
return new WebLink(comparisonLabel, comparisonUrl);
}
}