Files
shields/services/circleci/circleci.service.js
Paul Melnikow fafb22efee Move "good" badge helpers from lib/ to services/ (#3101)
This moves a few helpers from `lib/` to `services/`:

build-status.js
build-status.spec.js
color-formatters.js
color-formatters.spec.js
contributor-count.js
licenses.js
licenses.spec.js
php-version.js
php-version.spec.js
text-formatters.js
text-formatters.spec.js
version.js
version.spec.js

And one from `lib/` to `core/`:

unhandled-rejection.spec.js

The diff is long, but the changes are straightforward.

Ref #2832
2019-02-27 20:47:46 -05:00

112 lines
3.1 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { isBuildStatus, renderBuildStatusBadge } = require('../build-status')
const { BaseJsonService } = require('..')
const circleSchema = Joi.array()
.items(Joi.object({ status: isBuildStatus }))
.min(1)
.max(1)
.required()
const documentation = `
<p>
Please note that <code>status</code> tokens will not work. Instead, you should generate an <code>all</code> scoped token.
<br />
For the sake of security, please use <b>Project Tokens</b> and never <b>Personal Tokens</b> as they grant full read write permissions to your projects.
<br />
For more information about managing Circle CI tokens, please read this <a target="_blank" href="https://circleci.com/docs/2.0/managing-api-tokens">article</a>.
</p>
`
module.exports = class CircleCi extends BaseJsonService {
async fetch({ token, vcsType, userRepo, branch }) {
let url = `https://circleci.com/api/v1.1/project/${vcsType}/${userRepo}`
if (branch != null) {
url += `/tree/${branch}`
}
const query = { filter: 'completed', limit: 1 }
if (token) {
query['circle-token'] = token
}
return this._requestJson({
url,
schema: circleSchema,
options: { qs: query },
errorMessages: { 404: 'project not found' },
})
}
static render({ status }) {
return renderBuildStatusBadge({ status: status.replace('_', ' ') })
}
async handle({ token, vcsType, userRepo, branch }) {
const json = await this.fetch({
token,
vcsType: vcsType || 'github',
userRepo,
branch,
})
return this.constructor.render({ status: json[0].status })
}
// Metadata
static get defaultBadgeData() {
return { label: 'build' }
}
static get category() {
return 'build'
}
static get route() {
return {
base: 'circleci',
format:
'(?:token/(\\w+)/)?project/(?:(github|bitbucket)/)?([^/]+/[^/]+)(?:/(.*))?',
capture: ['token', 'vcsType', 'userRepo', 'branch'],
}
}
static get examples() {
return [
{
title: 'CircleCI (all branches)',
pattern: 'project/:vcsType/:owner/:repo',
namedParams: {
vcsType: 'github',
owner: 'RedSparr0w',
repo: 'node-csgo-parser',
},
staticPreview: this.render({ status: 'success' }),
},
{
title: 'CircleCI branch',
pattern: 'project/:vcsType/:owner/:repo/:branch',
namedParams: {
vcsType: 'github',
owner: 'RedSparr0w',
repo: 'node-csgo-parser',
branch: 'master',
},
staticPreview: this.render({ status: 'success' }),
},
{
title: 'CircleCI token',
pattern: 'token/:token/project/:vcsType/:owner/:repo/:branch',
namedParams: {
token: 'b90b5c49e59a4c67ba3a92f7992587ac7a0408c2',
vcsType: 'github',
owner: 'RedSparr0w',
repo: 'node-csgo-parser',
branch: 'master',
},
staticPreview: this.render({ status: 'success' }),
documentation,
},
]
}
}