From c009f3cf51914bcd0b036191da35161a720087ea Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Sun, 3 Mar 2019 13:54:15 -0600 Subject: [PATCH] Refactor [continuousphp] (#3134) --- .../continuousphp/continuousphp.service.js | 125 +++++++----------- services/continuousphp/continuousphp.spec.js | 19 +++ .../continuousphp/continuousphp.tester.js | 7 +- 3 files changed, 71 insertions(+), 80 deletions(-) create mode 100644 services/continuousphp/continuousphp.spec.js diff --git a/services/continuousphp/continuousphp.service.js b/services/continuousphp/continuousphp.service.js index 56d6fb84f2..1a94570e40 100644 --- a/services/continuousphp/continuousphp.service.js +++ b/services/continuousphp/continuousphp.service.js @@ -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 }) } } diff --git a/services/continuousphp/continuousphp.spec.js b/services/continuousphp/continuousphp.spec.js new file mode 100644 index 0000000000..be92335ffa --- /dev/null +++ b/services/continuousphp/continuousphp.spec.js @@ -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', + }) + }) +}) diff --git a/services/continuousphp/continuousphp.tester.js b/services/continuousphp/continuousphp.tester.js index 3935e360b6..6fa7f59fc5 100644 --- a/services/continuousphp/continuousphp.tester.js +++ b/services/continuousphp/continuousphp.tester.js @@ -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' })