Skip to content

Commit 899d483

Browse files
committed
(GH-21) Began work of including config for labels
- Refactored Options to include base options for LogFilePath, and TargetPath - Split other options so milestone isn't expected for all - Added some additional tests to cover issue not including correct label - Added configuration options for labels to include and exclude - Configured logging for Command Line Application
1 parent 8ad1b0a commit 899d483

16 files changed

+245
-86
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// <copyright file="BaseGitHubSubConfig.cs" company="gep13">
2+
// Copyright (c) gep13. All rights reserved.
3+
// </copyright>
4+
//-----------------------------------------------------------------------
5+
6+
namespace GitHubReleaseManager.Cli
7+
{
8+
using CommandLine;
9+
using Octokit;
10+
11+
public abstract class BaseGitHubSubConfig : BaseSubOptions
12+
{
13+
[Option('u', "username", HelpText = "The username to access GitHub with.", Required = true)]
14+
public string UserName { get; set; }
15+
16+
[Option('p', "password", HelpText = "The password to access GitHub with.", Required = true)]
17+
public string Password { get; set; }
18+
19+
[Option('o', "owner", HelpText = "The owner of the repository.", Required = true)]
20+
public string RepositoryOwner { get; set; }
21+
22+
[Option('r', "repository", HelpText = "The name of the repository.", Required = true)]
23+
public string RepositoryName { get; set; }
24+
25+
public GitHubClient CreateGitHubClient()
26+
{
27+
var creds = new Credentials(this.UserName, this.Password);
28+
var github = new GitHubClient(new ProductHeaderValue("GitHubReleaseManager")) { Credentials = creds };
29+
return github;
30+
}
31+
}
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="BaseSubOptions.cs" company="gep13">
3+
// Copyright (c) gep13. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace GitHubReleaseManager.Cli
8+
{
9+
using CommandLine;
10+
11+
public abstract class BaseSubOptions
12+
{
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; }
15+
16+
[Option('l', "logFilePath", HelpText = "Path to where log file should be created. Defaults to logging to console.", Required = false)]
17+
public string LogFilePath { get; set; }
18+
}
19+
}

Source/GitHubReleaseManager.Cli/CommonSubOptions.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,10 @@
77
namespace GitHubReleaseManager.Cli
88
{
99
using CommandLine;
10-
using Octokit;
1110

12-
public abstract class CommonSubOptions
11+
public abstract class CommonSubOptions : BaseGitHubSubConfig
1312
{
14-
[Option('u', "username", HelpText = "The username to access GitHub with.", Required = true)]
15-
public string UserName { get; set; }
16-
[Option('p', "password", HelpText = "The password to access GitHub with.", Required = true)]
17-
public string Password { get; set; }
18-
[Option('o', "owner", HelpText = "The owner of the repository.", Required = true)]
19-
public string RepositoryOwner { get; set; }
20-
[Option('r', "repository", HelpText = "The name of the repository.", Required = true)]
21-
public string RepositoryName { get; set; }
2213
[Option('m', "milestone", HelpText = "The milestone to use.", Required = true)]
2314
public string Milestone { get; set; }
24-
25-
public GitHubClient CreateGitHubClient()
26-
{
27-
var creds = new Credentials(this.UserName, this.Password);
28-
var github = new GitHubClient(new ProductHeaderValue("GitHubReleaseManager")) { Credentials = creds };
29-
return github;
30-
}
3115
}
3216
}

