Use promises for suggestions

This commit is contained in:
Thaddee Tyl 2015-01-18 16:21:26 +01:00
parent 45b12a458f
commit d75133fe18
2 changed files with 107 additions and 101 deletions

View File

@ -25,7 +25,8 @@
"redis": "~0.10.1", "redis": "~0.10.1",
"camp": "~13.11.9", "camp": "~13.11.9",
"semver": "~2.3.0", "semver": "~2.3.0",
"bower": "~1.3.11" "bower": "~1.3.11",
"promise": "~6.1.0"
}, },
"devDependencies": { "devDependencies": {
"ass": "~0.0.6", "ass": "~0.0.6",

View File

@ -1,5 +1,6 @@
var nodeUrl = require('url'); var nodeUrl = require('url');
var request = require('request'); var request = require('request');
var Promise = require('promise');
var serverSecrets; var serverSecrets;
try { try {
// Everything that cannot be checked in but is useful server-side // Everything that cannot be checked in but is useful server-side
@ -24,117 +25,121 @@ var suggest = function(data, end, ask) {
} }
}; };
// url: string
// cb: function({badges})
var github = function(url, cb) { var github = function(url, cb) {
var badges = [];
var userRepo = url.pathname.slice(1).split('/'); var userRepo = url.pathname.slice(1).split('/');
var user = userRepo[0]; var user = userRepo[0];
var repo = userRepo[1]; var repo = userRepo[1];
githubLicense(user, repo, function(err, badge) { Promise.all([
if (err == null) { githubLicense(user, repo),
badges.push(badge); ]).then(function(badges) {
}
cb({badges:badges}); cb({badges:badges});
}).catch(function(err) {
cb({badges:badges, err:err});
}); });
}; };
// user: eg, qubyte // user: eg, qubyte
// repo: eg, rubidium // repo: eg, rubidium
// end: function(err, {link, badge}) // returns a promise of {link, badge, name}
var githubLicense = function(user, repo, end) { var githubLicense = function(user, repo) {
// Step 1: Get the repo's default branch. return new Promise(function(resolve, reject) {
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + ''; // Step 1: Get the repo's default branch.
// Using our OAuth App secret grants us 5000 req/hour var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '';
// instead of the standard 60 req/hour. // Using our OAuth App secret grants us 5000 req/hour
if (serverSecrets) { // instead of the standard 60 req/hour.
apiUrl += '?client_id=' + serverSecrets.gh_client_id if (serverSecrets) {
+ '&client_secret=' + serverSecrets.gh_client_secret; apiUrl += '?client_id=' + serverSecrets.gh_client_id
} + '&client_secret=' + serverSecrets.gh_client_secret;
var badgeData = {text:['license',''], colorscheme:'blue'}; }
// A special User-Agent is required: var badgeData = {text:['license',''], colorscheme:'blue'};
// http://developer.github.com/v3/#user-agent-required // A special User-Agent is required:
request(apiUrl, { headers: { 'User-Agent': 'Shields.io' } }, function(err, res, buffer) { // http://developer.github.com/v3/#user-agent-required
if (err != null) { end(err); return; } request(apiUrl, { headers: { 'User-Agent': 'Shields.io' } }, function(err, res, buffer) {
try { if (err != null) { reject(err); return; }
if ((+res.headers['x-ratelimit-remaining']) === 0) { end(Error('rate limited')); return; } try {
var data = JSON.parse(buffer); if ((+res.headers['x-ratelimit-remaining']) === 0) { reject(Error('rate limited')); return; }
var defaultBranch = data.default_branch; var data = JSON.parse(buffer);
// Step 2: Get the SHA-1 hash of the branch tip. var defaultBranch = data.default_branch;
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/branches/' + defaultBranch; // Step 2: Get the SHA-1 hash of the branch tip.
if (serverSecrets) { var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/branches/' + defaultBranch;
apiUrl += '?client_id=' + serverSecrets.gh_client_id if (serverSecrets) {
+ '&client_secret=' + serverSecrets.gh_client_secret; apiUrl += '?client_id=' + serverSecrets.gh_client_id
} + '&client_secret=' + serverSecrets.gh_client_secret;
request(apiUrl, { headers: { 'User-Agent': 'Shields.io' } }, function(err, res, buffer) { }
if (err != null) { end(err); return; } request(apiUrl, { headers: { 'User-Agent': 'Shields.io' } }, function(err, res, buffer) {
try { if (err != null) { reject(err); return; }
if ((+res.headers['x-ratelimit-remaining']) === 0) { end(Error('rate limited')); return; } try {
var data = JSON.parse(buffer); if ((+res.headers['x-ratelimit-remaining']) === 0) { reject(Error('rate limited')); return; }
var branchTip = data.commit.sha; var data = JSON.parse(buffer);
// Step 3: Get the tree at the commit. var branchTip = data.commit.sha;
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/git/trees/' + branchTip; // Step 3: Get the tree at the commit.
if (serverSecrets) { var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/git/trees/' + branchTip;
apiUrl += '?client_id=' + serverSecrets.gh_client_id if (serverSecrets) {
+ '&client_secret=' + serverSecrets.gh_client_secret; apiUrl += '?client_id=' + serverSecrets.gh_client_id
} + '&client_secret=' + serverSecrets.gh_client_secret;
request(apiUrl, { headers: { 'User-Agent': 'Shields.io' } }, function(err, res, buffer) { }
if (err != null) { end(err); return; } request(apiUrl, { headers: { 'User-Agent': 'Shields.io' } }, function(err, res, buffer) {
try { if (err != null) { reject(err); return; }
if ((+res.headers['x-ratelimit-remaining']) === 0) { end(Error('rate limited')); return; } try {
var data = JSON.parse(buffer); if ((+res.headers['x-ratelimit-remaining']) === 0) { reject(Error('rate limited')); return; }
var treeArray = data.tree; var data = JSON.parse(buffer);
var licenseBlob; var treeArray = data.tree;
var licenseFilename; var licenseBlob;
// Crawl each file in the root directory var licenseFilename;
for (var i = 0; i < treeArray.length; i++) { // Crawl each file in the root directory
if (treeArray[i].type != 'blob') { for (var i = 0; i < treeArray.length; i++) {
continue; if (treeArray[i].type != 'blob') {
} continue;
if (treeArray[i].path.match(/(LICENSE|COPYING|COPYRIGHT).*/i)) {
licenseBlob = treeArray[i].sha;
licenseFilename = treeArray[i].path;
break;
}
}
// Could not find license file
if (!licenseBlob) { end(Error('no license file')); return; }
// Step 4: Get the license blob.
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/git/blobs/' + licenseBlob;
var link = 'https://raw.githubusercontent.com/' +
[user, repo, defaultBranch, licenseFilename].join('/');
if (serverSecrets) {
apiUrl += '?client_id=' + serverSecrets.gh_client_id
+ '&client_secret=' + serverSecrets.gh_client_secret;
}
// Get the raw blob instead of JSON
// https://developer.github.com/v3/media/
request(apiUrl, { headers: { 'User-Agent': 'Shields.io', 'Accept': 'appplication/vnd.github.raw' } },
function(err, res, buffer) {
if (err != null) { end(err); return; }
try {
if ((+res.headers['x-ratelimit-remaining']) === 0) { end(Error('rate limited')); return; }
var license = guessLicense(buffer);
if (license) {
badgeData.text[1] = license;
end(null, {
link: link,
badge: shieldsBadge(badgeData),
name: 'GitHub license'
});
return;
} else {
// Not a recognized license
end(Error('unknown license'));
return;
} }
} catch(e) { end(e); return; } if (treeArray[i].path.match(/(LICENSE|COPYING|COPYRIGHT).*/i)) {
}); licenseBlob = treeArray[i].sha;
} catch(e) { end(e); return; } licenseFilename = treeArray[i].path;
}); break;
} catch(e) { end(e); return; } }
}); }
} catch(e) { end(e); return; } // Could not find license file
if (!licenseBlob) { reject(Error('no license file')); return; }
// Step 4: Get the license blob.
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/git/blobs/' + licenseBlob;
var link = 'https://raw.githubusercontent.com/' +
[user, repo, defaultBranch, licenseFilename].join('/');
if (serverSecrets) {
apiUrl += '?client_id=' + serverSecrets.gh_client_id
+ '&client_secret=' + serverSecrets.gh_client_secret;
}
// Get the raw blob instead of JSON
// https://developer.github.com/v3/media/
request(apiUrl, { headers: { 'User-Agent': 'Shields.io', 'Accept': 'appplication/vnd.github.raw' } },
function(err, res, buffer) {
if (err != null) { reject(err); return; }
try {
if ((+res.headers['x-ratelimit-remaining']) === 0) { reject(Error('rate limited')); return; }
var license = guessLicense(buffer);
if (license) {
badgeData.text[1] = license;
resolve({
link: link,
badge: shieldsBadge(badgeData),
name: 'GitHub license'
});
return;
} else {
// Not a recognized license
reject(Error('unknown license'));
return;
}
} catch(e) { reject(e); return; }
});
} catch(e) { reject(e); return; }
});
} catch(e) { reject(e); return; }
});
} catch(e) { reject(e); return; }
});
}); });
}; };