Files
shields/services/azure-devops/azure-devops-coverage.service.js
Paul Melnikow ce0ddf93fc Inject secrets into the services (#3652)
This is a reworking of #3410 based on some feedback @calebcartwright left on that PR.

The goals of injecting the secrets are threefold:

1. Simplify testing
2. Be consistent with all of the other config (which is injected)
3. Encapsulate the sensitive auth-related code in one place so it can be studied and tested thoroughly

- Rather than add more code to BaseService to handle authorization logic, it delegates that to an AuthHelper class.
- When the server starts, it fetches the credentials from `config` and injects them into `BaseService.register()` which passes them to `invoke()`.
- In `invoke()` the service's auth configuration is checked (`static get auth()`, much like `static get route()`).
- If the auth config is present, an AuthHelper instance is created and attached to the new instance.
- Then within the service, the password, basic auth config, or bearer authentication can be accessed via e.g. `this.authHelper.basicAuth` and passed to `this._requestJson()` and friends.
- Everything is being done very explicitly, so it should be very clear where and how the configured secrets are being used.
- Testing different configurations of services can now be done by injecting the config into `invoke()` in `.spec` files instead of mocking global state in the service tests as was done before. See the new Jira spec files for a good example of this.

Ref #3393
2019-07-09 23:14:36 -04:00

145 lines
4.0 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const {
coveragePercentage: coveragePercentageColor,
} = require('../color-formatters')
const AzureDevOpsBase = require('./azure-devops-base')
const { keywords } = require('./azure-devops-helpers')
const documentation = `
<p>
To obtain your own badge, you need to get 3 pieces of information:
<code>ORGANIZATION</code>, <code>PROJECT</code> and <code>DEFINITION_ID</code>.
</p>
<p>
First, you need to select your build definition and look at the url:
</p>
<img
src="https://user-images.githubusercontent.com/3749820/47259976-e2d9ec80-d4b2-11e8-92cc-7c81089a7a2c.png"
alt="ORGANIZATION is after the dev.azure.com part, PROJECT is right after that, DEFINITION_ID is at the end after the id= part." />
<p>
Your badge will then have the form:
<code>https://img.shields.io/azure-devops/coverage/ORGANIZATION/PROJECT/DEFINITION_ID.svg</code>.
</p>
<p>
Optionally, you can specify a named branch:
<code>https://img.shields.io/azure-devops/coverage/ORGANIZATION/PROJECT/DEFINITION_ID/NAMED_BRANCH.svg</code>.
</p>
`
const buildCodeCoverageSchema = Joi.object({
coverageData: Joi.array()
.items(
Joi.object({
coverageStats: Joi.array()
.items(
Joi.object({
label: Joi.string().required(),
total: Joi.number().required(),
covered: Joi.number().required(),
})
)
.min(1)
.required(),
})
)
.required(),
}).required()
module.exports = class AzureDevOpsCoverage extends AzureDevOpsBase {
static get category() {
return 'coverage'
}
static get route() {
return {
base: 'azure-devops/coverage',
pattern: ':organization/:project/:definitionId/:branch*',
}
}
static get examples() {
return [
{
title: 'Azure DevOps coverage',
pattern: ':organization/:project/:definitionId',
namedParams: {
organization: 'swellaby',
project: 'opensource',
definitionId: '25',
},
staticPreview: this.render({ coverage: 100 }),
keywords,
documentation,
},
{
title: 'Azure DevOps coverage (branch)',
pattern: ':organization/:project/:definitionId/:branch',
namedParams: {
organization: 'swellaby',
project: 'opensource',
definitionId: '25',
branch: 'master',
},
staticPreview: this.render({ coverage: 100 }),
keywords,
documentation,
},
]
}
static get defaultBadgeData() {
return { label: 'coverage' }
}
static render({ coverage }) {
return {
message: `${coverage.toFixed(0)}%`,
color: coveragePercentageColor(coverage),
}
}
async handle({ organization, project, definitionId, branch }) {
const auth = this.authHelper.basicAuth
const errorMessages = {
404: 'build pipeline or coverage not found',
}
const buildId = await this.getLatestCompletedBuildId(
organization,
project,
definitionId,
branch,
auth,
errorMessages
)
// Microsoft documentation: https://docs.microsoft.com/en-us/rest/api/azure/devops/test/code%20coverage/get%20build%20code%20coverage?view=azure-devops-rest-5.0
const url = `https://dev.azure.com/${organization}/${project}/_apis/test/codecoverage`
const options = {
qs: {
buildId,
'api-version': '5.0-preview.1',
},
auth,
}
const json = await this.fetch({
url,
options,
schema: buildCodeCoverageSchema,
errorMessages,
})
let covered = 0
let total = 0
json.coverageData.forEach(cd => {
cd.coverageStats.forEach(coverageStat => {
if (coverageStat.label === 'Line' || coverageStat.label === 'Lines') {
covered += coverageStat.covered
total += coverageStat.total
}
})
})
const coverage = covered ? (covered / total) * 100 : 0
return this.constructor.render({ coverage })
}
}