refactor: remove semver dependency from github datasource

Instead, perform isVersion filter in lookup
This commit is contained in:
Rhys Arkins 2018-06-05 08:15:11 +02:00
parent 468349d8f4
commit 05260a5b16
4 changed files with 14 additions and 9 deletions

View file

@ -1,5 +1,4 @@
const ghGot = require('../platform/github/gh-got-wrapper'); const ghGot = require('../platform/github/gh-got-wrapper');
const versioning = require('../versioning');
module.exports = { module.exports = {
getDependency, getDependency,
@ -48,10 +47,8 @@ async function getDependency(repo, options = {}) {
return null; return null;
} }
// Filter by semver if no versionScheme provided // Filter by semver if no versionScheme provided
const { isVersion } = versioning(options.versionScheme || 'semver');
versions = versions.filter(version => isVersion(version));
if (options.clean === 'true') { if (options.clean === 'true') {
versions = versions.map(version => version.replace(/^v/, '')); versions = versions.map(version => version.replace(/^v(\d)/, '$1'));
} }
const dependency = { const dependency = {
repositoryUrl: 'https://github.com/' + repo, repositoryUrl: 'https://github.com/' + repo,

View file

@ -19,6 +19,7 @@ async function lookupUpdates(config) {
getMinor, getMinor,
isGreaterThan, isGreaterThan,
isRange, isRange,
isVersion,
matches, matches,
getNewValue, getNewValue,
} = versioning(config.versionScheme); } = versioning(config.versionScheme);
@ -54,7 +55,10 @@ async function lookupUpdates(config) {
// TODO: return an object // TODO: return an object
return [result]; return [result];
} }
const allVersions = Object.keys(dependency.versions); // Filter out any results from datasource that don't comply with our versioning scheme
const allVersions = Object.keys(dependency.versions).filter(v =>
isVersion(v)
);
// istanbul ignore if // istanbul ignore if
if (allVersions.length === 0) { if (allVersions.length === 0) {
const message = `No versions returned from registry for this package`; const message = `No versions returned from registry for this package`;

View file

@ -6,6 +6,8 @@ Object {
"versions": Object { "versions": Object {
"1.0.0": Object {}, "1.0.0": Object {},
"1.1.0": Object {}, "1.1.0": Object {},
"a": Object {},
"v": Object {},
}, },
} }
`; `;
@ -15,6 +17,8 @@ Object {
"repositoryUrl": "https://github.com/some/dep", "repositoryUrl": "https://github.com/some/dep",
"versions": Object { "versions": Object {
"1.0.0": Object {}, "1.0.0": Object {},
"a": Object {},
"v": Object {},
"v1.1.0": Object {}, "v1.1.0": Object {},
}, },
} }

View file

@ -9,27 +9,27 @@ describe('datasource/github', () => {
it('returns cleaned tags', async () => { it('returns cleaned tags', async () => {
const body = [ const body = [
{ ref: 'refs/tags/a' }, { ref: 'refs/tags/a' },
{ ref: 'refs/tags/b' }, { ref: 'refs/tags/v' },
{ ref: 'refs/tags/1.0.0' }, { ref: 'refs/tags/1.0.0' },
{ ref: 'refs/tags/v1.1.0' }, { ref: 'refs/tags/v1.1.0' },
]; ];
ghGot.mockReturnValueOnce({ headers: {}, body }); ghGot.mockReturnValueOnce({ headers: {}, body });
const res = await github.getDependency('some/dep', { clean: 'true' }); const res = await github.getDependency('some/dep', { clean: 'true' });
expect(res).toMatchSnapshot(); expect(res).toMatchSnapshot();
expect(Object.keys(res.versions)).toHaveLength(2); expect(Object.keys(res.versions)).toHaveLength(4);
expect(res.versions['1.1.0']).toBeDefined(); expect(res.versions['1.1.0']).toBeDefined();
}); });
it('returns releases', async () => { it('returns releases', async () => {
const body = [ const body = [
{ tag_name: 'a' }, { tag_name: 'a' },
{ tag_name: 'b' }, { tag_name: 'v' },
{ tag_name: '1.0.0' }, { tag_name: '1.0.0' },
{ tag_name: 'v1.1.0' }, { tag_name: 'v1.1.0' },
]; ];
ghGot.mockReturnValueOnce({ headers: {}, body }); ghGot.mockReturnValueOnce({ headers: {}, body });
const res = await github.getDependency('some/dep', { ref: 'release' }); const res = await github.getDependency('some/dep', { ref: 'release' });
expect(res).toMatchSnapshot(); expect(res).toMatchSnapshot();
expect(Object.keys(res.versions)).toHaveLength(2); expect(Object.keys(res.versions)).toHaveLength(4);
expect(res.versions['v1.1.0']).toBeDefined(); expect(res.versions['v1.1.0']).toBeDefined();
}); });
it('returns null for invalid ref', async () => { it('returns null for invalid ref', async () => {