Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(rubygems): fallback to info when version fails (#7323)
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
henrysachs and rarkins committed Sep 22, 2020
1 parent 9f99c5e commit edc2016
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
55 changes: 39 additions & 16 deletions lib/datasource/rubygems/get.ts
Expand Up @@ -50,23 +50,46 @@ export async function getDependency(
return null;
}

const versions = (await fetch(dependency, registry, VERSIONS_PATH)) || [];
let versions = [];
let releases = [];
try {
versions = await fetch(dependency, registry, VERSIONS_PATH);
} catch (err) {
if (err.statusCode === 400 || err.statusCode === 404) {
logger.debug(
{ registry },
'versions endpoint returns error - falling back to info endpoint'
);
} else {
throw err;
}
}

const releases = versions.map(
({
number: version,
platform: rubyPlatform,
created_at: releaseTimestamp,
rubygems_version: rubygemsVersion,
ruby_version: rubyVersion,
}) => ({
version,
rubyPlatform,
releaseTimestamp,
rubygemsVersion,
rubyVersion,
})
);
if (versions.length === 0 && info.version) {
logger.warn('falling back to the version from the info endpoint');
releases = [
{
version: info.version,
rubyPlatform: info.platform,
},
];
} else {
releases = versions.map(
({
number: version,
platform: rubyPlatform,
created_at: releaseTimestamp,
rubygems_version: rubygemsVersion,
ruby_version: rubyVersion,
}) => ({
version,
rubyPlatform,
releaseTimestamp,
rubygemsVersion,
rubyVersion,
})
);
}

return {
releases,
Expand Down
20 changes: 20 additions & 0 deletions lib/datasource/rubygems/index.spec.ts
Expand Up @@ -167,5 +167,25 @@ describe('datasource/rubygems', () => {
expect(await getPkgReleases(params)).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('falls back to info when version request fails', async () => {
httpMock
.scope('https://thirdparty.com/')
.get('/api/v1/gems/rails.json')
.reply(200, railsInfo)
.get('/api/v1/versions/rails.json')
.reply(400, {});
const res = await getPkgReleases(params);
expect(res.releases).toHaveLength(1);
expect(res.releases[0].version).toBe(railsInfo.version);
});
it('errors when version request fails with anything other than 400 or 404', async () => {
httpMock
.scope('https://thirdparty.com/')
.get('/api/v1/gems/rails.json')
.reply(200, railsInfo)
.get('/api/v1/versions/rails.json')
.reply(500, {});
expect(await getPkgReleases(params)).toBeNull();
});
});
});
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -96,7 +96,8 @@
"Viral Ruparel <viralruparel@gmail.com>",
"Vladimir Starkov <iamstarkov@gmail.com>",
"Mikhail Yakushin <driver733@gmail.com>",
"Sebastian Poxhofer <sebastian@poxhofer.at>"
"Sebastian Poxhofer <sebastian@poxhofer.at>",
"Henry Sachs <henrysachs@gmail.com>"
],
"license": "AGPL-3.0",
"bugs": {
Expand Down

0 comments on commit edc2016

Please sign in to comment.