diff --git a/__snapshots__/make-badge.spec.js b/__snapshots__/make-badge.spec.js index c2feaa5a1bdfc..cfce1a6c5675d 100644 --- a/__snapshots__/make-badge.spec.js +++ b/__snapshots__/make-badge.spec.js @@ -2,29 +2,18 @@ exports['The badge generator SVG should always produce the same SVG (unless we h cactuscactusgrowngrown ` -exports['The badge generator JSON should always produce the same JSON (unless we have changed something!) 1'] = ` -{ - "name": "cactus", - "label": "cactus", - "value": "grown", - "message": "grown", - "color": null, - "link": ["https://example.com/","https://other.example.com/"] -} -` - -exports['The badge generator badges with logos should always produce the same badge shields GitHub logo default color (#333333) 1'] = ` +exports['The badge generator badges with logos should always produce the same badge shields GitHub logo custom color (whitesmoke) 1'] = ` labellabelmessagemessage ` -exports['The badge generator badges with logos should always produce the same badge shields GitHub logo custom color (whitesmoke) 1'] = ` +exports['The badge generator badges with logos should always produce the same badge shields GitHub logo default color (#333333) 1'] = ` labellabelmessagemessage ` -exports['The badge generator badges with logos should always produce the same badge simple-icons javascript logo default color (#F7DF1E) 1'] = ` +exports['The badge generator badges with logos should always produce the same badge simple-icons javascript logo custom color (rgba(46,204,113,0.8)) 1'] = ` labellabelmessagemessage ` -exports['The badge generator badges with logos should always produce the same badge simple-icons javascript logo custom color (rgba(46,204,113,0.8)) 1'] = ` +exports['The badge generator badges with logos should always produce the same badge simple-icons javascript logo default color (#F7DF1E) 1'] = ` labellabelmessagemessage ` diff --git a/core/base-service/legacy-request-handler.spec.js b/core/base-service/legacy-request-handler.spec.js index 2a8b57e65f9be..b47e72d36462a 100644 --- a/core/base-service/legacy-request-handler.spec.js +++ b/core/base-service/legacy-request-handler.spec.js @@ -109,6 +109,7 @@ describe('The request handler', function() { label: 'testing', message: '123', color: 'lightgrey', + link: [], }) }) }) @@ -130,6 +131,7 @@ describe('The request handler', function() { label: 'testing', message: '123', color: 'lightgrey', + link: [], }) }) }) @@ -158,6 +160,7 @@ describe('The request handler', function() { label: 'testing', message: '123', color: 'lightgrey', + link: [], }) }) @@ -174,6 +177,7 @@ describe('The request handler', function() { label: 'testing', message: 'Maximum response size exceeded', color: 'lightgrey', + link: [], }) }) diff --git a/gh-badges/lib/make-badge.js b/gh-badges/lib/make-badge.js index ca8b09c1888bc..220c55692d811 100644 --- a/gh-badges/lib/make-badge.js +++ b/gh-badges/lib/make-badge.js @@ -21,58 +21,56 @@ templateFiles.forEach(async filename => { const extension = path.extname(filename).slice(1) const style = filename.slice(0, -`-template.${extension}`.length) // Compile the template. Necessary to always have a working template. - templates[`${style}-${extension}`] = dot.template(templateData) - if (extension === 'svg') { - // Substitute dot code. - const mapping = new Map() - let mappingIndex = 1 - const untemplatedSvg = templateData.replace(/{{.*?}}/g, match => { - // Weird substitution that currently works for all templates. - const mapKey = `99999990${mappingIndex}.1` - mappingIndex++ - mapping.set(mapKey, match) - return mapKey - }) - - const svgo = new SVGO() - const { data, error } = await svgo.optimize(untemplatedSvg) - - if (error !== undefined) { - console.error( - `Template ${filename}: ${error}\n` + - ' Generated untemplated SVG:\n' + - `---\n${untemplatedSvg}---\n` - ) - return - } + templates[style] = dot.template(templateData) + // Substitute dot code. + const mapping = new Map() + let mappingIndex = 1 + const untemplatedSvg = templateData.replace(/{{.*?}}/g, match => { + // Weird substitution that currently works for all templates. + const mapKey = `99999990${mappingIndex}.1` + mappingIndex++ + mapping.set(mapKey, match) + return mapKey + }) + + const svgo = new SVGO() + const { data, error } = await svgo.optimize(untemplatedSvg) + + if (error !== undefined) { + console.error( + `Template ${filename}: ${error}\n` + + ' Generated untemplated SVG:\n' + + `---\n${untemplatedSvg}---\n` + ) + return + } - // Substitute dot code back. - let svg = data - const unmappedKeys = [] - mapping.forEach((value, key) => { - let keySubstituted = false - svg = svg.replace(RegExp(key, 'g'), () => { - keySubstituted = true - return value - }) - if (!keySubstituted) { - unmappedKeys.push(key) - } + // Substitute dot code back. + let svg = data + const unmappedKeys = [] + mapping.forEach((value, key) => { + let keySubstituted = false + svg = svg.replace(RegExp(key, 'g'), () => { + keySubstituted = true + return value }) - if (unmappedKeys.length > 0) { - console.error( - `Template ${filename} has unmapped keys ` + - `${unmappedKeys.join(', ')}.\n` + - ' Generated untemplated SVG:\n' + - `---\n${untemplatedSvg}\n---\n` + - ' Generated template:\n' + - `---\n${svg}\n---\n` - ) - return + if (!keySubstituted) { + unmappedKeys.push(key) } - - templates[`${style}-${extension}`] = dot.template(svg) + }) + if (unmappedKeys.length > 0) { + console.error( + `Template ${filename} has unmapped keys ` + + `${unmappedKeys.join(', ')}.\n` + + ' Generated untemplated SVG:\n' + + `---\n${untemplatedSvg}\n---\n` + + ' Generated template:\n' + + `---\n${svg}\n---\n` + ) + return } + + templates[style] = dot.template(svg) }) function escapeXml(s) { @@ -92,7 +90,7 @@ function capitalize(s) { return s.charAt(0).toUpperCase() + s.slice(1) } -function makeBadge({ +module.exports = function makeBadge({ format, template, text, @@ -109,12 +107,27 @@ function makeBadge({ // String coercion and whitespace removal. text = text.map(value => `${value}`.trim()) - if (format !== 'json') { - format = 'svg' + const [left, right] = text + + color = normalizeColor(color || colorB || colorscheme) + labelColor = normalizeColor(labelColor || colorA) + + // This ought to be the responsibility of the server, not `makeBadge`. + if (format === 'json') { + return JSON.stringify({ + label: left, + message: right, + logoWidth, + color, + labelColor, + link: links, + name: left, + value: right, + }) } - if (!(`${template}-${format}` in templates)) { - template = format === 'svg' ? 'flat' : 'default' + if (!(template in templates)) { + template = 'flat' } if (template.startsWith('popout')) { if (logo) { @@ -131,7 +144,6 @@ function makeBadge({ text = text.map(value => value.toUpperCase()) } - const [left, right] = text let leftWidth = (anafanafo(left) / 10) | 0 // Increase chances of pixel grid alignment. if (leftWidth % 2 === 0) { @@ -152,9 +164,6 @@ function makeBadge({ logoPadding = logo ? 3 : 0 } - color = color || colorB || colorscheme - labelColor = labelColor || colorA - const context = { text: [left, right], escapedText: text.map(escapeXml), @@ -164,18 +173,13 @@ function makeBadge({ logoPosition, logoWidth, logoPadding, - // `color` and `labelColor` are included for the `default` JSON template. - color: normalizeColor(color), - labelColor: normalizeColor(labelColor), colorA: toSvgColor(labelColor), colorB: toSvgColor(color), escapeXml, } - const templateFn = templates[`${template}-${format}`] + const templateFn = templates[template] // The call to template() can raise an exception. return templateFn(context) } - -module.exports = makeBadge diff --git a/gh-badges/lib/make-badge.spec.js b/gh-badges/lib/make-badge.spec.js index a467a5ed08487..d3beed9f50c94 100644 --- a/gh-badges/lib/make-badge.spec.js +++ b/gh-badges/lib/make-badge.spec.js @@ -3,7 +3,6 @@ const { test, given, forCases } = require('sazerac') const { expect } = require('chai') const snapshot = require('snap-shot-it') -const eol = require('eol') const isSvg = require('is-svg') const makeBadge = require('./make-badge') @@ -63,7 +62,7 @@ describe('The badge generator', function() { given('almostred'), given('brightmaroon'), given('cactus') - ).expect(null) + ).expect(undefined) }) }) @@ -93,34 +92,18 @@ describe('The badge generator', function() { }) describe('JSON', function() { - it('should always produce the same JSON (unless we have changed something!)', function() { + it('should produce the expected JSON', function() { const json = makeBadge({ text: ['cactus', 'grown'], format: 'json', links: ['https://example.com/', 'https://other.example.com/'], }) - const jsonWithLFEndings = eol.lf(json) - snapshot(jsonWithLFEndings) - }) - - it('should replace unknown json template with "default"', function() { - const jsonBadgeWithUnknownStyle = makeBadge({ - text: ['name', 'Bob'], - format: 'json', - template: 'unknown_style', - }) - const jsonBadgeWithDefaultStyle = makeBadge({ - text: ['name', 'Bob'], - format: 'json', - template: 'default', - }) - expect(jsonBadgeWithUnknownStyle).to.equal(jsonBadgeWithDefaultStyle) - expect(JSON.parse(jsonBadgeWithUnknownStyle)).to.deep.equal({ - name: 'name', - value: 'Bob', - label: 'name', - message: 'Bob', - color: null, + expect(JSON.parse(json)).to.deep.equal({ + name: 'cactus', + label: 'cactus', + value: 'grown', + message: 'grown', + link: ['https://example.com/', 'https://other.example.com/'], }) }) diff --git a/gh-badges/templates/default-template.json b/gh-badges/templates/default-template.json deleted file mode 100644 index 19630a2913913..0000000000000 --- a/gh-badges/templates/default-template.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "name": {{=JSON.stringify(it.text[0])}}, - "label": {{=JSON.stringify(it.text[0])}}, - "value": {{=JSON.stringify(it.text[1])}}, - "message": {{=JSON.stringify(it.text[1])}},{{?it.logoWidth}} - "logoWidth": {{=JSON.stringify(it.logoWidth)}},{{?}}{{?it.labelColor}} - "labelColor": {{=JSON.stringify(it.labelColor)}},{{?}} - "color": {{=JSON.stringify(it.color || null)}}{{?(it.links[0] && it.links[0].length || it.links[1] && it.links[1].length)}}, - "link": {{=JSON.stringify(it.links)}}{{?}} -} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 5c03021e20988..303bce9997f7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4684,6 +4684,7 @@ "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", "requires": { "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", "supports-color": "^5.3.0" }, "dependencies": { @@ -4694,6 +4695,11 @@ "requires": { "color-convert": "^1.9.0" } + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" } } }, @@ -8531,12 +8537,6 @@ } } }, - "eol": { - "version": "0.9.1", - "resolved": "https://registry.npmjs.org/eol/-/eol-0.9.1.tgz", - "integrity": "sha512-Ds/TEoZjwggRoz/Q2O7SE3i4Jm66mqTDfmdHdq/7DKVk3bro9Q8h6WdXKdPqFLMoqxrDK5SVRzHVPOS6uuGtrg==", - "dev": true - }, "errno": { "version": "0.1.7", "resolved": "https://registry.npmjs.org/errno/-/errno-0.1.7.tgz", diff --git a/package.json b/package.json index 97c71a7dbe6cf..8dfb5b53a1e37 100644 --- a/package.json +++ b/package.json @@ -147,7 +147,6 @@ "danger-plugin-no-test-shortcuts": "^2.0.0", "enzyme": "^3.9.0", "enzyme-adapter-react-16": "^1.12.1", - "eol": "^0.9.1", "escape-string-regexp": "^2.0.0", "eslint": "^5.16.0", "eslint-config-prettier": "^4.1.0", diff --git a/services/dependabot/dependabot.tester.js b/services/dependabot/dependabot.tester.js index eb215cde7c0ab..610e0649a5f38 100644 --- a/services/dependabot/dependabot.tester.js +++ b/services/dependabot/dependabot.tester.js @@ -9,7 +9,7 @@ t.create('semver stability (valid)') label: 'semver stability', message: isIntegerPercentage, link: [ - 'https://dependabot.com/compatibility-score.html?package-manager=bundler&dependency-name=puma&version-scheme=semver', + 'https://dependabot.com/compatibility-score.html?package-manager=bundler&dependency-name=puma&version-scheme=semver', ], }) diff --git a/services/twitter/twitter.tester.js b/services/twitter/twitter.tester.js index 943dd22c7ee0e..9df67492df6a6 100644 --- a/services/twitter/twitter.tester.js +++ b/services/twitter/twitter.tester.js @@ -32,7 +32,7 @@ t.create('URL') label: 'tweet', message: '', link: [ - 'https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fshields.io', + 'https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fshields.io', 'https://twitter.com/search?q=https%3A%2F%2Fshields.io', ], })