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..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)).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..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, (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 8d884007e4e54..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, (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 new file mode 100644 index 0000000000000..19d6f242ab0a8 --- /dev/null +++ b/services/codacy/codacy.tester.js @@ -0,0 +1,45 @@ +'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 f0e345b0df49e..11fffe2e8cea1 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)