'use strict' const BaseSvgService = require('../base-svg-scraping') const { NotFound } = require('../errors') const { keywords, fetch, render } = require('./azure-devops-helpers') const documentation = `
A badge requires three pieces of information: ORGANIZATION,
PROJECT_ID and DEFINITION_ID.
To start, edit your build definition and look at the url:
Then use the Azure DevOps REST API to translate the
PROJECT_NAME to a PROJECT_ID.
Navigate to https://dev.azure.com/ORGANIZATION/_apis/projects/PROJECT_NAME
`
module.exports = class AzureDevOpsBuild extends BaseSvgService {
static get category() {
return 'build'
}
static get route() {
return {
base: '',
format: '(?:azure-devops|vso)/build/([^/]+)/([^/]+)/([^/]+)(?:/(.+))?',
capture: ['organization', 'projectId', 'definitionId', 'branch'],
}
}
static get examples() {
return [
{
title: 'Azure DevOps builds',
pattern: 'azure-devops/build/:organization/:projectId/:definitionId',
namedParams: {
organization: 'totodem',
projectId: '8cf3ec0e-d0c2-4fcd-8206-ad204f254a96',
definitionId: '2',
},
staticPreview: render({ status: 'succeeded' }),
keywords,
documentation,
},
{
title: 'Azure DevOps builds (branch)',
pattern:
'azure-devops/build/:organization/:projectId/:definitionId/:branch',
namedParams: {
organization: 'totodem',
projectId: '8cf3ec0e-d0c2-4fcd-8206-ad204f254a96',
definitionId: '2',
branch: 'master',
},
staticPreview: render({ status: 'succeeded' }),
keywords,
documentation,
},
]
}
async handle({ organization, projectId, definitionId, branch }) {
// Microsoft documentation: https://docs.microsoft.com/en-us/rest/api/vsts/build/status/get
const { status } = await fetch(this, {
url: `https://dev.azure.com/${organization}/${projectId}/_apis/build/status/${definitionId}`,
qs: { branchName: branch },
errorMessages: {
404: 'user or project not found',
},
})
if (status === 'set up now') {
throw new NotFound({ prettyMessage: 'definition not found' })
}
if (status === 'unknown') {
throw new NotFound({ prettyMessage: 'project not found' })
}
return render({ status })
}
}