Skip to content

Commit

Permalink
Remove unauthenticated sources to avoid keep hitting the same failing…
Browse files Browse the repository at this point in the history
… source(s) for all packages
  • Loading branch information
ThomasArdal committed Nov 22, 2023
1 parent fb1073c commit 14db42d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/NuPU/UpdateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,7 +46,7 @@ public override async Task<int> 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<Package>();
using (var fileStream = File.OpenRead(csProjFile.FullName))
Expand Down Expand Up @@ -95,6 +96,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, UpdateComma
AnsiConsole.Markup(package.Id);

var showUpToDate = true;
var sourcesToDelete = new List<PackageSource>();
foreach (var source in enabledSources)
{
var repository = new SourceRepository(source, Repository.Provider.GetCoreV3());
Expand Down Expand Up @@ -172,16 +174,32 @@ public override async Task<int> 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);
}
}

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<string> ignoreDirs)
{
if (ignoreDirs.Count == 0) return false;
Expand Down

0 comments on commit 14db42d

Please sign in to comment.