Remove legacy helper makeBadgeData (#3392)

Close #3369
This commit is contained in:
Paul Melnikow
2019-04-29 19:44:25 -04:00
committed by GitHub
parent b752bbfd5a
commit b2e21da34d
6 changed files with 49 additions and 121 deletions

View File

@@ -3,14 +3,11 @@
const { expect } = require('chai')
const sinon = require('sinon')
const Joi = require('joi')
const { makeBadgeData } = require('../../lib/badge-data')
const makeBadge = require('../../gh-badges/lib/make-badge')
const BaseSvgScrapingService = require('./base-svg-scraping')
function makeExampleSvg({ label, message }) {
const badgeData = makeBadgeData('this is the label', {})
badgeData.text[1] = 'this is the result!'
return makeBadge(badgeData)
return makeBadge({ text: ['this is the label', 'this is the result!'] })
}
const schema = Joi.object({

View File

@@ -6,7 +6,6 @@ const request = require('request')
const queryString = require('query-string')
const LruCache = require('../../gh-badges/lib/lru-cache')
const makeBadge = require('../../gh-badges/lib/make-badge')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const log = require('../server/log')
const { setCacheHeaders } = require('./cache-headers')
const {
@@ -15,6 +14,7 @@ const {
ShieldsRuntimeError,
} = require('./errors')
const { makeSend } = require('./legacy-result-sender')
const coalesceBadge = require('./coalesce-badge')
// We avoid calling the vendor's server for computation of the information in a
// number of badges.
@@ -38,8 +38,8 @@ vendorDomain.on('error', err => {
log.error('Vendor hook error:', err.stack)
})
// These query parameters are available to any badge. For the most part they
// are used by makeBadgeData (see `lib/badge-data.js`) and related functions.
// These query parameters are available to any badge. They are handled by
// `coalesceBadge`.
const globalQueryParams = new Set([
'label',
'style',
@@ -169,15 +169,18 @@ function handleRequest(cacheHeaderConfig, handlerOptions) {
return
}
ask.res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
const badgeData = getBadgeData('vendor', filteredQueryParams)
badgeData.text[1] = 'unresponsive'
const badgeData = coalesceBadge(
filteredQueryParams,
{ label: 'vendor', message: 'unresponsive' },
{}
)
const svg = makeBadge(badgeData)
let extension
try {
extension = match[0].split('.').pop()
} catch (e) {
extension = 'svg'
}
const svg = makeBadge(badgeData)
setCacheHeadersOnResponse(ask.res)
makeSend(extension, ask.res, end)(svg)
}, 25000)

View File

@@ -5,7 +5,7 @@ const fetch = require('node-fetch')
const nock = require('nock')
const portfinder = require('portfinder')
const Camp = require('camp')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const coalesceBadge = require('./coalesce-badge')
const {
handleRequest,
clearRequestCache,
@@ -19,17 +19,31 @@ async function performTwoRequests(baseUrl, first, second) {
function fakeHandler(queryParams, match, sendBadge, request) {
const [, someValue, format] = match
const badgeData = getBadgeData('testing', queryParams)
badgeData.text[1] = someValue
const badgeData = coalesceBadge(
queryParams,
{
label: 'testing',
message: someValue,
},
{}
)
sendBadge(format, badgeData)
}
function createFakeHandlerWithCacheLength(cacheLengthSeconds) {
return function fakeHandler(queryParams, match, sendBadge, request) {
const [, someValue, format] = match
const badgeData = getBadgeData('testing', queryParams)
badgeData.text[1] = someValue
badgeData.cacheLengthSeconds = cacheLengthSeconds
const badgeData = coalesceBadge(
queryParams,
{
label: 'testing',
message: someValue,
},
{},
{
_cacheLength: cacheLengthSeconds,
}
)
sendBadge(format, badgeData)
}
}
@@ -37,13 +51,21 @@ function createFakeHandlerWithCacheLength(cacheLengthSeconds) {
function fakeHandlerWithNetworkIo(queryParams, match, sendBadge, request) {
const [, someValue, format] = match
request('https://www.google.com/foo/bar', (err, res, buffer) => {
const badgeData = getBadgeData('testing', queryParams)
let message
if (err) {
badgeData.text[1] = err.prettyMessage
sendBadge(format, badgeData)
return
message = err.prettyMessage
} else {
message = someValue
}
badgeData.text[1] = someValue
const badgeData = coalesceBadge(
queryParams,
{
label: 'testing',
message,
format,
},
{}
)
sendBadge(format, badgeData)
})
}

View File

@@ -8,7 +8,6 @@ const Joi = require('joi')
const Camp = require('camp')
const makeBadge = require('../../gh-badges/lib/make-badge')
const GithubConstellation = require('../../services/github/github-constellation')
const { makeBadgeData } = require('../../lib/badge-data')
const suggest = require('../../services/suggest')
const { loadServiceClasses } = require('../base-service/loader')
const { makeSend } = require('../base-service/legacy-result-sender')
@@ -165,13 +164,12 @@ module.exports = class Server {
const { camp } = this
camp.notfound(/\.(svg|png|gif|jpg|json)/, (query, match, end, request) => {
const format = match[1]
const badgeData = makeBadgeData('404', query)
badgeData.text[1] = 'badge not found'
badgeData.colorB = 'red'
// Add format to badge data.
badgeData.format = format
const svg = makeBadge(badgeData)
const [, format] = match
const svg = makeBadge({
text: ['404', 'badge not found'],
color: 'red',
format,
})
makeSend(format, request.res, end)(svg)
})

View File

@@ -1,53 +0,0 @@
'use strict'
const coalesce = require('../core/base-service/coalesce')
const toArray = require('../core/base-service/to-array')
const { makeLogo } = require('./logos')
function makeLabel(defaultLabel, overrides) {
return `${
overrides.label === undefined
? (defaultLabel || '').toLowerCase()
: overrides.label
}`
}
// Generate the initial badge data. Pass the URL query parameters, which
// override the default label.
//
// The following parameters are supported:
//
// - label
// - style
// - logo
// - logoWidth
// - link
// - color and colorB
// - labelColor and colorA
// - maxAge
// - cacheSeconds
//
// Note: `maxAge` and `cacheSeconds` are handled by cache(), not this function.
function makeBadgeData(defaultLabel, overrides) {
const colorA = coalesce(overrides.labelColor, overrides.colorA)
const colorB = coalesce(overrides.color, overrides.colorB)
return {
text: [makeLabel(defaultLabel, overrides), 'n/a'],
colorscheme: 'lightgrey',
template: overrides.style,
logo: makeLogo(undefined, overrides),
logoPosition: +overrides.logoPosition,
logoWidth: +overrides.logoWidth,
links: toArray(overrides.link),
// Scoutcamp sometimes turns these into numbers.
colorA: typeof colorA === 'number' ? `${colorA}` : colorA,
colorB: typeof colorB === 'number' ? `${colorB}` : colorB,
}
}
module.exports = {
toArray,
makeLabel,
makeBadgeData,
}

View File

@@ -1,39 +0,0 @@
'use strict'
const { test, given } = require('sazerac')
const { makeLabel, makeBadgeData } = require('./badge-data')
describe('Badge data helpers', function() {
test(makeLabel, () => {
given('my badge', {}).expect('my badge')
given('My bAdge', {}).expect('my badge')
given('my badge', { label: 'no, my badge' }).expect('no, my badge')
given('my badge', { label: 'no, MY badge' }).expect('no, MY badge')
given('my badge', { label: false }).expect('false')
given('my badge', { label: 0 }).expect('0')
given('my badge', { label: '' }).expect('')
})
test(makeBadgeData, () => {
given('my badge', {
label: 'no, my badge',
style: 'flat-square',
logo: 'image/svg+xml;base64,PHN2ZyB4bWxu',
logoPosition: 10,
logoWidth: '25',
link: 'https://example.com/',
colorA: 'blue',
colorB: 'f00bae',
}).expect({
text: ['no, my badge', 'n/a'],
colorscheme: 'lightgrey',
template: 'flat-square',
logo: 'data:image/svg+xml;base64,PHN2ZyB4bWxu',
logoPosition: 10,
logoWidth: 25,
links: ['https://example.com/'],
colorA: 'blue',
colorB: 'f00bae',
})
})
})