GitHub watch integration

Fixes #508
This commit is contained in:
Thaddee Tyl 2015-08-19 00:09:50 +02:00
parent 8cc7b946c3
commit b11919d1ef
2 changed files with 62 additions and 6 deletions

View File

@ -2588,6 +2588,10 @@ cache(function(data, match, sendBadge, request) {
var badgeData = getBadgeData('forks', data);
if (badgeData.template === 'social') {
badgeData.logo = badgeData.logo || logos.github;
badgeData.links = [
'https://github.com/' + user + '/' + repo + '/fork',
'https://github.com/' + user + '/' + repo + '/network',
];
}
// A special User-Agent is required:
// http://developer.github.com/v3/#user-agent-required
@ -2630,6 +2634,10 @@ cache(function(data, match, sendBadge, request) {
var badgeData = getBadgeData('stars', data);
if (badgeData.template === 'social') {
badgeData.logo = badgeData.logo || logos.github;
badgeData.links = [
'https://github.com/' + user + '/' + repo,
'https://github.com/' + user + '/' + repo + '/stargazers',
];
}
// A special User-Agent is required:
// http://developer.github.com/v3/#user-agent-required
@ -2654,6 +2662,50 @@ cache(function(data, match, sendBadge, request) {
});
}));
// GitHub watchers integration.
camp.route(/^\/github\/watchers\/([^\/]+)\/([^\/]+)\.(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];
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('watchers', data);
if (badgeData.template === 'social') {
badgeData.logo = badgeData.logo || logos.github;
badgeData.links = [
'https://github.com/' + user + '/' + repo,
'https://github.com/' + user + '/' + repo + '/watchers',
];
}
// 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.
}
badgeData.text[1] = JSON.parse(buffer).subscribers_count;
badgeData.colorscheme = null;
badgeData.colorB = '#4183C4';
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));
// GitHub user followers integration.
camp.route(/^\/github\/followers\/([^\/]+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {

View File

@ -467,16 +467,20 @@ Pixel-perfect   Retina-ready   Fast   Consistent   Hackable
<h3 id="social"> Social </h3>
<table class='badge'><tbody>
<tr><th> GitHub forks: </th>
<td><img src='/github/forks/badges/shields.svg?style=social' alt=''/></td>
<td><code>https://img.shields.io/github/forks/badges/shields.svg?style=social</code></td>
<td><img src='/github/forks/badges/shields.svg?style=social&label=Fork' alt=''/></td>
<td><code>https://img.shields.io/github/forks/badges/shields.svg?style=social&amp;label=Fork</code></td>
</tr>
<tr><th> GitHub stars: </th>
<td><img src='/github/stars/badges/shields.svg?style=social' alt=''/></td>
<td><code>https://img.shields.io/github/stars/badges/shields.svg?style=social</code></td>
<td><img src='/github/stars/badges/shields.svg?style=social&label=Star' alt=''/></td>
<td><code>https://img.shields.io/github/stars/badges/shields.svg?style=social&amp;label=Star</code></td>
</tr>
<tr><th> GitHub watchers: </th>
<td><img src='/github/watchers/badges/shields.svg?style=social&label=Watch' alt=''/></td>
<td><code>https://img.shields.io/github/watchers/badges/shields.svg?style=social&amp;label=Watch</code></td>
</tr>
<tr><th> GitHub followers: </th>
<td><img src='/github/followers/espadrine.svg?style=social' alt=''/></td>
<td><code>https://img.shields.io/github/followers/espadrine.svg?style=social</code></td>
<td><img src='/github/followers/espadrine.svg?style=social&label=Follow' alt=''/></td>
<td><code>https://img.shields.io/github/followers/espadrine.svg?style=social&amp;label=Follow</code></td>
</tr>
<tr><th> Twitter: </th>
<td><img src='/twitter/url/https/shields.io.svg?style=social' alt=''/></td>