feat: add CodeFactor grade badge (#3588)

This commit is contained in:
Caleb Cartwright
2019-06-22 09:13:34 -05:00
committed by GitHub
parent 95b6e48bff
commit c312eae3e3
4 changed files with 187 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
'use strict'
const Joi = require('@hapi/joi')
const { BaseSvgScrapingService } = require('..')
const { letterGrades } = require('./codefactor-helpers')
const schema = Joi.object({
message: Joi.allow(...Object.keys(letterGrades)),
}).required()
module.exports = class CodeFactorGrade extends BaseSvgScrapingService {
static get category() {
return 'analysis'
}
static get route() {
return {
base: 'codefactor/grade',
pattern: ':vcsType(github|bitbucket)/:user/:repo/:branch*',
}
}
static get examples() {
return [
{
title: 'CodeFactor Grade',
namedParams: {
vcsType: 'github',
user: 'pallets',
repo: 'flask',
branch: 'master',
},
staticPreview: this.render({ grade: 'B+' }),
},
]
}
static get defaultBadgeData() {
return {
label: 'code quality',
}
}
static render({ grade }) {
return {
message: grade,
color: letterGrades[grade].color,
}
}
async handle({ vcsType, user, repo, branch }) {
const { message } = await this._requestSvg({
schema,
url: `https://codefactor.io/repository/${vcsType}/${user}/${repo}/badge/${branch ||
''}`,
errorMessages: { 404: 'repo or branch not found' },
})
return this.constructor.render({ grade: message })
}
}

View File

@@ -0,0 +1,57 @@
'use strict'
const { test, given } = require('sazerac')
const CodeFactorGrade = require('./codefactor-grade.service')
describe('CodeFactorGrade', function() {
test(CodeFactorGrade.render, () => {
given({ grade: 'A' }).expect({
message: 'A',
color: 'brightgreen',
})
given({ grade: 'A-' }).expect({
message: 'A-',
color: 'green',
})
given({ grade: 'B+' }).expect({
message: 'B+',
color: 'yellowgreen',
})
given({ grade: 'B' }).expect({
message: 'B',
color: 'yellowgreen',
})
given({ grade: 'B-' }).expect({
message: 'B-',
color: 'yellowgreen',
})
given({ grade: 'C+' }).expect({
message: 'C+',
color: 'yellow',
})
given({ grade: 'C' }).expect({
message: 'C',
color: 'yellow',
})
given({ grade: 'C-' }).expect({
message: 'C-',
color: 'yellow',
})
given({ grade: 'D+' }).expect({
message: 'D+',
color: 'orange',
})
given({ grade: 'D' }).expect({
message: 'D',
color: 'orange',
})
given({ grade: 'D-' }).expect({
message: 'D-',
color: 'orange',
})
given({ grade: 'F' }).expect({
message: 'F',
color: 'red',
})
})
})

View File

@@ -0,0 +1,27 @@
'use strict'
const Joi = require('@hapi/joi')
const t = (module.exports = require('../tester').createServiceTester())
const { letterGrades } = require('./codefactor-helpers')
const codeFactorGrade = Joi.allow(...Object.keys(letterGrades))
t.create('Grade')
.get('/github/google/guava.json')
.expectBadge({
label: 'code quality',
message: codeFactorGrade,
})
t.create('Grade (branch)')
.get('/github/pallets/flask/master.json')
.expectBadge({
label: 'code quality',
message: codeFactorGrade,
})
t.create('Grade (nonexistent repo)')
.get('/github/badges/asdfasdfasdfasdfasfae.json')
.expectBadge({
label: 'code quality',
message: 'repo or branch not found',
})

View File

@@ -0,0 +1,43 @@
'use strict'
// https://support.codefactor.io/i14-glossary
const letterGrades = {
A: {
color: 'brightgreen',
},
'A-': {
color: 'green',
},
'B+': {
color: 'yellowgreen',
},
B: {
color: 'yellowgreen',
},
'B-': {
color: 'yellowgreen',
},
'C+': {
color: 'yellow',
},
C: {
color: 'yellow',
},
'C-': {
color: 'yellow',
},
'D+': {
color: 'orange',
},
D: {
color: 'orange',
},
'D-': {
color: 'orange',
},
F: {
color: 'red',
},
}
module.exports = { letterGrades }