Skip to content

Commit

Permalink
Revert "refactor(packagist): Use schema for packages.json file (ren…
Browse files Browse the repository at this point in the history
…ovatebot#19930)"

This reverts commit ca66ada.
  • Loading branch information
zharinov committed Jan 24, 2023
1 parent 1d34936 commit eccf7fc
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 95 deletions.
10 changes: 5 additions & 5 deletions lib/modules/datasource/packagist/index.spec.ts
Expand Up @@ -57,10 +57,10 @@ describe('modules/datasource/packagist/index', () => {
const packagesOnly = {
packages: {
'vendor/package-name': {
'dev-master': { version: 'dev-master' },
'1.0.x-dev': { version: '1.0.x-dev' },
'0.0.1': { version: '0.0.1' },
'1.0.0': { version: '1.0.0' },
'dev-master': {},
'1.0.x-dev': {},
'0.0.1': {},
'1.0.0': {},
},
},
};
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('modules/datasource/packagist/index', () => {
packages: [],
includes: {
'include/all$afbf74d51f31c7cbb5ff10304f9290bfb4f4e68b.json': {
sha256: 'afbf74d51f31c7cbb5ff10304f9290bfb4f4e68b',
sha1: 'afbf74d51f31c7cbb5ff10304f9290bfb4f4e68b',
},
},
};
Expand Down
67 changes: 53 additions & 14 deletions lib/modules/datasource/packagist/index.ts
@@ -1,5 +1,4 @@
import URL from 'url';
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import { cache } from '../../../util/cache/package/decorator';
Expand All @@ -12,7 +11,13 @@ import * as composerVersioning from '../../versioning/composer';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import * as schema from './schema';
import type { AllPackages, PackagistFile, RegistryFile } from './types';
import type {
AllPackages,
PackageMeta,
PackagistFile,
RegistryFile,
RegistryMeta,
} from './types';

export class PackagistDatasource extends Datasource {
static readonly id = 'packagist';
Expand All @@ -36,12 +41,46 @@ export class PackagistDatasource extends Datasource {
return username && password ? { username, password } : {};
}

private async getRegistryMeta(regUrl: string): Promise<schema.RegistryMeta> {
private async getRegistryMeta(regUrl: string): Promise<RegistryMeta | null> {
const url = URL.resolve(ensureTrailingSlash(regUrl), 'packages.json');
const opts = PackagistDatasource.getHostOpts(url);
const { body } = await this.http.getJson(url, opts);
const res = schema.RegistryMeta.parse(body);
return res;
const res = (await this.http.getJson<PackageMeta>(url, opts)).body;
const meta: RegistryMeta = {
providerPackages: {},
packages: res.packages,
};
if (res.includes) {
meta.includesFiles = [];
for (const [name, val] of Object.entries(res.includes)) {
const file = {
key: name.replace(val.sha256, '%hash%'),
sha256: val.sha256,
};
meta.includesFiles.push(file);
}
}
if (res['providers-url']) {
meta.providersUrl = res['providers-url'];
}
if (res['providers-lazy-url']) {
meta.providersLazyUrl = res['providers-lazy-url'];
}
if (res['provider-includes']) {
meta.files = [];
for (const [key, val] of Object.entries(res['provider-includes'])) {
const file = {
key,
sha256: val.sha256,
};
meta.files.push(file);
}
}
if (res.providers) {
for (const [key, val] of Object.entries(res.providers)) {
meta.providerPackages[key] = val.sha256;
}
}
return meta;
}

private static isPrivatePackage(regUrl: string): boolean {
Expand Down Expand Up @@ -147,7 +186,7 @@ export class PackagistDatasource extends Datasource {
}
}
const allPackages: AllPackages = {
packages: packages as never, // TODO: fix types (#9610)
packages,
providersUrl,
providersLazyUrl,
providerPackages,
Expand Down Expand Up @@ -209,13 +248,13 @@ export class PackagistDatasource extends Datasource {
return includesPackages[packageName];
}
let pkgUrl: string;
const hash = providerPackages[packageName];
if (providersUrl && !is.undefined(hash)) {
let url = providersUrl.replace('%package%', packageName);
if (hash) {
url = url.replace('%hash%', hash);
}
pkgUrl = URL.resolve(registryUrl, url);
if (packageName in providerPackages) {
pkgUrl = URL.resolve(
registryUrl,
providersUrl!
.replace('%package%', packageName)
.replace('%hash%', providerPackages[packageName])
);
} else if (providersLazyUrl) {
pkgUrl = URL.resolve(
registryUrl,
Expand Down
73 changes: 0 additions & 73 deletions lib/modules/datasource/packagist/schema.ts
Expand Up @@ -144,76 +144,3 @@ export function parsePackagesResponses(

return result;
}

const RegistryFile = z.object({
key: z.string(),
sha256: z.string(),
});
export type RegistryFile = z.infer<typeof RegistryFile>;

const RegistryMetaFile = z.object({
sha256: z.string().nullable(),
});
type RegistryMetaFile = z.infer<typeof RegistryMetaFile>;

const RegistryMetaFiles = z
.record(RegistryMetaFile.nullable().catch(null))
.transform((obj) => {
// Remove all null values
// TODO: extract as schema utility
const result: Record<string, RegistryMetaFile> = {};
for (const [key, val] of Object.entries(obj)) {
if (val !== null) {
result[key] = val;
}
}
return result;
});

const RegistryMetaIncludes = RegistryMetaFiles.transform(
(obj): RegistryFile[] => {
const result: RegistryFile[] = [];
for (const [key, { sha256 }] of Object.entries(obj)) {
if (sha256) {
result.push({ key, sha256 });
}
}
return result;
}
)
.nullable()
.catch(null);

export const RegistryMeta = z
.object({
['packages']: z.record(z.record(ComposerRelease)).nullable().catch(null),
['includes']: RegistryMetaIncludes,
['provider-includes']: RegistryMetaIncludes,
['providers-url']: z.string().nullable().catch(null),
['providers-lazy-url']: z.string().optional().nullable().catch(null),
['providers']: RegistryMetaFiles.transform((obj) => {
const result: Record<string, string | null> = {};
for (const [key, { sha256 }] of Object.entries(obj)) {
result[key] = sha256;
}
return result;
}).catch({}),
})
.transform(
({
['packages']: packages,
['includes']: includesFiles,
['providers']: providerPackages,
['provider-includes']: files,
['providers-url']: providersUrl,
['providers-lazy-url']: providersLazyUrl,
}) => ({
packages,
includesFiles,
providerPackages,
files,
providersUrl,
providersLazyUrl,
})
);
export type RegistryMeta = z.infer<typeof RegistryMeta>;
14 changes: 11 additions & 3 deletions lib/modules/datasource/packagist/types.ts
@@ -1,5 +1,13 @@
import type { ReleaseResult } from '../types';

export interface PackageMeta {
includes?: Record<string, { sha256: string }>;
packages: Record<string, RegistryFile>;
'provider-includes': Record<string, { sha256: string }>;
providers: Record<string, { sha256: string }>;
'providers-lazy-url'?: string;
'providers-url'?: string;
}
export interface RegistryFile {
key: string;
sha256: string;
Expand All @@ -20,9 +28,9 @@ export interface PackagistFile {

export interface AllPackages {
packages: Record<string, RegistryFile>;
providersUrl: string | null;
providersLazyUrl: string | null;
providerPackages: Record<string, string | null>;
providersUrl?: string;
providersLazyUrl?: string;
providerPackages: Record<string, string>;

includesPackages: Record<string, ReleaseResult>;
}

0 comments on commit eccf7fc

Please sign in to comment.