Skip to content

Commit 11ea99e

Browse files
Factorize PackageDownloadRunner.TryGetRepositoriesForPackage method and add test (#6953)
1 parent d102a45 commit 11ea99e

19 files changed

Lines changed: 410 additions & 64 deletions

File tree

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Package/Download/PackageDownloadRunner.cs

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,31 @@ public static async Task<int> RunAsync(PackageDownloadArgs args, ILoggerWithColo
9191
}
9292
else
9393
{
94-
if (!TryGetRepositoriesForPackage(
95-
package.Id,
96-
args,
97-
packageSourceMapping!,
98-
allRepositories,
99-
logger,
100-
out sourceRepositories))
94+
var mappedNames = packageSourceMapping!.GetConfiguredPackageSources(package.Id);
95+
96+
if (mappedNames.Count == 0)
97+
{
98+
// fail, no sources mapped for this package
99+
var notConsideredSources = string.Join(
100+
", ",
101+
allRepositories.Select(repository => repository.PackageSource));
102+
103+
logger.LogError(string.Format(
104+
CultureInfo.CurrentCulture,
105+
Strings.PackageDownloadCommand_PackageSourceMapping_NoSourcesMapped,
106+
package.Id,
107+
notConsideredSources));
108+
109+
downloadedAllSuccessfully &= false;
110+
continue;
111+
}
112+
113+
sourceRepositories = GetMappedRepositories(mappedNames, allRepositories, package.Id, logger);
114+
115+
if (DetectAndReportInsecureSources(args.AllowInsecureConnections, sourceRepositories.Select(r => r.PackageSource), logger))
101116
{
102-
return ExitCodeError;
117+
downloadedAllSuccessfully &= false;
118+
continue;
103119
}
104120
}
105121

@@ -224,67 +240,49 @@ await ResolvePackageDownloadVersion(
224240
return (versionToDownload, downloadSourceRepository);
225241
}
226242

227-
/// <summary>
228-
/// Builds the set of SourceRepository objects to use for a given package,
229-
/// applying package source mapping
230-
/// validating HTTP usage only on the *effective* sources.
231-
/// </summary>
232-
private static bool TryGetRepositoriesForPackage(
233-
string packageId,
234-
PackageDownloadArgs args,
235-
PackageSourceMapping packageSourceMapping,
243+
internal static IReadOnlyList<SourceRepository> GetMappedRepositories(
244+
IReadOnlyList<string> mappedNames,
236245
IReadOnlyList<SourceRepository> allRepos,
237-
ILoggerWithColor logger,
238-
out IReadOnlyList<SourceRepository> repositories)
246+
string packageId,
247+
ILoggerWithColor logger)
239248
{
240-
var mappedNames = packageSourceMapping.GetConfiguredPackageSources(packageId);
249+
var mappedRepos = new List<SourceRepository>(mappedNames.Count);
241250

242-
// Only validate insecure sources when mapping produced something
243-
if (mappedNames.Count > 0)
251+
foreach (var mappedName in mappedNames)
244252
{
245-
var mappedRepos = new List<SourceRepository>(mappedNames.Count);
246-
foreach (var mappedName in mappedNames)
247-
{
248-
SourceRepository? repo = null;
249-
for (int i = 0; i < allRepos.Count; i++)
250-
{
251-
if (string.Equals(allRepos[i].PackageSource.Name, mappedName, StringComparison.OrdinalIgnoreCase))
252-
{
253-
repo = allRepos[i];
254-
break;
255-
}
256-
}
253+
SourceRepository? repo = FindRepositoryByName(mappedName, allRepos);
257254

258-
if (repo != null)
259-
{
260-
mappedRepos.Add(repo);
261-
}
262-
else
263-
{
264-
logger.LogVerbose(
265-
string.Format(
266-
CultureInfo.CurrentCulture,
267-
Strings.PackageDownloadCommand_PackageSourceMapping_NoSuchSource,
268-
mappedName,
269-
packageId));
270-
}
255+
if (repo != null)
256+
{
257+
mappedRepos.Add(repo);
271258
}
272-
273-
if (DetectAndReportInsecureSources(args.AllowInsecureConnections, mappedRepos.Select(repo => repo.PackageSource), logger))
259+
else
274260
{
275-
repositories = [];
276-
return false;
261+
logger.LogVerbose(
262+
string.Format(
263+
CultureInfo.CurrentCulture,
264+
Strings.PackageDownloadCommand_PackageSourceMapping_NoSuchSource,
265+
mappedName,
266+
packageId));
277267
}
278-
279-
repositories = mappedRepos;
280-
return true;
281268
}
282-
else
269+
270+
return mappedRepos;
271+
}
272+
273+
private static SourceRepository? FindRepositoryByName(
274+
string mappedName,
275+
IReadOnlyList<SourceRepository> allRepos)
276+
{
277+
for (int i = 0; i < allRepos.Count; i++)
283278
{
284-
// No mapping for this package: fall back to all sources
285-
repositories = allRepos;
286-
return true;
279+
if (string.Equals(allRepos[i].PackageSource.Name, mappedName, StringComparison.OrdinalIgnoreCase))
280+
{
281+
return allRepos[i];
282+
}
287283
}
284+
285+
return null;
288286
}
289287

290288
private static async Task<bool> DownloadPackageAsync(

src/NuGet.Core/NuGet.CommandLine.XPlat/Strings.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/NuGet.Core/NuGet.CommandLine.XPlat/Strings.resx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,4 +1162,9 @@ Do not translate "PackageVersion"</comment>
11621162
<comment>0 - package source name
11631163
1 - package name</comment>
11641164
</data>
1165+
<data name="PackageDownloadCommand_PackageSourceMapping_NoSourcesMapped" xml:space="preserve">
1166+
<value>Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</value>
1167+
<comment>{0} = package ID
1168+
{1} = comma-separated list of all configured sources </comment>
1169+
</data>
11651170
</root>

src/NuGet.Core/NuGet.CommandLine.XPlat/xlf/Strings.cs.xlf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,12 @@ Další informace najdete tady: https://docs.nuget.org/docs/reference/command-li
929929
<target state="translated">Identifikátor balíčku (např. Newtonsoft.Json)</target>
930930
<note />
931931
</trans-unit>
932+
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSourcesMapped">
933+
<source>Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</source>
934+
<target state="new">Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</target>
935+
<note>{0} = package ID
936+
{1} = comma-separated list of all configured sources </note>
937+
</trans-unit>
932938
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSuchSource">
933939
<source>The mapped source '{0}' for package '{1}' was not found among the configured sources.</source>
934940
<target state="translated">Mapovaný zdroj {0} pro balíček {1} nebyl nalezen mezi nakonfigurovanými zdroji.</target>

src/NuGet.Core/NuGet.CommandLine.XPlat/xlf/Strings.de.xlf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,12 @@ Weitere Informationen finden Sie unter: https://docs.nuget.org/docs/reference/co
929929
<target state="translated">Paketbezeichner (z. B. „Newtonsoft.Json“).</target>
930930
<note />
931931
</trans-unit>
932+
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSourcesMapped">
933+
<source>Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</source>
934+
<target state="new">Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</target>
935+
<note>{0} = package ID
936+
{1} = comma-separated list of all configured sources </note>
937+
</trans-unit>
932938
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSuchSource">
933939
<source>The mapped source '{0}' for package '{1}' was not found among the configured sources.</source>
934940
<target state="translated">Die zugeordnete Quelle {0} für das Paket {1} wurde unter den konfigurierten Quellen nicht gefunden.</target>

src/NuGet.Core/NuGet.CommandLine.XPlat/xlf/Strings.es.xlf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,12 @@ Para obtener más información, visite https://docs.nuget.org/docs/reference/com
929929
<target state="translated">Identificador del paquete (por ejemplo, 'Newtonsoft.Json').</target>
930930
<note />
931931
</trans-unit>
932+
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSourcesMapped">
933+
<source>Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</source>
934+
<target state="new">Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</target>
935+
<note>{0} = package ID
936+
{1} = comma-separated list of all configured sources </note>
937+
</trans-unit>
932938
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSuchSource">
933939
<source>The mapped source '{0}' for package '{1}' was not found among the configured sources.</source>
934940
<target state="translated">No se encontró el origen asignado '{0}' para el paquete '{1}' entre los orígenes configurados.</target>

src/NuGet.Core/NuGet.CommandLine.XPlat/xlf/Strings.fr.xlf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,12 @@ Pour plus d'informations, visitez https://docs.nuget.org/docs/reference/command-
929929
<target state="translated">Identificateur de package (par exemple, « Newtonsoft.Json »).</target>
930930
<note />
931931
</trans-unit>
932+
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSourcesMapped">
933+
<source>Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</source>
934+
<target state="new">Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</target>
935+
<note>{0} = package ID
936+
{1} = comma-separated list of all configured sources </note>
937+
</trans-unit>
932938
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSuchSource">
933939
<source>The mapped source '{0}' for package '{1}' was not found among the configured sources.</source>
934940
<target state="translated">La source mappée « {0} » pour le package « {1} » est introuvable parmi les sources configurées.</target>

src/NuGet.Core/NuGet.CommandLine.XPlat/xlf/Strings.it.xlf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,12 @@ Per altre informazioni, vedere https://docs.nuget.org/docs/reference/command-lin
929929
<target state="translated">Identificatore del pacchetto ,ad esempio 'Newtonsoft.Json'.</target>
930930
<note />
931931
</trans-unit>
932+
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSourcesMapped">
933+
<source>Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</source>
934+
<target state="new">Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</target>
935+
<note>{0} = package ID
936+
{1} = comma-separated list of all configured sources </note>
937+
</trans-unit>
932938
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSuchSource">
933939
<source>The mapped source '{0}' for package '{1}' was not found among the configured sources.</source>
934940
<target state="translated">L'origine mappata '{0}' per il pacchetto '{1}' non è stata trovata tra le origini configurate.</target>

src/NuGet.Core/NuGet.CommandLine.XPlat/xlf/Strings.ja.xlf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,12 @@ For more information, visit https://docs.nuget.org/docs/reference/command-line-r
929929
<target state="translated">パッケージ識別子 (例: 'Newtonsoft.Json')。</target>
930930
<note />
931931
</trans-unit>
932+
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSourcesMapped">
933+
<source>Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</source>
934+
<target state="new">Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</target>
935+
<note>{0} = package ID
936+
{1} = comma-separated list of all configured sources </note>
937+
</trans-unit>
932938
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSuchSource">
933939
<source>The mapped source '{0}' for package '{1}' was not found among the configured sources.</source>
934940
<target state="translated">パッケージ '{1}' のマップされたソース '{0}' が、構成済みのソースの中に見つかりませんでした。</target>

src/NuGet.Core/NuGet.CommandLine.XPlat/xlf/Strings.ko.xlf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,12 @@ For more information, visit https://docs.nuget.org/docs/reference/command-line-r
929929
<target state="translated">패키지 식별자(예: 'Newtonsoft.Json').</target>
930930
<note />
931931
</trans-unit>
932+
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSourcesMapped">
933+
<source>Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</source>
934+
<target state="new">Unable to download package '{0}'. PackageSourceMapping is enabled, but no mapped sources were found for this package. The following source(s) were not considered: {1}.</target>
935+
<note>{0} = package ID
936+
{1} = comma-separated list of all configured sources </note>
937+
</trans-unit>
932938
<trans-unit id="PackageDownloadCommand_PackageSourceMapping_NoSuchSource">
933939
<source>The mapped source '{0}' for package '{1}' was not found among the configured sources.</source>
934940
<target state="translated">구성된 원본 중 '{1}' 패키지에 대해 매핑된 원본 '{0}'을(를) 찾을 수 없습니다.</target>

0 commit comments

Comments
 (0)