Skip to content

Commit 5f31632

Browse files
committed
C#: Move GetAllFeeds to the feed manager.
1 parent 6a2a337 commit 5f31632

3 files changed

Lines changed: 75 additions & 73 deletions

File tree

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FeedManager.cs

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.Immutable;
4+
using System.IO;
45
using System.Linq;
56
using System.Net;
67
using System.Net.Http;
@@ -20,23 +21,38 @@ internal sealed partial class FeedManager : IDisposable
2021

2122
private readonly ILogger logger;
2223
private readonly IDotNet dotnet;
24+
private readonly FileProvider fileProvider;
2325
private readonly DependabotProxy? dependabotProxy;
2426
private readonly DependencyDirectory emptyPackageDirectory;
2527

2628
public ImmutableHashSet<string> PrivateRegistryFeeds { get; }
2729
public bool HasPrivateRegistryFeeds { get; }
2830
public bool CheckNugetFeedResponsiveness { get; } = EnvironmentVariables.GetBooleanOptOut(EnvironmentVariableNames.CheckNugetFeedResponsiveness);
2931

30-
public FeedManager(ILogger logger, IDotNet dotnet, DependabotProxy? dependabotProxy)
32+
public FeedManager(ILogger logger, IDotNet dotnet, DependabotProxy? dependabotProxy, FileProvider fileProvider)
3133
{
3234
this.logger = logger;
3335
this.dotnet = dotnet;
3436
this.dependabotProxy = dependabotProxy;
37+
this.fileProvider = fileProvider;
3538
PrivateRegistryFeeds = dependabotProxy?.RegistryURLs.ToImmutableHashSet() ?? [];
3639
HasPrivateRegistryFeeds = PrivateRegistryFeeds.Count > 0;
3740
emptyPackageDirectory = new DependencyDirectory("empty", "empty package", logger);
3841
}
3942

43+
private string? GetDirectoryName(string path)
44+
{
45+
try
46+
{
47+
return new FileInfo(path).Directory?.FullName;
48+
}
49+
catch (Exception exc)
50+
{
51+
logger.LogWarning($"Failed to get directory of '{path}': {exc}");
52+
}
53+
return null;
54+
}
55+
4056
private IEnumerable<string> GetFeeds(Func<IList<string>> getNugetFeeds)
4157
{
4258
var results = getNugetFeeds();
@@ -65,11 +81,11 @@ private IEnumerable<string> GetFeeds(Func<IList<string>> getNugetFeeds)
6581
}
6682
}
6783

68-
public IEnumerable<string> GetFeedsFromFolder(string folderPath) =>
84+
private IEnumerable<string> GetFeedsFromFolder(string folderPath) =>
6985
GetFeeds(() => dotnet.GetNugetFeedsFromFolder(folderPath));
7086

7187

72-
public IEnumerable<string> GetFeedsFromNugetConfig(string nugetConfigPath) =>
88+
private IEnumerable<string> GetFeedsFromNugetConfig(string nugetConfigPath) =>
7389
GetFeeds(() => dotnet.GetNugetFeeds(nugetConfigPath));
7490

7591
private string FeedsToRestoreArgument(IEnumerable<string> feeds)
@@ -108,7 +124,7 @@ private string FeedsToRestoreArgument(IEnumerable<string> feeds)
108124
}
109125

110126
// Find the path specific feeds.
111-
var folder = FileUtils.GetDirectoryName(path, logger);
127+
var folder = GetDirectoryName(path);
112128
var feedsToConsider = folder is not null ? GetFeedsFromFolder(folder).ToHashSet() : new HashSet<string>();
113129

