From c3029596b7dc7dc5b1b19ceb2a8958952ac359b8 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov <zharinov@users.noreply.github.com> Date: Tue, 20 Jun 2023 10:08:38 +0300 Subject: [PATCH] feat(packagist): Query packages respecting `available-packages` (#22877) --- .../datasource/packagist/index.spec.ts | 46 +++++++++++++++++++ lib/modules/datasource/packagist/index.ts | 4 ++ .../datasource/packagist/schema.spec.ts | 1 + lib/modules/datasource/packagist/schema.ts | 12 ++++- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/lib/modules/datasource/packagist/index.spec.ts b/lib/modules/datasource/packagist/index.spec.ts index 9f90e00704..9f973a8605 100644 --- a/lib/modules/datasource/packagist/index.spec.ts +++ b/lib/modules/datasource/packagist/index.spec.ts @@ -542,5 +542,51 @@ describe('modules/datasource/packagist/index', () => { releases: [{ gitRef: 'v2.5.4', version: '2.5.4' }], }); }); + + it('respects "available-packages" list', async () => { + httpMock + .scope('https://example.com') + .get('/packages.json') + .twice() + .reply(200, { + 'metadata-url': 'https://example.com/p2/%package%.json', + 'available-packages': ['foo/bar'], + }) + .get('/p2/foo/bar.json') + .reply(200, { + minified: 'composer/2.0', + packages: { + 'foo/bar': [ + { + name: 'foo/bar', + version: 'v1.2.3', + }, + ], + }, + }) + .get('/p2/foo/bar~dev.json') + .reply(404); + config.registryUrls = ['https://example.com']; + + const foo = await getPkgReleases({ + ...config, + datasource, + versioning, + packageName: 'foo/foo', + }); + expect(foo).toBeNull(); + + const bar = await getPkgReleases({ + ...config, + datasource, + versioning, + packageName: 'foo/bar', + }); + + expect(bar).toEqual({ + registryUrl: 'https://example.com', + releases: [{ gitRef: 'v1.2.3', version: '1.2.3' }], + }); + }); }); }); diff --git a/lib/modules/datasource/packagist/index.ts b/lib/modules/datasource/packagist/index.ts index 8970551bbd..2fc93ff295 100644 --- a/lib/modules/datasource/packagist/index.ts +++ b/lib/modules/datasource/packagist/index.ts @@ -184,6 +184,10 @@ export class PackagistDatasource extends Datasource { try { const meta = await this.getRegistryMeta(registryUrl); + if (meta.availablePackages && !meta.availablePackages.has(packageName)) { + return null; + } + if (meta.metadataUrl) { const packagistResult = await this.packagistV2Lookup( registryUrl, diff --git a/lib/modules/datasource/packagist/schema.spec.ts b/lib/modules/datasource/packagist/schema.spec.ts index 12541ea984..78d90f49f6 100644 --- a/lib/modules/datasource/packagist/schema.spec.ts +++ b/lib/modules/datasource/packagist/schema.spec.ts @@ -384,6 +384,7 @@ describe('modules/datasource/packagist/schema', () => { providersLazyUrl: null, providersUrl: null, metadataUrl: null, + availablePackages: null, }); }); }); diff --git a/lib/modules/datasource/packagist/schema.ts b/lib/modules/datasource/packagist/schema.ts index 4a9375c7cb..0b1616716a 100644 --- a/lib/modules/datasource/packagist/schema.ts +++ b/lib/modules/datasource/packagist/schema.ts @@ -189,8 +189,9 @@ export const PackagistFile = PackagesResponse.merge( export type PackagistFile = z.infer<typeof PackagistFile>; export const RegistryMeta = z - .preprocess( - (x) => (is.plainObject(x) ? x : {}), + .record(z.unknown()) + .catch({}) + .pipe( PackagistFile.merge( z.object({ ['includes']: LooseRecord(HashSpec) @@ -206,6 +207,11 @@ export const RegistryMeta = z ['providers-lazy-url']: z.string().nullable().catch(null), ['providers-url']: z.string().nullable().catch(null), ['metadata-url']: z.string().nullable().catch(null), + ['available-packages']: z + .array(z.string()) + .transform((xs) => new Set(xs)) + .nullable() + .catch(null), }) ) ) @@ -218,6 +224,7 @@ export const RegistryMeta = z ['providers-lazy-url']: providersLazyUrl, ['providers-url']: providersUrl, ['metadata-url']: metadataUrl, + ['available-packages']: availablePackages, }) => ({ packages, includesFiles, @@ -227,6 +234,7 @@ export const RegistryMeta = z providersLazyUrl, metadataUrl, includesPackages: {} as Record<string, ReleaseResult | null>, + availablePackages, }) ); export type RegistryMeta = z.infer<typeof RegistryMeta>; -- GitLab