Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use most permissive license color #2196

Merged
merged 4 commits into from
Oct 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions lib/licenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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),
}
}
Expand Down
12 changes: 11 additions & 1 deletion lib/licenses.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand All @@ -30,7 +40,7 @@ describe('license helpers', function() {
})
given({ licenses: ['MPL-2.0', 'MIT'] }).expect({
message: 'MPL-2.0, MIT',
color: 'lightgrey',
color: 'green',
})
})
})