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

[travis php-eye jenkins] Use github auth in php-v badges #1403

Merged
merged 2 commits into from
Dec 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions lib/nuget-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ function mapNugetFeed({ camp, cache }, pattern, offset, getInfo) {

function getNugetData(apiUrl, id, request, done) {
// get service index document
regularUpdate(apiUrl + '/index.json',
regularUpdate({
url: apiUrl + '/index.json',
// The endpoint changes once per year (ie, a period of n = 1 year).
// We minimize the users' waiting time for information.
// With l = latency to fetch the endpoint and x = endpoint update period
Expand All @@ -148,17 +149,9 @@ function mapNugetFeed({ camp, cache }, pattern, offset, getInfo) {
// right endpoint.
// So the waiting time within n years is n*l/x + x years, for which a
// derivation yields an optimum at x = sqrt(n*l), roughly 42 minutes.
(42 * 60 * 1000),
function(buffer) {
const data = JSON.parse(buffer);

const searchQueryResources = data.resources.filter(function(resource) {
return resource['@type'] === 'SearchQueryService';
});

return searchQueryResources;
},
function(err, searchQueryResources) {
intervalMillis: 42 * 60 * 1000,
scraper: json => json.resources.filter(resource => resource['@type'] === 'SearchQueryService'),
}, (err, searchQueryResources) => {
if (err != null) { done(err); return; }

// query autocomplete service
Expand All @@ -167,7 +160,7 @@ function mapNugetFeed({ camp, cache }, pattern, offset, getInfo) {
+ '?q=packageid:' + encodeURIComponent(id.toLowerCase()) // NuGet package id (lowercase)
+ '&prerelease=true'; // Include prerelease versions?

request(reqUrl, function(err, res, buffer) {
request(reqUrl, (err, res, buffer) => {
if (err != null) {
done(err);
return;
Expand Down
163 changes: 90 additions & 73 deletions lib/php-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,11 @@
*/
'use strict';

const {listCompare} = require('./version.js');
const {omitv} = require('./text-formatters.js');
const request = require('request');
const uniq = require('lodash.uniq');

// Return a negative value if v1 < v2,
// zero if v1 = v2,
// a positive value otherwise.
//
// See https://getcomposer.org/doc/04-schema.md#version
// and https://github.com/badges/shields/issues/319#issuecomment-74411045
function compare(v1, v2) {
// Omit the starting `v`.
const rawv1 = omitv(v1);
const rawv2 = omitv(v2);
let v1data, v2data;
try {
v1data = numberedVersionData(rawv1);
v2data = numberedVersionData(rawv2);
} catch(e) {
return asciiVersionCompare(rawv1, rawv2);
}

// Compare the numbered part (eg, 1.0.0 < 2.0.0).
const numbersCompare = listCompare(v1data.numbers, v2data.numbers);
if (numbersCompare !== 0) {
return numbersCompare;
}

// Compare the modifiers (eg, alpha < beta).
if (v1data.modifier < v2data.modifier) {
return -1;
} else if (v1data.modifier > v2data.modifier) {
return 1;
}

// Compare the modifier counts (eg, alpha1 < alpha3).
if (v1data.modifierCount < v2data.modifierCount) {
return -1;
} else if (v1data.modifierCount > v2data.modifierCount) {
return 1;
}

return 0;
}
exports.compare = compare;

function latest(versions) {
let latest = versions[0];
for (let i = 1; i < versions.length; i++) {
if (compare(latest, versions[i]) < 0) {
latest = versions[i];
}
}
return latest;
}
exports.latest = latest;

// Whether a version is stable.
function isStable(version) {
const rawVersion = omitv(version);
let versionData;
try {
versionData = numberedVersionData(rawVersion);
} catch(e) {
return false;
}
// normal or patch
return (versionData.modifier === 3) || (versionData.modifier === 4);
}
exports.isStable = isStable;

// === Private helper functions ===
const {listCompare} = require('./version');
const {omitv} = require('./text-formatters');
const { regularUpdate } = require('./regular-update');

// Return a negative value if v1 < v2,
// zero if v1 = v2, a positive value otherwise.
Expand Down Expand Up @@ -175,6 +108,69 @@ function numberedVersionData(version) {
};
}

// Return a negative value if v1 < v2,
// zero if v1 = v2,
// a positive value otherwise.
//
// See https://getcomposer.org/doc/04-schema.md#version
// and https://github.com/badges/shields/issues/319#issuecomment-74411045
function compare(v1, v2) {
// Omit the starting `v`.
const rawv1 = omitv(v1);
const rawv2 = omitv(v2);
let v1data, v2data;
try {
v1data = numberedVersionData(rawv1);
v2data = numberedVersionData(rawv2);
} catch(e) {
return asciiVersionCompare(rawv1, rawv2);
}

// Compare the numbered part (eg, 1.0.0 < 2.0.0).
const numbersCompare = listCompare(v1data.numbers, v2data.numbers);
if (numbersCompare !== 0) {
return numbersCompare;
}

// Compare the modifiers (eg, alpha < beta).
if (v1data.modifier < v2data.modifier) {
return -1;
} else if (v1data.modifier > v2data.modifier) {
return 1;
}

// Compare the modifier counts (eg, alpha1 < alpha3).
if (v1data.modifierCount < v2data.modifierCount) {
return -1;
} else if (v1data.modifierCount > v2data.modifierCount) {
return 1;
}

return 0;
}

function latest(versions) {
let latest = versions[0];
for (let i = 1; i < versions.length; i++) {
if (compare(latest, versions[i]) < 0) {
latest = versions[i];
}
}
return latest;
}

function isStable(version) {
const rawVersion = omitv(version);
let versionData;
try {
versionData = numberedVersionData(rawVersion);
} catch(e) {
return false;
}
// normal or patch
return (versionData.modifier === 3) || (versionData.modifier === 4);
}

function minorVersion(version) {
const result = version.match(/^(\d+)(?:\.(\d+))?(?:\.(\d+))?/);

Expand All @@ -184,7 +180,6 @@ function minorVersion(version) {

return result[1] + '.' + (result[2] ? result[2] : '0');
}
exports.minorVersion = minorVersion;

function versionReduction(versions, phpReleases) {
if (!versions.length) {
Expand Down Expand Up @@ -213,4 +208,26 @@ function versionReduction(versions, phpReleases) {

return versions.join(', ');
}
exports.versionReduction = versionReduction;

function getPhpReleases(githubRequest, cb) {
regularUpdate({
url: 'https://api.github.com/repos/php/php-src/git/refs/tags',
intervalMillis: 24 * 3600 * 1000, // 1 day
scraper: tags => uniq(
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])),
request: (url, options, cb) => githubRequest(request, url, {}, cb),
}, cb);
}

module.exports = {
compare,
latest,
isStable,
minorVersion,
versionReduction,
getPhpReleases,
};
41 changes: 26 additions & 15 deletions lib/regular-update.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
'use strict';

const request = require('request');

// Map from URL to { timestamp: last fetch time, interval: in milliseconds,
// data: data }.
// Map from URL to { timestamp: last fetch time, data: data }.
let regularUpdateCache = Object.create(null);

// url: a string, scraper: a function that takes string data at that URL.
// interval: number in milliseconds.
// cb: a callback function that takes an error and data returned by the scraper.
function regularUpdate(url, interval, scraper, cb, options) {
function regularUpdate({
url,
intervalMillis,
json = true,
scraper = buffer => buffer,
options = {},
request = require('request'),
}, cb) {
const timestamp = Date.now();
const cache = regularUpdateCache[url];
if (cache != null &&
(timestamp - cache.timestamp) < interval) {
cb(null, regularUpdateCache[url].data);
const cached = regularUpdateCache[url];
if (cached != null &&
(timestamp - cached.timestamp) < intervalMillis) {
cb(null, cached.data);
return;
}
request(url, options || {}, function(err, res, buffer) {
request(url, options, (err, res, buffer) => {
if (err != null) { cb(err); return; }
if (regularUpdateCache[url] == null) {
regularUpdateCache[url] = { timestamp: 0, data: 0 };
Copy link
Member Author

Choose a reason for hiding this comment

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

These lines didn't make sense to me.

Copy link
Member

Choose a reason for hiding this comment

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

Doesn't looks like it does anything to me.


let reqData;
if (json) {
try {
reqData = JSON.parse(buffer);
} catch(e) { cb(e); return; }
} else {
reqData = buffer;
}

let data;
try {
data = scraper(buffer);
data = scraper(reqData);
} catch(e) { cb(e); return; }
regularUpdateCache[url].timestamp = timestamp;
regularUpdateCache[url].data = data;

regularUpdateCache[url] = { timestamp, data };
cb(null, data);
});
}
Expand Down
Loading