Skip to content

Commit 3fdbb9a

Browse files
committed
Sort the issues based on configuration
1 parent a23abbd commit 3fdbb9a

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/GitReleaseManager.Core/ReleaseNotes/ReleaseNotesBuilder.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,34 @@ private Dictionary<string, List<Issue>> GetIssuesDict(List<Issue> issues)
147147
var issueLabels = _configuration.IssueLabelsInclude;
148148
var excludedIssueLabels = _configuration.IssueLabelsExclude;
149149

150+
Func<SortIssuesBy, string> getSortPropertyName = (sortBy) =>
151+
{
152+
return sortBy switch
153+
{
154+
SortIssuesBy.Title => "Title",
155+
SortIssuesBy.Id => "PublicNumber",
156+
_ => throw new NotSupportedException($"'{sortBy}' is an unknown sort property."),
157+
};
158+
};
159+
160+
Func<Issue, object?> keySelector = (i) => typeof(Issue).GetProperty(getSortPropertyName(_configuration.Create.SortIssuesBy)).GetValue(i, null);
161+
150162
var issuesByLabel = issues
151163
.Where(o => !o.Labels.Any(l => excludedIssueLabels.Any(eil => string.Equals(eil, l.Name, StringComparison.OrdinalIgnoreCase))))
152164
.SelectMany(o => o.Labels, (issue, label) => new { Label = label.Name, Issue = issue })
153165
.Where(o => issueLabels.Any(il => string.Equals(il, o.Label, StringComparison.OrdinalIgnoreCase)))
154166
.GroupBy(o => o.Label, o => o.Issue)
155-
.OrderBy(o => o.Key)
156-
.ToDictionary(o => GetValidLabel(o.Key, o.Count()), o => o.OrderBy(issue => issue.PublicNumber).ToList());
167+
.OrderBy(o => o.Key) // Sort the labels alphabetically
168+
.ToDictionary(o => GetValidLabel(o.Key, o.Count()), o =>
169+
{
170+
// Sort the issues within each label group based on configuration
171+
return _configuration.Create.SortIssuesDirection switch
172+
{
173+
SortDirection.Ascending => o.OrderBy(i => keySelector(i)).ToList(),
174+
SortDirection.Descending => o.OrderByDescending(i => keySelector(i)).ToList(),
175+
_ => throw new NotSupportedException($"'{_configuration.Create.SortIssuesDirection}' is an unknown sort direction."),
176+
};
177+
});
157178

158179
return issuesByLabel;
159180
}

src/GitReleaseManager.IntegrationTests/ReleaseNotesBuilderIntegrationTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public async Task SingleMilestone()
7777
// Indicate that we want to include the 'Contributors' section in the release notes
7878
configuration.Create.IncludeContributors = true;
7979

80+
// Configure sorting
81+
configuration.Create.SortIssuesBy = Core.Model.SortIssuesBy.Title;
82+
configuration.Create.SortIssuesDirection = Core.Model.SortDirection.Descending;
83+
8084
var vcsProvider = new GitHubProvider(_gitHubClient, _mapper, _graphQlClient);
8185
var releaseNotesBuilder = new ReleaseNotesBuilder(vcsProvider, _logger, fileSystem, configuration, new TemplateFactory(fileSystem, configuration, TemplateKind.Create));
8286
var result = await releaseNotesBuilder.BuildReleaseNotesAsync("GitTools", "GitReleaseManager", "0.12.0", string.Empty).ConfigureAwait(false); // 0.12.0 contains a mix of issues and PRs

0 commit comments

Comments
 (0)