Refactor [continuousphp] (#3134)

This commit is contained in:
Caleb Cartwright
2019-03-03 13:54:15 -06:00
committed by chris48s
parent 711c3044dc
commit c009f3cf51
3 changed files with 71 additions and 80 deletions

View File

@@ -1,19 +1,36 @@
'use strict'
const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const Joi = require('joi')
const { isBuildStatus, renderBuildStatusBadge } = require('../build-status')
const { BaseJsonService } = require('..')
// This legacy service should be rewritten to use e.g. BaseJsonService.
//
// Tips for rewriting:
// https://github.com/badges/shields/blob/master/doc/rewriting-services.md
//
// Do not base new services on this code.
module.exports = class ContinuousPhp extends LegacyService {
const schema = Joi.object({
status: Joi.alternatives().try(isBuildStatus, Joi.equal('unknown')),
}).required()
const statusMap = {
unstable: 'yellow',
running: 'blue',
}
module.exports = class ContinuousPhp extends BaseJsonService {
static get category() {
return 'build'
}
static get defaultBadgeData() {
return { label: 'continuousphp' }
}
static render({ status }) {
const badge = renderBuildStatusBadge({ label: 'build', status })
const customColor = statusMap[status]
if (customColor) {
badge.color = customColor
}
return badge
}
static get route() {
return {
base: 'continuousphp',
@@ -23,6 +40,16 @@ module.exports = class ContinuousPhp extends LegacyService {
static get examples() {
return [
{
title: 'continuousphp',
pattern: ':provider/:user/:repo',
namedParams: {
provider: 'git-hub',
user: 'doctrine',
repo: 'dbal',
},
staticPreview: renderBuildStatusBadge({ status: 'passing' }),
},
{
title: 'continuousphp',
pattern: ':provider/:user/:repo/:branch',
@@ -32,75 +59,25 @@ module.exports = class ContinuousPhp extends LegacyService {
repo: 'dbal',
branch: 'master',
},
staticPreview: {
label: 'build',
message: 'passing',
color: 'brightgreen',
},
staticPreview: renderBuildStatusBadge({ status: 'passing' }),
},
]
}
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]
async fetch({ provider, user, repo, branch }) {
const url = `https://status.continuousphp.com/${provider}/${user}/${repo}/status-info`
return this._requestJson({
schema,
url,
options: { qs: { branch } },
errorMessages: {
404: 'project not found',
},
})
}
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)
}
})
})
)
async handle({ provider, user, repo, branch }) {
const json = await this.fetch({ provider, user, repo, branch })
return this.constructor.render({ status: json.status })
}
}

View File

@@ -0,0 +1,19 @@
'use strict'
const { test, given } = require('sazerac')
const ContinuousPhp = require('./continuousphp.service')
describe('ContinuousPhp', function() {
test(ContinuousPhp.render, () => {
given({ status: 'unstable' }).expect({
label: 'build',
message: 'unstable',
color: 'yellow',
})
given({ status: 'running' }).expect({
label: 'build',
message: 'running',
color: 'blue',
})
})
})

View File

@@ -20,9 +20,4 @@ t.create('build status on named branch')
t.create('unknown repo')
.get('/git-hub/this-repo/does-not-exist.json')
.expectBadge({ label: 'build', message: 'invalid' })
t.create('connection error')
.get('/git-hub/doctrine/dbal.json')
.networkOff()
.expectBadge({ label: 'build', message: 'invalid' })
.expectBadge({ label: 'continuousphp', message: 'project not found' })