Skip to content

Commit

Permalink
[Travis] Add travis-ci.com support (badges#1711)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan de la Beldad Fernandez authored and PyvesB committed Jun 17, 2018
1 parent 7cd3720 commit 4308b60
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
14 changes: 12 additions & 2 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
28 changes: 14 additions & 14 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
}
Expand Down Expand Up @@ -384,31 +384,32 @@ 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)) {
sendBadge(format, badgeData);
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') {
Expand All @@ -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);
Expand Down
32 changes: 32 additions & 0 deletions services/travis/travis.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 4308b60

Please sign in to comment.