[Github] File size badge (#745)

Closes #730
This commit is contained in:
Andre Caetano
2017-05-01 15:28:45 -03:00
committed by Paul Melnikow
parent 87af3bf672
commit a6d81f2391
4 changed files with 76 additions and 0 deletions

View File

@@ -26,6 +26,7 @@
"camp": "~16.2.3",
"chrome-web-store-item-property": "~1.1.2",
"dot": "~1.0.3",
"pretty-bytes": "^3.0.1",
"gm": "^1.23.0",
"json-autosave": "~1.1.2",
"pdfkit": "~0.8.0",

View File

@@ -31,6 +31,7 @@ var svg2img = require('./lib/svg-to-img.js');
var loadLogos = require('./lib/load-logos.js');
var githubAuth = require('./lib/github-auth.js');
var querystring = require('querystring');
var prettyBytes = require('pretty-bytes');
var xml2js = require('xml2js');
var serverSecrets = require('./lib/server-secrets');
if (serverSecrets && serverSecrets.gh_client_id) {
@@ -3436,6 +3437,48 @@ cache(function(data, match, sendBadge, request) {
});
}));
// GitHub file size.
camp.route(/^\/github\/size\/([^\/]+)\/([^\/]+)\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var user = match[1]; // eg, mashape
var repo = match[2]; // eg, apistatus
var path = match[3];
var format = match[4];
var apiUrl = githubApiUrl + '/repos/' + user + '/' + repo + '/contents/' + path;
var badgeData = getBadgeData('size', data);
if (badgeData.template === 'social') {
badgeData.logo = badgeData.logo || logos.github;
}
githubAuth.request(request, apiUrl, {}, function(err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
if (res.statusCode === 404) {
badgeData.text[1] = 'repo or file not found';
sendBadge(format, badgeData);
return;
}
var body = JSON.parse(buffer);
if (body && Number.isInteger(body.size)) {
badgeData.text[1] = prettyBytes(body.size);
badgeData.colorscheme = 'green';
sendBadge(format, badgeData);
} else {
badgeData.text[1] = 'not a regular file';
sendBadge(format, badgeData);
}
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));
// Bitbucket issues integration.
camp.route(/^\/bitbucket\/issues(-raw)?\/([^\/]+)\/([^\/]+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {

28
service-tests/github.js Normal file
View File

@@ -0,0 +1,28 @@
'use strict';
const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const t = new ServiceTester({ id: 'github', title: 'Github' });
module.exports = t;
t.create('File size')
.get('/size/webcaetano/craft/build/craft.min.js.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('size'),
value: Joi.string().regex(/^[0-9]*[.]?[0-9]+\s(B|kB|MB|GB|TB|PB|EB|ZB|YB)$/),
}));
t.create('File size 404')
.get('/size/webcaetano/craft/build/does-not-exist.min.js.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('size'),
value: Joi.string().regex(/^repo or file not found$/),
}));
t.create('File size for "not a regular file"')
.get('/size/webcaetano/craft/build.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('size'),
value: Joi.string().regex(/^not a regular file$/),
}));

View File

@@ -798,6 +798,10 @@ Pixel-perfect   Retina-ready   Fast   Consistent   Hackable
<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 data-keywords='GitHub file size' data-doc='githubDoc'> Github file size: </th>
<td><img src='/github/size/webcaetano/craft/build/craft.min.js.svg' alt=''/></td>
<td><code>https://img.shields.io/github/size/webcaetano/craft/build/craft.min.js.svg</code></td>
</tr>
<tr><th> Bitbucket issues: </th>
<td><img src='/bitbucket/issues/atlassian/python-bitbucket.svg' alt=''/></td>
<td><code>https://img.shields.io/bitbucket/issues/atlassian/python-bitbucket.svg</code></td>