This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathBitbucketCommitWorder.cs
239 lines (192 loc) · 8.13 KB
/
BitbucketCommitWorder.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NuGet.Packaging.Core;
using NuGet.Versioning;
using NuKeeper.Abstractions.CollaborationPlatform;
using NuKeeper.Abstractions.Formats;
using NuKeeper.Abstractions.RepositoryInspection;
namespace NuKeeper.BitBucket
{
public class BitbucketCommitWorder : ICommitWorder
{
private const string CommitEmoji = "📦";
public string MakePullRequestTitle(IReadOnlyCollection<PackageUpdateSet> updates)
{
if (updates == null)
{
throw new ArgumentNullException(nameof(updates));
}
if (updates.Count == 1)
{
return PackageTitle(updates.First());
}
return $"Automatic update of {updates.Count} packages";
}
private static string PackageTitle(PackageUpdateSet updates)
{
return $"Automatic update of {updates.SelectedId} to {updates.SelectedVersion}";
}
public string MakeCommitMessage(PackageUpdateSet updates)
{
if (updates == null)
{
throw new ArgumentNullException(nameof(updates));
}
return $":{CommitEmoji}: {PackageTitle(updates)}";
}
public string MakeCommitDetails(IReadOnlyCollection<PackageUpdateSet> updates)
{
if (updates == null)
{
throw new ArgumentNullException(nameof(updates));
}
var builder = new StringBuilder();
if (updates.Count > 1)
{
MultiPackagePrefix(updates, builder);
}
foreach (var update in updates)
{
builder.AppendLine(MakeCommitVersionDetails(update));
}
AddCommitFooter(builder);
return builder.ToString();
}
private static void MultiPackagePrefix(IReadOnlyCollection<PackageUpdateSet> updates, StringBuilder builder)
{
var packageNames = updates
.Select(p => CodeQuote(p.SelectedId))
.JoinWithCommas();
var projects = updates.SelectMany(
u => u.CurrentPackages)
.Select(p => p.Path.FullName)
.Distinct()
.ToList();
var projectOptS = (projects.Count > 1) ? "s" : string.Empty;
builder.AppendLine($"{updates.Count} packages were updated in {projects.Count} project{projectOptS}:");
builder.AppendLine(packageNames);
builder.AppendLine("");
builder.AppendLine("**Details of updated packages**");
builder.AppendLine("");
}
private static string MakeCommitVersionDetails(PackageUpdateSet updates)
{
var versionsInUse = updates.CurrentPackages
.Select(u => u.Version)
.Distinct()
.ToList();
var oldVersions = versionsInUse
.Select(v => CodeQuote(v.ToString()))
.ToList();
var minOldVersion = versionsInUse.Min();
var newVersion = CodeQuote(updates.SelectedVersion.ToString());
var packageId = CodeQuote(updates.SelectedId);
var changeLevel = ChangeLevel(minOldVersion, updates.SelectedVersion);
var builder = new StringBuilder();
if (oldVersions.Count == 1)
{
builder.AppendLine($"NuKeeper has generated a {changeLevel} update of {packageId} to {newVersion} from {oldVersions.JoinWithCommas()}");
}
else
{
builder.AppendLine($"NuKeeper has generated a {changeLevel} update of {packageId} to {newVersion}");
builder.AppendLine($"{oldVersions.Count} versions of {packageId} were found in use: {oldVersions.JoinWithCommas()}");
}
if (updates.Selected.Published.HasValue)
{
var packageWithVersion = CodeQuote(updates.SelectedId + " " + updates.SelectedVersion);
var pubDateString = CodeQuote(DateFormat.AsUtcIso8601(updates.Selected.Published));
var pubDate = updates.Selected.Published.Value.UtcDateTime;
var ago = TimeSpanFormat.Ago(pubDate, DateTime.UtcNow);
builder.AppendLine($"{packageWithVersion} was published at {pubDateString}, {ago}");
}
var highestVersion = updates.Packages.Major?.Identity.Version;
if (highestVersion != null && (highestVersion > updates.SelectedVersion))
{
LogHighestVersion(updates, highestVersion, builder);
}
builder.AppendLine();
if (updates.CurrentPackages.Count == 1)
{
builder.AppendLine("1 project update:");
}
else
{
builder.AppendLine($"{updates.CurrentPackages.Count} project updates:");
}
foreach (var current in updates.CurrentPackages)
{
var line = $"Updated {CodeQuote(current.Path.RelativePath)} to {packageId} {CodeQuote(updates.SelectedVersion.ToString())} from {CodeQuote(current.Version.ToString())}";
builder.AppendLine(line);
}
if (SourceIsPublicNuget(updates.Selected.Source.SourceUri))
{
builder.AppendLine(NugetPackageLink(updates.Selected.Identity));
}
return builder.ToString();
}
private static void AddCommitFooter(StringBuilder builder)
{
builder.AppendLine();
builder.AppendLine("This is an automated update. Merge only if it passes tests");
builder.AppendLine("**NuKeeper**: https://github.com/NuKeeperDotNet/NuKeeper");
}
private static string ChangeLevel(NuGetVersion oldVersion, NuGetVersion newVersion)
{
if (newVersion.Major > oldVersion.Major)
{
return "major";
}
if (newVersion.Minor > oldVersion.Minor)
{
return "minor";
}
if (newVersion.Patch > oldVersion.Patch)
{
return "patch";
}
if (!newVersion.IsPrerelease && oldVersion.IsPrerelease)
{
return "out of beta";
}
return string.Empty;
}
private static void LogHighestVersion(PackageUpdateSet updates, NuGetVersion highestVersion, StringBuilder builder)
{
var allowedChange = CodeQuote(updates.AllowedChange.ToString());
var highest = CodeQuote(updates.SelectedId + " " + highestVersion);
var highestPublishedAt = HighestPublishedAt(updates.Packages.Major.Published);
builder.AppendLine(
$"There is also a higher version, {highest}{highestPublishedAt}, " +
$"but this was not applied as only {allowedChange} version changes are allowed.");
}
private static string HighestPublishedAt(DateTimeOffset? highestPublishedAt)
{
if (!highestPublishedAt.HasValue)
{
return string.Empty;
}
var highestPubDate = highestPublishedAt.Value;
var formattedPubDate = CodeQuote(DateFormat.AsUtcIso8601(highestPubDate));
var highestAgo = TimeSpanFormat.Ago(highestPubDate.UtcDateTime, DateTime.UtcNow);
return $" published at {formattedPubDate}, {highestAgo}";
}
private static string CodeQuote(string value)
{
return "`" + value + "`";
}
private static bool SourceIsPublicNuget(Uri sourceUrl)
{
return
sourceUrl != null &&
sourceUrl.ToString().StartsWith("https://api.nuget.org/", StringComparison.OrdinalIgnoreCase);
}
private static string NugetPackageLink(PackageIdentity package)
{
var url = $"https://www.nuget.org/packages/{package.Id}/{package.Version}";
return $"[{package.Id} {package.Version} on NuGet.org]({url})";
}
}
}