This is in response to [this conversation](https://github.com/badges/shields/issues/2237#issuecomment-446812796). The change turned out being even easier than I had anticipated because the [API supports a buildStatus filter](https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/list?view=azure-devops-rest-5.0#buildstatus). I did quite a bit of local testing to make sure this is solid. I also ran tests and checked coverage.
66 lines
1.4 KiB
JavaScript
66 lines
1.4 KiB
JavaScript
'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 AzureDevOpsBase extends BaseJsonService {
|
|
async fetch({ url, options, schema, errorMessages }) {
|
|
return this._requestJson({
|
|
schema,
|
|
url,
|
|
options,
|
|
errorMessages,
|
|
})
|
|
}
|
|
|
|
async getLatestCompletedBuildId(
|
|
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,
|
|
statusFilter: 'completed',
|
|
'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
|
|
}
|
|
}
|