From 019347737bded3c081bbf3b997800dbfa08caffa Mon Sep 17 00:00:00 2001 From: PyvesB Date: Sun, 9 Sep 2018 10:37:01 +0100 Subject: [PATCH 1/2] Fixed Codacy badges --- lib/svg-badge-parser.js | 9 ++-- lib/svg-badge-parser.spec.js | 2 +- services/codacy/codacy-coverage.service.js | 2 +- services/codacy/codacy-grade.service.js | 2 +- services/codacy/codacy.tester.js | 52 +++++++++++++++++++++ services/readthedocs/readthedocs.service.js | 2 +- services/vso/vso.service.js | 2 +- 7 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 services/codacy/codacy.tester.js diff --git a/lib/svg-badge-parser.js b/lib/svg-badge-parser.js index 144d52e4c953e..3f627ca1151b5 100644 --- a/lib/svg-badge-parser.js +++ b/lib/svg-badge-parser.js @@ -3,14 +3,13 @@ const nodeifySync = require('./nodeify-sync') const leadingWhitespace = /(?:\r\n\s*|\r\s*|\n\s*)/g -const getValue = />([^<>]+)<\/text><\/g>/ -function valueFromSvgBadge(svg) { +function valueFromSvgBadge(svg, valueMatcher) { if (typeof svg !== 'string') { throw TypeError('Parameter should be a string') } const stripped = svg.replace(leadingWhitespace, '') - const match = getValue.exec(stripped) + const match = valueMatcher.exec(stripped) if (match) { return match[1] } else { @@ -20,12 +19,12 @@ function valueFromSvgBadge(svg) { // Get data from a svg-style badge. // cb: function(err, string) -function fetchFromSvg(request, url, cb) { +function fetchFromSvg(request, url, valueMatcher, cb) { request(url, (err, res, buffer) => { if (err !== null) { cb(err) } else { - nodeifySync(() => valueFromSvgBadge(buffer), cb) + nodeifySync(() => valueFromSvgBadge(buffer, valueMatcher), cb) } }) } diff --git a/lib/svg-badge-parser.spec.js b/lib/svg-badge-parser.spec.js index abff3a46390f3..be8ff82cd8453 100644 --- a/lib/svg-badge-parser.spec.js +++ b/lib/svg-badge-parser.spec.js @@ -14,6 +14,6 @@ describe('The SVG badge parser', function() { const exampleSvg = makeBadge(badgeData) - expect(valueFromSvgBadge(exampleSvg)).to.equal('this is the result!') + expect(valueFromSvgBadge(exampleSvg, />([^<>]+)<\/text><\/g>/)).to.equal('this is the result!') }) }) diff --git a/services/codacy/codacy-coverage.service.js b/services/codacy/codacy-coverage.service.js index 381df73248fed..5eb78847b9f50 100644 --- a/services/codacy/codacy-coverage.service.js +++ b/services/codacy/codacy-coverage.service.js @@ -28,7 +28,7 @@ module.exports = class CodacyCoverage extends LegacyService { '?' + query const badgeData = getBadgeData('coverage', data) - fetchFromSvg(request, url, (err, res) => { + fetchFromSvg(request, url, /text-anchor="middle">([^<>]+)<\/text>/, (err, res) => { if (err != null) { badgeData.text[1] = 'inaccessible' sendBadge(format, badgeData) diff --git a/services/codacy/codacy-grade.service.js b/services/codacy/codacy-grade.service.js index 8d884007e4e54..aadb87acc4ef4 100644 --- a/services/codacy/codacy-grade.service.js +++ b/services/codacy/codacy-grade.service.js @@ -25,7 +25,7 @@ module.exports = class CodacyGrade extends LegacyService { '?' + query const badgeData = getBadgeData('code quality', data) - fetchFromSvg(request, url, (err, res) => { + fetchFromSvg(request, url, /visibility="hidden">([^<>]+)<\/text>/, (err, res) => { if (err != null) { badgeData.text[1] = 'inaccessible' sendBadge(format, badgeData) diff --git a/services/codacy/codacy.tester.js b/services/codacy/codacy.tester.js new file mode 100644 index 0000000000000..e5079922a1cd6 --- /dev/null +++ b/services/codacy/codacy.tester.js @@ -0,0 +1,52 @@ +'use strict' + +const Joi = require('joi') +const ServiceTester = require('../service-tester') +const { isIntegerPercentage } = require('../test-validators') +const isCodacyGrade = Joi.equal( + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', +) + +const t = new ServiceTester({ id: 'codacy', title: 'Codacy' }) +module.exports = t + +t.create('Coverage') + .get('/coverage/59d607d0e311408885e418004068ea58.json') + .expectJSONTypes( + Joi.object().keys({ + name: 'coverage', + value: isIntegerPercentage, + }) + ) + +t.create('Coverage on branch') + .get('/coverage/59d607d0e311408885e418004068ea58/master.json') + .expectJSONTypes( + Joi.object().keys({ + name: 'coverage', + value: isIntegerPercentage, + }) + ) + +t.create('Code qualiy') + .get('/grade/e27821fb6289410b8f58338c7e0bc686.json') + .expectJSONTypes( + Joi.object().keys({ + name: 'code quality', + value: isCodacyGrade, + }) + ) + +t.create('Code qualiy on branch') + .get('/grade/e27821fb6289410b8f58338c7e0bc686/master.json') + .expectJSONTypes( + Joi.object().keys({ + name: 'code quality', + value: isCodacyGrade, + }) + ) diff --git a/services/readthedocs/readthedocs.service.js b/services/readthedocs/readthedocs.service.js index 9da36fa8b5fd4..b2a70b7cefae8 100644 --- a/services/readthedocs/readthedocs.service.js +++ b/services/readthedocs/readthedocs.service.js @@ -20,7 +20,7 @@ module.exports = class ReadTheDocs extends LegacyService { if (version != null) { url += '?version=' + encodeURIComponent(version) } - fetchFromSvg(request, url, (err, res) => { + fetchFromSvg(request, url, />([^<>]+)<\/text><\/g>/, (err, res) => { if (err != null) { badgeData.text[1] = 'inaccessible' sendBadge(format, badgeData) diff --git a/services/vso/vso.service.js b/services/vso/vso.service.js index e699cd29e8413..b6e79977d5a88 100644 --- a/services/vso/vso.service.js +++ b/services/vso/vso.service.js @@ -5,7 +5,7 @@ const { fetchFromSvg } = require('../../lib/svg-badge-parser') const { makeBadgeData: getBadgeData } = require('../../lib/badge-data') const fetchVstsBadge = (request, url, badgeData, sendBadge, format) => { - fetchFromSvg(request, url, (err, res) => { + fetchFromSvg(request, url, />([^<>]+)<\/text><\/g>/, (err, res) => { if (err != null) { badgeData.text[1] = 'inaccessible' sendBadge(format, badgeData) From cacad2906724f4d32c97771addaf1ce64ab19777 Mon Sep 17 00:00:00 2001 From: PyvesB Date: Sun, 9 Sep 2018 10:56:58 +0100 Subject: [PATCH 2/2] Ran Prettier --- lib/svg-badge-parser.spec.js | 4 +- services/codacy/codacy-coverage.service.js | 33 +++++++----- services/codacy/codacy-grade.service.js | 63 ++++++++++++---------- services/codacy/codacy.tester.js | 15 ++---- 4 files changed, 60 insertions(+), 55 deletions(-) diff --git a/lib/svg-badge-parser.spec.js b/lib/svg-badge-parser.spec.js index be8ff82cd8453..72769ec362639 100644 --- a/lib/svg-badge-parser.spec.js +++ b/lib/svg-badge-parser.spec.js @@ -14,6 +14,8 @@ describe('The SVG badge parser', function() { const exampleSvg = makeBadge(badgeData) - expect(valueFromSvgBadge(exampleSvg, />([^<>]+)<\/text><\/g>/)).to.equal('this is the result!') + expect(valueFromSvgBadge(exampleSvg, />([^<>]+)<\/text><\/g>/)).to.equal( + 'this is the result!' + ) }) }) diff --git a/services/codacy/codacy-coverage.service.js b/services/codacy/codacy-coverage.service.js index 5eb78847b9f50..0257e2122feee 100644 --- a/services/codacy/codacy-coverage.service.js +++ b/services/codacy/codacy-coverage.service.js @@ -28,21 +28,26 @@ module.exports = class CodacyCoverage extends LegacyService { '?' + query const badgeData = getBadgeData('coverage', data) - fetchFromSvg(request, url, /text-anchor="middle">([^<>]+)<\/text>/, (err, res) => { - if (err != null) { - badgeData.text[1] = 'inaccessible' - sendBadge(format, badgeData) - return + fetchFromSvg( + request, + url, + /text-anchor="middle">([^<>]+)<\/text>/, + (err, res) => { + if (err != null) { + badgeData.text[1] = 'inaccessible' + sendBadge(format, badgeData) + return + } + try { + badgeData.text[1] = res + badgeData.colorscheme = coveragePercentageColor(parseInt(res)) + sendBadge(format, badgeData) + } catch (e) { + badgeData.text[1] = 'invalid' + sendBadge(format, badgeData) + } } - try { - badgeData.text[1] = res - badgeData.colorscheme = coveragePercentageColor(parseInt(res)) - sendBadge(format, badgeData) - } catch (e) { - badgeData.text[1] = 'invalid' - sendBadge(format, badgeData) - } - }) + ) }) ) } diff --git a/services/codacy/codacy-grade.service.js b/services/codacy/codacy-grade.service.js index aadb87acc4ef4..7723e460d159b 100644 --- a/services/codacy/codacy-grade.service.js +++ b/services/codacy/codacy-grade.service.js @@ -25,38 +25,43 @@ module.exports = class CodacyGrade extends LegacyService { '?' + query const badgeData = getBadgeData('code quality', data) - fetchFromSvg(request, url, /visibility="hidden">([^<>]+)<\/text>/, (err, res) => { - if (err != null) { - badgeData.text[1] = 'inaccessible' - sendBadge(format, badgeData) - return - } - try { - badgeData.text[1] = res - if (res === 'A') { - badgeData.colorscheme = 'brightgreen' - } else if (res === 'B') { - badgeData.colorscheme = 'green' - } else if (res === 'C') { - badgeData.colorscheme = 'yellowgreen' - } else if (res === 'D') { - badgeData.colorscheme = 'yellow' - } else if (res === 'E') { - badgeData.colorscheme = 'orange' - } else if (res === 'F') { - badgeData.colorscheme = 'red' - } else if (res === 'X') { + fetchFromSvg( + request, + url, + /visibility="hidden">([^<>]+)<\/text>/, + (err, res) => { + if (err != null) { + badgeData.text[1] = 'inaccessible' + sendBadge(format, badgeData) + return + } + try { + badgeData.text[1] = res + if (res === 'A') { + badgeData.colorscheme = 'brightgreen' + } else if (res === 'B') { + badgeData.colorscheme = 'green' + } else if (res === 'C') { + badgeData.colorscheme = 'yellowgreen' + } else if (res === 'D') { + badgeData.colorscheme = 'yellow' + } else if (res === 'E') { + badgeData.colorscheme = 'orange' + } else if (res === 'F') { + badgeData.colorscheme = 'red' + } else if (res === 'X') { + badgeData.text[1] = 'invalid' + badgeData.colorscheme = 'lightgrey' + } else { + badgeData.colorscheme = 'red' + } + sendBadge(format, badgeData) + } catch (e) { badgeData.text[1] = 'invalid' - badgeData.colorscheme = 'lightgrey' - } else { - badgeData.colorscheme = 'red' + sendBadge(format, badgeData) } - sendBadge(format, badgeData) - } catch (e) { - badgeData.text[1] = 'invalid' - sendBadge(format, badgeData) } - }) + ) }) ) } diff --git a/services/codacy/codacy.tester.js b/services/codacy/codacy.tester.js index e5079922a1cd6..19d6f242ab0a8 100644 --- a/services/codacy/codacy.tester.js +++ b/services/codacy/codacy.tester.js @@ -3,14 +3,7 @@ const Joi = require('joi') const ServiceTester = require('../service-tester') const { isIntegerPercentage } = require('../test-validators') -const isCodacyGrade = Joi.equal( - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', -) +const isCodacyGrade = Joi.equal('A', 'B', 'C', 'D', 'E', 'F') const t = new ServiceTester({ id: 'codacy', title: 'Codacy' }) module.exports = t @@ -23,7 +16,7 @@ t.create('Coverage') value: isIntegerPercentage, }) ) - + t.create('Coverage on branch') .get('/coverage/59d607d0e311408885e418004068ea58/master.json') .expectJSONTypes( @@ -32,7 +25,7 @@ t.create('Coverage on branch') value: isIntegerPercentage, }) ) - + t.create('Code qualiy') .get('/grade/e27821fb6289410b8f58338c7e0bc686.json') .expectJSONTypes( @@ -41,7 +34,7 @@ t.create('Code qualiy') value: isCodacyGrade, }) ) - + t.create('Code qualiy on branch') .get('/grade/e27821fb6289410b8f58338c7e0bc686/master.json') .expectJSONTypes(