Skip to content

Commit 1653715

Browse files
committed
(GH-48) Added ability to provide input file
- Rather than create release notes from an existing milestone, instead, use an input file as the source of the description for the release on GitHub
1 parent 01fa391 commit 1653715

File tree

8 files changed

+143
-94
lines changed

8 files changed

+143
-94
lines changed

Source/GitHubReleaseManager.Cli/GitHubReleaseManager.Cli.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
<Compile Include="Options\BaseGitHubSubOptions.cs" />
8181
<Compile Include="Options\BaseSubOptions.cs" />
8282
<Compile Include="Options\CloseSubOptions.cs" />
83-
<Compile Include="Options\CommonSubOptions.cs" />
8483
<Compile Include="Options\CreateSubOptions.cs" />
8584
<Compile Include="Options\ExportSubOptions.cs" />
8685
<Compile Include="Options\InitSubOptions.cs" />

Source/GitHubReleaseManager.Cli/Options/AddAssetSubOptions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ namespace GitHubReleaseManager.Cli.Options
1010

1111
using CommandLine;
1212

13-
public class AddAssetSubOptions : CommonSubOptions
13+
public class AddAssetSubOptions : BaseGitHubSubOptions
1414
{
1515
[OptionList('a', "assets", Separator = ',', HelpText = "Paths to the files to include in the release.", Required = true)]
1616
public IList<string> AssetPaths { get; set; }
17+
18+
[Option('t', "tagName", HelpText = "The name of the release (Typically this is the generated SemVer Version Number).", Required = true)]
19+
public string TagName { get; set; }
1720
}
1821
}

Source/GitHubReleaseManager.Cli/Options/BaseSubOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace GitHubReleaseManager.Cli.Options
1010

1111
public abstract class BaseSubOptions
1212
{
13-
[Option('t', "targetPath", HelpText = "The directory on which GitHubReleaseManager should be executed. Defaults to current directory.", Required = false)]
14-
public string TargetPath { get; set; }
13+
[Option('d', "targetDirectory", HelpText = "The directory on which GitHubReleaseManager should be executed. Defaults to current directory.", Required = false)]
14+
public string TargetDirectory { get; set; }
1515

1616
[Option('l', "logFilePath", HelpText = "Path to where log file should be created. Defaults to logging to console.", Required = false)]
1717
public string LogFilePath { get; set; }

Source/GitHubReleaseManager.Cli/Options/CloseSubOptions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
namespace GitHubReleaseManager.Cli.Options
88
{
9-
public class CloseSubOptions : CommonSubOptions
9+
using CommandLine;
10+
11+
public class CloseSubOptions : BaseGitHubSubOptions
1012
{
13+
[Option('m', "milestone", HelpText = "The milestone to use.", Required = true)]
14+
public string Milestone { get; set; }
1115
}
1216
}

Source/GitHubReleaseManager.Cli/Options/CommonSubOptions.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

Source/GitHubReleaseManager.Cli/Options/CreateSubOptions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ namespace GitHubReleaseManager.Cli.Options
1010

1111
using CommandLine;
1212

13-
public class CreateSubOptions : CommonSubOptions
13+
public class CreateSubOptions : BaseGitHubSubOptions
1414
{
1515
[OptionList('a', "assets", Separator = ',', HelpText = "Paths to the files to include in the release.", Required = false)]
1616
public IList<string> AssetPaths { get; set; }
1717

1818
[Option('c', "targetcommitish", HelpText = "The commit to tag. Can be a branch or SHA. Defaults to repository's default branch.", Required = false)]
1919
public string TargetCommitish { get; set; }
20+
21+
[Option('m', "milestone", HelpText = "The milestone to use.", Required = false)]
22+
public string Milestone { get; set; }
23+
24+
[Option('n', "name", HelpText = "The name of the release (Typically this is the generated SemVer Version Number.", Required = false)]
25+
public string Name { get; set; }
26+
27+
[Option('i', "inputFilePath", HelpText = "The path to the file to be used as the content of the release notes.", Required = false)]
28+
public string InputFilePath { get; set; }
2029
}
2130
}

