* chore(deps-dev): bump prettier from 2.8.8 to 3.0.0 Bumps [prettier](https://github.com/prettier/prettier) from 2.8.8 to 3.0.0. - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md) - [Commits](https://github.com/prettier/prettier/compare/2.8.8...3.0.0) --- updated-dependencies: - dependency-name: prettier dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * reformat all the things (prettier 3) * update tests to await calls to prettier.format() --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: chris48s <git@chris-shaw.dev>
74 lines
2.1 KiB
JavaScript
74 lines
2.1 KiB
JavaScript
import Joi from 'joi'
|
|
import { coveragePercentage as coveragePercentageColor } from '../color-formatters.js'
|
|
import { BaseSvgScrapingService, NotFound } from '../index.js'
|
|
|
|
const schema = Joi.object({
|
|
message: Joi.alternatives()
|
|
.try(Joi.string().regex(/^[0-9]+%$/), Joi.equal('!'))
|
|
.required(),
|
|
}).required()
|
|
|
|
export default class CodacyCoverage extends BaseSvgScrapingService {
|
|
static category = 'coverage'
|
|
static route = { base: 'codacy/coverage', pattern: ':projectId/:branch*' }
|
|
|
|
static examples = [
|
|
{
|
|
title: 'Codacy coverage',
|
|
pattern: ':projectId',
|
|
namedParams: { projectId: 'd5402a91aa7b4234bd1c19b5e86a63be' },
|
|
staticPreview: this.render({ percentage: 90 }),
|
|
},
|
|
{
|
|
title: 'Codacy branch coverage',
|
|
pattern: ':projectId/:branch',
|
|
namedParams: {
|
|
projectId: 'd5402a91aa7b4234bd1c19b5e86a63be',
|
|
branch: 'master',
|
|
},
|
|
staticPreview: this.render({ percentage: 90 }),
|
|
},
|
|
]
|
|
|
|
static defaultBadgeData = { label: 'coverage' }
|
|
|
|
static render({ percentage }) {
|
|
return {
|
|
message: `${percentage}%`,
|
|
color: coveragePercentageColor(percentage),
|
|
}
|
|
}
|
|
|
|
static transform({ coverageString }) {
|
|
return {
|
|
percentage: parseFloat(coverageString.replace(/%$/, '')),
|
|
}
|
|
}
|
|
|
|
async handle({ projectId, branch }) {
|
|
const { message: coverageString } = await this._requestSvg({
|
|
schema,
|
|
url: `https://api.codacy.com/project/badge/coverage/${encodeURIComponent(
|
|
projectId,
|
|
)}`,
|
|
options: { searchParams: { branch } },
|
|
valueMatcher: /text-anchor="middle">([^<>]+)<\/text>/,
|
|
httpErrors: {
|
|
404: 'project not found',
|
|
},
|
|
})
|
|
|
|
// When sending an invalid branch, Codacy ignores the branch, failing
|
|
// silently, so we can't provide an error message for this case.
|
|
|
|
if (coverageString === '!') {
|
|
throw new NotFound({
|
|
prettyMessage: 'not enabled for this project',
|
|
})
|
|
}
|
|
|
|
const { percentage } = this.constructor.transform({ coverageString })
|
|
return this.constructor.render({ percentage })
|
|
}
|
|
}
|