Skip to content

Commit d30e5ef

Browse files
committed
Add support for gist token
1 parent bd7f0cf commit d30e5ef

File tree

5 files changed

+44
-84
lines changed

5 files changed

+44
-84
lines changed

src/dotnet-releaser/DevHosting/GitHubDevHosting.cs

+27-76
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@ public class GitHubDevHosting : IDevHosting
2121
private readonly ISimpleLogger _log;
2222
private readonly string _url;
2323
private readonly string _apiToken;
24+
private readonly string _gistApiToken;
2425
private readonly GitHubClient _client;
25-
26-
public GitHubDevHosting(ISimpleLogger log, DevHostingConfiguration hostingConfiguration, string apiToken, string apiTokenUsage)
26+
private readonly GitHubClient _clientGist;
27+
28+
public GitHubDevHosting(ISimpleLogger log, DevHostingConfiguration hostingConfiguration, string apiToken, string apiTokenUsage, string? gistApiToken = null)
2729
{
2830
Logger = log;
2931
Configuration = hostingConfiguration;
3032
_log = log;
3133
_url = hostingConfiguration.Base;
3234
_apiToken = apiToken;
35+
_gistApiToken = gistApiToken ?? apiToken;
3336
ApiTokenUsage = apiTokenUsage;
3437
_client = new GitHubClient(new ProductHeaderValue(nameof(ReleaserApp)), new Uri(hostingConfiguration.Api));
38+
_clientGist = new GitHubClient(new ProductHeaderValue(nameof(ReleaserApp)), new Uri(hostingConfiguration.Api));
3539
}
3640

3741
public ISimpleLogger Logger { get; }
@@ -44,7 +48,7 @@ public GitHubDevHosting(ISimpleLogger log, DevHostingConfiguration hostingConfig
4448

4549
public async Task<bool> Connect()
4650
{
47-
var tokenAuth = new Credentials(_apiToken); // NOTE: not real token
51+
var tokenAuth = new Credentials(_apiToken);
4852
_client.Credentials = tokenAuth;
4953

5054
_log.Info($"Connecting to GitHub ({ApiTokenUsage})");
@@ -57,6 +61,13 @@ public async Task<bool> Connect()
5761
_log.Error($"Unable to connect GitHub ({ApiTokenUsage}). Reason: {ex.Message}");
5862
return false;
5963
}
64+
65+
_clientGist.Credentials = tokenAuth;
66+
if (_gistApiToken != _apiToken)
67+
{
68+
_clientGist.Credentials = new Credentials(_gistApiToken);
69+
}
70+
6071
return true;
6172
}
6273