Source/GitHubReleaseManager.Cli/ExportSubOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace GitHubReleaseManager.Cli
88
{
99
using CommandLine;
1010

11-
public class ExportSubOptions : CommonSubOptions
11+
public class ExportSubOptions : BaseGitHubSubConfig
1212
{
1313
[Option('f', "fileOutputPath", HelpText = "Path to the file export releases.", Required = true)]
1414
public string FileOutputPath { get; set; }

Source/GitHubReleaseManager.Cli/GitHubReleaseManager.Cli.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
</ItemGroup>
7676
<ItemGroup>
7777
<Compile Include="AssemblyInfo.cs" />
78+
<Compile Include="BaseGitHubSubConfig.cs" />
79+
<Compile Include="BaseSubOptions.cs" />
7880
<Compile Include="CloseSubOptions.cs" />
7981
<Compile Include="CommonSubOptions.cs" />
8082
<Compile Include="CreateSubOptions.cs" />

Source/GitHubReleaseManager.Cli/InitSubOptions.cs

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

77
namespace GitHubReleaseManager.Cli
88
{
9-
public class InitSubOptions
9+
using CommandLine;
10+
using CommandLine.Text;
11+
12+
public class InitSubOptions : BaseSubOptions
1013
{
14+
[HelpVerbOption]
15+
public string GetUsage(string verb)
16+
{
17+
return HelpText.AutoBuild(this, verb);
18+
}
1119
}
1220
}

Source/GitHubReleaseManager.Cli/Program.cs

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
namespace GitHubReleaseManager.Cli
88
{
99
using System;
10+
using System.Collections.Generic;
11+
using System.Globalization;
1012
using System.IO;
1113
using System.Linq;
14+
using System.Text;
1215
using System.Threading.Tasks;
1316
using CommandLine;
1417
using GitHubReleaseManager.Configuration;
@@ -18,6 +21,8 @@ namespace GitHubReleaseManager.Cli
1821

1922
public static class Program
2023
{
24+
private static StringBuilder log = new StringBuilder();
25+
2126
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Not required")]
2227
private static int Main(string[] args)
2328
{
@@ -30,13 +35,21 @@ private static int Main(string[] args)
3035
options,
3136
(verb, subOptions) =>
3237
{
38+
if (subOptions != null)
39+
{
40+
if (string.IsNullOrEmpty(((BaseSubOptions)subOptions).TargetPath))
41+
{
42+
((BaseSubOptions)subOptions).TargetPath = Environment.CurrentDirectory;
43+
}
44+
45+
ConfigureLogging(((BaseSubOptions)subOptions).LogFilePath);
46+
}
47+
3348
var fileSystem = new FileSystem();
34-
var currentFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
35-
var currentDirectory = Path.GetDirectoryName(currentFilePath);
3649

3750
if (verb == "create")
3851
{
39-
result = CreateReleaseAsync((CreateSubOptions)subOptions).Result;
52+
result = CreateReleaseAsync((CreateSubOptions)subOptions, fileSystem).Result;
4053
}
4154

4255
if (verb == "close")
@@ -56,13 +69,13 @@ private static int Main(string[] args)
5669

5770
if (verb == "init")
5871
{
59-
ConfigurationProvider.WriteSample(currentDirectory, fileSystem);
72+
ConfigurationProvider.WriteSample(((InitSubOptions)subOptions).TargetPath, fileSystem);
6073
result = 0;
6174
}
6275

6376
if (verb == "showconfig")
6477
{
65-
Console.WriteLine(ConfigurationProvider.GetEffectiveConfigAsString(currentDirectory, fileSystem));
78+
Console.WriteLine(ConfigurationProvider.GetEffectiveConfigAsString(((ShowConfigSubOptions)subOptions).TargetPath, fileSystem));
6679
result = 0;
6780
}
6881
}))
@@ -73,13 +86,14 @@ private static int Main(string[] args)
7386
return result;
7487
}
7588

76-
private static async Task<int> CreateReleaseAsync(CreateSubOptions options)
89+
private static async Task<int> CreateReleaseAsync(CreateSubOptions subOptions, IFileSystem fileSystem)
7790
{
7891
try
7992
{
80-
var github = options.CreateGitHubClient();
93+
var github = subOptions.CreateGitHubClient();
94+
var configuration = ConfigurationProvider.Provide(subOptions.TargetPath, fileSystem);
8195

82-
await CreateRelease(github, options.RepositoryOwner, options.RepositoryName, options.Milestone, options.TargetCommitish, options.AssetPath);
96+
await CreateRelease(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone, subOptions.TargetCommitish, subOptions.AssetPath, configuration);
8397

8498
return 0;
8599
}
@@ -91,13 +105,13 @@ private static async Task<int> CreateReleaseAsync(CreateSubOptions options)
91105
}
92106
}
93107

94-
private static async Task<int> CloseMilestoneAsync(CloseSubOptions options)
108+
private static async Task<int> CloseMilestoneAsync(CloseSubOptions subOptions)
95109
{
96110
try
97111
{
98-
var github = options.CreateGitHubClient();
112+
var github = subOptions.CreateGitHubClient();
99113

100-
await CloseMilestone(github, options.RepositoryOwner, options.RepositoryName, options.Milestone);
114+
await CloseMilestone(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone);
101115

102116
return 0;
103117
}
@@ -109,15 +123,15 @@ private static async Task<int> CloseMilestoneAsync(CloseSubOptions options)
109123
}
110124
}
111125

