Skip to content

Commit 5ec2277

Browse files
authored
(GH-72) Don't create release when not required (#183)
(GH-72) Don't create release when not required
2 parents 64adaf4 + aefb15e commit 5ec2277

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

Source/GitReleaseManager.Tests/ReleaseNotesBuilderTests.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@ namespace GitReleaseManager.Tests
1818
[TestFixture]
1919
public class ReleaseNotesBuilderTests
2020
{
21+
public TestContext TestContext { get; set; }
22+
23+
[OneTimeSetUp]
24+
public void Configure()
25+
{
26+
Logger.WriteError = s => TestContext.WriteLine($"Error: {s}");
27+
Logger.WriteInfo = s => TestContext.WriteLine($"Info: {s}");
28+
Logger.WriteWarning = s => TestContext.WriteLine($"Warning: {s}");
29+
}
30+
2131
[Test]
2232
public void NoCommitsNoIssues()
2333
{
24-
AcceptTest(0);
34+
Assert.Throws<AggregateException>(() => AcceptTest(0));
2535
}
2636

2737
[Test]
@@ -33,7 +43,7 @@ public void NoCommitsSomeIssues()
3343
[Test]
3444
public void SomeCommitsNoIssues()
3545
{
36-
AcceptTest(5);
46+
Assert.Throws<AggregateException>(() => AcceptTest(5));
3747
}
3848

3949
[Test]
@@ -45,7 +55,7 @@ public void SomeCommitsSomeIssues()
4555
[Test]
4656
public void SingularCommitsNoIssues()
4757
{
48-
AcceptTest(1);
58+
Assert.Throws<AggregateException>(() => AcceptTest(1));
4959
}
5060

5161
[Test]

Source/GitReleaseManager/ReleaseNotesBuilder.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public async Task<string> BuildReleaseNotes()
4646
var previousMilestone = GetPreviousMilestone();
4747
var numberOfCommits = await _vcsProvider.GetNumberOfCommitsBetween(previousMilestone, _targetMilestone, _user, _repository).ConfigureAwait(false);
4848

49+
if (issues.Count == 0)
50+
{
51+
var logMessage = string.Format("No closed issues have been found for milestone {0}, or all assigned issues are meant to be excluded from release notes, aborting creation of release.", _milestoneTitle);
52+
Logger.WriteError(logMessage);
53+
throw new Exception(logMessage);
54+
}
55+
4956
if (issues.Count > 0)
5057
{
5158
var issuesText = string.Format(issues.Count == 1 ? "{0} issue" : "{0} issues", issues.Count);
@@ -109,18 +116,19 @@ private string GetLabel(string label, Func<LabelAlias, string> func)
109116
return alias != null ? func(alias) : null;
110117
}
111118

112-
private void CheckForValidLabels(Issue issue)
119+
private bool CheckForValidLabels(Issue issue)
113120
{
114-
var count = 0;
121+
var includedIssuesCount = 0;
122+
var excludedIssuesCount = 0;
115123

116124
foreach (var issueLabel in issue.Labels)
117125
{
118-
count += _configuration.IssueLabelsInclude.Count(issueToInclude => issueLabel.Name.ToUpperInvariant() == issueToInclude.ToUpperInvariant());
126+
includedIssuesCount += _configuration.IssueLabelsInclude.Count(issueToInclude => issueLabel.Name.ToUpperInvariant() == issueToInclude.ToUpperInvariant());
119127

120-
count += _configuration.IssueLabelsExclude.Count(issueToExclude => issueLabel.Name.ToUpperInvariant() == issueToExclude.ToUpperInvariant());
128+
excludedIssuesCount += _configuration.IssueLabelsExclude.Count(issueToExclude => issueLabel.Name.ToUpperInvariant() == issueToExclude.ToUpperInvariant());
121129
}
122130

123-
if (count != 1)
131+
if (includedIssuesCount + excludedIssuesCount != 1)
124132
{
125133
var allIssueLabels = _configuration.IssueLabelsInclude.Union(_configuration.IssueLabelsExclude).ToList();
126134
var allIssuesExceptLast = allIssueLabels.Take(allIssueLabels.Count - 1);
@@ -131,6 +139,13 @@ private void CheckForValidLabels(Issue issue)
131139
var message = string.Format(CultureInfo.InvariantCulture, "Bad Issue {0} expected to find a single label with either {1} or {2}.", issue.HtmlUrl, allIssuesExceptLastString, lastLabel);
132140
throw new InvalidOperationException(message);
133141
}
142+
143+
if (includedIssuesCount > 0)
144+
{
145+
return true;
146+
}
147+
148+
return false;
134149
}
135150

136151
private void AddIssues(StringBuilder stringBuilder, List<Issue> issues)
@@ -181,9 +196,24 @@ private async Task LoadMilestones()
181196
private async Task<List<Issue>> GetIssues(Milestone milestone)
182197
{
183198
var issues = await _vcsProvider.GetIssuesAsync(milestone).ConfigureAwait(false);
199+
200+
var hasIncludedIssues = false;
201+
184202
foreach (var issue in issues)
185203
{
186-
CheckForValidLabels(issue);
204+
if (CheckForValidLabels(issue))
205+
{
206+
hasIncludedIssues = true;
207+
}
208+
}
209+
210+
// If there are no issues assigned to the milestone that have a label that is part
211+
// of the labels to include array, then that is essentially the same as having no
212+
// closed issues assigned to the milestone. In this scenario, we want to raise an
213+
// error, so return an emtpy issues list.
214+
if (!hasIncludedIssues)
215+
{
216+
return new List<Issue>();
187217
}
188218

189219
return issues;

0 commit comments

Comments
 (0)