Files
shields/services/gitlab/gitlab-coverage.service.js
dependabot-preview[bot] ee83590942 Build(deps-dev): bump @typescript-eslint/eslint-plugin from 2.34.0 to 4.19.0, run [GitHub GitlabCoverage F-Droid Wordpress VisualStudioAppCenter Ore Packagist] (#6319)
* Build(deps-dev): bump @typescript-eslint/eslint-plugin

Bumps [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/eslint-plugin) from 2.34.0 to 4.19.0.
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v4.19.0/packages/eslint-plugin)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* chore: update eslint camelcase rule

* chore: eslint camelcase updates for wordpress

* chore: fix eslint camelcase for misc other services

* chore: fix eslint camelcase issue in github and gitlab

* chore: run prettier

* chore: account for JSX globals

* deps: bump typescript-eslint-parser to v4

* chore: frontend lint changes

* chore: update eslint config to only run @typescript-eslint/explicit-module-boundary-types on ts files

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Caleb Cartwright <caleb.cartwright@outlook.com>
2021-03-28 08:26:01 -05:00

142 lines
3.9 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { coveragePercentage } = require('../color-formatters')
const { optionalUrl } = require('../validators')
const { BaseSvgScrapingService, NotFound } = require('..')
const schema = Joi.object({
message: Joi.string()
.regex(/^([0-9]+\.[0-9]+%)|unknown$/)
.required(),
}).required()
const queryParamSchema = Joi.object({
gitlab_url: optionalUrl,
job_name: Joi.string(),
}).required()
const documentation = `
<p>
Important: If your project is publicly visible, but the badge is like this:
<img src="https://img.shields.io/badge/coverage-not&nbsp;set&nbsp;up-red" alt="coverage not set up"/>
</p>
<p>
Check if your pipelines are publicly visible as well.<br />
Navigate to your project settings on GitLab and choose General Pipelines under CI/CD.<br />
Then tick the setting Public pipelines.
</p>
<p>
Now your settings should look like this:
</p>
<img src="https://user-images.githubusercontent.com/12065866/67156911-e225a180-f324-11e9-93ad-10aafbb3e69e.png" alt="Setting Public pipelines set"/>
<p>
Also make sure you have set up code covrage parsing as described <a href="https://docs.gitlab.com/ee/ci/pipelines/settings.html#test-coverage-parsing">here</a>
</p>
<p>
Your badge should be working fine now.
</p>
`
module.exports = class GitlabCoverage extends BaseSvgScrapingService {
static category = 'coverage'
static route = {
base: 'gitlab/coverage',
pattern: ':user/:repo/:branch',
queryParamSchema,
}
static examples = [
{
title: 'Gitlab code coverage',
namedParams: {
user: 'gitlab-org',
repo: 'gitlab-runner',
branch: 'master',
},
staticPreview: this.render({ coverage: 67 }),
documentation,
},
{
title: 'Gitlab code coverage (specific job)',
namedParams: {
user: 'gitlab-org',
repo: 'gitlab-runner',
branch: 'master',
},
queryParams: { job_name: 'test coverage report' },
staticPreview: this.render({ coverage: 96 }),
documentation,
},
{
title: 'Gitlab code coverage (self-hosted)',
namedParams: { user: 'GNOME', repo: 'libhandy', branch: 'master' },
queryParams: { gitlab_url: 'https://gitlab.gnome.org' },
staticPreview: this.render({ coverage: 93 }),
documentation,
},
{
title: 'Gitlab code coverage (self-hosted, specific job)',
namedParams: { user: 'GNOME', repo: 'libhandy', branch: 'master' },
queryParams: {
gitlab_url: 'https://gitlab.gnome.org',
job_name: 'unit-test',
},
staticPreview: this.render({ coverage: 93 }),
documentation,
},
]
static defaultBadgeData = { label: 'coverage' }
static render({ coverage }) {
return {
message: `${coverage.toFixed(0)}%`,
color: coveragePercentage(coverage),
}
}
async fetch({
user,
repo,
branch,
gitlab_url: baseUrl = 'https://gitlab.com',
job_name: jobName,
}) {
// Since the URL doesn't return a usable value when an invalid job name is specified,
// it is recommended to not use the query param at all if not required
jobName = jobName ? `?job=${jobName}` : ''
const url = `${baseUrl}/${user}/${repo}/badges/${branch}/coverage.svg${jobName}`
const errorMessages = {
401: 'repo not found',
404: 'repo not found',
}
return this._requestSvg({
schema,
url,
errorMessages,
})
}
static transform({ coverage }) {
if (coverage === 'unknown') {
throw new NotFound({ prettyMessage: 'not set up' })
}
return Number(coverage.slice(0, -1))
}
async handle({ user, repo, branch }, { gitlab_url, job_name }) {
const { message: coverage } = await this.fetch({
user,
repo,
branch,
gitlab_url,
job_name,
})
return this.constructor.render({
coverage: this.constructor.transform({ coverage }),
})
}
}