Files
shields/services/shippable/shippable.service.js
chris48s c762d971b4 upgrade Joi and related packages (#4060)
* update dependencies

* (core) children --> keys

* (core) fix/update BaseService validate

* (core) update error messages in tests

* (core) only Joi.attempt if we've got a Joi schema

* (core) allow 'expected' to be a regex

* (services) pass 2 schema to .alternatives()

* (services) functions --> Joi schema

* (services) update expected error message

* (services) explicit check for color: undefined

* re-bump joi

* (services) wrap another regex

* (core/services) remove use of array arguments

* (core/services) when --> conditional

* (services) remove more array arguments

* fix spelling in var name

* DRY up sonar helper
2019-09-23 17:36:56 +01:00

86 lines
2.1 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { renderBuildStatusBadge } = require('../build-status')
const { BaseJsonService, NotFound } = require('..')
// source: https://github.com/badges/shields/pull/1362#discussion_r161693830
const statusCodes = {
0: 'waiting',
10: 'queued',
20: 'processing',
30: 'success',
40: 'skipped',
50: 'unstable',
60: 'timeout',
70: 'cancelled',
80: 'failed',
90: 'stopped',
}
const schema = Joi.array()
.items(
Joi.object({
branchName: Joi.string().required(),
statusCode: Joi.number()
.valid(...Object.keys(statusCodes).map(key => parseInt(key)))
.required(),
}).required()
)
.required()
module.exports = class Shippable extends BaseJsonService {
static get category() {
return 'build'
}
static get route() {
return {
base: 'shippable',
pattern: ':projectId/:branch*',
}
}
static get examples() {
return [
{
title: 'Shippable',
pattern: ':projectId',
namedParams: { projectId: '5444c5ecb904a4b21567b0ff' },
staticPreview: this.render({ code: 30 }),
},
{
title: 'Shippable branch',
pattern: ':projectId/:branch',
namedParams: {
projectId: '5444c5ecb904a4b21567b0ff',
branch: 'master',
},
staticPreview: this.render({ code: 30 }),
},
]
}
static get defaultBadgeData() {
return { label: 'shippable' }
}
static render({ code }) {
return renderBuildStatusBadge({ label: 'build', status: statusCodes[code] })
}
async fetch({ projectId }) {
const url = `https://api.shippable.com/projects/${projectId}/branchRunStatus`
return this._requestJson({ schema, url })
}
async handle({ projectId, branch = 'master' }) {
const data = await this.fetch({ projectId })
const builds = data.filter(result => result.branchName === branch)
if (builds.length === 0) {
throw new NotFound({ prettyMessage: 'branch not found' })
}
return this.constructor.render({ code: builds[0].statusCode })
}
}