Skip to content

Commit

Permalink
Add better message for Connecting To GitHub to differentiate GitHub…
Browse files Browse the repository at this point in the history
… tokens
  • Loading branch information
xoofx committed Sep 8, 2023
1 parent 53e0a01 commit ff9ee05
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/DotNetReleaser.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task CheckGitHub()

var logger = new MockSimpleLogger();

var githubHosting = new GitHubDevHosting(logger, devHosting, "TBD");
var githubHosting = new GitHubDevHosting(logger, devHosting, "TBD", "TBD");
var branches = await githubHosting.GetBranchNamesForCommit("xoofx", "dotnet-releaser", "afe9d28493c05d24f16b5ffdac011b73f66e3c5c");
}

Expand Down
2 changes: 2 additions & 0 deletions src/DotNetReleaser.Tests/MockDevHosting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public MockDevHosting(ISimpleLogger logger, DevHostingConfiguration configuratio
Logger = logger;
Configuration = configuration;
ApiToken = string.Empty;
ApiTokenUsage = string.Empty;
}

public ISimpleLogger Logger { get; }

public DevHostingConfiguration Configuration { get; }
public string ApiToken { get; }
public string ApiTokenUsage { get; }

public Task<bool> Connect()

Expand Down
8 changes: 5 additions & 3 deletions src/dotnet-releaser/DevHosting/GitHubDevHosting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ public class GitHubDevHosting : IDevHosting
private readonly string _apiToken;
private readonly GitHubClient _client;

public GitHubDevHosting(ISimpleLogger log, DevHostingConfiguration hostingConfiguration, string apiToken)
public GitHubDevHosting(ISimpleLogger log, DevHostingConfiguration hostingConfiguration, string apiToken, string apiTokenUsage)
{
Logger = log;
Configuration = hostingConfiguration;
_log = log;
_url = hostingConfiguration.Base;
_apiToken = apiToken;
ApiTokenUsage = apiTokenUsage;
_client = new GitHubClient(new ProductHeaderValue(nameof(ReleaserApp)), new Uri(hostingConfiguration.Api));
}

Expand All @@ -38,20 +39,21 @@ public GitHubDevHosting(ISimpleLogger log, DevHostingConfiguration hostingConfig

public DevHostingConfiguration Configuration { get; }
public string ApiToken => _apiToken;
public string ApiTokenUsage { get; }

public async Task<bool> Connect()
{
var tokenAuth = new Credentials(_apiToken); // NOTE: not real token
_client.Credentials = tokenAuth;

_log.Info("Connecting to GitHub");
_log.Info($"Connecting to GitHub ({ApiTokenUsage})");
try
{
_ = await _client.Repository.Get(Configuration.User, Configuration.Repo);
}
catch (Exception ex)
{
_log.Error($"Unable to connect GitHub. Reason: {ex.Message}");
_log.Error($"Unable to connect GitHub ({ApiTokenUsage}). Reason: {ex.Message}");
return false;
}
return true;
Expand Down
2 changes: 2 additions & 0 deletions src/dotnet-releaser/IDevHosting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface IDevHosting
DevHostingConfiguration Configuration { get; }

string ApiToken { get; }

string ApiTokenUsage { get; }

Task<bool> Connect();

Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-releaser/ReleaserApp.Changelog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private async Task<bool> ListOrUpdateChangelog(string configurationFilePath, str
return false;
}

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

return await ListOrUpdateChangelog(devHosting, version, update);
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet-releaser/ReleaserApp.Configuring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public partial class ReleaserApp
// Connect to GitHub if we have a token
if (!string.IsNullOrEmpty(githubApiToken))
{
devHosting = await ConnectToDevHosting(hostingConfiguration, githubApiToken);
devHosting = await ConnectToDevHosting(hostingConfiguration, githubApiToken, "For this CI");
if (devHosting is null)
{
return null; // return false;
Expand All @@ -63,7 +63,7 @@ public partial class ReleaserApp
// Connet to GitHub for extra access
if (!string.IsNullOrEmpty(githubApiTokenExtra))
{
devHostingExtra = await ConnectToDevHosting(hostingConfiguration, githubApiTokenExtra);
devHostingExtra = await ConnectToDevHosting(hostingConfiguration, githubApiTokenExtra, "For External Repositories");
if (devHostingExtra is null)
{
return null; // return false;
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet-releaser/ReleaserApp.DevHosting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace DotNetReleaser;

public partial class ReleaserApp
{
private async Task<IDevHosting?> ConnectToDevHosting(DevHostingConfiguration hostingConfiguration, string githubApiToken)
private async Task<IDevHosting?> ConnectToDevHosting(DevHostingConfiguration hostingConfiguration, string githubApiToken, string apiTokenUsage)
{
var hosting = new GitHubDevHosting(_logger, hostingConfiguration, githubApiToken);
var hosting = new GitHubDevHosting(_logger, hostingConfiguration, githubApiToken, apiTokenUsage);

if (await hosting.Connect())
{
Expand Down

0 comments on commit ff9ee05

Please sign in to comment.