Skip to content

Commit

Permalink
fix(travis): Recognise travis-ci.com URLs
Browse files Browse the repository at this point in the history
As the transition to travis-ci.com is nearing completion, the
legacy dot-org URLs are becoming more and more rare, which is
negatively affecting the package quality score on <https://npms.io/>

* Recognise travis-ci.com URLs.
* Recognise the site (org vs com) because otherwise the API URL
  that we return will not work as expected (they are different
  realms, not aliased!).
* Move the canonical URL detection higher up because it is
  important that we parse dot-org vs dot-com correctly if it is
  available.

Fixes #56.
  • Loading branch information
Krinkle committed Aug 24, 2020
1 parent dfb3f8a commit 55bcce0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/travis.js
Expand Up @@ -8,18 +8,18 @@ module.exports = function (parsedUrl) {
let match;
let data;

if ((match = url.match(/img.shields.io\/travis\/(.+)\/(.+)\/(.+)\..+/))) {
if ((match = url.match(/travis-ci.(com|org)\/(.+)\/(.+)\..+/))) {
data = { user: match[2], repo: match[3], branch: parsedUrl.query.branch, site: match[1] };
} else if ((match = url.match(/img.shields.io\/travis\/(.+)\/(.+)\/(.+)\..+/))) {
data = { user: match[1], repo: match[2], branch: match[3] };
} else if ((match = url.match(/img.shields.io\/travis\/(.+)\/(.+)\..+/))) {
data = { user: match[1], repo: match[2] };
} else if ((match = url.match(/travis-ci.org\/(.+)\/(.+)\..+/))) {
data = { user: match[1], repo: match[2], branch: parsedUrl.query.branch };
} else {
return null;
}

const travisUrl =
`https://api.travis-ci.org/${data.user}/${data.repo}.svg${data.branch ? `?branch=${data.branch}` : ''}`;
`https://api.travis-ci.${data.site || 'org'}/${data.user}/${data.repo}.svg${data.branch ? `?branch=${data.branch}` : ''}`;
const shieldsUrl =
`https://img.shields.io/travis/${data.user}/${data.repo}${data.branch ? `/${data.branch}` : ''}`;

Expand Down
15 changes: 15 additions & 0 deletions test/spec/travis.js
Expand Up @@ -29,6 +29,21 @@ test('travis: travis-ci.org (with branch)', t => {
t.deepEqual(badge.info.modifiers, { branch: 'master' });
});

test('travis: travis-ci.com (with branch)', t => {
const user = 'IndigoUnited';
const repo = 'js-promtie';
const branch = 'master';

const badge = detectBadges(`https://travis-ci.com/${user}/${repo}.svg?branch=${branch}`)[0];

t.is(badge.urls.original, `https://travis-ci.com/${user}/${repo}.svg?branch=${branch}`);
t.is(badge.urls.service, `https://api.travis-ci.com/${user}/${repo}.svg?branch=${branch}`);
t.is(badge.urls.content, `https://img.shields.io/travis/${user}/${repo}/${branch}.json`);
t.is(badge.info.service, 'travis');
t.is(badge.info.type, 'build');
t.deepEqual(badge.info.modifiers, { branch: 'master' });
});

test('travis: shields.io', t => {
const user = 'IndigoUnited';
const repo = 'js-promtie';
Expand Down

0 comments on commit 55bcce0

Please sign in to comment.