Refactor badge color functions (#2742)
- Replace the idea of color schemes with the idea of named colors (since none of our colorschemes have used `colorA`)
- Pass through the normalized color to `_shields_test` to harmonize with BaseService and simplify testing
- Update service tests
- Move responsibility for color generation into the npm package
- Remove several color helper functions and their tests
- Update gh-badge public API to accept `color` and `labelColor`
This is a precursor to refactoring some of the logo code for #2473.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
const { colorScheme: colorsB } = require('../test-helpers')
|
||||
const { invalidJSON } = require('../response-fixtures')
|
||||
|
||||
const t = (module.exports = require('../create-service-tester')())
|
||||
@@ -12,7 +11,7 @@ t.create('commit status - commit in branch')
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'in master',
|
||||
colorB: colorsB.brightgreen,
|
||||
color: 'brightgreen',
|
||||
})
|
||||
|
||||
t.create(
|
||||
@@ -31,7 +30,7 @@ t.create(
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'in master',
|
||||
colorB: colorsB.brightgreen,
|
||||
color: 'brightgreen',
|
||||
})
|
||||
|
||||
t.create('commit status - commit not in branch')
|
||||
@@ -41,7 +40,7 @@ t.create('commit status - commit not in branch')
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'commit or branch not found',
|
||||
colorB: colorsB.lightgrey,
|
||||
color: 'lightgrey',
|
||||
})
|
||||
|
||||
t.create('commit status - unknown commit id')
|
||||
@@ -51,7 +50,7 @@ t.create('commit status - unknown commit id')
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'not in v1.27.1',
|
||||
colorB: colorsB.yellow,
|
||||
color: 'yellow',
|
||||
})
|
||||
|
||||
t.create('commit status - unknown branch')
|
||||
@@ -61,7 +60,7 @@ t.create('commit status - unknown branch')
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'commit or branch not found',
|
||||
colorB: colorsB.lightgrey,
|
||||
color: 'lightgrey',
|
||||
})
|
||||
|
||||
t.create('commit status - no common ancestor between commit and branch')
|
||||
@@ -71,7 +70,7 @@ t.create('commit status - no common ancestor between commit and branch')
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'no common ancestor',
|
||||
colorB: colorsB.lightgrey,
|
||||
color: 'lightgrey',
|
||||
})
|
||||
|
||||
t.create('commit status - invalid JSON')
|
||||
@@ -88,7 +87,7 @@ t.create('commit status - invalid JSON')
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'invalid',
|
||||
colorB: colorsB.lightgrey,
|
||||
color: 'lightgrey',
|
||||
})
|
||||
|
||||
t.create('commit status - network error')
|
||||
@@ -99,7 +98,7 @@ t.create('commit status - network error')
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'inaccessible',
|
||||
colorB: colorsB.red,
|
||||
color: 'red',
|
||||
})
|
||||
|
||||
t.create('commit status - github server error')
|
||||
@@ -116,7 +115,7 @@ t.create('commit status - github server error')
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'invalid',
|
||||
colorB: colorsB.lightgrey,
|
||||
color: 'lightgrey',
|
||||
})
|
||||
|
||||
t.create('commit status - 404 with empty JSON form github')
|
||||
@@ -133,7 +132,7 @@ t.create('commit status - 404 with empty JSON form github')
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'invalid',
|
||||
colorB: colorsB.lightgrey,
|
||||
color: 'lightgrey',
|
||||
})
|
||||
|
||||
t.create('commit status - 404 with invalid JSON form github')
|
||||
@@ -150,5 +149,5 @@ t.create('commit status - 404 with invalid JSON form github')
|
||||
.expectJSON({
|
||||
name: 'commit status',
|
||||
value: 'invalid',
|
||||
colorB: colorsB.lightgrey,
|
||||
color: 'lightgrey',
|
||||
})
|
||||
|
||||
@@ -66,7 +66,7 @@ module.exports = class GithubFollowers extends LegacyService {
|
||||
}
|
||||
try {
|
||||
badgeData.text[1] = JSON.parse(buffer).followers
|
||||
badgeData.colorscheme = null
|
||||
badgeData.colorscheme = undefined
|
||||
badgeData.colorB = '#4183C4'
|
||||
sendBadge(format, badgeData)
|
||||
} catch (e) {
|
||||
|
||||
@@ -63,7 +63,7 @@ module.exports = class GithubForks extends LegacyService {
|
||||
const data = JSON.parse(buffer)
|
||||
const forks = data.forks_count
|
||||
badgeData.text[1] = forks
|
||||
badgeData.colorscheme = null
|
||||
badgeData.colorscheme = undefined
|
||||
badgeData.colorB = '#4183C4'
|
||||
sendBadge(format, badgeData)
|
||||
} catch (e) {
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
const LegacyService = require('../legacy-service')
|
||||
const {
|
||||
makeColorB,
|
||||
makeLabel: getLabel,
|
||||
makeBadgeData: getBadgeData,
|
||||
makeLogo: getLogo,
|
||||
@@ -116,11 +115,8 @@ module.exports = class GithubIssueDetail extends LegacyService {
|
||||
switch (which) {
|
||||
case 's': {
|
||||
const state = (badgeData.text[1] = parsedData.state)
|
||||
badgeData.colorscheme = null
|
||||
badgeData.colorB = makeColorB(
|
||||
githubStateColor(state),
|
||||
queryParams
|
||||
)
|
||||
badgeData.colorscheme = undefined
|
||||
badgeData.colorB = queryParams.colorB || githubStateColor(state)
|
||||
break
|
||||
}
|
||||
case 'title':
|
||||
@@ -136,21 +132,17 @@ module.exports = class GithubIssueDetail extends LegacyService {
|
||||
.map(i => i.name)
|
||||
.join(' | ')
|
||||
if (parsedData.labels.length === 1) {
|
||||
badgeData.colorscheme = null
|
||||
badgeData.colorB = makeColorB(
|
||||
parsedData.labels[0].color,
|
||||
queryParams
|
||||
)
|
||||
badgeData.colorscheme = undefined
|
||||
badgeData.colorB =
|
||||
queryParams.colorB || parsedData.labels[0].color
|
||||
}
|
||||
break
|
||||
case 'comments': {
|
||||
badgeData.text[0] = getLabel('comments', queryParams)
|
||||
const comments = (badgeData.text[1] = parsedData.comments)
|
||||
badgeData.colorscheme = null
|
||||
badgeData.colorB = makeColorB(
|
||||
githubCommentsColor(comments),
|
||||
queryParams
|
||||
)
|
||||
badgeData.colorscheme = undefined
|
||||
badgeData.colorB =
|
||||
queryParams.coloB || githubCommentsColor(comments)
|
||||
break
|
||||
}
|
||||
case 'age':
|
||||
|
||||
@@ -4,7 +4,6 @@ const LegacyService = require('../legacy-service')
|
||||
const {
|
||||
makeBadgeData: getBadgeData,
|
||||
makeLogo: getLogo,
|
||||
setBadgeColor,
|
||||
} = require('../../lib/badge-data')
|
||||
const { licenseToColor } = require('../../lib/licenses')
|
||||
const {
|
||||
@@ -76,7 +75,7 @@ module.exports = class GithubLicense extends LegacyService {
|
||||
} else {
|
||||
badgeData.text[1] = license.spdx_id
|
||||
}
|
||||
setBadgeColor(badgeData, licenseToColor(license.spdx_id))
|
||||
badgeData.colorB = licenseToColor(license.spdx_id)
|
||||
sendBadge(format, badgeData)
|
||||
} else {
|
||||
badgeData.text[1] = 'missing'
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const { makeColor } = require('../../lib/badge-data')
|
||||
const { licenseToColor } = require('../../lib/licenses')
|
||||
const { colorScheme: colorsB } = require('../test-helpers')
|
||||
|
||||
const publicDomainLicenseColor = makeColor(licenseToColor('CC0-1.0'))
|
||||
const permissiveLicenseColor = colorsB[licenseToColor('MIT')]
|
||||
const copyleftLicenseColor = colorsB[licenseToColor('GPL-3.0')]
|
||||
const unknownLicenseColor = colorsB[licenseToColor()]
|
||||
const publicDomainLicenseColor = licenseToColor('CC0-1.0')
|
||||
const permissiveLicenseColor = licenseToColor('MIT')
|
||||
const copyleftLicenseColor = licenseToColor('GPL-3.0')
|
||||
const unknownLicenseColor = licenseToColor()
|
||||
|
||||
const t = (module.exports = require('../create-service-tester')())
|
||||
|
||||
@@ -16,7 +14,7 @@ t.create('Public domain license')
|
||||
.expectJSON({
|
||||
name: 'license',
|
||||
value: 'CC0-1.0',
|
||||
colorB: publicDomainLicenseColor,
|
||||
color: `#${publicDomainLicenseColor}`,
|
||||
})
|
||||
|
||||
t.create('Copyleft license')
|
||||
@@ -24,23 +22,23 @@ t.create('Copyleft license')
|
||||
.expectJSON({
|
||||
name: 'license',
|
||||
value: 'GPL-3.0',
|
||||
colorB: copyleftLicenseColor,
|
||||
color: copyleftLicenseColor,
|
||||
})
|
||||
|
||||
t.create('Permissive license')
|
||||
.get('/atom/atom.json?style=_shields_test')
|
||||
.expectJSON({ name: 'license', value: 'MIT', colorB: permissiveLicenseColor })
|
||||
.expectJSON({ name: 'license', value: 'MIT', color: permissiveLicenseColor })
|
||||
|
||||
t.create('License for repo without a license')
|
||||
.get('/badges/badger.json?style=_shields_test')
|
||||
.expectJSON({ name: 'license', value: 'missing', colorB: colorsB.red })
|
||||
.expectJSON({ name: 'license', value: 'missing', color: 'red' })
|
||||
|
||||
t.create('License for repo with an unrecognized license')
|
||||
.get('/philokev/sopel-noblerealms.json?style=_shields_test')
|
||||
.expectJSON({
|
||||
name: 'license',
|
||||
value: 'unknown',
|
||||
colorB: unknownLicenseColor,
|
||||
color: unknownLicenseColor,
|
||||
})
|
||||
|
||||
t.create('License with SPDX id not appearing in configuration')
|
||||
@@ -63,7 +61,7 @@ t.create('License with SPDX id not appearing in configuration')
|
||||
.expectJSON({
|
||||
name: 'license',
|
||||
value: 'EFL-1.0',
|
||||
colorB: unknownLicenseColor,
|
||||
color: unknownLicenseColor,
|
||||
})
|
||||
|
||||
t.create('License for unknown repo')
|
||||
@@ -71,7 +69,7 @@ t.create('License for unknown repo')
|
||||
.expectJSON({
|
||||
name: 'license',
|
||||
value: 'repo not found',
|
||||
colorB: colorsB.lightgrey,
|
||||
color: 'lightgrey',
|
||||
})
|
||||
|
||||
t.create('License - API rate limit exceeded')
|
||||
@@ -89,5 +87,5 @@ t.create('License - API rate limit exceeded')
|
||||
.expectJSON({
|
||||
name: 'license',
|
||||
value: 'access denied',
|
||||
colorB: colorsB.lightgrey,
|
||||
color: 'lightgrey',
|
||||
})
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('joi')
|
||||
const { colorScheme: colorsB } = require('../test-helpers')
|
||||
|
||||
const t = (module.exports = require('../create-service-tester')())
|
||||
|
||||
@@ -27,7 +26,7 @@ t.create('Stars (named color override)')
|
||||
Joi.object().keys({
|
||||
name: 'stars',
|
||||
value: Joi.string().regex(/^\w+$/),
|
||||
colorB: Joi.equal(colorsB.yellow).required(),
|
||||
color: 'yellow',
|
||||
})
|
||||
)
|
||||
|
||||
@@ -37,6 +36,6 @@ t.create('Stars (hex color override)')
|
||||
Joi.object().keys({
|
||||
name: 'stars',
|
||||
value: Joi.string().regex(/^\w+$/),
|
||||
colorB: Joi.equal('#abcdef').required(),
|
||||
color: '#abcdef',
|
||||
})
|
||||
)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('joi')
|
||||
const { colorScheme: colorsB } = require('../test-helpers')
|
||||
|
||||
const t = (module.exports = require('../create-service-tester')())
|
||||
|
||||
@@ -30,7 +29,7 @@ t.create('Tag (mocked response, no pre-releases, semver ordering)')
|
||||
.get('/repos/foo/bar/tags')
|
||||
.reply(200, tagsFixture)
|
||||
)
|
||||
.expectJSON({ name: 'tag', value: 'v1.2', colorB: colorsB.blue })
|
||||
.expectJSON({ name: 'tag', value: 'v1.2', color: 'blue' })
|
||||
|
||||
t.create('Tag (mocked response, include pre-releases, semver ordering)')
|
||||
.get('/tag-pre/foo/bar.json?style=_shields_test')
|
||||
@@ -39,7 +38,7 @@ t.create('Tag (mocked response, include pre-releases, semver ordering)')
|
||||
.get('/repos/foo/bar/tags')
|
||||
.reply(200, tagsFixture)
|
||||
)
|
||||
.expectJSON({ name: 'tag', value: 'v1.3-beta3', colorB: colorsB.orange })
|
||||
.expectJSON({ name: 'tag', value: 'v1.3-beta3', color: 'orange' })
|
||||
|
||||
t.create('Tag (mocked response, date ordering)')
|
||||
.get('/tag-date/foo/bar.json?style=_shields_test')
|
||||
@@ -48,4 +47,4 @@ t.create('Tag (mocked response, date ordering)')
|
||||
.get('/repos/foo/bar/tags')
|
||||
.reply(200, tagsFixture)
|
||||
)
|
||||
.expectJSON({ name: 'tag', value: 'cheese', colorB: colorsB.blue })
|
||||
.expectJSON({ name: 'tag', value: 'cheese', color: 'blue' })
|
||||
|
||||
Reference in New Issue
Block a user