From d3fdadacb4891b663ee282d0797e271c8483150b Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Tue, 1 Oct 2024 15:47:58 -0300 Subject: [PATCH] When auth fails during nuget stats, render failure 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. --- src/Commands/GitHub.cs | 4 ++++ src/Commands/NuGetStatsCommand.cs | 14 ++++++++++++-- src/Commands/Process.cs | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Commands/GitHub.cs b/src/Commands/GitHub.cs index dcd50e10..3c208f3c 100644 --- a/src/Commands/GitHub.cs +++ b/src/Commands/GitHub.cs @@ -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; } diff --git a/src/Commands/NuGetStatsCommand.cs b/src/Commands/NuGetStatsCommand.cs index b769a4cc..de002ffc 100644 --- a/src/Commands/NuGetStatsCommand.cs +++ b/src/Commands/NuGetStatsCommand.cs @@ -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; } @@ -92,13 +98,17 @@ public override ValidationResult Validate() public override async Task 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; } diff --git a/src/Commands/Process.cs b/src/Commands/Process.cs index 211bbc48..a6920712 100644 --- a/src/Commands/Process.cs +++ b/src/Commands/Process.cs @@ -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; }