|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
| 6 | +using System.IO; |
6 | 7 | using System.Linq; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Net.Http.Json; |
| 10 | +using System.Text; |
| 11 | +using System.Text.Json.Serialization; |
7 | 12 | using System.Threading.Tasks; |
8 | 13 | using Nuke.Common; |
| 14 | +using Nuke.Common.CI.GitHubActions; |
9 | 15 | using Nuke.Common.IO; |
10 | 16 | using Nuke.Common.Tooling; |
11 | 17 | using Nuke.Common.Tools.DotNet; |
| 18 | +using Octokit; |
12 | 19 | using Serilog; |
13 | 20 | using static Nuke.Common.Tools.DotNet.DotNetTasks; |
| 21 | +using static Nuke.Common.Tools.Git.GitTasks; |
14 | 22 |
|
15 | 23 | partial class Build |
16 | 24 | { |
@@ -126,4 +134,182 @@ DotNetNuGetPushSettings NuGetPushSettings |
126 | 134 | return ret; |
127 | 135 | } |
128 | 136 | } |
| 137 | + |
| 138 | + private void GetVersionInfo( |
| 139 | + string changelog, |
| 140 | + out string silkVersion, |
| 141 | + out string silkVersionSuffix, |
| 142 | + out string silkReleaseNotes |
| 143 | + ) |
| 144 | + { |
| 145 | + var lines = changelog.Split(["\r\n", "\r", "\n"], StringSplitOptions.None); |
| 146 | + |
| 147 | + var capturing = false; |
| 148 | + var notes = new StringBuilder(); |
| 149 | + string? version = null; |
| 150 | + string? versionSuffix = null; |
| 151 | + foreach (var theLine in lines) |
| 152 | + { |
| 153 | + var line = theLine.Trim(); |
| 154 | + if (!line.StartsWith("##")) |
| 155 | + { |
| 156 | + if (capturing) |
| 157 | + { |
| 158 | + notes.AppendLine(line); |
| 159 | + } |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + if (capturing) |
| 164 | + { |
| 165 | + break; |
| 166 | + } |
| 167 | + |
| 168 | + version = line[2..].Trim(); |
| 169 | + var versionStop = version.IndexOf(' '); |
| 170 | + if (versionStop != -1) |
| 171 | + { |
| 172 | + version = version[..versionStop]; |
| 173 | + } |
| 174 | + versionSuffix = string.Empty; |
| 175 | + var versionSuffixStart = version.IndexOf('-'); |
| 176 | + if (versionSuffixStart != -1) |
| 177 | + { |
| 178 | + versionSuffix = version[(versionSuffixStart + 1)..]; |
| 179 | + version = version[..versionSuffixStart]; |
| 180 | + } |
| 181 | + versionSuffix = versionSuffix.Trim(); |
| 182 | + version = version.Trim(); |
| 183 | + capturing = true; |
| 184 | + } |
| 185 | + |
| 186 | + if (!capturing) |
| 187 | + { |
| 188 | + throw new Exception("Failed to determine version information."); |
| 189 | + } |
| 190 | + |
| 191 | + silkVersion = version ?? "3.0.0"; |
| 192 | + silkVersionSuffix = versionSuffix ?? "unknown"; |
| 193 | + silkReleaseNotes = notes.ToString(); |
| 194 | + } |
| 195 | + |
| 196 | + private async Task CreateReleaseAsync(string version, string versionSuffix, string releaseNotes) |
| 197 | + { |
| 198 | + var tag = |
| 199 | + $"v{version}{(string.IsNullOrWhiteSpace(versionSuffix) ? string.Empty : $"-{versionSuffix}")}"; |
| 200 | + if (Environment.GetEnvironmentVariable("SILK_ACTIONS_DEPLOY_KEY") is { } deployKey) |
| 201 | + { |
| 202 | + await File.WriteAllTextAsync(TemporaryDirectory / "deploy_key.pem", deployKey); |
| 203 | + Git( |
| 204 | + $"config core.sshCommand \"ssh -i \\\"{TemporaryDirectory / "deploy_key.pem"}\\\"\"" |
| 205 | + ); |
| 206 | + } |
| 207 | + |
| 208 | + Git($"config user.email \"[email protected]\""); |
| 209 | + Git($"config user.name \"The Silk.NET Automaton\""); |
| 210 | + Git($"tag {tag}"); |
| 211 | + Git($"push origin {tag}"); |
| 212 | + var github = new GitHubClient( |
| 213 | + new ProductHeaderValue("Silk.NET-CI"), |
| 214 | + new Octokit.Internal.InMemoryCredentialStore( |
| 215 | + new Credentials(GitHubActions.Instance.Token) |
| 216 | + ) |
| 217 | + ); |
| 218 | + var lines = releaseNotes |
| 219 | + .Split(["\r\n", "\r", "\n"], StringSplitOptions.None) |
| 220 | + .SkipWhile(string.IsNullOrWhiteSpace) |
| 221 | + .ToList(); |
| 222 | + await github.Repository.Release.Create( |
| 223 | + "dotnet", |
| 224 | + "Silk.NET", |
| 225 | + new NewRelease(tag) |
| 226 | + { |
| 227 | + Prerelease = !string.IsNullOrWhiteSpace(versionSuffix), |
| 228 | + Body = releaseNotes, |
| 229 | + DiscussionCategoryName = "Q&A", |
| 230 | + Name = lines[0].Replace("Silk.NET", string.Empty).Trim(), // tradition |
| 231 | + TargetCommitish = GitCurrentCommit(), |
| 232 | + } |
| 233 | + ); |
| 234 | + } |
| 235 | + |
| 236 | + private void CommitShippedApi() |
| 237 | + { |
| 238 | + if (GitIsDetached()) |
| 239 | + { |
| 240 | + Git($"checkout {GitHubActions.Instance.RefName}"); |
| 241 | + } |
| 242 | + Git("add **/PublicAPI.*.txt"); |
| 243 | + Git("commit -m \"Update public API after release\""); |
| 244 | + } |
| 245 | + |
| 246 | + private readonly record struct VersionsList( |
| 247 | + [property: JsonPropertyName("versions")] List<string> Versions |
| 248 | + ); |
| 249 | + |
| 250 | + private async Task WaitForNuGetToUpdateAsync(string version, string versionSuffix) |
| 251 | + { |
| 252 | + var fullVersion = string.IsNullOrWhiteSpace(versionSuffix) |
| 253 | + ? version |
| 254 | + : $"{version}-{versionSuffix}"; |
| 255 | + using var http = new HttpClient(); |
| 256 | + for (var i = 0; i < 60; i++) |
| 257 | + { |
| 258 | + var versions = await ( |
| 259 | + await http.GetAsync("https://api.nuget.org/v3-flatcontainer/silk.net/index.json") |
| 260 | + ) |
| 261 | + .EnsureSuccessStatusCode() |
| 262 | + .Content.ReadFromJsonAsync<VersionsList>(); |
| 263 | + if (versions.Versions.Contains(fullVersion)) |
| 264 | + { |
| 265 | + return; |
| 266 | + } |
| 267 | + |
| 268 | + await Task.Delay(TimeSpan.FromMinutes(1)); |
| 269 | + } |
| 270 | + |
| 271 | + throw new Exception("NuGet didn't update after an hour."); |
| 272 | + } |
| 273 | + |
| 274 | + [Parameter("Discord release announcement webhook.")] |
| 275 | + readonly string? DiscordWebhook; |
| 276 | + |
| 277 | + private readonly record struct Webhook( |
| 278 | + [property: JsonPropertyName("content")] string Content, |
| 279 | + [property: JsonPropertyName("allowed_mentions")] |
| 280 | + Dictionary<string, string[]> AllowedMentions |
| 281 | + ); |
| 282 | + |
| 283 | + private async Task SendWebhookAsync(string version, string versionSuffix, string releaseNotes) |
| 284 | + { |
| 285 | + if (DiscordWebhook is null) |
| 286 | + { |
| 287 | + return; |
| 288 | + } |
| 289 | + |
| 290 | + var fullVersion = string.IsNullOrWhiteSpace(versionSuffix) |
| 291 | + ? version |
| 292 | + : $"{version}-{versionSuffix}"; |
| 293 | + var lines = releaseNotes |
| 294 | + .Trim() |
| 295 | + .Split(["\r\n", "\r", "\n"], StringSplitOptions.None) |
| 296 | + .SkipWhile(string.IsNullOrWhiteSpace) |
| 297 | + .ToList(); |
| 298 | + var headline = $"{lines[0].Trim()} (v{fullVersion})"; |
| 299 | + var message = |
| 300 | + $"**__{headline}__** <@335783158223994890>\n\n" |
| 301 | + + $"Get it on NuGet: https://nuget.org/packages/Silk.NET/{fullVersion}\n\n" |
| 302 | + + string.Join('\n', lines.Skip(1).SkipWhile(string.IsNullOrWhiteSpace)) |
| 303 | + + $"\n\n**__Get {headline} on NuGet__**"; |
| 304 | + using var http = new HttpClient(); |
| 305 | + ( |
| 306 | + await http.PostAsJsonAsync( |
| 307 | + DiscordWebhook, |
| 308 | + new Webhook( |
| 309 | + message, |
| 310 | + new Dictionary<string, string[]> { { "users", ["335783158223994890"] } } |
| 311 | + ) |
| 312 | + ) |
| 313 | + ).EnsureSuccessStatusCode(); |
| 314 | + } |
129 | 315 | } |
0 commit comments