From 1497b4962bf43767c793a9807459006e462f5f51 Mon Sep 17 00:00:00 2001 From: IKEDA Sho <suicaicoca@gmail.com> Date: Thu, 1 Aug 2019 02:24:57 +0900 Subject: [PATCH] fix(datasource): massage GitHub sourceUrl with http and/or www (#4217) --- lib/datasource/metadata.js | 28 +++++++++++-------- .../__snapshots__/metadata.spec.js.snap | 6 ++-- test/datasource/metadata.spec.js | 14 +++++++++- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/datasource/metadata.js b/lib/datasource/metadata.js index 601247523a..a8ffb86dc6 100644 --- a/lib/datasource/metadata.js +++ b/lib/datasource/metadata.js @@ -68,6 +68,7 @@ function addMetaData(dep, datasource, lookupName) { if (!dep) { return; } + const depName = lookupName ? lookupName.toLowerCase() : null; if ( manualChangelogUrls[datasource] && @@ -78,24 +79,29 @@ function addMetaData(dep, datasource, lookupName) { if (manualSourceUrls[datasource] && manualSourceUrls[datasource][depName]) { dep.sourceUrl = manualSourceUrls[datasource][depName]; } + + /** + * @param {string} url + */ + const massageGithubUrl = url => { + return url + .replace('http:', 'https:') + .replace('www.github.com', 'github.com') + .split('/') + .slice(0, 5) + .join('/'); + }; if (dep.sourceUrl && dep.sourceUrl.includes('github.com')) { - dep.sourceUrl = parse( - dep.sourceUrl - .split('/') - .slice(0, 5) - .join('/') - ); + dep.sourceUrl = parse(massageGithubUrl(dep.sourceUrl)); } if ( !dep.sourceUrl && dep.changelogUrl && - dep.changelogUrl.startsWith('https://github.com/') + dep.changelogUrl.match(/^https?:\/\/(www\.)?github\.com/) ) { - dep.sourceUrl = dep.changelogUrl - .split('/') - .slice(0, 5) - .join('/'); + dep.sourceUrl = massageGithubUrl(dep.changelogUrl); } + // Clean up any empty urls const urls = ['homepage', 'sourceUrl', 'changelogUrl']; for (const url of urls) { diff --git a/test/datasource/__snapshots__/metadata.spec.js.snap b/test/datasource/__snapshots__/metadata.spec.js.snap index f881ca449e..ad37c73d22 100644 --- a/test/datasource/__snapshots__/metadata.spec.js.snap +++ b/test/datasource/__snapshots__/metadata.spec.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`datasource/terraform Should handle manualChangelogUrls 1`] = ` +exports[`datasource/metadata Should handle manualChangelogUrls 1`] = ` Object { "changelogUrl": "https://github.com/django/django/tree/master/docs/releases", "releases": Array [ @@ -25,7 +25,7 @@ Object { } `; -exports[`datasource/terraform Should handle manualSourceUrls 1`] = ` +exports[`datasource/metadata Should handle manualSourceUrls 1`] = ` Object { "releases": Array [ Object { @@ -49,7 +49,7 @@ Object { } `; -exports[`datasource/terraform Should handle parsing of sourceUrls correctly 1`] = ` +exports[`datasource/metadata Should handle parsing of sourceUrls correctly 1`] = ` Object { "releases": Array [ Object { diff --git a/test/datasource/metadata.spec.js b/test/datasource/metadata.spec.js index 958387bab6..2d8ddaf16a 100644 --- a/test/datasource/metadata.spec.js +++ b/test/datasource/metadata.spec.js @@ -1,6 +1,6 @@ const { addMetaData } = require('../../lib/datasource/metadata'); -describe('datasource/terraform', () => { +describe('datasource/metadata', () => { it('Should do nothing if dep is not specified', () => { expect(addMetaData()).toBeUndefined(); }); @@ -64,4 +64,16 @@ describe('datasource/terraform', () => { addMetaData(dep, datasource, lookupName); expect(dep).toMatchSnapshot(); }); + + it('Should handle parsing/converting of GitHub sourceUrls with http and www correctly', () => { + const dep = { + sourceUrl: 'http://www.github.com/mockk/mockk/', + releases: [{ version: '1.9.3' }], + }; + const datasource = 'maven'; + const lookupName = 'io.mockk:mockk'; + + addMetaData(dep, datasource, lookupName); + expect(dep.sourceUrl).toEqual('https://github.com/mockk/mockk'); + }); }); -- GitLab