Add github license integration

This commit is contained in:
Montana Flynn 2015-04-10 06:52:37 -07:00
parent a6563d2968
commit bb637cd398
2 changed files with 55 additions and 0 deletions

View File

@ -2113,6 +2113,54 @@ 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, mashape
var repo = match[2]; // eg, apistatus
var format = match[3];
var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo;
var badgeData = getBadgeData('license', data);
// 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;
}
// Custom user-agent and accept headers are required
// http://developer.github.com/v3/#user-agent-required
// https://developer.github.com/v3/licenses/
var customHeaders = {
'User-Agent': 'Shields.io',
'Accept': 'application/vnd.github.drax-preview+json'
};
request(apiUrl, { headers: customHeaders }, function(err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
}
try {
if ((+res.headers['x-ratelimit-remaining']) === 0) {
return; // Hope for the best in the cache.
} else if (res.statusCode === 404) {
throw new Error(); // Show invalid badge in this case
}
var body = JSON.parse(buffer);
if (body.license != null) {
badgeData.text[1] = body.license.name;
badgeData.colorscheme = 'blue';
sendBadge(format, badgeData);
} else {
badgeData.text[1] = 'unknown';
sendBadge(format, badgeData);
}
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));
// Chef cookbook integration.
camp.route(/^\/cookbook\/v\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {

View File

@ -514,6 +514,10 @@ Pixel-perfect   Retina-ready   Fast   Consistent   Hackable
<td><img src='/github/followers/espadrine.svg' alt=''/></td>
<td><code>https://img.shields.io/github/followers/espadrine.svg</code></td>
</tr>
<tr><th data-keywords='github license'> GitHub license: </th>
<td><img src='/github/license/mashape/apistatus.svg' alt=''/></td>
<td><code>https://img.shields.io/github/license/mashape/apistatus.svg</code></td>
</tr>
<tr><th> WordPress rating: </th>
<td><img src='/wordpress/plugin/r/akismet.svg' alt=''/></td>
<td><code>https://img.shields.io/wordpress/plugin/r/akismet.svg</code></td>
@ -753,6 +757,9 @@ is where the current server got started.
<a class='photo' href='https://github.com/raphink'>
<img alt='raphink' src='https://avatars.githubusercontent.com/u/650430?s=80'>
</a>
<a class='photo' href='https://github.com/montanaflynn'>
<img alt='montanaflynn' src='https://avatars.githubusercontent.com/u/24260?s=80'>
</a>
<p><small>:wq</small></p>
</main>