From 4308b60c8f37a9385e29636a911e7c920b03816a Mon Sep 17 00:00:00 2001 From: Ivan de la Beldad Fernandez Date: Sun, 17 Jun 2018 10:14:00 +0200 Subject: [PATCH] [Travis] Add travis-ci.com support (#1711) --- lib/all-badge-examples.js | 14 ++++++++++++-- server.js | 28 ++++++++++++++-------------- services/travis/travis.tester.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/lib/all-badge-examples.js b/lib/all-badge-examples.js index 6b9ca23c2558d..6ace79631fb38 100644 --- a/lib/all-badge-examples.js +++ b/lib/all-badge-examples.js @@ -118,15 +118,25 @@ const allBadgeExamples = [ }, examples: [ { - title: 'Travis', + title: 'Travis (.org)', previewUri: '/travis/rust-lang/rust.svg', exampleUri: '/travis/USER/REPO.svg' }, { - title: 'Travis branch', + title: 'Travis (.org) branch', previewUri: '/travis/rust-lang/rust/master.svg', exampleUri: '/travis/USER/REPO/BRANCH.svg' }, + { + title: 'Travis (.com)', + previewUri: '/travis/com/ivandelabeldad/rackian-gateway.svg', + exampleUri: '/travis/com/USER/REPO.svg' + }, + { + title: 'Travis (.com) branch', + previewUri: '/travis/com/ivandelabeldad/rackian-gateway/master.svg', + exampleUri: '/travis/com/USER/REPO/BRANCH.svg' + }, { title: 'Wercker', previewUri: '/wercker/ci/wercker/docs.svg' diff --git a/server.js b/server.js index b3f8a27b17038..fa88066453e73 100644 --- a/server.js +++ b/server.js @@ -328,7 +328,7 @@ cache(function(data, match, sendBadge, request) { const format = match[3]; const options = { method: 'GET', - uri: 'https://api.travis-ci.org/repos/' + userRepo + '/branches/' + version, + uri: `https://api.travis-ci.org/repos/${userRepo}/branches/${version}`, }; const badgeData = getBadgeData('PHP', data); getPhpReleases(githubAuth.request, (err, phpReleases) => { @@ -339,7 +339,7 @@ cache(function(data, match, sendBadge, request) { } request(options, (err, res, buffer) => { if (err !== null) { - log.error('Travis CI error: ' + err.stack); + log.error(`Travis CI error: ${err.stack}`); if (res) { log.error('' + res); } @@ -384,23 +384,24 @@ cache(function(data, match, sendBadge, request) { }); })); -// Travis integration -camp.route(/^\/travis(-ci)?\/([^/]+\/[^/]+)(?:\/(.+))?\.(svg|png|gif|jpg|json)$/, +// Travis integration (.org and .com) +camp.route(/^\/travis(-ci)?\/(?:(com)\/)?([^/]+\/[^/]+)(?:\/(.+))?\.(svg|png|gif|jpg|json)$/, cache(function(data, match, sendBadge, request) { - var userRepo = match[2]; // eg, espadrine/sc - var branch = match[3]; - var format = match[4]; - var options = { + const travisDomain = match[2] || 'org'; // (com | org) org by default + const userRepo = match[3]; // eg, espadrine/sc + const branch = match[4]; + const format = match[5]; + const options = { method: 'HEAD', - uri: 'https://api.travis-ci.org/' + userRepo + '.svg', + uri: `https://api.travis-ci.${travisDomain}/${userRepo}.svg`, }; if (branch != null) { - options.uri += '?branch=' + branch; + options.uri += `?branch=${branch}`; } - var badgeData = getBadgeData('build', data); + const badgeData = getBadgeData('build', data); request(options, function(err, res) { if (err != null) { - log.error('Travis error: ' + err.stack); + log.error(`Travis error: ${err.stack}`); if (res) { log.error(''+res); } } if (checkErrorResponse(badgeData, err, res)) { @@ -408,7 +409,7 @@ cache(function(data, match, sendBadge, request) { return; } try { - var state = res.headers['content-disposition'] + const state = res.headers['content-disposition'] .match(/filename="(.+)\.svg"/)[1]; badgeData.text[1] = state; if (state === 'passing') { @@ -419,7 +420,6 @@ cache(function(data, match, sendBadge, request) { badgeData.text[1] = state; } sendBadge(format, badgeData); - } catch(e) { badgeData.text[1] = 'invalid'; sendBadge(format, badgeData); diff --git a/services/travis/travis.tester.js b/services/travis/travis.tester.js index b93c2eb961e63..39aa1ac4ac563 100644 --- a/services/travis/travis.tester.js +++ b/services/travis/travis.tester.js @@ -39,6 +39,38 @@ t.create('connection error') .networkOff() .expectJSON({ name: 'build', value: 'inaccessible' }); +// Travis (.com) CI + +t.create('build status on default branch') + .get('/com/ivandelabeldad/rackian-gateway.json') + .expectJSONTypes(Joi.object().keys({ + name: 'build', + value: Joi.equal('failing', 'passing', 'unknown') + })); + +t.create('build status on named branch') + .get('/com/ivandelabeldad/rackian-gateway.json') + .expectJSONTypes(Joi.object().keys({ + name: 'build', + value: Joi.equal('failing', 'passing', 'unknown') + })); + +t.create('unknown repo') + .get('/com/this-repo/does-not-exist.json') + .expectJSON({ name: 'build', value: 'unknown' }); + +t.create('missing content-disposition header') + .get('/com/foo/bar.json') + .intercept(nock => nock('https://api.travis-ci.com') + .head('/foo/bar.svg') + .reply(200)) + .expectJSON({ name: 'build', value: 'invalid' }); + +t.create('connection error') + .get('/com/foo/bar.json') + .networkOff() + .expectJSON({ name: 'build', value: 'inaccessible' }); + // PHP version from .travis.yml t.create('gets the package version of symfony')