Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Azure DevOps tests service [AzureDevOpsTests AzureDevOpsCoverage] #2412

Merged
merged 5 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions services/azure-devops/azure-devops-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict'

const Joi = require('joi')
const BaseJsonService = require('../base-json')
const { NotFound } = require('../errors')

const latestBuildSchema = Joi.object({
count: Joi.number().required(),
value: Joi.array()
.items(
Joi.object({
id: Joi.number().required(),
})
)
.required(),
}).required()

module.exports = class BaseAzureDevOpsService extends BaseJsonService {
async fetch({ url, options, schema, errorMessages }) {
return this._requestJson({
schema,
url,
options,
errorMessages,
})
}

async getLatestBuildId(
organization,
project,
definitionId,
branch,
headers,
errorMessages
) {
// Microsoft documentation: https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/list?view=azure-devops-rest-5.0
const url = `https://dev.azure.com/${organization}/${project}/_apis/build/builds`
const options = {
qs: {
definitions: definitionId,
$top: 1,
'api-version': '5.0-preview.4',
},
headers,
}

if (branch) {
options.qs.branch = branch
}

const json = await this.fetch({
url,
options,
schema: latestBuildSchema,
errorMessages,
})

if (json.count !== 1) {
throw new NotFound({ prettyMessage: 'build pipeline not found' })
}

return json.value[0].id
}
}
61 changes: 8 additions & 53 deletions services/azure-devops/azure-devops-coverage.service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const Joi = require('joi')
const BaseJsonService = require('../base-json')
const { NotFound } = require('../errors')
const BaseAzureDevOpsService = require('./azure-devops-base')
const { keywords, getHeaders } = require('./azure-devops-helpers')

const documentation = `
Expand All @@ -29,17 +28,6 @@ const {
coveragePercentage: coveragePercentageColor,
} = require('../../lib/color-formatters')

const latestBuildSchema = Joi.object({
count: Joi.number().required(),
value: Joi.array()
.items(
Joi.object({
id: Joi.number().required(),
})
)
.required(),
}).required()

const buildCodeCoverageSchema = Joi.object({
coverageData: Joi.array()
.items(
Expand All @@ -59,7 +47,7 @@ const buildCodeCoverageSchema = Joi.object({
.required(),
}).required()

module.exports = class AzureDevOpsCoverage extends BaseJsonService {
module.exports = class AzureDevOpsCoverage extends BaseAzureDevOpsService {
static render({ coverage }) {
return {
message: `${coverage.toFixed(0)}%`,
Expand Down Expand Up @@ -113,52 +101,18 @@ module.exports = class AzureDevOpsCoverage extends BaseJsonService {
}
}

async fetch({ url, options, schema }) {
return this._requestJson({
schema,
url,
options,
errorMessages: {
404: 'build pipeline or coverage not found',
},
})
}

async getLatestBuildId(organization, project, definitionId, branch, headers) {
// Microsoft documentation: https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/list?view=azure-devops-rest-5.0
const url = `https://dev.azure.com/${organization}/${project}/_apis/build/builds`
const options = {
qs: {
definitions: definitionId,
$top: 1,
'api-version': '5.0-preview.4',
},
headers,
}
if (branch) {
options.qs.branch = branch
}
const json = await this.fetch({
url,
options,
schema: latestBuildSchema,
})

if (json.count !== 1) {
throw new NotFound({ prettyMessage: 'build pipeline not found' })
}

return json.value[0].id
}

async handle({ organization, project, definitionId, branch }) {
const headers = getHeaders()
const errorMessages = {
404: 'build pipeline or coverage not found',
}
const buildId = await this.getLatestBuildId(
organization,
project,
definitionId,
branch,
headers
headers,
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`
Expand All @@ -173,6 +127,7 @@ module.exports = class AzureDevOpsCoverage extends BaseJsonService {
url,
options,
schema: buildCodeCoverageSchema,
errorMessages,
})

let covered = 0
Expand Down
Loading