-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathReleaseNotesBuilder.cs
206 lines (169 loc) · 8.42 KB
/
ReleaseNotesBuilder.cs
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
//-----------------------------------------------------------------------
// <copyright file="ReleaseNotesBuilder.cs" company="GitTools Contributors">
// Copyright (c) 2015 - Present - GitTools Contributors
// </copyright>
//-----------------------------------------------------------------------
namespace GitReleaseManager.Core
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GitReleaseManager.Core.Configuration;
using Octokit;
public class ReleaseNotesBuilder
{
private IGitHubClient gitHubClient;
private string user;
private string repository;
private string milestoneTitle;
private ReadOnlyCollection<Milestone> milestones;
private Milestone targetMilestone;
private Config configuration;
public ReleaseNotesBuilder(IGitHubClient gitHubClient, string user, string repository, string milestoneTitle, Config configuration)
{
this.gitHubClient = gitHubClient;
this.user = user;
this.repository = repository;
this.milestoneTitle = milestoneTitle;
this.configuration = configuration;
}
public async Task<string> BuildReleaseNotes()
{
this.LoadMilestones();
this.GetTargetMilestone();
var issues = await this.GetIssues(this.targetMilestone);
var stringBuilder = new StringBuilder();
var previousMilestone = this.GetPreviousMilestone();
var numberOfCommits = await this.gitHubClient.GetNumberOfCommitsBetween(previousMilestone, this.targetMilestone);
if (issues.Count > 0)
{
var issuesText = string.Format(issues.Count == 1 ? "{0} issue" : "{0} issues", issues.Count);
if (numberOfCommits > 0)
{
var commitsLink = this.GetCommitsLink(previousMilestone);
var commitsText = string.Format(numberOfCommits == 1 ? "{0} commit" : "{0} commits", numberOfCommits);
stringBuilder.AppendFormat(@"As part of this release we had [{0}]({1}) which resulted in [{2}]({3}) being closed.", commitsText, commitsLink, issuesText, this.targetMilestone.HtmlUrl());
}
else
{
stringBuilder.AppendFormat(@"As part of this release we had [{0}]({1}) closed.", issuesText, this.targetMilestone.HtmlUrl());
}
}
else if (numberOfCommits > 0)
{
var commitsLink = this.GetCommitsLink(previousMilestone);
var commitsText = string.Format(numberOfCommits == 1 ? "{0} commit" : "{0} commits", numberOfCommits);
stringBuilder.AppendFormat(@"As part of this release we had [{0}]({1}).", commitsText, commitsLink);
}
stringBuilder.AppendLine();
stringBuilder.AppendLine(this.targetMilestone.Description);
stringBuilder.AppendLine();
this.AddIssues(stringBuilder, issues);
if (this.configuration.Create.IncludeFooter)
{
this.AddFooter(stringBuilder);
}
return stringBuilder.ToString();
}
private void Append(IEnumerable<Issue> issues, string label, StringBuilder stringBuilder)
{
var features = issues.Where(x => x.Labels.Any(l => l.Name == label)).ToList();
if (features.Count > 0)
{
var singular = this.GetLabel(label, alias => alias.Header) ?? label;
var plural = this.GetLabel(label, alias => alias.Plural) ?? label + "s";
stringBuilder.AppendFormat("__{0}__\r\n\r\n", features.Count == 1 ? singular : plural);
foreach (var issue in features)
{
stringBuilder.AppendFormat("- [__#{0}__]({1}) {2}\r\n", issue.Number, issue.HtmlUrl, issue.Title);
}
stringBuilder.AppendLine();
}
}
private string GetLabel(string label, Func<LabelAlias, string> func)
{
var alias = this.configuration.LabelAliases.FirstOrDefault(x => x.Name.Equals(label, StringComparison.OrdinalIgnoreCase));
return alias != null ? func(alias) : null;
}
private void CheckForValidLabels(Issue issue)
{
var count = 0;
foreach (var issueLabel in issue.Labels)
{
count += this.configuration.IssueLabelsInclude.Count(issueToInclude => issueLabel.Name.ToUpperInvariant() == issueToInclude.ToUpperInvariant());
count += this.configuration.IssueLabelsExclude.Count(issueToExclude => issueLabel.Name.ToUpperInvariant() == issueToExclude.ToUpperInvariant());
}
if (count != 1 && !this.configuration.IssueLabelsMany)
{
var allIssueLabels = this.configuration.IssueLabelsInclude.Union(this.configuration.IssueLabelsExclude).ToList();
var allIssuesExceptLast = allIssueLabels.Take(allIssueLabels.Count - 1);
var lastLabel = allIssueLabels.Last();
var allIssuesExceptLastString = string.Join(", ", allIssuesExceptLast);
var message = string.Format(CultureInfo.InvariantCulture, "Bad Issue {0} expected to find a single label with either {1} or {2}.", issue.HtmlUrl, allIssuesExceptLastString, lastLabel);
throw new InvalidOperationException(message);
}
}
private void AddIssues(StringBuilder stringBuilder, List<Issue> issues)
{
foreach (var issueLabel in this.configuration.IssueLabelsInclude)
{
this.Append(issues, issueLabel, stringBuilder);
}
}
private Milestone GetPreviousMilestone()
{
var currentVersion = this.targetMilestone.Version();
return this.milestones
.OrderByDescending(m => m.Version())
.Distinct().ToList()
.SkipWhile(x => x.Version() >= currentVersion)
.FirstOrDefault();
}
private string GetCommitsLink(Milestone previousMilestone)
{
if (previousMilestone == null)
{
return string.Format(CultureInfo.InvariantCulture, "https://github.com/{0}/{1}/commits/{2}", this.user, this.repository, this.targetMilestone.Title);
}
return string.Format(CultureInfo.InvariantCulture, "https://github.com/{0}/{1}/compare/{2}...{3}", this.user, this.repository, previousMilestone.Title, this.targetMilestone.Title);
}
private void AddFooter(StringBuilder stringBuilder)
{
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "### {0}", this.configuration.Create.FooterHeading));
var footerContent = this.configuration.Create.FooterContent;
if (this.configuration.Create.FooterIncludesMilestone)
{
if (!string.IsNullOrEmpty(this.configuration.Create.MilestoneReplaceText))
{
footerContent = footerContent.Replace(this.configuration.Create.MilestoneReplaceText, this.milestoneTitle);
}
}
stringBuilder.Append(footerContent);
}
private void LoadMilestones()
{
this.milestones = this.gitHubClient.GetMilestones();
}
private async Task<List<Issue>> GetIssues(Milestone milestone)
{
var issues = await this.gitHubClient.GetIssues(milestone);
foreach (var issue in issues)
{
this.CheckForValidLabels(issue);
}
return issues;
}
private void GetTargetMilestone()
{
this.targetMilestone = this.milestones.FirstOrDefault(x => x.Title == this.milestoneTitle);
if (this.targetMilestone == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Could not find milestone for '{0}'.", this.milestoneTitle));
}
}
}
}