112-
private static async Task<int> CloseAndPublishReleaseAsync(PublishSubOptions options)
126+
private static async Task<int> CloseAndPublishReleaseAsync(PublishSubOptions subOptions)
113127
{
114128
try
115129
{
116-
var github = options.CreateGitHubClient();
130+
var github = subOptions.CreateGitHubClient();
117131

118-
await CloseMilestone(github, options.RepositoryOwner, options.RepositoryName, options.Milestone);
132+
await CloseMilestone(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone);
119133

120-
await PublishRelease(github, options.RepositoryOwner, options.RepositoryName, options.Milestone);
134+
await PublishRelease(github, subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone);
121135

122136
return 0;
123137
}
@@ -129,15 +143,15 @@ private static async Task<int> CloseAndPublishReleaseAsync(PublishSubOptions opt
129143
}
130144
}
131145

132-
private static async Task<int> ExportReleasesAsync(ExportSubOptions options)
146+
private static async Task<int> ExportReleasesAsync(ExportSubOptions subOptions)
133147
{
134148
try
135149
{
136-
var github = options.CreateGitHubClient();
150+
var github = subOptions.CreateGitHubClient();
137151

138-
var releasesMarkdown = await ExportReleases(github, options.RepositoryOwner, options.RepositoryName);
152+
var releasesMarkdown = await ExportReleases(github, subOptions.RepositoryOwner, subOptions.RepositoryName);
139153

140-
using (var sw = new StreamWriter(File.Open(options.FileOutputPath, FileMode.OpenOrCreate)))
154+
using (var sw = new StreamWriter(File.Open(subOptions.FileOutputPath, FileMode.OpenOrCreate)))
141155
{
142156
sw.Write(releasesMarkdown);
143157
}
@@ -152,9 +166,9 @@ private static async Task<int> ExportReleasesAsync(ExportSubOptions options)
152166
}
153167
}
154168

155-
private static async Task CreateRelease(GitHubClient github, string owner, string repository, string milestone, string targetCommitish, string asset)
169+
private static async Task CreateRelease(GitHubClient github, string owner, string repository, string milestone, string targetCommitish, string asset, Config configuration)
156170
{
157-
var releaseNotesBuilder = new ReleaseNotesBuilder(new DefaultGitHubClient(github, owner, repository), owner, repository, milestone);
171+
var releaseNotesBuilder = new ReleaseNotesBuilder(new DefaultGitHubClient(github, owner, repository), owner, repository, milestone, configuration);
158172

159173
var result = await releaseNotesBuilder.BuildReleaseNotes();
160174

@@ -219,5 +233,48 @@ private static async Task PublishRelease(GitHubClient github, string owner, stri
219233

220234
await github.Release.Edit(owner, repository, release.Id, releaseUpdate);
221235
}
236+
237+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "This is required here")]
238+
private static void ConfigureLogging(string logFilePath)
239+
{
240+
var writeActions = new List<Action<string>>
241+
{
242+
s => log.AppendLine(s)
243+
};
244+
245+
if (logFilePath == "console")
246+
{
247+
writeActions.Add(Console.WriteLine);
248+
}
249+
else if (!string.IsNullOrEmpty(logFilePath))
250+
{
251+
try
252+
{
253+
Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
254+
if (File.Exists(logFilePath))
255+
{
256+
using (File.CreateText(logFilePath))
257+
{
258+
}
259+
}
260+
261+
writeActions.Add(x => WriteLogEntry(logFilePath, x));
262+
}
263+
catch (Exception ex)
264+
{
265+
Console.WriteLine("Failed to configure logging: " + ex.Message);
266+
}
267+
}
268+
269+
Logger.WriteInfo = s => writeActions.ForEach(a => a(s));
270+
Logger.WriteWarning = s => writeActions.ForEach(a => a(s));
271+
Logger.WriteError = s => writeActions.ForEach(a => a(s));
272+
}
273+
274+
private static void WriteLogEntry(string logFilePath, string s)
275+
{
276+
var contents = string.Format(CultureInfo.InvariantCulture, "{0}\t\t{1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), s);
277+
File.AppendAllText(logFilePath, contents);
278+
}
222279
}
223280
}

