From 8e15ebfbf57310c9fe33e199ec446ac09d441e4d Mon Sep 17 00:00:00 2001 From: Otiel Date: Fri, 18 Oct 2024 19:01:26 +0200 Subject: [PATCH 1/7] feat(manager/nuget): add support for "disabledPackageSources" element in nuget.config file --- lib/modules/manager/nuget/util.spec.ts | 96 ++++++++++++++++++++++++++ lib/modules/manager/nuget/util.ts | 18 +++-- 2 files changed, 110 insertions(+), 4 deletions(-) diff --git a/lib/modules/manager/nuget/util.spec.ts b/lib/modules/manager/nuget/util.spec.ts index 13097f69016a5f..3cb2c62c514e07 100644 --- a/lib/modules/manager/nuget/util.spec.ts +++ b/lib/modules/manager/nuget/util.spec.ts @@ -106,6 +106,102 @@ describe('modules/manager/nuget/util', () => { 'NuGet.Common', ]); }); + + it('reads nuget config file with default registry disabled', async () => { + fs.findUpLocal.mockReturnValue( + Promise.resolve('NuGet.config'), + ); + fs.readLocalFile.mockResolvedValueOnce( + codeBlock` + + + + + + + + `, + ); + + const registries = await getConfiguredRegistries('NuGet.config'); + expect(registries?.length).toBe(1); + expect(registries![0].name).toBe('contoso.com'); + expect(registries![0].url).toBe('https://contoso.com/packages/'); + }); + + it('reads nuget config file with default registry disabled given default registry added', async () => { + fs.findUpLocal.mockReturnValue( + Promise.resolve('NuGet.config'), + ); + fs.readLocalFile.mockResolvedValueOnce( + codeBlock` + + + + + + + + + `, + ); + + const registries = await getConfiguredRegistries('NuGet.config'); + expect(registries?.length).toBe(1); + expect(registries![0].name).toBe('contoso.com'); + expect(registries![0].url).toBe('https://contoso.com/packages/'); + }); + + it('reads nuget config file with unknown disabled source', async () => { + fs.findUpLocal.mockReturnValue( + Promise.resolve('NuGet.config'), + ); + fs.readLocalFile.mockResolvedValueOnce( + codeBlock` + + + + + + + + `, + ); + + const registries = await getConfiguredRegistries('NuGet.config'); + expect(registries?.length).toBe(2); + expect(registries![0].name).toBe('nuget.org'); + expect(registries![0].url).toBe('https://api.nuget.org/v3/index.json'); + + expect(registries![1].name).toBe('contoso.com'); + expect(registries![1].url).toBe('https://contoso.com/packages/'); + }); + + it('reads nuget config file with disabled source with value false', async () => { + fs.findUpLocal.mockReturnValue( + Promise.resolve('NuGet.config'), + ); + fs.readLocalFile.mockResolvedValueOnce( + codeBlock` + + + + + + + + + `, + ); + + const registries = await getConfiguredRegistries('NuGet.config'); + expect(registries?.length).toBe(2); + expect(registries![0].name).toBe('nuget.org'); + expect(registries![0].url).toBe('https://api.nuget.org/v3/index.json'); + + expect(registries![1].name).toBe('contoso.com'); + expect(registries![1].url).toBe('https://contoso.com/packages/'); + }); }); describe('applyRegistries', () => { diff --git a/lib/modules/manager/nuget/util.ts b/lib/modules/manager/nuget/util.ts index 38126468e4462f..244fbf8763d176 100644 --- a/lib/modules/manager/nuget/util.ts +++ b/lib/modules/manager/nuget/util.ts @@ -52,13 +52,15 @@ export async function getConfiguredRegistries( } const packageSources = nuGetConfig.childNamed('packageSources'); - if (!packageSources) { + const disabledPackageSources = nuGetConfig.childNamed('disabledPackageSources'); + + if (!packageSources && !disabledPackageSources) { return undefined; } const packageSourceMapping = nuGetConfig.childNamed('packageSourceMapping'); - const registries = getDefaultRegistries(); + let registries = getDefaultRegistries(); // Map optional source mapped package patterns to default registries for (const registry of registries) { @@ -70,7 +72,7 @@ export async function getConfiguredRegistries( registry.sourceMappedPackagePatterns = sourceMappedPackagePatterns; } - for (const child of packageSources.children) { + for (const child of packageSources?.children) { if (child.type === 'element') { if (child.name === 'clear') { logger.debug(`clearing registry URLs`); @@ -108,9 +110,17 @@ export async function getConfiguredRegistries( ); } } - // child.name === 'remove' not supported } } + + for (const child of disabledPackageSources?.children) { + if (child.type === 'element' && child.name === 'add' && child.attr.value === 'true') { + let disabledRegistryKey = child.attr.key; + registries = registries.filter(o => o.name !== disabledRegistryKey); + logger.debug(`Disabled registry with key: ${disabledRegistryKey}`); + } + } + return registries; } From 9bb3515305ae6a3cd9030b994e22532e7a3854d4 Mon Sep 17 00:00:00 2001 From: Otiel Date: Fri, 18 Oct 2024 19:10:28 +0200 Subject: [PATCH 2/7] Add missing test --- lib/modules/manager/nuget/util.spec.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/modules/manager/nuget/util.spec.ts b/lib/modules/manager/nuget/util.spec.ts index 3cb2c62c514e07..097b5c60007015 100644 --- a/lib/modules/manager/nuget/util.spec.ts +++ b/lib/modules/manager/nuget/util.spec.ts @@ -108,6 +108,23 @@ describe('modules/manager/nuget/util', () => { }); it('reads nuget config file with default registry disabled', async () => { + fs.findUpLocal.mockReturnValue( + Promise.resolve('NuGet.config'), + ); + fs.readLocalFile.mockResolvedValueOnce( + codeBlock` + + + + + `, + ); + + const registries = await getConfiguredRegistries('NuGet.config'); + expect(registries?.length).toBe(0); + }); + + it('reads nuget config file with default registry disabled and added sources', async () => { fs.findUpLocal.mockReturnValue( Promise.resolve('NuGet.config'), ); From 1ef410cc9d7653f72dcb6dd758fb83384c6954e4 Mon Sep 17 00:00:00 2001 From: Otiel Date: Fri, 18 Oct 2024 23:06:58 +0200 Subject: [PATCH 3/7] Fix potential issue while referencing undefined value --- lib/modules/manager/nuget/util.ts | 82 ++++++++++++++++--------------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/lib/modules/manager/nuget/util.ts b/lib/modules/manager/nuget/util.ts index 244fbf8763d176..c198923f281b02 100644 --- a/lib/modules/manager/nuget/util.ts +++ b/lib/modules/manager/nuget/util.ts @@ -72,52 +72,56 @@ export async function getConfiguredRegistries( registry.sourceMappedPackagePatterns = sourceMappedPackagePatterns; } - for (const child of packageSources?.children) { - if (child.type === 'element') { - if (child.name === 'clear') { - logger.debug(`clearing registry URLs`); - registries.length = 0; - } else if (child.name === 'add') { - const isHttpUrl = regEx(/^https?:\/\//i).test(child.attr.value); - if (isHttpUrl) { - let registryUrl = child.attr.value; - if (child.attr.protocolVersion) { - registryUrl += `#protocolVersion=${child.attr.protocolVersion}`; - } - const sourceMappedPackagePatterns = packageSourceMapping - ?.childWithAttribute('key', child.attr.key) - ?.childrenNamed('package') - .map((packagePattern) => packagePattern.attr['pattern']); - - logger.debug( - { + if (packageSources) { + for (const child of packageSources.children) { + if (child.type === 'element') { + if (child.name === 'clear') { + logger.debug(`clearing registry URLs`); + registries.length = 0; + } else if (child.name === 'add') { + const isHttpUrl = regEx(/^https?:\/\//i).test(child.attr.value); + if (isHttpUrl) { + let registryUrl = child.attr.value; + if (child.attr.protocolVersion) { + registryUrl += `#protocolVersion=${child.attr.protocolVersion}`; + } + const sourceMappedPackagePatterns = packageSourceMapping + ?.childWithAttribute('key', child.attr.key) + ?.childrenNamed('package') + .map((packagePattern) => packagePattern.attr['pattern']); + + logger.debug( + { + name: child.attr.key, + registryUrl, + sourceMappedPackagePatterns, + }, + `Adding registry URL ${registryUrl}`, + ); + + registries.push({ name: child.attr.key, - registryUrl, + url: registryUrl, sourceMappedPackagePatterns, - }, - `Adding registry URL ${registryUrl}`, - ); - - registries.push({ - name: child.attr.key, - url: registryUrl, - sourceMappedPackagePatterns, - }); - } else { - logger.debug( - { registryUrl: child.attr.value }, - 'ignoring local registry URL', - ); + }); + } else { + logger.debug( + { registryUrl: child.attr.value }, + 'ignoring local registry URL', + ); + } } } } } - for (const child of disabledPackageSources?.children) { - if (child.type === 'element' && child.name === 'add' && child.attr.value === 'true') { - let disabledRegistryKey = child.attr.key; - registries = registries.filter(o => o.name !== disabledRegistryKey); - logger.debug(`Disabled registry with key: ${disabledRegistryKey}`); + if (disabledPackageSources) { + for (const child of disabledPackageSources.children) { + if (child.type === 'element' && child.name === 'add' && child.attr.value === 'true') { + let disabledRegistryKey = child.attr.key; + registries = registries.filter(o => o.name !== disabledRegistryKey); + logger.debug(`Disabled registry with key: ${disabledRegistryKey}`); + } } } From 3749f21311fca766a73167da4c872aba9740db97 Mon Sep 17 00:00:00 2001 From: Otiel Date: Sat, 19 Oct 2024 10:43:18 +0200 Subject: [PATCH 4/7] Fix linting issues (reported by lint-eslint and lint-prettier jobs) --- lib/modules/manager/nuget/util.spec.ts | 2 +- lib/modules/manager/nuget/util.ts | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/modules/manager/nuget/util.spec.ts b/lib/modules/manager/nuget/util.spec.ts index 097b5c60007015..71fc451ab22c2a 100644 --- a/lib/modules/manager/nuget/util.spec.ts +++ b/lib/modules/manager/nuget/util.spec.ts @@ -215,7 +215,7 @@ describe('modules/manager/nuget/util', () => { expect(registries?.length).toBe(2); expect(registries![0].name).toBe('nuget.org'); expect(registries![0].url).toBe('https://api.nuget.org/v3/index.json'); - + expect(registries![1].name).toBe('contoso.com'); expect(registries![1].url).toBe('https://contoso.com/packages/'); }); diff --git a/lib/modules/manager/nuget/util.ts b/lib/modules/manager/nuget/util.ts index c198923f281b02..a69ab0277d6da1 100644 --- a/lib/modules/manager/nuget/util.ts +++ b/lib/modules/manager/nuget/util.ts @@ -52,7 +52,9 @@ export async function getConfiguredRegistries( } const packageSources = nuGetConfig.childNamed('packageSources'); - const disabledPackageSources = nuGetConfig.childNamed('disabledPackageSources'); + const disabledPackageSources = nuGetConfig.childNamed( + 'disabledPackageSources' + ); if (!packageSources && !disabledPackageSources) { return undefined; @@ -117,9 +119,11 @@ export async function getConfiguredRegistries( if (disabledPackageSources) { for (const child of disabledPackageSources.children) { - if (child.type === 'element' && child.name === 'add' && child.attr.value === 'true') { - let disabledRegistryKey = child.attr.key; - registries = registries.filter(o => o.name !== disabledRegistryKey); + if (child.type === 'element' && + child.name === 'add' && + child.attr.value === 'true') { + const disabledRegistryKey = child.attr.key; + registries = registries.filter((o) => o.name !== disabledRegistryKey); logger.debug(`Disabled registry with key: ${disabledRegistryKey}`); } } From 64be0b155d45360fdcefb66db4fc9e8926e467e2 Mon Sep 17 00:00:00 2001 From: Otiel Date: Sat, 19 Oct 2024 10:55:34 +0200 Subject: [PATCH 5/7] Fix failing test --- lib/modules/manager/nuget/util.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/modules/manager/nuget/util.spec.ts b/lib/modules/manager/nuget/util.spec.ts index 71fc451ab22c2a..3641d6a3784de8 100644 --- a/lib/modules/manager/nuget/util.spec.ts +++ b/lib/modules/manager/nuget/util.spec.ts @@ -202,6 +202,7 @@ describe('modules/manager/nuget/util', () => { codeBlock` + From f4b7e58dcafdd4d14cbe2163c387158715db3d44 Mon Sep 17 00:00:00 2001 From: Otiel Date: Sat, 19 Oct 2024 15:35:19 +0200 Subject: [PATCH 6/7] Fix lint-prettier error --- lib/modules/manager/nuget/util.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/modules/manager/nuget/util.ts b/lib/modules/manager/nuget/util.ts index a69ab0277d6da1..ed35dcd8b4bd73 100644 --- a/lib/modules/manager/nuget/util.ts +++ b/lib/modules/manager/nuget/util.ts @@ -53,7 +53,7 @@ export async function getConfiguredRegistries( const packageSources = nuGetConfig.childNamed('packageSources'); const disabledPackageSources = nuGetConfig.childNamed( - 'disabledPackageSources' + 'disabledPackageSources', ); if (!packageSources && !disabledPackageSources) { @@ -119,9 +119,11 @@ export async function getConfiguredRegistries( if (disabledPackageSources) { for (const child of disabledPackageSources.children) { - if (child.type === 'element' && - child.name === 'add' && - child.attr.value === 'true') { + if ( + child.type === 'element' && + child.name === 'add' && + child.attr.value === 'true' + ) { const disabledRegistryKey = child.attr.key; registries = registries.filter((o) => o.name !== disabledRegistryKey); logger.debug(`Disabled registry with key: ${disabledRegistryKey}`); From 0d1ebac148ada9bacf0e60446d953be65d0fd9ed Mon Sep 17 00:00:00 2001 From: Otiel Date: Sat, 19 Oct 2024 15:54:57 +0200 Subject: [PATCH 7/7] Fix whitespaces --- lib/modules/manager/nuget/util.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/modules/manager/nuget/util.ts b/lib/modules/manager/nuget/util.ts index ed35dcd8b4bd73..6923c7b8b989b8 100644 --- a/lib/modules/manager/nuget/util.ts +++ b/lib/modules/manager/nuget/util.ts @@ -120,8 +120,8 @@ export async function getConfiguredRegistries( if (disabledPackageSources) { for (const child of disabledPackageSources.children) { if ( - child.type === 'element' && - child.name === 'add' && + child.type === 'element' && + child.name === 'add' && child.attr.value === 'true' ) { const disabledRegistryKey = child.attr.key;