Source/GitHubReleaseManager.Cli/Options/PublishSubOptions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
namespace GitHubReleaseManager.Cli.Options
88
{
9-
public class PublishSubOptions : CommonSubOptions
9+
using CommandLine;
10+
11+
public class PublishSubOptions : BaseGitHubSubOptions
1012
{
13+
[Option('t', "tagName", HelpText = "The name of the release (Typically this is the generated SemVer Version Number).", Required = true)]
14+
public string TagName { get; set; }
1115
}
1216
}

Source/GitHubReleaseManager.Cli/Program.cs

Lines changed: 117 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -35,87 +35,87 @@ private static int Main(string[] args)
3535
args,
3636
options,
3737
(verb, subOptions) =>
38-
{
39-
result = 1;
38+
{
39+
result = 1;
4040

41-
var baseSubOptions = subOptions as BaseSubOptions;
42-
if (baseSubOptions != null)
41+
var baseSubOptions = subOptions as BaseSubOptions;
42+
if (baseSubOptions != null)
43+
{
44+
if (string.IsNullOrEmpty(baseSubOptions.TargetDirectory))
4345
{
44-
if (string.IsNullOrEmpty(baseSubOptions.TargetPath))
45-
{
46-
baseSubOptions.TargetPath = Environment.CurrentDirectory;
47-
}
48-
49-
ConfigureLogging(baseSubOptions.LogFilePath);
46+
baseSubOptions.TargetDirectory = Environment.CurrentDirectory;
5047
}
5148

52-
var fileSystem = new FileSystem();
49+
ConfigureLogging(baseSubOptions.LogFilePath);
50+
}
51+
52+
var fileSystem = new FileSystem();
5353

54-
if (verb == "create")
54+
if (verb == "create")
55+
{
56+
var createSubOptions = baseSubOptions as CreateSubOptions;
57+
if (createSubOptions != null)
5558
{
56-
var createSubOptions = baseSubOptions as CreateSubOptions;
57-
if (createSubOptions != null)
58-
{
59-
result = CreateReleaseAsync(createSubOptions, fileSystem).Result;
60-
}
59+
result = CreateReleaseAsync(createSubOptions, fileSystem).Result;
6160
}
61+
}
6262

63-
if (verb == "addasset")
63+
if (verb == "addasset")
64+
{
65+
var addAssetSubOptions = baseSubOptions as AddAssetSubOptions;
66+
if (addAssetSubOptions != null)
6467
{
65-
var addAssetSubOptions = baseSubOptions as AddAssetSubOptions;
66-
if (addAssetSubOptions != null)
67-
{
68-
result = AddAssetsAsync(addAssetSubOptions).Result;
69-
}
68+
result = AddAssetsAsync(addAssetSubOptions).Result;
7069
}
70+
}
7171

72-
if (verb == "close")
72+
if (verb == "close")
73+
{
74+
var closeSubOptions = baseSubOptions as CloseSubOptions;
75+
if (closeSubOptions != null)
7376
{
74-
var closeSubOptions = baseSubOptions as CloseSubOptions;
75-
if (closeSubOptions != null)
76-
{
77-
result = CloseMilestoneAsync(closeSubOptions).Result;
78-
}
77+
result = CloseMilestoneAsync(closeSubOptions).Result;
7978
}
79+
}
8080

81-
if (verb == "publish")
81+
if (verb == "publish")
82+
{
83+
var publishSubOptions = baseSubOptions as PublishSubOptions;
84+
if (publishSubOptions != null)
8285
{
83-
var publishSubOptions = baseSubOptions as PublishSubOptions;
84-
if (publishSubOptions != null)
85-
{
86-
result = CloseAndPublishReleaseAsync(publishSubOptions).Result;
87-
}
86+
result = PublishReleaseAsync(publishSubOptions).Result;
8887
}
88+
}
8989

90-
if (verb == "export")
90+
if (verb == "export")
91+
{
92+
var exportSubOptions = baseSubOptions as ExportSubOptions;
93+
if (exportSubOptions != null)
9194
{
92-
var exportSubOptions = baseSubOptions as ExportSubOptions;
93-
if (exportSubOptions != null)
94-
{
95-
result = ExportReleasesAsync(exportSubOptions, fileSystem).Result;
96-
}
95+
result = ExportReleasesAsync(exportSubOptions, fileSystem).Result;
9796
}
97+
}
9898

99-
if (verb == "init")
99+
if (verb == "init")
100+
{
101+
var initSubOptions = baseSubOptions as InitSubOptions;
102+
if (initSubOptions != null)
100103
{
101-
var initSubOptions = baseSubOptions as InitSubOptions;
102-
if (initSubOptions != null)
103-
{
104-
ConfigurationProvider.WriteSample(initSubOptions.TargetPath, fileSystem);
105-
result = 0;
106-
}
104+
ConfigurationProvider.WriteSample(initSubOptions.TargetDirectory, fileSystem);
105+
result = 0;
107106
}
107+
}
108108

109-
if (verb == "showconfig")
109+
if (verb == "showconfig")
110+
{
111+
var showConfigSubOptions = baseSubOptions as ShowConfigSubOptions;
112+
if (showConfigSubOptions != null)
110113
{
111-
var showConfigSubOptions = baseSubOptions as ShowConfigSubOptions;
112-
if (showConfigSubOptions != null)
113-
{
114-
Console.WriteLine(ConfigurationProvider.GetEffectiveConfigAsString(showConfigSubOptions.TargetPath, fileSystem));
115-
result = 0;
116-
}
114+
Console.WriteLine(ConfigurationProvider.GetEffectiveConfigAsString(showConfigSubOptions.TargetDirectory, fileSystem));
115+
result = 0;
117116
}
118-
}))
117+
}
118+
}))
119119
{
120120
return 1;
121121
}
@@ -128,9 +128,16 @@ private static async Task<int> CreateReleaseAsync(CreateSubOptions subOptions, I
128128
try
129129
{
130130
var github = subOptions.CreateGitHubClient();
131-
var configuration = ConfigurationProvider.Provide(subOptions.TargetPath, fileSystem);
131+
var configuration = ConfigurationProvider.Provide(subOptions.TargetDirectory, fileSystem);
132132

133-
await CreateRelease(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone, subOptions.TargetCommitish, subOptions.AssetPaths, configuration);
133+
if (string.IsNullOrEmpty(subOptions.Milestone))
134+
{
135+
await CreateReleaseFromMilestone(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone, subOptions.TargetCommitish, subOptions.AssetPaths, configuration);
136+
}
137+
else
138+
{
139+
await CreateReleaseFromInputFile(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Name, subOptions.InputFilePath, subOptions.TargetCommitish, subOptions.AssetPaths, configuration);
140+
}
134141

135142
return 0;
136143
}
@@ -148,7 +155,7 @@ private static async Task<int> AddAssetsAsync(AddAssetSubOptions subOptions)
148155
{
149156
var github = subOptions.CreateGitHubClient();
150157

151-
await AddAssets(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone, subOptions.AssetPaths);
158+
await AddAssets(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.TagName, subOptions.AssetPaths);
152159

153160
return 0;
154161
}
@@ -178,15 +185,13 @@ private static async Task<int> CloseMilestoneAsync(CloseSubOptions subOptions)
178185
}
179186
}
180187

