Files
shields/services/continuousphp/continuousphp.service.js
Paul Melnikow 02ec19fd22 BaseService terminology: Rename url to route (#2278)
The term “url” is overloaded in services, to refer to the Shields route and also the API URL. Calling the Shields URL a “route” is on the whole more descriptive, and makes it clearer and more obvious which one of these we’re talking about. It’s a small thing, though seems like an improvement.

We have a few functions called `buildUrl`. I’ve renamed them to `buildRoute` when they refer to routes, and left them as `buildUrl` when they refer to API URLs.

I included a minor style tweak and some formatting cleanup in `TUTORIAL.md`.
2018-11-09 15:11:03 -05:00

87 lines
2.3 KiB
JavaScript

'use strict'
const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
module.exports = class ContinuousPhp extends LegacyService {
static get category() {
return 'build'
}
static get route() {
return { base: 'continuousphp' }
}
static get examples() {
return [
{
title: 'continuousphp',
previewUrl: 'git-hub/doctrine/dbal/master',
},
]
}
static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/continuousphp\/([^/]+)\/([^/]+\/[^/]+)(?:\/(.+))?\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const provider = match[1]
const userRepo = match[2]
const branch = match[3]
const format = match[4]
const options = {
method: 'GET',
uri: `https://status.continuousphp.com/${provider}/${userRepo}/status-info`,
headers: {
Accept: 'application/json',
},
}
if (branch != null) {
options.uri += `?branch=${branch}`
}
const badgeData = getBadgeData('build', data)
request(options, (err, res) => {
if (err != null) {
console.error(`continuousphp error: ${err.stack}`)
if (res) {
console.error(`${res}`)
}
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
return
}
try {
const status = JSON.parse(res['body']).status
badgeData.text[1] = status
if (status === 'passing') {
badgeData.colorscheme = 'brightgreen'
} else if (status === 'failing') {
badgeData.colorscheme = 'red'
} else if (status === 'unstable') {
badgeData.colorscheme = 'yellow'
} else if (status === 'running') {
badgeData.colorscheme = 'blue'
} else if (status === 'unknown') {
badgeData.colorscheme = 'lightgrey'
} else {
badgeData.text[1] = status
}
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
}
}