From ed41f40500c23168a5186dabb3f41ad8271a37f9 Mon Sep 17 00:00:00 2001 From: Danek Duvall Date: Sun, 22 Oct 2023 22:08:21 -0400 Subject: [PATCH 1/8] fix(nuget): gracefuly accept a lack of a PackageBaseAddress resource --- .../v3_index_no_packagebaseaddress.json | 35 +++++++++++++++++++ lib/modules/datasource/nuget/index.spec.ts | 18 ++++++++++ lib/modules/datasource/nuget/v3.ts | 3 ++ 3 files changed, 56 insertions(+) create mode 100644 lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json diff --git a/lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json b/lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json new file mode 100644 index 00000000000000..28db1dfae09e1d --- /dev/null +++ b/lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json @@ -0,0 +1,35 @@ +{ + "version": "3.0.0", + "resources": [ + { + "@id": "https://api.nuget.org/v3/query", + "@type": "SearchQueryService", + "comment": "Filter and search for packages by keyword." + }, + { + "@id": "https://api.nuget.org/v3/query", + "@type": "SearchQueryService/3.0.0-beta", + "comment": "Filter and search for packages by keyword." + }, + { + "@id": "https://api.nuget.org/v3/query", + "@type": "SearchQueryService/3.0.0-rc", + "comment": "Filter and search for packages by keyword." + }, + { + "@id": "https://api.nuget.org/v3/metadata", + "@type": "RegistrationsBaseUrl", + "comment": "Get package metadata." + }, + { + "@id": "https://api.nuget.org/v3/metadata", + "@type": "RegistrationsBaseUrl/3.0.0-beta", + "comment": "Get package metadata." + }, + { + "@id": "https://api.nuget.org/v3/metadata", + "@type": "RegistrationsBaseUrl/3.0.0-rc", + "comment": "Get package metadata." + } + ] +} diff --git a/lib/modules/datasource/nuget/index.spec.ts b/lib/modules/datasource/nuget/index.spec.ts index 816bbd16bb8942..448d71a6c0914b 100644 --- a/lib/modules/datasource/nuget/index.spec.ts +++ b/lib/modules/datasource/nuget/index.spec.ts @@ -29,6 +29,9 @@ const pkgListV2Page1of2 = Fixtures.get('nunit/v2_paginated_1.xml'); const pkgListV2Page2of2 = Fixtures.get('nunit/v2_paginated_2.xml'); const nugetIndexV3 = Fixtures.get('v3_index.json'); +const nugetIndexV3NoPackageBaseAddress = Fixtures.get( + 'v3_index_no_packagebaseaddress.json' +); const nlogMocks = [ { @@ -243,6 +246,21 @@ describe('modules/datasource/nuget/index', () => { expect(res).toBeNull(); }); + // this still doesn't cover that throw + it('returns null for something', async () => { + httpMock + .scope('https://api.nuget.org') + .get('/v3/index.json') + .reply(200, nugetIndexV3NoPackageBaseAddress) + .get('/v3/metadata/nunit/index.json') + .reply(200, {}); + // XXX or do we need to/should we test getResourceUrl directly? + const res = await getPkgReleases({ + ...configV3, + }); + expect(res).toBeNull(); + }); + it('returns null for non 200 (v3v2)', async () => { httpMock.scope('https://api.nuget.org').get('/v3/index.json').reply(500); httpMock diff --git a/lib/modules/datasource/nuget/v3.ts b/lib/modules/datasource/nuget/v3.ts index eb5e088c11462a..edf1658abc01e2 100644 --- a/lib/modules/datasource/nuget/v3.ts +++ b/lib/modules/datasource/nuget/v3.ts @@ -68,6 +68,9 @@ export async function getResourceUrl( ? semver.compare(x.version, y.version) : /* istanbul ignore next: hard to test */ 0 ); + if (services.length === 0) { + throw new Error(); + } const { serviceId, version } = services.pop()!; // istanbul ignore if From 0c20820c0745748977706feb4b28de5a7f096e94 Mon Sep 17 00:00:00 2001 From: Danek Duvall Date: Mon, 23 Oct 2023 17:48:54 -0400 Subject: [PATCH 2/8] fix testing --- ...v3_registration_no_packagebaseaddress.json | 21 +++++++++++++++++ lib/modules/datasource/nuget/index.spec.ts | 23 ++++++++++++------- lib/modules/datasource/nuget/v3.ts | 5 +++- 3 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 lib/modules/datasource/nuget/__fixtures__/v3_registration_no_packagebaseaddress.json diff --git a/lib/modules/datasource/nuget/__fixtures__/v3_registration_no_packagebaseaddress.json b/lib/modules/datasource/nuget/__fixtures__/v3_registration_no_packagebaseaddress.json new file mode 100644 index 00000000000000..f6776bf97b1904 --- /dev/null +++ b/lib/modules/datasource/nuget/__fixtures__/v3_registration_no_packagebaseaddress.json @@ -0,0 +1,21 @@ +{ + "count": 1, + "items": [ + { + "@id": "https://api.nuget.org/v3/metadata/nunit/5.0.json", + "lower": "5.0", + "upper": "5.0", + "count": 1, + "items": [ + { + "@id": "foo", + "packageContent": "foo", + "catalogEntry": { + "id": "nunit", + "version": "5.0" + } + } + ] + } + ] +} diff --git a/lib/modules/datasource/nuget/index.spec.ts b/lib/modules/datasource/nuget/index.spec.ts index 448d71a6c0914b..b9ea3d70e374d2 100644 --- a/lib/modules/datasource/nuget/index.spec.ts +++ b/lib/modules/datasource/nuget/index.spec.ts @@ -29,9 +29,6 @@ const pkgListV2Page1of2 = Fixtures.get('nunit/v2_paginated_1.xml'); const pkgListV2Page2of2 = Fixtures.get('nunit/v2_paginated_2.xml'); const nugetIndexV3 = Fixtures.get('v3_index.json'); -const nugetIndexV3NoPackageBaseAddress = Fixtures.get( - 'v3_index_no_packagebaseaddress.json' -); const nlogMocks = [ { @@ -246,19 +243,29 @@ describe('modules/datasource/nuget/index', () => { expect(res).toBeNull(); }); - // this still doesn't cover that throw - it('returns null for something', async () => { + // This is for coverage and to make sure we don't completely fall over--we + // don't have a way to test if we successfully replaced the TypeError. + it(`doesn't trigger a TypeError when PackageBaseAddress is missing from service index`, async () => { + const nugetIndexV3NoPackageBaseAddress = Fixtures.get( + 'v3_index_no_packagebaseaddress.json' + ); + const nugetRegistrationV3NoPackageBaseAddress = Fixtures.get( + 'v3_registration_no_packagebaseaddress.json' + ); + httpMock .scope('https://api.nuget.org') .get('/v3/index.json') .reply(200, nugetIndexV3NoPackageBaseAddress) .get('/v3/metadata/nunit/index.json') - .reply(200, {}); - // XXX or do we need to/should we test getResourceUrl directly? + .reply(200, nugetRegistrationV3NoPackageBaseAddress) + .get('/v3/index.json') + .reply(200, nugetIndexV3NoPackageBaseAddress); const res = await getPkgReleases({ ...configV3, }); - expect(res).toBeNull(); + expect(res).not.toBeNull(); + expect(res!.releases).toHaveLength(1); }); it('returns null for non 200 (v3v2)', async () => { diff --git a/lib/modules/datasource/nuget/v3.ts b/lib/modules/datasource/nuget/v3.ts index edf1658abc01e2..a18e54ea8bd5a3 100644 --- a/lib/modules/datasource/nuget/v3.ts +++ b/lib/modules/datasource/nuget/v3.ts @@ -68,9 +68,12 @@ export async function getResourceUrl( ? semver.compare(x.version, y.version) : /* istanbul ignore next: hard to test */ 0 ); + + // replace a TypeError in the deconstruction with a custom one if (services.length === 0) { - throw new Error(); + throw new Error(`no ${resourceType} services found`); } + const { serviceId, version } = services.pop()!; // istanbul ignore if From 28e9132e1d7a9f33ce0a8e379ac0cc5870b039c3 Mon Sep 17 00:00:00 2001 From: Danek Duvall Date: Tue, 24 Oct 2023 12:54:09 -0400 Subject: [PATCH 3/8] viceice review --- .../v3_index_no_packagebaseaddress.json | 20 ------------------- lib/modules/datasource/nuget/v3.ts | 7 +++++-- 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json b/lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json index 28db1dfae09e1d..c7c7ae1bbeb410 100644 --- a/lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json +++ b/lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json @@ -1,21 +1,6 @@ { "version": "3.0.0", "resources": [ - { - "@id": "https://api.nuget.org/v3/query", - "@type": "SearchQueryService", - "comment": "Filter and search for packages by keyword." - }, - { - "@id": "https://api.nuget.org/v3/query", - "@type": "SearchQueryService/3.0.0-beta", - "comment": "Filter and search for packages by keyword." - }, - { - "@id": "https://api.nuget.org/v3/query", - "@type": "SearchQueryService/3.0.0-rc", - "comment": "Filter and search for packages by keyword." - }, { "@id": "https://api.nuget.org/v3/metadata", "@type": "RegistrationsBaseUrl", @@ -25,11 +10,6 @@ "@id": "https://api.nuget.org/v3/metadata", "@type": "RegistrationsBaseUrl/3.0.0-beta", "comment": "Get package metadata." - }, - { - "@id": "https://api.nuget.org/v3/metadata", - "@type": "RegistrationsBaseUrl/3.0.0-rc", - "comment": "Get package metadata." } ] } diff --git a/lib/modules/datasource/nuget/v3.ts b/lib/modules/datasource/nuget/v3.ts index a18e54ea8bd5a3..e10938666b4d4c 100644 --- a/lib/modules/datasource/nuget/v3.ts +++ b/lib/modules/datasource/nuget/v3.ts @@ -69,9 +69,12 @@ export async function getResourceUrl( : /* istanbul ignore next: hard to test */ 0 ); - // replace a TypeError in the deconstruction with a custom one if (services.length === 0) { - throw new Error(`no ${resourceType} services found`); + logger.debug( + { url, servicesIndexRaw }, + `no ${resourceType} services found` + ); + return null; } const { serviceId, version } = services.pop()!; From 60b09a7577d5c693c5ee36f984ad19a4e7e08c22 Mon Sep 17 00:00:00 2001 From: Danek Duvall Date: Tue, 24 Oct 2023 16:38:58 -0400 Subject: [PATCH 4/8] inline the JSON test responses --- .../v3_index_no_packagebaseaddress.json | 15 ------ ...v3_registration_no_packagebaseaddress.json | 21 -------- lib/modules/datasource/nuget/index.spec.ts | 52 +++++++++++++++---- 3 files changed, 43 insertions(+), 45 deletions(-) delete mode 100644 lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json delete mode 100644 lib/modules/datasource/nuget/__fixtures__/v3_registration_no_packagebaseaddress.json diff --git a/lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json b/lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json deleted file mode 100644 index c7c7ae1bbeb410..00000000000000 --- a/lib/modules/datasource/nuget/__fixtures__/v3_index_no_packagebaseaddress.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "version": "3.0.0", - "resources": [ - { - "@id": "https://api.nuget.org/v3/metadata", - "@type": "RegistrationsBaseUrl", - "comment": "Get package metadata." - }, - { - "@id": "https://api.nuget.org/v3/metadata", - "@type": "RegistrationsBaseUrl/3.0.0-beta", - "comment": "Get package metadata." - } - ] -} diff --git a/lib/modules/datasource/nuget/__fixtures__/v3_registration_no_packagebaseaddress.json b/lib/modules/datasource/nuget/__fixtures__/v3_registration_no_packagebaseaddress.json deleted file mode 100644 index f6776bf97b1904..00000000000000 --- a/lib/modules/datasource/nuget/__fixtures__/v3_registration_no_packagebaseaddress.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "count": 1, - "items": [ - { - "@id": "https://api.nuget.org/v3/metadata/nunit/5.0.json", - "lower": "5.0", - "upper": "5.0", - "count": 1, - "items": [ - { - "@id": "foo", - "packageContent": "foo", - "catalogEntry": { - "id": "nunit", - "version": "5.0" - } - } - ] - } - ] -} diff --git a/lib/modules/datasource/nuget/index.spec.ts b/lib/modules/datasource/nuget/index.spec.ts index b9ea3d70e374d2..d70b4fc5e503b0 100644 --- a/lib/modules/datasource/nuget/index.spec.ts +++ b/lib/modules/datasource/nuget/index.spec.ts @@ -246,21 +246,55 @@ describe('modules/datasource/nuget/index', () => { // This is for coverage and to make sure we don't completely fall over--we // don't have a way to test if we successfully replaced the TypeError. it(`doesn't trigger a TypeError when PackageBaseAddress is missing from service index`, async () => { - const nugetIndexV3NoPackageBaseAddress = Fixtures.get( - 'v3_index_no_packagebaseaddress.json' - ); - const nugetRegistrationV3NoPackageBaseAddress = Fixtures.get( - 'v3_registration_no_packagebaseaddress.json' - ); + const nugetIndex = ` + { + "version": "3.0.0", + "resources": [ + { + "@id": "https://api.nuget.org/v3/metadata", + "@type": "RegistrationsBaseUrl", + "comment": "Get package metadata." + }, + { + "@id": "https://api.nuget.org/v3/metadata", + "@type": "RegistrationsBaseUrl/3.0.0-beta", + "comment": "Get package metadata." + } + ] + } + `; + const nunitRegistration = ` + { + "count": 1, + "items": [ + { + "@id": "https://api.nuget.org/v3/metadata/nunit/5.0.json", + "lower": "5.0", + "upper": "5.0", + "count": 1, + "items": [ + { + "@id": "foo", + "packageContent": "foo", + "catalogEntry": { + "id": "nunit", + "version": "5.0" + } + } + ] + } + ] + } + `; httpMock .scope('https://api.nuget.org') .get('/v3/index.json') - .reply(200, nugetIndexV3NoPackageBaseAddress) + .reply(200, nugetIndex) .get('/v3/metadata/nunit/index.json') - .reply(200, nugetRegistrationV3NoPackageBaseAddress) + .reply(200, nunitRegistration) .get('/v3/index.json') - .reply(200, nugetIndexV3NoPackageBaseAddress); + .reply(200, nugetIndex); const res = await getPkgReleases({ ...configV3, }); From 7823277c6c2155e4cca66c6142a0ed9eb2bad52f Mon Sep 17 00:00:00 2001 From: Danek Duvall Date: Wed, 25 Oct 2023 16:50:53 -0700 Subject: [PATCH 5/8] test the logger input instead of whining about being unable to do the check --- lib/modules/datasource/nuget/index.spec.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/modules/datasource/nuget/index.spec.ts b/lib/modules/datasource/nuget/index.spec.ts index d70b4fc5e503b0..00ab8f13272572 100644 --- a/lib/modules/datasource/nuget/index.spec.ts +++ b/lib/modules/datasource/nuget/index.spec.ts @@ -2,6 +2,7 @@ import { mockDeep } from 'jest-mock-extended'; import { getPkgReleases } from '..'; import { Fixtures } from '../../../../test/fixtures'; import * as httpMock from '../../../../test/http-mock'; +import { logger } from '../../../../test/util'; import * as _hostRules from '../../../util/host-rules'; import { id as versioning } from '../../versioning/nuget'; import { parseRegistryUrl } from './common'; @@ -243,9 +244,7 @@ describe('modules/datasource/nuget/index', () => { expect(res).toBeNull(); }); - // This is for coverage and to make sure we don't completely fall over--we - // don't have a way to test if we successfully replaced the TypeError. - it(`doesn't trigger a TypeError when PackageBaseAddress is missing from service index`, async () => { + it('logs instead of triggering a TypeError when PackageBaseAddress is missing from service index', async () => { const nugetIndex = ` { "version": "3.0.0", @@ -300,6 +299,13 @@ describe('modules/datasource/nuget/index', () => { }); expect(res).not.toBeNull(); expect(res!.releases).toHaveLength(1); + expect(logger.logger.debug).toHaveBeenCalledWith( + { + url: 'https://api.nuget.org/v3/index.json', + servicesIndexRaw: JSON.parse(nugetIndex), + }, + 'no PackageBaseAddress services found' + ); }); it('returns null for non 200 (v3v2)', async () => { From 2686a467af5aa3e5c45ea7c840a974e66db950d3 Mon Sep 17 00:00:00 2001 From: Danek Duvall Date: Wed, 25 Oct 2023 17:01:54 -0700 Subject: [PATCH 6/8] cache the missing service --- lib/modules/datasource/nuget/v3.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/modules/datasource/nuget/v3.ts b/lib/modules/datasource/nuget/v3.ts index e10938666b4d4c..5da279f235d0d1 100644 --- a/lib/modules/datasource/nuget/v3.ts +++ b/lib/modules/datasource/nuget/v3.ts @@ -70,6 +70,7 @@ export async function getResourceUrl( ); if (services.length === 0) { + await packageCache.set(cacheNamespace, resultCacheKey, null, 60); logger.debug( { url, servicesIndexRaw }, `no ${resourceType} services found` From d78d4fa21d1fbd89d5afd78cfbff395b07f29b79 Mon Sep 17 00:00:00 2001 From: Danek Duvall Date: Thu, 26 Oct 2023 09:52:22 -0700 Subject: [PATCH 7/8] use .twice() instead of repeating the path --- lib/modules/datasource/nuget/index.spec.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/modules/datasource/nuget/index.spec.ts b/lib/modules/datasource/nuget/index.spec.ts index 00ab8f13272572..342a93d6636b95 100644 --- a/lib/modules/datasource/nuget/index.spec.ts +++ b/lib/modules/datasource/nuget/index.spec.ts @@ -289,11 +289,10 @@ describe('modules/datasource/nuget/index', () => { httpMock .scope('https://api.nuget.org') .get('/v3/index.json') + .twice() .reply(200, nugetIndex) .get('/v3/metadata/nunit/index.json') - .reply(200, nunitRegistration) - .get('/v3/index.json') - .reply(200, nugetIndex); + .reply(200, nunitRegistration); const res = await getPkgReleases({ ...configV3, }); From 76cf1be316dc7e19ce16ead90352aac802d1b7c6 Mon Sep 17 00:00:00 2001 From: Danek Duvall Date: Thu, 26 Oct 2023 09:54:14 -0700 Subject: [PATCH 8/8] the unversioned RegistrationsBaseUrl isn't needed --- lib/modules/datasource/nuget/index.spec.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/modules/datasource/nuget/index.spec.ts b/lib/modules/datasource/nuget/index.spec.ts index 342a93d6636b95..b1c05b17bf6e2f 100644 --- a/lib/modules/datasource/nuget/index.spec.ts +++ b/lib/modules/datasource/nuget/index.spec.ts @@ -249,11 +249,6 @@ describe('modules/datasource/nuget/index', () => { { "version": "3.0.0", "resources": [ - { - "@id": "https://api.nuget.org/v3/metadata", - "@type": "RegistrationsBaseUrl", - "comment": "Get package metadata." - }, { "@id": "https://api.nuget.org/v3/metadata", "@type": "RegistrationsBaseUrl/3.0.0-beta",