Repository: badges/shields
Pull request #3134 (2019-03-02 06:40:01)

Refactor [continuousphp]

shields-ci: 
<!--
  0 failure: 
  0 warning: 
  1 messages
  
  DangerID: danger-id-default;
-->



<table>
  <thead>
    <tr>
      <th width="50"></th>
      <th width="100%" data-danger-table="true">Messages</th>
    </tr>
  </thead>
  <tbody><tr>
      <td>:book:</td>
      <td>:sparkles: Thanks for your contribution to Shields, @calebcartwright!</td>
    </tr>
  </tbody>
</table>


<p align="right">
  Generated by :no_entry_sign: <a href="https://danger.systems/js">dangerJS</a> against 7017f8aecd95a4c53255a0b58c1b3dd25710f99f
</p>


Patch:
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]
-
-        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
-            }
+  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',
+      },
+    })
+  }
 
-            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',
+    })
+  })
+})


Test patch:
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' })