Source/GitHubReleaseManager.Cli/ShowConfigSubOptions.cs

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

77
namespace GitHubReleaseManager.Cli
88
{
9-
public class ShowConfigSubOptions
9+
public class ShowConfigSubOptions : BaseSubOptions
1010
{
1111
}
1212
}

Source/GitHubReleaseManager.Tests/ReleaseNotesBuilderIntegrationTests.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
namespace GitHubReleaseManager.Tests
88
{
9+
using System;
910
using System.Diagnostics;
1011
using GitHubReleaseManager;
12+
using GitHubReleaseManager.Configuration;
13+
using GitHubReleaseManager.Helpers;
14+
1115
using NUnit.Framework;
1216

1317
[TestFixture]
@@ -18,8 +22,11 @@ public class ReleaseNotesBuilderIntegrationTests
1822
public async void SingleMilestone()
1923
{
2024
var gitHubClient = ClientBuilder.Build();
25+
var fileSystem = new FileSystem();
26+
var currentDirectory = Environment.CurrentDirectory;
27+
var configuration = ConfigurationProvider.Provide(currentDirectory, fileSystem);
2128

22-
var releaseNotesBuilder = new ReleaseNotesBuilder(new DefaultGitHubClient(gitHubClient, "Particular", "NServiceBus"), "Particular", "NServiceBus", "5.1.0");
29+
var releaseNotesBuilder = new ReleaseNotesBuilder(new DefaultGitHubClient(gitHubClient, "Particular", "NServiceBus"), "Particular", "NServiceBus", "5.1.0", configuration);
2330
var result = await releaseNotesBuilder.BuildReleaseNotes();
2431
Debug.WriteLine(result);
2532
ClipBoardHelper.SetClipboard(result);
@@ -30,8 +37,11 @@ public async void SingleMilestone()
3037
public async void SingleMilestone3()
3138
{
3239
var gitHubClient = ClientBuilder.Build();
40+
var fileSystem = new FileSystem();
41+
var currentDirectory = Environment.CurrentDirectory;
42+
var configuration = ConfigurationProvider.Provide(currentDirectory, fileSystem);
3343

34-
var releaseNotesBuilder = new ReleaseNotesBuilder(new DefaultGitHubClient(gitHubClient, "Particular", "ServiceControl"), "Particular", "ServiceControl", "1.0.0-Beta4");
44+
var releaseNotesBuilder = new ReleaseNotesBuilder(new DefaultGitHubClient(gitHubClient, "Particular", "ServiceControl"), "Particular", "ServiceControl", "1.0.0-Beta4", configuration);
3545
var result = await releaseNotesBuilder.BuildReleaseNotes();
3646
Debug.WriteLine(result);
3747
ClipBoardHelper.SetClipboard(result);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
As part of this release we had [3 issues](https://github.com/FakeRepo/issues/issues?milestone=0&state=closed) closed.
22

33

4+
__Bug__
5+
6+
- [__#1__](http://example.com/1) Issue 1
7+
48
__Feature__
59

610
- [__#2__](http://example.com/2) Issue 2
@@ -9,9 +13,5 @@ __Improvement__
913

1014
- [__#3__](http://example.com/3) Issue 3
1115

12-
__Bug__
13-
14-
- [__#1__](http://example.com/1) Issue 1
15-
1616
### Where to get it
1717
You can download this release from [chocolatey](https://chocolatey.org/packages/ChocolateyGUI/1.2.3)

0 commit comments

Comments
 (0)