It’s hard to understand how #3813 is persisting. The schema should reject keys not in the object… it’s very puzzling. This replaces the code used to get the color with a function call that throws an explicit error when the key is missing. It should at least make it clear what the key value is when this fails in the future.
61 lines
1.3 KiB
JavaScript
61 lines
1.3 KiB
JavaScript
'use strict'
|
|
|
|
const Joi = require('@hapi/joi')
|
|
const { isValidGrade, gradeColor } = require('./codefactor-helpers')
|
|
const { BaseSvgScrapingService } = require('..')
|
|
|
|
const schema = Joi.object({
|
|
message: isValidGrade,
|
|
}).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: gradeColor(grade),
|
|
}
|
|
}
|
|
|
|
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 })
|
|
}
|
|
}
|