114130
if (HasPrivateRegistryFeeds)
@@ -282,7 +298,7 @@ public bool IsDefaultFeedReachable()
282298
/// <param name="isFallback">Whether the feeds are fallback feeds or not.</param>
283299
/// <param name="isTimeout">Whether a timeout occurred while checking the feeds.</param>
284300
/// <returns>The list of feeds that could be reached.</returns>
285-
public List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool isFallback, out bool isTimeout)
301+
private List<string> GetReachableNuGetFeeds(HashSet<string> feedsToCheck, bool isFallback, out bool isTimeout)
286302
{
287303
var fallbackStr = isFallback ? "fallback " : "";
288304
logger.LogInfo($"Checking {fallbackStr}NuGet feed reachability on feeds: {string.Join(", ", feedsToCheck.OrderBy(f => f))}");
@@ -334,6 +350,58 @@ public List<string> GetReachableFallbackNugetFeeds(HashSet<string>? feedsFromNug
334350
return GetReachableNuGetFeeds(fallbackFeeds, isFallback: true, out var _);
335351
}
336352

353+
public (HashSet<string> explicitFeeds, HashSet<string> allFeeds) GetAllFeeds()
354+
{
355+
var nugetConfigs = fileProvider.NugetConfigs;
356+
357+
// Find feeds that are explicitly configured in the NuGet configuration files that we found.
358+
var explicitFeeds = nugetConfigs
359+
.SelectMany(GetFeedsFromNugetConfig)
360+
.ToHashSet();
361+
362+
if (explicitFeeds.Count > 0)
363+
{
364+
logger.LogInfo($"Found {explicitFeeds.Count} NuGet feeds in nuget.config files: {string.Join(", ", explicitFeeds.OrderBy(f => f))}");
365+
}
366+
else
367+
{
368+
logger.LogDebug("No NuGet feeds found in nuget.config files.");
369+
}
370+
371+
// If private package registries are configured for C#, then consider those
372+
// in addition to the ones that are configured in `nuget.config` files.
373+
if (HasPrivateRegistryFeeds)
374+
{
375+
logger.LogInfo($"Found {PrivateRegistryFeeds.Count} private registry feeds configured for C#: {string.Join(", ", PrivateRegistryFeeds.OrderBy(f => f))}");
376+
explicitFeeds.UnionWith(PrivateRegistryFeeds);
377+
}
378+
379+
HashSet<string> allFeeds = [];
380+
381+
// Add all explicitFeeds to the set of all feeds.
382+
allFeeds.UnionWith(explicitFeeds);
383+
384+
// Obtain the list of feeds from the root source directory.
385+
// If a NuGet file is present it will be respected, otherwise we will just get the machine/environment specific feeds.
386+
var nugetFeedsFromRoot = GetFeedsFromFolder(fileProvider.SourceDir.FullName);
387+
allFeeds.UnionWith(nugetFeedsFromRoot);
388+
389+
if (nugetConfigs.Count > 0)
390+
{
391+
var nugetConfigFeeds = nugetConfigs
392+
.Select(GetDirectoryName)
393+
.Where(folder => folder != null)
394+
.SelectMany(folder => GetFeedsFromFolder(folder!))
395+
.ToHashSet();
396+
397+
allFeeds.UnionWith(nugetConfigFeeds);
398+
}
399+
400+
logger.LogInfo($"Found {allFeeds.Count} NuGet feeds (with inherited ones) in nuget.config files: {string.Join(", ", allFeeds.OrderBy(f => f))}");
401+
402+
return (explicitFeeds, allFeeds);
403+
}
404+
337405
[GeneratedRegex(@"^E\s(.*)$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]
338406
private static partial Regex EnabledNugetFeed();
339407

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public NugetPackageRestorer(
4848
PackageDirectory = new DependencyDirectory("packages", "package", logger);
4949
legacyPackageDirectory = new DependencyDirectory("legacypackages", "legacy package", logger);
5050
missingPackageDirectory = new DependencyDirectory("missingpackages", "missing package", logger);
51-
feedManager = new FeedManager(logger, dotnet, dependabotProxy);
51+
feedManager = new FeedManager(logger, dotnet, dependabotProxy, fileProvider);
5252
}
5353

5454
public string? TryRestore(string package)
@@ -120,7 +120,7 @@ public HashSet<AssemblyLookupLocation> Restore()
120120
// Find feeds that are configured in NuGet.config files and divide them into ones that
121121
// are explicitly configured for the project or by a private registry, and "all feeds"
122122
// (including inherited ones) from other locations on the host outside of the working directory.
123-
(explicitFeeds, var allFeeds) = GetAllFeeds();
123+
(explicitFeeds, var allFeeds) = feedManager.GetAllFeeds();
124124

125125
if (feedManager.CheckNugetFeedResponsiveness)
126126
{
@@ -667,59 +667,6 @@ private void EmitNugetConfigDiagnostics()
667667
}
668668
}
669669

670-
671-
private (HashSet<string> explicitFeeds, HashSet<string> allFeeds) GetAllFeeds()
672-
{
673-
var nugetConfigs = fileProvider.NugetConfigs;
674-
675-
// Find feeds that are explicitly configured in the NuGet configuration files that we found.
676-
var explicitFeeds = nugetConfigs
677-
.SelectMany(config => feedManager.GetFeedsFromNugetConfig(config))
678-
.ToHashSet();
679-
680-
if (explicitFeeds.Count > 0)
681-
{
682-
logger.LogInfo($"Found {explicitFeeds.Count} NuGet feeds in nuget.config files: {string.Join(", ", explicitFeeds.OrderBy(f => f))}");
683-
}
684-
else
685-
{
686-
logger.LogDebug("No NuGet feeds found in nuget.config files.");
687-
}
688-
689-
// If private package registries are configured for C#, then consider those
690-
// in addition to the ones that are configured in `nuget.config` files.
691-
if (feedManager.HasPrivateRegistryFeeds)
692-
{
693-
logger.LogInfo($"Found {feedManager.PrivateRegistryFeeds.Count} private registry feeds configured for C#: {string.Join(", ", feedManager.PrivateRegistryFeeds.OrderBy(f => f))}");
694-
explicitFeeds.UnionWith(feedManager.PrivateRegistryFeeds);
695-
}
696-
697-
HashSet<string> allFeeds = [];
698-
699-
// Add all explicitFeeds to the set of all feeds.
700-
allFeeds.UnionWith(explicitFeeds);
701-
702-
// Obtain the list of feeds from the root source directory.
703-
// If a NuGet file is present it will be respected, otherwise we will just get the machine/environment specific feeds.
704-
var nugetFeedsFromRoot = feedManager.GetFeedsFromFolder(fileProvider.SourceDir.FullName);
705-
allFeeds.UnionWith(nugetFeedsFromRoot);
706-
707-
if (nugetConfigs.Count > 0)
708-
{
709-
var nugetConfigFeeds = nugetConfigs
710-
.Select(path => FileUtils.GetDirectoryName(path, logger))
711-
.Where(folder => folder != null)
712-
.SelectMany(folder => feedManager.GetFeedsFromFolder(folder!))
713-
.ToHashSet();
714-
715-
allFeeds.UnionWith(nugetConfigFeeds);
716-
}
717-
718-
logger.LogInfo($"Found {allFeeds.Count} NuGet feeds (with inherited ones) in nuget.config files: {string.Join(", ", allFeeds.OrderBy(f => f))}");
719-
720-
return (explicitFeeds, allFeeds);
721-
}
722-
723670
[GeneratedRegex(@"<TargetFramework>.*</TargetFramework>", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]
724671
private static partial Regex TargetFramework();
725672

csharp/extractor/Semmle.Util/FileUtils.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,6 @@ public static FileInfo CreateTemporaryFile(string extension, out bool shouldClea
240240
return new FileInfo(outputPath);
241241
}
242242

243-
public static string? GetDirectoryName(string path, ILogger logger)
244-
{
245-
try
246-
{
247-
return new FileInfo(path).Directory?.FullName;
248-
}
249-
catch (Exception exc)
250-
{
251-
logger.LogWarning($"Failed to get directory of '{path}': {exc}");
252-
}
253-
return null;
254-
}
255-
256243
public static string SafeGetDirectoryName(string path, ILogger logger)
257244
{
258245
try

0 commit comments

Comments
 (0)