Get PHP version from [Packagist] (#1256)
This commit is contained in:
committed by
Paul Melnikow
parent
4ec806e6e5
commit
0d3d33bf1b
@@ -966,7 +966,14 @@ const allBadgeExamples = [
|
||||
{
|
||||
title: 'JetBrains Plugins',
|
||||
previewUri: '/jetbrains/plugin/v/9630-a8translate.svg'
|
||||
}
|
||||
},
|
||||
{
|
||||
title: 'PHP from Packagist',
|
||||
previewUri: '/packagist/php-v/symfony/symfony.svg',
|
||||
keywords: [
|
||||
'PHP'
|
||||
]
|
||||
},
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -7395,6 +7395,39 @@ cache(function(data, match, sendBadge, request) {
|
||||
});
|
||||
}));
|
||||
|
||||
// PHP version from Packagist
|
||||
camp.route(/^\/packagist\/php-v\/([^/]+\/[^/]+)(?:\/([^/]+))?\.(svg|png|gif|jpg|json)$/,
|
||||
cache(function(data, match, sendBadge, request) {
|
||||
const userRepo = match[1]; // eg, espadrine/sc
|
||||
const version = match[2] ? match[2] : 'dev-master';
|
||||
const format = match[3];
|
||||
const options = {
|
||||
method: 'GET',
|
||||
uri: 'https://packagist.org/p/' + userRepo + '.json',
|
||||
};
|
||||
const badgeData = getBadgeData('PHP', data);
|
||||
request(options, function(err, res, buffer) {
|
||||
if (err !== null) {
|
||||
log.error('Packagist error: ' + err.stack);
|
||||
if (res) {
|
||||
log.error('' + res);
|
||||
}
|
||||
badgeData.text[1] = 'invalid';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const data = JSON.parse(buffer);
|
||||
badgeData.text[1] = data.packages[userRepo][version].require.php;
|
||||
badgeData.colorscheme = 'blue';
|
||||
} catch(e) {
|
||||
badgeData.text[1] = 'invalid';
|
||||
}
|
||||
sendBadge(format, badgeData);
|
||||
});
|
||||
}));
|
||||
|
||||
// Any badge.
|
||||
camp.route(/^\/(:|badge\/)(([^-]|--)*?)-(([^-]|--)*)-(([^-]|--)+)\.(svg|png|gif|jpg)$/,
|
||||
function(data, match, end, ask) {
|
||||
|
||||
@@ -11,6 +11,20 @@ const isVPlusTripleDottedVersion = withRegex(/^v[0-9]+.[0-9]+.[0-9]+$/);
|
||||
|
||||
const isVPlusDottedVersionAtLeastOne = withRegex(/^v\d+(\.\d+)?(\.\d+)?$/);
|
||||
|
||||
// Simple regex for test Composer versions rule
|
||||
// https://getcomposer.org/doc/articles/versions.md
|
||||
// Examples:
|
||||
// 7.1
|
||||
// >=5.6
|
||||
// >1.0 <2.0
|
||||
// !=1.0 <1.1 || >=1.2
|
||||
// 7.1.*
|
||||
// 7.* || 5.6.*
|
||||
// This regex not support branches, minimum-stability, ref and any (*)
|
||||
// https://getcomposer.org/doc/04-schema.md#package-links
|
||||
// https://getcomposer.org/doc/04-schema.md#minimum-stability
|
||||
const isComposerVersion = withRegex(/^\s*(>=|>|<|<=|!=|\^|~)?\d+(\.(\*|(\d+(\.(\d+|\*))?)))?((\s*\|\|)?\s*(>=|>|<|<=|!=|\^|~)?\d+(\.(\*|(\d+(\.(\d+|\*))?)))?)*\s*$/);
|
||||
|
||||
const isStarRating = withRegex(/^(?=.{5}$)(\u2605{0,5}[\u00BC\u00BD\u00BE]?\u2606{0,5})$/);
|
||||
|
||||
// Required to be > 0, beacuse accepting zero masks many problems.
|
||||
@@ -31,6 +45,7 @@ module.exports = {
|
||||
isSemver,
|
||||
isVPlusTripleDottedVersion,
|
||||
isVPlusDottedVersionAtLeastOne,
|
||||
isComposerVersion,
|
||||
isStarRating,
|
||||
isMetric,
|
||||
isMetricOverTimePeriod,
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const Joi = require('joi');
|
||||
const ServiceTester = require('./runner/service-tester');
|
||||
const { isComposerVersion } = require('./helpers/validators');
|
||||
|
||||
const t = new ServiceTester({ id: 'packagist', title: 'PHP version from Packagist' });
|
||||
module.exports = t;
|
||||
|
||||
t.create('gets the package version of symfony')
|
||||
.get('/php-v/symfony/symfony.json')
|
||||
.expectJSONTypes(Joi.object().keys({ name: 'PHP', value: isComposerVersion }));
|
||||
|
||||
t.create('gets the package version of symfony 2.8')
|
||||
.get('/php-v/symfony/symfony/v2.8.0.json')
|
||||
.expectJSONTypes(Joi.object().keys({ name: 'PHP', value: isComposerVersion }));
|
||||
|
||||
t.create('invalid package name')
|
||||
.get('/php-v/frodo/is-not-a-package.json')
|
||||
.expectJSON({ name: 'PHP', value: 'invalid' });
|
||||
Reference in New Issue
Block a user