Skip to content
Snippets Groups Projects
Commit 194dbc93 authored by Florian Greinacher's avatar Florian Greinacher Committed by Rhys Arkins
Browse files

fix(NuGet): handling of paginated package versions (#3613)

parent e17f998f
No related branches found
Tags 16.7.5
No related merge requests found
......@@ -7,42 +7,53 @@ module.exports = {
};
async function getPkgReleases(feedUrl, pkgName) {
const pkgUrlList = `${feedUrl}/FindPackagesById()?id=%27${pkgName}%27&$select=Version,IsLatestVersion,ProjectUrl`;
const dep = {
pkgName,
};
try {
const pkgVersionsListRaw = await got(pkgUrlList, { platform: 'nuget' });
if (pkgVersionsListRaw.statusCode !== 200) {
logger.debug(
{ dependency: pkgName, pkgVersionsListRaw },
`nuget registry failure: status code != 200`
);
return null;
}
const pkgInfoList = new XmlDocument(
pkgVersionsListRaw.body
).children.filter(node => node.name === 'entry');
dep.releases = [];
for (const pkgInfo of pkgInfoList || []) {
const pkgVersion = getPkgProp(pkgInfo, 'Version');
dep.releases.push({
version: pkgVersion,
});
try {
const pkgIsLatestVersion = getPkgProp(pkgInfo, 'IsLatestVersion');
if (pkgIsLatestVersion === 'true') {
dep.sourceUrl = parse(getPkgProp(pkgInfo, 'ProjectUrl'));
}
} catch (err) /* istanbul ignore next */ {
let pkgUrlList = `${feedUrl}/FindPackagesById()?id=%27${pkgName}%27&$select=Version,IsLatestVersion,ProjectUrl`;
do {
const pkgVersionsListRaw = await got(pkgUrlList, { platform: 'nuget' });
if (pkgVersionsListRaw.statusCode !== 200) {
logger.debug(
{ err, pkgName, feedUrl },
`nuget registry failure: can't parse pkg info for project url`
{ dependency: pkgName, pkgVersionsListRaw },
`nuget registry failure: status code != 200`
);
return null;
}
const pkgVersionsListDoc = new XmlDocument(pkgVersionsListRaw.body);
const pkgInfoList = pkgVersionsListDoc.children.filter(
node => node.name === 'entry'
);
for (const pkgInfo of pkgInfoList || []) {
const pkgVersion = getPkgProp(pkgInfo, 'Version');
dep.releases.push({
version: pkgVersion,
});
try {
const pkgIsLatestVersion = getPkgProp(pkgInfo, 'IsLatestVersion');
if (pkgIsLatestVersion === 'true') {
dep.sourceUrl = parse(getPkgProp(pkgInfo, 'ProjectUrl'));
}
} catch (err) /* istanbul ignore next */ {
logger.debug(
{ err, pkgName, feedUrl },
`nuget registry failure: can't parse pkg info for project url`
);
}
}
}
const nextPkgUrlListLink = pkgVersionsListDoc.children.find(
node => node.name === 'link' && node.attr.rel === 'next'
);
pkgUrlList = nextPkgUrlListLink ? nextPkgUrlListLink.attr.href : null;
} while (pkgUrlList !== null);
return dep;
} catch (err) {
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`datasource/nuget getPkgReleases handles paginated results (v2) 1`] = `
Object {
"pkgName": "nunit",
"releases": Array [
Object {
"version": "1.0.0",
},
Object {
"version": "2.0.0",
},
],
}
`;
exports[`datasource/nuget getPkgReleases processes real data (v2) 1`] = `
Object {
"pkgName": "nunit",
......
......@@ -27,6 +27,15 @@ const pkgListV2WithoutProjectUrl = fs.readFileSync(
'utf8'
);
const pkgListV2Page1of2 = fs.readFileSync(
'test/datasource/nuget/_fixtures/nunitV2_paginated_1.xml',
'utf8'
);
const pkgListV2Page2of2 = fs.readFileSync(
'test/datasource/nuget/_fixtures/nunitV2_paginated_2.xml',
'utf8'
);
const nugetIndexV3 = fs.readFileSync(
'test/datasource/nuget/_fixtures/indexV3.json',
'utf8'
......@@ -323,5 +332,20 @@ describe('datasource/nuget', () => {
expect(res).toMatchSnapshot();
expect(res.sourceUrl).not.toBeDefined();
});
it('handles paginated results (v2)', async () => {
got.mockReturnValueOnce({
body: pkgListV2Page1of2,
statusCode: 200,
});
got.mockReturnValueOnce({
body: pkgListV2Page2of2,
statusCode: 200,
});
const res = await datasource.getPkgReleases({
...configV2,
});
expect(res).not.toBeNull();
expect(res).toMatchSnapshot();
});
});
});
<feed xml:base="https://www.nuget.org/api/v2" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
<id>http://schemas.datacontract.org/2004/07/</id>
<link rel="self" href="https://www.nuget.org/api/v2/Packages"/>
<link rel="next" href="https://example.org"/>
<entry>
<m:properties>
<d:Version>1.0.0</d:Version>
<d:IsLatestVersion>false</d:IsLatestVersion>
</m:properties>
</entry>
</feed>
\ No newline at end of file
<feed xml:base="https://www.nuget.org/api/v2" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
<id>http://schemas.datacontract.org/2004/07/</id>
<link rel="self" href="https://www.nuget.org/api/v2/Packages"/>
<entry>
<m:properties>
<d:Version>2.0.0</d:Version>
<d:IsLatestVersion>true</d:IsLatestVersion>
</m:properties>
</entry>
</feed>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment