Remove dead code from github license badge
This commit is contained in:
parent
990a62a429
commit
d8fc63d72d
141
server.js
141
server.js
|
@ -1932,147 +1932,6 @@ cache(function(data, match, sendBadge, request) {
|
|||
});
|
||||
}));
|
||||
|
||||
// GitHub license integration.
|
||||
camp.route(/^\/github\/license\/([^\/]+)\/([^\/]+)\.(svg|png|gif|jpg|json)$/,
|
||||
cache(function(data, match, sendBadge, request) {
|
||||
var user = match[1]; // eg, qubyte/rubidium
|
||||
var repo = match[2];
|
||||
var format = match[3];
|
||||
|
||||
// Step 1: Get the repo's default branch.
|
||||
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '';
|
||||
// Using our OAuth App secret grants us 5000 req/hour
|
||||
// instead of the standard 60 req/hour.
|
||||
if (serverSecrets) {
|
||||
apiUrl += '?client_id=' + serverSecrets.gh_client_id
|
||||
+ '&client_secret=' + serverSecrets.gh_client_secret;
|
||||
}
|
||||
var badgeData = getBadgeData('license', data);
|
||||
// A special User-Agent is required:
|
||||
// http://developer.github.com/v3/#user-agent-required
|
||||
request(apiUrl, { headers: githubHeaders }, function(err, res, buffer) {
|
||||
if (err != null) {
|
||||
badgeData.text[1] = 'inaccessible';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if ((+res.headers['x-ratelimit-remaining']) === 0) {
|
||||
return; // Hope for the best in the cache.
|
||||
}
|
||||
var data = JSON.parse(buffer);
|
||||
var defaultBranch = data.default_branch;
|
||||
// Step 2: Get the SHA-1 hash of the branch tip.
|
||||
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/branches/' + defaultBranch;
|
||||
if (serverSecrets) {
|
||||
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) {
|
||||
badgeData.text[1] = 'inaccessible';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if ((+res.headers['x-ratelimit-remaining']) === 0) {
|
||||
return; // Hope for the best in the cache.
|
||||
}
|
||||
var data = JSON.parse(buffer);
|
||||
var branchTip = data.commit.sha;
|
||||
// Step 3: Get the tree at the commit.
|
||||
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/git/trees/' + branchTip;
|
||||
if (serverSecrets) {
|
||||
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) {
|
||||
badgeData.text[1] = 'inaccessible';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if ((+res.headers['x-ratelimit-remaining']) === 0) {
|
||||
return; // Hope for the best in the cache.
|
||||
}
|
||||
var data = JSON.parse(buffer);
|
||||
var treeArray = data.tree;
|
||||
var licenseBlob;
|
||||
// Crawl each file in the root directory
|
||||
for(var i = 0; i < treeArray.length; i++) {
|
||||
if(treeArray[i].type != 'blob') {
|
||||
continue;
|
||||
}
|
||||
if(treeArray[i].path.match(/(LICENSE|COPYING|COPYRIGHT).*/i)) {
|
||||
licenseBlob = treeArray[i].sha;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Could not find license file
|
||||
if(!licenseBlob) {
|
||||
badgeData.text[1] = 'unknown';
|
||||
badgeData.colorscheme = 'red';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
// Step 4: Get the license blob.
|
||||
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo + '/git/blobs/' + licenseBlob;
|
||||
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) {
|
||||
badgeData.text[1] = 'inaccessible';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if ((+res.headers['x-ratelimit-remaining']) === 0) {
|
||||
return; // Hope for the best in the cache.
|
||||
}
|
||||
var license = guessLicense(buffer);
|
||||
badgeData.colorscheme = 'red';
|
||||
if(license) {
|
||||
badgeData.text[1] = license;
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
} else {
|
||||
// Not a recognized license
|
||||
badgeData.text[1] = 'unknown';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
} catch(e) {
|
||||
badgeData.text[1] = 'invalid';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
});
|
||||
} catch(e) {
|
||||
badgeData.text[1] = 'invalid';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
});
|
||||
} catch(e) {
|
||||
badgeData.text[1] = 'invalid';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
});
|
||||
} catch(e) {
|
||||
badgeData.text[1] = 'invalid';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
// Chef cookbook integration.
|
||||
camp.route(/^\/cookbook\/v\/(.*)\.(svg|png|gif|jpg|json)$/,
|
||||
cache(function(data, match, sendBadge, request) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user