Skip to content

Commit

Permalink
When auth fails during nuget stats, render failure
Browse files Browse the repository at this point in the history
It can be helpful to see the actual error from the GH CLI when authentication fails. We only propagate the error as the output to the caller from our process helper if there was no output already (i.e. some graphql APIs return failure but still provide data). This eliminates the chance of breaking existing code that relied on that.

Also added a debug-only option to pass in a direct token, which is easier for debug runs of the CLI.
  • Loading branch information
kzu committed Oct 1, 2024
1 parent f239643 commit d3fdada
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Commands/GitHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ class TransientToken : IDisposable
if (!TryExecute("gh", $"auth login --with-token", token, out var output))
{
Debug.Fail(output);
AnsiConsole.MarkupLine("[red]Failed to authenticate with provided token[/]");
if (output.Trim().Length > 0)
AnsiConsole.MarkupLine($"[dim]{output.Trim()}[/]");

return null;
}

Expand Down
14 changes: 12 additions & 2 deletions src/Commands/NuGetStatsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public class NuGetStatsSettings : ToSSettings
[CommandOption("--with-token")]
public bool WithToken { get; set; }

#if DEBUG
[Description(@"Specific GitHub authentication token for debug")]
[CommandOption("--token")]
public string? Token { get; set; }
#endif

[Description("Pages to skip")]
[CommandOption("--skip", IsHidden = true)]
public int Skip { get; set; }
Expand Down Expand Up @@ -92,13 +98,17 @@ public override ValidationResult Validate()
public override async Task<int> ExecuteAsync(CommandContext context, NuGetStatsSettings settings)
{
string? token = default;
#if DEBUG
token = settings.Token;
#endif
if (settings.WithToken)
token = new StreamReader(Console.OpenStandardInput()).ReadToEnd().Trim();

using var withToken = GitHub.WithToken(token);
if (!string.IsNullOrEmpty(token) && withToken is null)
// Whether via custom token or exiting one, we need to ensure user can be authenticated with the GH CLI
if ((token != null && withToken == null) || await base.ExecuteAsync(context, settings) is not 0)
{
AnsiConsole.MarkupLine(":cross_mark: [yellow]Invalid GitHub token provided[/]");
AnsiConsole.MarkupLine(":cross_mark: [yellow]GitHub CLI auth status could not be determined[/]");
return -1;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Commands/Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ static bool TryExecuteCore(string program, string arguments, string? input, out
output = output.Trim();
if (string.IsNullOrEmpty(output))
output = null;
if (output == null && gotError && !string.IsNullOrWhiteSpace(error))
output = error.Trim();

return !gotError && proc.ExitCode == 0;
}
Expand Down

0 comments on commit d3fdada

Please sign in to comment.