-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathProgram.cs
357 lines (284 loc) · 13.7 KB
/
Program.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="GitTools Contributors">
// Copyright (c) 2015 - Present - GitTools Contributors
// </copyright>
//-----------------------------------------------------------------------
namespace GitReleaseManager.Cli
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using CommandLine;
using GitReleaseManager.Cli.Options;
using GitReleaseManager.Core;
using GitReleaseManager.Core.Configuration;
using GitReleaseManager.Core.Helpers;
using Octokit;
using FileMode = System.IO.FileMode;
public static class Program
{
private static StringBuilder log = new StringBuilder();
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Not required")]
private static int Main(string[] args)
{
// Just add the TLS 1.2 protocol to the Service Point manager until
// we've upgraded to latest Octokit.
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
var fileSystem = new FileSystem();
return Parser.Default.ParseArguments<CreateSubOptions, AddAssetSubOptions, CloseSubOptions, PublishSubOptions, ExportSubOptions, InitSubOptions, ShowConfigSubOptions>(args)
.MapResult(
(CreateSubOptions opts) => CreateReleaseAsync(opts, fileSystem).Result,
(AddAssetSubOptions opts) => AddAssetsAsync(opts).Result,
(CloseSubOptions opts) => CloseMilestoneAsync(opts).Result,
(PublishSubOptions opts) => PublishReleaseAsync(opts).Result,
(ExportSubOptions opts) => ExportReleasesAsync(opts, fileSystem).Result,
(InitSubOptions opts) => CreateSampleConfigFile(opts, fileSystem),
(ShowConfigSubOptions opts) => ShowConfig(opts, fileSystem),
errs => 1);
}
private static async Task<int> CreateReleaseAsync(CreateSubOptions subOptions, IFileSystem fileSystem)
{
try
{
ConfigureLogging(subOptions.LogFilePath);
var github = subOptions.CreateGitHubClient();
var configuration = ConfigurationProvider.Provide(subOptions.TargetDirectory ?? Environment.CurrentDirectory, fileSystem);
Release release;
if (!string.IsNullOrEmpty(subOptions.Milestone))
{
release = await CreateReleaseFromMilestone(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone, subOptions.TargetCommitish, subOptions.AssetPaths, subOptions.Prerelease, configuration);
}
else
{
release = await CreateReleaseFromInputFile(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Name, subOptions.InputFilePath, subOptions.TargetCommitish, subOptions.AssetPaths, subOptions.Prerelease);
}
Console.WriteLine(release.HtmlUrl);
return 0;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return 1;
}
}
private static async Task<int> AddAssetsAsync(AddAssetSubOptions subOptions)
{
try
{
ConfigureLogging(subOptions.LogFilePath);
var github = subOptions.CreateGitHubClient();
await AddAssets(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.TagName, subOptions.AssetPaths);
return 0;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return 1;
}
}
private static async Task<int> CloseMilestoneAsync(CloseSubOptions subOptions)
{
try
{
ConfigureLogging(subOptions.LogFilePath);
var github = subOptions.CreateGitHubClient();
await CloseMilestone(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone);
return 0;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return 1;
}
}
private static async Task<int> PublishReleaseAsync(PublishSubOptions subOptions)
{
try
{
ConfigureLogging(subOptions.LogFilePath);
var github = subOptions.CreateGitHubClient();
await PublishRelease(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.TagName);
return 0;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return 1;
}
}
private static async Task<int> ExportReleasesAsync(ExportSubOptions subOptions, IFileSystem fileSystem)
{
try
{
ConfigureLogging(subOptions.LogFilePath);
var github = subOptions.CreateGitHubClient();
var configuration = ConfigurationProvider.Provide(subOptions.TargetDirectory ?? Environment.CurrentDirectory, fileSystem);
var releasesMarkdown = await ExportReleases(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.TagName, configuration);
using (var sw = new StreamWriter(File.Open(subOptions.FileOutputPath, FileMode.OpenOrCreate)))
{
sw.Write(releasesMarkdown);
}
return 0;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return 1;
}
}
private static int CreateSampleConfigFile(InitSubOptions subOptions, IFileSystem fileSystem)
{
ConfigureLogging(subOptions.LogFilePath);
ConfigurationProvider.WriteSample(subOptions.TargetDirectory ?? Environment.CurrentDirectory, fileSystem);
return 0;
}
private static int ShowConfig(ShowConfigSubOptions subOptions, IFileSystem fileSystem)
{
ConfigureLogging(subOptions.LogFilePath);
Console.WriteLine(ConfigurationProvider.GetEffectiveConfigAsString(subOptions.TargetDirectory ?? Environment.CurrentDirectory, fileSystem));
return 0;
}
private static async Task<Release> CreateReleaseFromMilestone(GitHubClient github, string owner, string repository, string milestone, string targetCommitish, IList<string> assets, bool prerelease, Config configuration)
{
var releaseNotesBuilder = new ReleaseNotesBuilder(new DefaultGitHubClient(github, owner, repository), owner, repository, milestone, configuration);
var result = await releaseNotesBuilder.BuildReleaseNotes();
var releaseUpdate = CreateNewRelease(milestone, result, prerelease, targetCommitish);
var release = await github.Release.Create(owner, repository, releaseUpdate);
await AddAssets(github, assets, release);
return release;
}
private static async Task<Release> CreateReleaseFromInputFile(GitHubClient github, string owner, string repository, string name, string inputFilePath, string targetCommitish, IList<string> assets, bool prerelease)
{
if (!File.Exists(inputFilePath))
{
throw new ArgumentException("Unable to locate input file.");
}
var inputFileContents = File.ReadAllText(inputFilePath);
var releaseUpdate = CreateNewRelease(name, inputFileContents, prerelease, targetCommitish);
var release = await github.Release.Create(owner, repository, releaseUpdate);
await AddAssets(github, assets, release);
return release;
}
private static async Task AddAssets(GitHubClient github, string owner, string repository, string tagName, IList<string> assets)
{
var releases = await github.Release.GetAll(owner, repository);
var release = releases.FirstOrDefault(r => r.TagName == tagName);
if (release == null)
{
Logger.WriteError("Unable to find Release with specified tagName");
return;
}
await AddAssets(github, assets, release);
}
private static async Task<string> ExportReleases(GitHubClient github, string owner, string repository, string tagName, Config configuration)
{
var releaseNotesExporter = new ReleaseNotesExporter(new DefaultGitHubClient(github, owner, repository), configuration);
var result = await releaseNotesExporter.ExportReleaseNotes(tagName);
return result;
}
private static async Task CloseMilestone(GitHubClient github, string owner, string repository, string milestoneTitle)
{
var milestoneClient = github.Issue.Milestone;
var openMilestones = await milestoneClient.GetAllForRepository(owner, repository, new MilestoneRequest { State = ItemState.Open });
var milestone = openMilestones.FirstOrDefault(m => m.Title == milestoneTitle);
if (milestone == null)
{
return;
}
await milestoneClient.Update(owner, repository, milestone.Number, new MilestoneUpdate { State = ItemState.Closed });
}
private static async Task PublishRelease(GitHubClient github, string owner, string repository, string tagName)
{
var releases = await github.Release.GetAll(owner, repository);
var release = releases.FirstOrDefault(r => r.TagName == tagName);
if (release == null)
{
return;
}
var releaseUpdate = new ReleaseUpdate { TagName = tagName, Draft = false };
await github.Release.Edit(owner, repository, release.Id, releaseUpdate);
}
private static async Task AddAssets(GitHubClient github, IList<string> assets, Release release)
{
if (assets != null)
{
foreach (var asset in assets)
{
if (!File.Exists(asset))
{
continue;
}
var upload = new ReleaseAssetUpload
{
FileName = Path.GetFileName(asset),
ContentType = "application/octet-stream",
RawData = File.Open(asset, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
};
await github.Release.UploadAsset(release, upload);
// Make sure to tidy up the stream that was created above
upload.RawData.Dispose();
}
}
}
private static NewRelease CreateNewRelease(string name, string body, bool prerelease, string targetCommitish)
{
var newRelease = new NewRelease(name)
{
Draft = true,
Body = body,
Name = name,
Prerelease = prerelease
};
if (!string.IsNullOrEmpty(targetCommitish))
{
newRelease.TargetCommitish = targetCommitish;
}
return newRelease;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Not required.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "This is required here.")]
private static void ConfigureLogging(string logFilePath)
{
var writeActions = new List<Action<string>>
{
s => log.AppendLine(s)
};
if (!string.IsNullOrEmpty(logFilePath))
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
if (File.Exists(logFilePath))
{
using (File.CreateText(logFilePath))
{
}
}
writeActions.Add(x => WriteLogEntry(logFilePath, x));
}
catch (Exception ex)
{
Console.WriteLine("Failed to configure logging: " + ex.Message);
}
}
else
{
// if nothing else is specified, write to console
writeActions.Add(Console.WriteLine);
}
Logger.WriteInfo = s => writeActions.ForEach(a => a(s));
Logger.WriteWarning = s => writeActions.ForEach(a => a(s));
Logger.WriteError = s => writeActions.ForEach(a => a(s));
}
private static void WriteLogEntry(string logFilePath, string s)
{
var contents = string.Format(CultureInfo.InvariantCulture, "{0}\t\t{1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), s);
File.AppendAllText(logFilePath, contents);
}
}
}