Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed [Codacy] badges (affects [vso readthedocs]) #2066

Merged
merged 4 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/svg-badge-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
})
}
Expand Down
4 changes: 3 additions & 1 deletion lib/svg-badge-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
)
})
})
33 changes: 19 additions & 14 deletions services/codacy/codacy-coverage.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
)
})
)
}
Expand Down
63 changes: 34 additions & 29 deletions services/codacy/codacy-grade.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
)
})
)
}
Expand Down
45 changes: 45 additions & 0 deletions services/codacy/codacy.tester.js
Original file line number Diff line number Diff line change
@@ -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,
})
)
2 changes: 1 addition & 1 deletion services/readthedocs/readthedocs.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion services/vso/vso.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down