From 14db42d7600cc9a31831ac202e2b3968dd6ad2ba Mon Sep 17 00:00:00 2001 From: Thomas Ardal Date: Wed, 22 Nov 2023 11:27:45 +0100 Subject: [PATCH] Remove unauthenticated sources to avoid keep hitting the same failing source(s) for all packages --- src/NuPU/UpdateCommand.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/NuPU/UpdateCommand.cs b/src/NuPU/UpdateCommand.cs index 78b16df..b764844 100644 --- a/src/NuPU/UpdateCommand.cs +++ b/src/NuPU/UpdateCommand.cs @@ -11,6 +11,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Xml; @@ -45,7 +46,7 @@ public override async Task ExecuteAsync(CommandContext context, UpdateComma foreach (var csProjFile in csProjFiles.Where(f => !Ignored(f, ignoreDirs))) { var settings = Settings.LoadDefaultSettings(csProjFile.Directory.FullName); - var enabledSources = SettingsUtility.GetEnabledSources(settings); + var enabledSources = SettingsUtility.GetEnabledSources(settings).ToList(); AnsiConsole.MarkupLine($"Analyzing [yellow]{csProjFile.FullName}[/]"); var packages = new List(); using (var fileStream = File.OpenRead(csProjFile.FullName)) @@ -95,6 +96,7 @@ public override async Task ExecuteAsync(CommandContext context, UpdateComma AnsiConsole.Markup(package.Id); var showUpToDate = true; + var sourcesToDelete = new List(); foreach (var source in enabledSources) { var repository = new SourceRepository(source, Repository.Provider.GetCoreV3()); @@ -172,9 +174,19 @@ public override async Task ExecuteAsync(CommandContext context, UpdateComma } } } + catch (FatalProtocolException ex) when (IsAuthenticationError(ex)) + { + sourcesToDelete.Add(source); + } catch { } } + // If we had any unauthenticated sources we remove them to avoid requesting them for the next package. + foreach (var sourceToDelete in sourcesToDelete) + { + if (enabledSources.Contains(sourceToDelete)) enabledSources.Remove(sourceToDelete); + } + if (showUpToDate) AnsiConsole.MarkupLine(UpToDate); } } @@ -182,6 +194,12 @@ public override async Task ExecuteAsync(CommandContext context, UpdateComma return 0; } + private static bool IsAuthenticationError(FatalProtocolException ex) + { + var baseException = ex.GetBaseException() as HttpRequestException; + return baseException == null ? false : baseException.StatusCode == System.Net.HttpStatusCode.Unauthorized; + } + private bool Ignored(FileInfo fileInfo, List ignoreDirs) { if (ignoreDirs.Count == 0) return false;