Files
shields/services/bitbucket/bitbucket-pipelines.service.js
Paul Melnikow 226fa67a02 Create shortcut for BaseService-related imports (#2809)
Continue to implement #2698:

- Add `core/base-service/index.js` (but hold off on moving the things it imports)
- Add shortcuts in `services/index.js` for Base*Service, errors, and deprecatedService. This file will be streamlined later to avoid cluttering it with rarely used bits.
- Apply consistent ordering of imports and use of `module.exports` in testers.
- Remove some renaming of imports.
- Remove obsolete tests here and there.
2019-01-21 15:41:24 -05:00

107 lines
2.6 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { renderBuildStatusBadge } = require('../../lib/build-status')
const { BaseJsonService } = require('..')
const bitbucketPipelinesSchema = Joi.object({
values: Joi.array()
.items(
Joi.object({
state: Joi.object({
name: Joi.string().required(),
result: Joi.object({
name: Joi.equal(
'SUCCESSFUL',
'FAILED',
'ERROR',
'STOPPED',
'EXPIRED'
),
}).required(),
}).required(),
})
)
.required(),
}).required()
module.exports = class BitbucketPipelines extends BaseJsonService {
async fetch({ user, repo, branch }) {
const url = `https://api.bitbucket.org/2.0/repositories/${user}/${repo}/pipelines/`
return this._requestJson({
url,
schema: bitbucketPipelinesSchema,
options: {
qs: {
fields: 'values.state',
page: 1,
pagelen: 2,
sort: '-created_on',
'target.ref_type': 'BRANCH',
'target.ref_name': branch,
},
},
errorMessages: { 403: 'private repo' },
})
}
static render({ status }) {
return renderBuildStatusBadge({ status: status.toLowerCase() })
}
static transform(data) {
const values = data.values.filter(
value => value.state && value.state.name === 'COMPLETED'
)
if (values.length > 0) {
return values[0].state.result.name
}
return 'never built'
}
async handle({ user, repo, branch }) {
const data = await this.fetch({ user, repo, branch: branch || 'master' })
return this.constructor.render({ status: this.constructor.transform(data) })
}
static get category() {
return 'build'
}
static get defaultBadgeData() {
return { label: 'build' }
}
static get route() {
return {
base: 'bitbucket/pipelines',
format: '([^/]+)/([^/]+)(?:/(.+))?',
capture: ['user', 'repo', 'branch'],
}
}
static get examples() {
return [
{
title: 'Bitbucket Pipelines',
pattern: ':user/:repo',
namedParams: {
user: 'atlassian',
repo: 'adf-builder-javascript',
},
staticPreview: this.render({ status: 'SUCCESSFUL' }),
},
{
title: 'Bitbucket Pipelines branch',
pattern: ':user/:repo/:branch',
namedParams: {
user: 'atlassian',
repo: 'adf-builder-javascript',
branch: 'task/SECO-2168',
},
staticPreview: this.render({ status: 'SUCCESSFUL' }),
},
]
}
}