Skip to content

Commit

Permalink
feat(composer): handle providers at packages.json root level (#6028)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanuel committed Apr 24, 2020
1 parent db9632a commit 2b9899b
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 7 deletions.
42 changes: 41 additions & 1 deletion lib/datasource/packagist/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,47 @@ Object {
}
`;

exports[`datasource/packagist getReleases supports providers packages 1`] = `
exports[`datasource/packagist getReleases supports provider-includes 1`] = `
Object {
"homepage": "https://wordpress.org/plugins/1beyt/",
"name": "wpackagist-plugin/1beyt",
"releases": Array [
Object {
"gitRef": "1.0",
"releaseTimestamp": undefined,
"version": "1.0",
},
Object {
"gitRef": "1.1",
"releaseTimestamp": undefined,
"version": "1.1",
},
Object {
"gitRef": "1.4",
"releaseTimestamp": undefined,
"version": "1.4",
},
Object {
"gitRef": "1.5",
"releaseTimestamp": undefined,
"version": "1.5",
},
Object {
"gitRef": "1.5.1",
"releaseTimestamp": undefined,
"version": "1.5.1",
},
Object {
"gitRef": "dev-trunk",
"releaseTimestamp": "2018-09-03 21:31:30",
"version": "dev-trunk",
},
],
"sourceUrl": "https://plugins.svn.wordpress.org/1beyt/",
}
`;

exports[`datasource/packagist getReleases supports providers 1`] = `
Object {
"homepage": "https://wordpress.org/plugins/1beyt/",
"name": "wpackagist-plugin/1beyt",
Expand Down
59 changes: 57 additions & 2 deletions lib/datasource/packagist/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('datasource/packagist', () => {
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
});
it('supports providers packages', async () => {
it('supports provider-includes', async () => {
const packagesJson = {
packages: [],
'providers-url': '/p/%package%$%hash%.json',
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('datasource/packagist', () => {
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
});
it('handles providers packages miss', async () => {
it('handles provider-includes miss', async () => {
const packagesJson = {
packages: [],
'providers-url': '/p/%package%$%hash%.json',
Expand Down Expand Up @@ -207,6 +207,61 @@ describe('datasource/packagist', () => {
});
expect(res).toBeNull();
});
it('supports providers', async () => {
const packagesJson = {
packages: [],
'providers-url': '/p/%package%$%hash%.json',
providers: {
'wpackagist-plugin/1337-rss-feed-made-for-sharing': {
sha256:
'e9b6c98c63f99e59440863a044cc80dd9cddbf5c426b05003dba98983b5757de',
},
'wpackagist-plugin/1beyt': {
sha256:
'b574a802b5bf20a58c0f027e73aea2a75d23a6f654afc298a8dc467331be316a',
},
},
};
got.mockReturnValueOnce({
body: packagesJson,
});
got.mockReturnValueOnce({
body: JSON.parse(beytJson),
});
const res = await packagist.getReleases({
...config,
lookupName: 'wpackagist-plugin/1beyt',
});
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
});
it('handles providers miss', async () => {
const packagesJson = {
packages: [],
'providers-url': '/p/%package%$%hash%.json',
providers: {
'wpackagist-plugin/1337-rss-feed-made-for-sharing': {
sha256:
'e9b6c98c63f99e59440863a044cc80dd9cddbf5c426b05003dba98983b5757de',
},
'wpackagist-plugin/1beyt': {
sha256:
'b574a802b5bf20a58c0f027e73aea2a75d23a6f654afc298a8dc467331be316a',
},
},
};
got.mockReturnValueOnce({
body: packagesJson,
});
got.mockReturnValueOnce({
body: JSON.parse(beytJson),
});
const res = await packagist.getReleases({
...config,
lookupName: 'some/other',
});
expect(res).toBeNull();
});
it('processes real versioned data', async () => {
got.mockReturnValueOnce({
body: JSON.parse(mailchimpJson),
Expand Down
24 changes: 20 additions & 4 deletions lib/datasource/packagist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface PackageMeta {
includes?: Record<string, { sha256: string }>;
packages: Record<string, RegistryFile>;
'provider-includes': Record<string, { sha256: string }>;
providers: Record<string, { sha256: string }>;
'providers-url'?: string;
}

Expand All @@ -38,6 +39,7 @@ interface RegistryFile {
}
interface RegistryMeta {
files?: RegistryFile[];
providerPackages: Record<string, string>;
providersUrl?: string;
includesFiles?: RegistryFile[];
packages?: Record<string, RegistryFile>;
Expand All @@ -48,7 +50,9 @@ async function getRegistryMeta(regUrl: string): Promise<RegistryMeta | null> {
const url = URL.resolve(regUrl.replace(/\/?$/, '/'), 'packages.json');
const opts = getHostOpts(url);
const res = (await http.getJson<PackageMeta>(url, opts)).body;
const meta: RegistryMeta = {};
const meta: RegistryMeta = {
providerPackages: {},
};
meta.packages = res.packages;
if (res.includes) {
meta.includesFiles = [];
Expand All @@ -60,8 +64,10 @@ async function getRegistryMeta(regUrl: string): Promise<RegistryMeta | null> {
meta.includesFiles.push(file);
}
}
if (res['providers-url'] && res['provider-includes']) {
if (res['providers-url']) {
meta.providersUrl = res['providers-url'];
}
if (res['provider-includes']) {
meta.files = [];
for (const [key, val] of Object.entries(res['provider-includes'])) {
const file = {
Expand All @@ -71,6 +77,11 @@ async function getRegistryMeta(regUrl: string): Promise<RegistryMeta | null> {
meta.files.push(file);
}
}
if (res.providers) {
for (const [key, val] of Object.entries(res.providers)) {
meta.providerPackages[key] = val.sha256;
}
}
return meta;
} catch (err) {
if (err.code === 'ETIMEDOUT') {
Expand Down Expand Up @@ -176,8 +187,13 @@ async function getAllPackages(regUrl: string): Promise<AllPackages | null> {
global.repoCache[`packagist-${regUrl}`] = null;
return null;
}
const { packages, providersUrl, files, includesFiles } = registryMeta;
const providerPackages: Record<string, string> = {};
const {
packages,
providersUrl,
files,
includesFiles,
providerPackages,
} = registryMeta;
if (files) {
const queue = files.map((file) => (): Promise<PackagistFile> =>
getPackagistFile(regUrl, file)
Expand Down

0 comments on commit 2b9899b

Please sign in to comment.