@@ -76,7 +87,17 @@ public async Task<List<ReleaseVersion>> GetAllReleaseTags(string user, string re
7687

7788
public async Task CreateOrUpdateGist(string gistId, string fileName, string content)
7889
{
79-
var gist = await _client.Gist.Get(gistId);
90+
Gist gist;
91+
try
92+
{
93+
gist = await _clientGist.Gist.Get(gistId);
94+
}
95+
catch (Exception ex)
96+
{
97+
_log.Error($"Unable to get the gist {gistId}. Reason: {ex.Message}");
98+
return;
99+
}
100+
80101
if (gist is null)
81102
{
82103
_log.Error($"The gist {gistId} for code coverage was not found");
@@ -96,81 +117,11 @@ public async Task CreateOrUpdateGist(string gistId, string fileName, string cont
96117
GistUpdate gistUpdate = new GistUpdate();
97118
//gistUpdate.Files.Add();
98119
gistUpdate.Files.Add(fileName, new GistFileUpdate() { NewFileName = fileName, Content = content });
99-
await _client.Gist.Edit(gistId, gistUpdate);
120+
await _clientGist.Gist.Edit(gistId, gistUpdate);
100121
}
101122
else
102123
{
103-
if (!gist.GitPushUrl.StartsWith("https://"))
104-
{
105-
_log.Warn($"The gist URL {gist.GitPushUrl} is not a standard gist URL and cannot be updated.");
106-
return;
107-
}
108-
109-
var uri = new Uri(gist.GitPushUrl);
110-
var gitCloneUrl = $"git@{uri.Host}:{uri.PathAndQuery.TrimStart('/')}";
111-
112-
var gistTempDirectory = Directory.CreateTempSubdirectory("dotnet-releaser-gist");
113-
try
114-
{
115-
var result = await GitRunner.Run("clone", new[] { gitCloneUrl, gistTempDirectory.FullName });
116-
117-
if (result.HasErrors)
118-
{
119-
_log.Error($"Unable to clone the gist {gistId} to {gistTempDirectory.FullName}. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
120-
return;
121-
}
122-
123-
var gistFile = Path.Combine(gistTempDirectory.FullName, fileName);
124-
await File.WriteAllTextAsync(gistFile, content);
125-
126-
result = await GitRunner.Run("config", new[] { "--local", "user.email", "[email protected]" }, gistTempDirectory.FullName);
127-
if (result.HasErrors)
128-
{
129-
_log.Error($"Unable to set the user.email for the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
130-
return;
131-
}
132-
133-
result = await GitRunner.Run("config", new[] { "--local", "user.name", "GitHub Action" }, gistTempDirectory.FullName);
134-
if (result.HasErrors)
135-
{
136-
_log.Error($"Unable to set the user.name for the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
137-
return;
138-
}
139-
140-
result = await GitRunner.Run("add", new[] { "." }, gistTempDirectory.FullName);
141-
if (result.HasErrors)
142-
{
143-
_log.Error($"Unable to add the file {fileName} to the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
144-
return;
145-
}
146-
147-
result = await GitRunner.Run("commit", new[] { "-m", $"Upload new file {fileName}" }, gistTempDirectory.FullName);
148-
if (result.HasErrors)
149-
{
150-
_log.Error($"Unable to commit the file {fileName} to the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
151-
return;
152-
}
153-
154-
result = await GitRunner.Run("push", Array.Empty<string>(), gistTempDirectory.FullName);
155-
if (result.HasErrors)
156-
{
157-
_log.Error($"Unable to push the file {fileName} to the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
158-
return;
159-
}
160-
}
161-
finally
162-
{
163-
try
164-
{
165-
gistTempDirectory.Delete(true);
166-
}
167-
catch
168-
{
169-
// ignore
170-
}
171-
}
172-
173-
_log.Warn($"New file uploaded to gist {gistId}");
124+
_log.Error($"The gist {gistId} does not contain a file {fileName}");
174125
}
175126
}
176127

src/dotnet-releaser/ReleaserApp.Changelog.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private async Task<bool> ListOrUpdateChangelog(string configurationFilePath, str
2626
return false;
2727
}
2828

29-
var devHosting = await ConnectToDevHosting(_config.GitHub, githubApiToken, "For Fetching Changelog");
29+
var devHosting = await ConnectToDevHosting(_config.GitHub, githubApiToken, "For Fetching Changelog", null);
3030
if (devHosting is null) return false;
3131

3232
return await ListOrUpdateChangelog(devHosting, version, update);

src/dotnet-releaser/ReleaserApp.Configuring.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace DotNetReleaser;
1515

1616
public partial class ReleaserApp
1717
{
18-
private async Task<(BuildInformation? buildInformation, IDevHosting? devHosting, IDevHosting? devHostingExtra)?> Configuring(string configurationFile, BuildKind buildKind, string githubApiToken, string? githubApiTokenExtra, string? nugetApiToken, bool forceArtifactsFolder, string? publishVersion)
18+
private async Task<(BuildInformation? buildInformation, IDevHosting? devHosting, IDevHosting? devHostingExtra)?> Configuring(string configurationFile, BuildKind buildKind, string githubApiToken, string? githubApiTokenExtra,
19+
string? githubApiTokenGist, string? nugetApiToken, bool forceArtifactsFolder, string? publishVersion)
1920
{
2021
// ------------------------------------------------------------------
2122
// Load Configuration
@@ -53,7 +54,7 @@ public partial class ReleaserApp
5354
// Connect to GitHub if we have a token
5455
if (!string.IsNullOrEmpty(githubApiToken))
5556
{
56-
devHosting = await ConnectToDevHosting(hostingConfiguration, githubApiToken, "For this CI");
57+
devHosting = await ConnectToDevHosting(hostingConfiguration, githubApiToken, "For this CI", githubApiTokenGist);
5758
if (devHosting is null)
5859
{
5960
return null; // return false;

src/dotnet-releaser/ReleaserApp.DevHosting.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace DotNetReleaser;
66

77
public partial class ReleaserApp
88
{
9-
private async Task<IDevHosting?> ConnectToDevHosting(DevHostingConfiguration hostingConfiguration, string githubApiToken, string apiTokenUsage)
9+
private async Task<IDevHosting?> ConnectToDevHosting(DevHostingConfiguration hostingConfiguration, string githubApiToken, string apiTokenUsage, string? githubApiTokenGist = null)
1010
{
11-
var hosting = new GitHubDevHosting(_logger, hostingConfiguration, githubApiToken, apiTokenUsage);
11+
var hosting = new GitHubDevHosting(_logger, hostingConfiguration, githubApiToken, apiTokenUsage, githubApiTokenGist);
1212

1313
if (await hosting.Connect())
1414
{

src/dotnet-releaser/ReleaserApp.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ CommandOption<string> AddGitHubTokenExtra(CommandLineApplication cmd)
174174
return cmd.Option<string>("--github-token-extra <token>", "GitHub Api Token. Required if publish homebrew to GitHub is true in the config file. In that case dotnet-releaser needs a personal access GitHub token which can create the homebrew repository. This token has usually more access than the --github-token that is only used for the current repository. ", CommandOptionType.SingleValue);
175175
}
176176

177+
CommandOption<string> AddGitHubTokenGist(CommandLineApplication cmd)
178+
{
179+
return cmd.Option<string>("--github-token-gist <token>", "GitHub Api Token. Required if publishing to a gist used for e.g coverage.", CommandOptionType.SingleValue);
180+
}
181+
177182
CommandArgument<string> AddTomlConfigurationArgument(CommandLineApplication cmd, bool forNew)
178183
{
179184
var arg = cmd.Argument<string>("dotnet-releaser.toml", forNew ? "TOML configuration file path to create. Default is: dotnet-releaser.toml" : "The input TOML configuration file.");
@@ -185,6 +190,7 @@ void AddPublishOrBuildArgs(CommandLineApplication cmd)
185190
{
186191
CommandOption<string>? nugetToken = null;
187192
CommandOption<string>? gitHubTokenExtra = null;
193+
CommandOption<string>? gitHubTokenGist = null;
188194
CommandOption<bool>? skipAppPackagesOption = null;
189195

190196
var githubToken = AddGitHubToken(cmd);
@@ -195,6 +201,7 @@ void AddPublishOrBuildArgs(CommandLineApplication cmd)
195201
nugetToken = cmd.Option<string>("--nuget-token <token>", "NuGet Api Token. Required if publish to NuGet is true in the config file", CommandOptionType.SingleValue);
196202

197203
gitHubTokenExtra = AddGitHubTokenExtra(cmd);
204+
gitHubTokenGist = AddGitHubTokenGist(cmd);
198205
}
199206
else
200207
{
@@ -237,7 +244,7 @@ void AddPublishOrBuildArgs(CommandLineApplication cmd)
237244
{
238245
appReleaser._tableBorder = GetTableBorderFromKind(tableKindOption.ParsedValue);
239246
}
240-
var result = await appReleaser.RunImpl(configurationFilePath, buildKind, githubToken.ParsedValue, gitHubTokenExtra?.ParsedValue, nugetToken?.ParsedValue, forceOption.ParsedValue, forceUploadOption?.ParsedValue ?? false, publishVersion?.ParsedValue);
247+
var result = await appReleaser.RunImpl(configurationFilePath, buildKind, githubToken.ParsedValue, gitHubTokenExtra?.ParsedValue, gitHubTokenGist?.ParsedValue, nugetToken?.ParsedValue, forceOption.ParsedValue, forceUploadOption?.ParsedValue ?? false, publishVersion?.ParsedValue);
241248
return result ? 0 : 1;
242249
});
243250
}
@@ -299,7 +306,8 @@ private async Task<bool> LoadConfiguration(string configurationFile)
299306
/// <summary>
300307
/// Runs the releaser app
301308
/// </summary>
302-
private async Task<bool> RunImpl(string configurationFile, BuildKind buildKind, string githubApiToken, string? githubApiTokenExtra, string? nugetApiToken, bool forceArtifactsFolder, bool forceUpload, string? publishVersion)
309+
private async Task<bool> RunImpl(string configurationFile, BuildKind buildKind, string githubApiToken, string? githubApiTokenExtra, string? gitHubTokenGist, string? nugetApiToken, bool forceArtifactsFolder, bool forceUpload,
310+
string? publishVersion)
303311
{
304312
BuildInformation? buildInformation = null;
305313
GitHubDevHostingConfiguration? hostingConfiguration = null;
@@ -310,7 +318,7 @@ private async Task<bool> RunImpl(string configurationFile, BuildKind buildKind,
310318
{
311319
_logger.Info($"dotnet-releaser {Version} - {buildKind.ToString().ToLowerInvariant()}");
312320
_logger.LogStartGroup($"Configuring");
313-
var result = await Configuring(configurationFile, buildKind, githubApiToken, githubApiTokenExtra, nugetApiToken, forceArtifactsFolder, publishVersion);
321+
var result = await Configuring(configurationFile, buildKind, githubApiToken, githubApiTokenExtra, gitHubTokenGist, nugetApiToken, forceArtifactsFolder, publishVersion);
314322
if (result is null) return false;
315323
buildInformation = result.Value.buildInformation!;
316324
devHosting = result.Value.devHosting;

0 commit comments

Comments
 (0)