181-
private static async Task<int> CloseAndPublishReleaseAsync(PublishSubOptions subOptions)
188+
private static async Task<int> PublishReleaseAsync(PublishSubOptions subOptions)
182189
{
183190
try
184191
{
185192
var github = subOptions.CreateGitHubClient();
186193

187-
await CloseMilestone(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone);
188-
189-
await PublishRelease(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone);
194+
await PublishRelease(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.TagName);
190195

191196
return 0;
192197
}
@@ -203,7 +208,7 @@ private static async Task<int> ExportReleasesAsync(ExportSubOptions subOptions,
203208
try
204209
{
205210
var github = subOptions.CreateGitHubClient();
206-
var configuration = ConfigurationProvider.Provide(subOptions.TargetPath, fileSystem);
211+
var configuration = ConfigurationProvider.Provide(subOptions.TargetDirectory, fileSystem);
207212

208213
var releasesMarkdown = await ExportReleases(github, subOptions.RepositoryOwner, subOptions.RepositoryName, configuration);
209214

@@ -222,7 +227,7 @@ private static async Task<int> ExportReleasesAsync(ExportSubOptions subOptions,
222227
}
223228
}
224229

225-
private static async Task CreateRelease(GitHubClient github, string owner, string repository, string milestone, string targetCommitish, IList<string> assets, Config configuration)
230+
private static async Task CreateReleaseFromMilestone(GitHubClient github, string owner, string repository, string milestone, string targetCommitish, IList<string> assets, Config configuration)
226231
{
227232
var releaseNotesBuilder = new ReleaseNotesBuilder(new DefaultGitHubClient(github, owner, repository), owner, repository, milestone, configuration);
228233

@@ -260,15 +265,56 @@ private static async Task CreateRelease(GitHubClient github, string owner, strin
260265
}
261266
}
262267

263-
private static async Task AddAssets(GitHubClient github, string owner, string repository, string milestone, IList<string> assetPaths)
268+
private static async Task CreateReleaseFromInputFile(GitHubClient github, string owner, string repository, string name, string inputFilePath, string targetCommitish, IList<string> assets, Config configuration)
269+
{
270+
if (!File.Exists(inputFilePath))
271+
{
272+
throw new ArgumentException("Unable to locate input file.");
273+
}
274+
275+
var inputFileContents = File.ReadAllText(inputFilePath);
276+
277+
var releaseUpdate = new ReleaseUpdate(name)
278+
{
279+
Draft = true,
280+
Body = inputFileContents,
281+
Name = name
282+
};
283+
284+
if (!string.IsNullOrEmpty(targetCommitish))
285+
{
286+
releaseUpdate.TargetCommitish = targetCommitish;
287+
}
288+
289+
var release = await github.Release.Create(owner, repository, releaseUpdate);
290+
291+
foreach (var asset in assets)
292+
{
293+
if (!File.Exists(asset))
294+
{
295+
continue;
296+
}
297+
298+
var upload = new ReleaseAssetUpload
299+
{
300+
FileName = Path.GetFileName(asset),
301+
ContentType = "application/octet-stream",
302+
RawData = File.Open(asset, FileMode.Open)
303+
};
304+
305+
await github.Release.UploadAsset(release, upload);
306+
}
307+
}
308+
309+
private static async Task AddAssets(GitHubClient github, string owner, string repository, string tagName, IList<string> assetPaths)
264310
{
265311
var releases = await github.Release.GetAll(owner, repository);
266312

267-
var release = releases.FirstOrDefault(r => r.TagName == milestone);
313+
var release = releases.FirstOrDefault(r => r.TagName == tagName);
268314

269315
if (release == null)
270316
{
271-
Logger.WriteError("Unable to find Release with specified milestone");
317+
Logger.WriteError("Unable to find Release with specified tagName");
272318
return;
273319
}
274320

@@ -312,17 +358,17 @@ private static async Task CloseMilestone(GitHubClient github, string owner, stri
312358
await milestoneClient.Update(owner, repository, milestone.Number, new MilestoneUpdate { State = ItemState.Closed });
313359
}
314360

315-
private static async Task PublishRelease(GitHubClient github, string owner, string repository, string milestone)
361+
private static async Task PublishRelease(GitHubClient github, string owner, string repository, string tagName)
316362
{
317363
var releases = await github.Release.GetAll(owner, repository);
318-
var release = releases.FirstOrDefault(r => r.Name == milestone);
364+
var release = releases.FirstOrDefault(r => r.TagName == tagName);
319365

320366
if (release == null)
321367
{
322368
return;
323369
}
324370

325-
var releaseUpdate = new ReleaseUpdate(milestone)
371+
var releaseUpdate = new ReleaseUpdate(tagName)
326372
{
327373
Draft = false
328374
};

0 commit comments

Comments
 (0)