Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP-Eye] Get tested PHP version from PHP-Eye #1372

Merged
merged 11 commits into from
Dec 28, 2017
10 changes: 9 additions & 1 deletion lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,15 @@ const allBadgeExamples = [
keywords: [
'PHP'
]
}
},
{
title: 'PHP version from PHP-Eye',
previewUri: '/php-eye/symfony/symfony.svg',
keywords: [
'PHP',
'php-eye'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these keywords are included in the title, there's no need to list them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why then did we mention them here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it was missed. Those could be removed too. Might be good to automate this check in a unit test.

]
},
]
},
{
Expand Down
94 changes: 94 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7792,6 +7792,100 @@ cache(function(data, match, sendBadge, request) {
});
}));


// PHP version from PHP-Eye
camp.route(/^\/php-eye\/([^/]+\/[^/]+)(?:\/([^/]+))?\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
const userRepo = match[1]; // eg, espadrine/sc
const version = match[2] || 'dev-master';
const format = match[3];
const options = {
method: 'GET',
uri: 'https://php-eye.com/api/v1/package/' + userRepo + '.json',
};
const badgeData = getBadgeData('Tested', data);
// Custom user-agent and accept headers are required
// http://developer.github.com/v3/#user-agent-required
// https://developer.github.com/v3/licenses/
const customHeaders = {
'User-Agent': 'Shields.io'
};
// require PHP releases
regularUpdate(
'https://api.github.com/repos/php/php-src/git/refs/tags',
(24 * 3600 * 1000), // 1 day
tags => {
return uniq(
JSON.parse(tags).
// only releases
filter((tag) => tag.ref.match(/^refs\/tags\/php-\d+\.\d+\.\d+$/) != null).
// get minor version of release
map((tag) => tag.ref.match(/^refs\/tags\/php-(\d+\.\d+)\.\d+$/)[1])
);
},
(err, phpReleases) => {
if (err != null) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
return;
}
request(options, function(err, res, buffer) {
if (err !== null) {
log.error('PHP-Eye error: ' + err.stack);
if (res) {
log.error('' + res);
}
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
return;
}

try {
const data = JSON.parse(buffer);
const travis = data.versions.filter((release) => release.name === version)[0].travis;

if (!travis.config_exists) {
badgeData.colorscheme = 'red';
badgeData.text[1] = 'not tested';
sendBadge(format, badgeData);
return;
}

let versions = [];
for (const index in travis.runtime_status) {
if (travis.runtime_status[index] === 3 && index.match(/^php\d\d$/) !== null) {
versions.push(index.replace(/^php(\d)(\d)$/, '$1.$2'));
}
}

let reduction = phpVersionReduction(versions, phpReleases);

if (travis.runtime_status.hhvm === 3) {
reduction += reduction ? ', ' : '';
reduction += 'HHVM';
}

if (reduction) {
badgeData.colorscheme = 'brightgreen';
badgeData.text[1] = reduction;
} else if (!versions.length) {
badgeData.colorscheme = 'red';
badgeData.text[1] = 'not tested';
} else {
badgeData.text[1] = 'invalid';
}
} catch(e) {
badgeData.text[1] = 'invalid';
}
sendBadge(format, badgeData);
});
},
{
headers: customHeaders
}
);
}));

// Any badge.
camp.route(/^\/(:|badge\/)(([^-]|--)*?)-(([^-]|--)*)-(([^-]|--)+)\.(svg|png|gif|jpg)$/,
function(data, match, end, ask) {
Expand Down
22 changes: 22 additions & 0 deletions service-tests/php-eye.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const ServiceTester = require('./runner/service-tester');

const t = new ServiceTester({ id: 'php-eye', title: 'PHP version from PHP-Eye' });
module.exports = t;

t.create('gets the package version of symfony')
.get('/symfony/symfony.json')
.expectJSON({ name: 'Tested', value: '7.1' });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that we have a test for the badge without a version because it ensures that code path is exercised. However I'm concerned this test will be brittle. Could you use a regex here instead of a constant '7.1'?


t.create('gets the package version of symfony 2.8')
.get('/symfony/symfony/v2.8.0.json')
.expectJSON({ name: 'Tested', value: '5.3 - 7.0, HHVM' });

t.create('gets the package version of yii')
.get('/yiisoft/yii.json')
.expectJSON({ name: 'Tested', value: '5.3 - 7.1' });

t.create('invalid package name')
.get('/frodo/is-not-a-package.json')
.expectJSON({ name: 'Tested', value: 'invalid' });