diff --git a/lib/all-badge-examples.js b/lib/all-badge-examples.js index 7d2dd63164f43..e8aeca88e1b6c 100644 --- a/lib/all-badge-examples.js +++ b/lib/all-badge-examples.js @@ -215,6 +215,11 @@ const allBadgeExamples = [ keywords: [ 'documentation' ] + }, + { + title: 'Bitrise', + previewUri: '/bitrise/cde737473028420d/master.svg?token=GCIdEzacE4GW32jLVrZb7A', + exampleUri: '/bitrise/APP-ID/BRANCH.svg?token=APP-STATUS-BADGE-TOKEN' } ] }, diff --git a/server.js b/server.js index aade6d97caf8f..1a8982d820975 100644 --- a/server.js +++ b/server.js @@ -5491,6 +5491,47 @@ cache({ }, })); +camp.route(/^\/bitrise\/([^/]+)(?:\/(.+))?\.(svg|png|gif|jpg|json)$/, +cache({ + queryParams: ['token'], + handler: (data, match, sendBadge, request) => { + const appId = match[1]; + const branch = match[2]; + const format = match[3]; + const token = data.token; + const badgeData = getBadgeData('bitrise', data); + let apiUrl = 'https://www.bitrise.io/app/' + appId + '/status.json?token=' + token; + if (typeof branch !== 'undefined') { + apiUrl += '&branch=' + branch; + } + + const statusColorScheme = { + success: 'brightgreen', + error: 'red', + unknown: 'lightgrey' + }; + + request(apiUrl, {json: true}, function(err, res, data) { + try { + if (!res || err !== null || res.statusCode !== 200) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + return; + } + + badgeData.text[1] = data.status; + badgeData.colorscheme = statusColorScheme[data.status]; + + sendBadge(format, badgeData); + } + catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + } + }); + }, +})); + // CircleCI build integration. // https://circleci.com/api/v1/project/BrightFlair/PHP.Gt?circle-token=0a5143728784b263d9f0238b8d595522689b3af2&limit=1&filter=completed camp.route(/^\/circleci\/(?:token\/(\w+))?[+/]?project\/(?:(github|bitbucket)\/)?([^/]+\/[^/]+)(?:\/(.*))?\.(svg|png|gif|jpg|json)$/, diff --git a/service-tests/bitrise.js b/service-tests/bitrise.js new file mode 100644 index 0000000000000..656009121c5bb --- /dev/null +++ b/service-tests/bitrise.js @@ -0,0 +1,53 @@ +'use strict'; + +const Joi = require('joi'); +const ServiceTester = require('./runner/service-tester'); + +const t = new ServiceTester({ id: 'bitrise', title: 'Bitrise' }); +module.exports = t; + +t.create('deploy status') + .get('/cde737473028420d/master.json?token=GCIdEzacE4GW32jLVrZb7A') + .expectJSONTypes(Joi.object().keys({ + name: 'bitrise', + value: Joi.equal( + 'success', + 'error', + 'unknown' + ) + })); + +t.create('deploy status without branch') + .get('/cde737473028420d.json?token=GCIdEzacE4GW32jLVrZb7A') + .expectJSONTypes(Joi.object().keys({ + name: 'bitrise', + value: Joi.equal( + 'success', + 'error', + 'unknown' + ) + })); + +t.create('unknown branch') + .get('/cde737473028420d/unknown.json?token=GCIdEzacE4GW32jLVrZb7A') + .expectJSON({ name: 'bitrise', value: 'unknown' }); + +t.create('invalid token') + .get('/cde737473028420d/unknown.json?token=token') + .expectJSON({ name: 'bitrise', value: 'inaccessible' }); + +t.create('invalid App ID') + .get('/invalid/master.json?token=GCIdEzacE4GW32jLVrZb7A') + .expectJSON({ name: 'bitrise', value: 'inaccessible' }); + +t.create('server error') + .get('/AppID/branch.json?token=token') + .intercept(nock => nock('https://www.bitrise.io') + .get('/app/AppID/status.json?token=token&branch=branch') + .reply(500, 'Something went wrong')) + .expectJSON({ name: 'bitrise', value: 'inaccessible' }); + +t.create('connection error') + .get('/AppID/branch.json?token=token') + .networkOff() + .expectJSON({ name: 'bitrise', value: 'inaccessible' });