From 4554f49c1ece77e745fc232b98058b698fce8443 Mon Sep 17 00:00:00 2001 From: Przemo Nowaczyk Date: Sat, 20 Oct 2018 15:15:10 +0200 Subject: [PATCH] use most permissive license color (#2196) * use most permissive license color * revert to possible non-array licenseToColor argument --- lib/licenses.js | 27 +++++++++++++++++++++------ lib/licenses.spec.js | 12 +++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/licenses.js b/lib/licenses.js index afb4eb3deeade..2ce34ad2e2f24 100644 --- a/lib/licenses.js +++ b/lib/licenses.js @@ -23,6 +23,7 @@ const licenseTypes = { 'Zlib', ], color: 'green', + priority: '2', }, // copyleft licenses require 'Disclose source' (https://choosealicense.com/appendix/#disclose-source) // or 'Same license' (https://choosealicense.com/appendix/#same-license) @@ -43,24 +44,40 @@ const licenseTypes = { 'OSL-3.0', ], color: 'orange', + priority: '1', }, // public domain licenses do not require 'License and copyright notice' (https://choosealicense.com/appendix/#include-copyright) 'public-domain': { spdxLicenseIds: ['CC0-1.0', 'Unlicense', 'WTFPL'], color: '7cd958', + priority: '3', }, } const licenseToColorMap = {} Object.keys(licenseTypes).forEach(licenseType => { - const { spdxLicenseIds, color } = licenseTypes[licenseType] + const { spdxLicenseIds, color, priority } = licenseTypes[licenseType] spdxLicenseIds.forEach(license => { - licenseToColorMap[license] = color + licenseToColorMap[license] = { color, priority } }) }) const defaultLicenseColor = 'lightgrey' -const licenseToColor = spdxId => - licenseToColorMap[spdxId] || defaultLicenseColor +const licenseToColor = spdxId => { + if (!Array.isArray(spdxId)) { + return ( + (licenseToColorMap[spdxId] && licenseToColorMap[spdxId].color) || + defaultLicenseColor + ) + } + const licenseType = spdxId + .filter(i => licenseToColorMap[i]) + .map(i => licenseToColorMap[i]) + .reduce((a, b) => (a.priority > b.priority ? a : b), { + color: defaultLicenseColor, + priority: 0, + }) + return licenseType.color +} function renderLicenseBadge({ license, licenses }) { if (licenses === undefined) { @@ -73,8 +90,6 @@ function renderLicenseBadge({ license, licenses }) { return { message: licenses.join(', '), - // TODO This does not provide a color when more than one license is - // present. Probably that should be fixed. color: licenseToColor(licenses), } } diff --git a/lib/licenses.spec.js b/lib/licenses.spec.js index 45048a1afd04f..d53385684017b 100644 --- a/lib/licenses.spec.js +++ b/lib/licenses.spec.js @@ -10,6 +10,16 @@ describe('license helpers', function() { given('Unlicense').expect('7cd958') given('unknown-license').expect('lightgrey') given(null).expect('lightgrey') + + given(['CC0-1.0', 'MPL-2.0']).expect('7cd958') + given(['MPL-2.0', 'CC0-1.0']).expect('7cd958') + given(['MIT', 'MPL-2.0']).expect('green') + given(['MPL-2.0', 'MIT']).expect('green') + given(['OFL-1.1', 'MPL-2.0']).expect('orange') + given(['MPL-2.0', 'OFL-1.1']).expect('orange') + given(['CC0-1.0', 'MIT', 'MPL-2.0']).expect('7cd958') + given(['UNKNOWN-1.0', 'MIT']).expect('green') + given(['UNKNOWN-1.0', 'UNKNOWN-2.0']).expect('lightgrey') }) test(renderLicenseBadge, () => { @@ -30,7 +40,7 @@ describe('license helpers', function() { }) given({ licenses: ['MPL-2.0', 'MIT'] }).expect({ message: 'MPL-2.0, MIT', - color: 'lightgrey', + color: 'green', }) }) })