Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codesponsor.io badge #1043

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions logo/codesponsor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5598,6 +5598,38 @@ cache(function(data, match, sendBadge, request) {
});
}));

// codesponsor.io integration
camp.route(/^\/codesponsor\/(.*)\/(.*)\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var token = match[1]; // eg, QMSjMHrtPhvfmCnk5Hbikhhr
var username = match[2]; // eg, hopsoft
var name = match[3]; // eg, bg
var format = match[4];

var apiUrl = 'https://app.codesponsor.io/shield/' + token + '/' + username + '/' + name;
var badgeData = getBadgeData('', data);

request(apiUrl, function (err, res, buffer) {
if (err) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}

try {
var data = JSON.parse(buffer.toString());
var dollars = data.distribution_cents / 100;
badgeData.logo = logos['codesponsor'];
badgeData.text[1] = ' ' + dollars.toLocaleString("en-US", { style: "currency", currency: "USD" }) + ' ';
badgeData.colorscheme = 'brightgreen';
sendBadge(format, badgeData);
} catch (e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

// Maintenance integration.
camp.route(/^\/maintenance\/([^\/]+)\/([^\/]+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
Expand Down
47 changes: 47 additions & 0 deletions service-tests/codesponsor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');

const t = new ServiceTester({ id: 'codesponsor', title: 'Code Sponsor' });
module.exports = t;

t.create('earnings on actual repo')
.get('/YOURTOKEN/hopsoft/bg.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal(''),
value: Joi.string().regex(/\$^[0-9]+(\.[0-9]+)?/)
}));

t.create('display USD amount')
.get('/YOURTOKEN/hopsoft/bg.json')
.intercept(nock => nock('https://app.codesponsor.io')
.get('/shield/hopsoft/bg')
.reply(200, {
distribution_cents: Joi.number(),
ts: 1500824233
})
)
.expectJSONTypes(Joi.object().keys({
name: Joi.equal(''),
value: Joi.string().regex(/\$^[0-9]+(\.[0-9]+)?/)
}));

t.create('unknown repo')
.get('/YOURTOKEN/hopsoft/this-does-not-exist.json')
.intercept(nock => nock('https://app.codesponsor.io')
.get('/shield/hopsoft/this-does-not-exist')
.reply(200, {
distribution_cents: 0,
ts: 1500824233
})
)
.expectJSONTypes(Joi.object().keys({
name: Joi.equal(''),
value: ' $0.00 '
}));

t.create('connection error')
.get('/YOURTOKEN/hopsoft/bg.json')
.networkOff()
.expectJSON({ name: 'sponsor', value: 'inaccessible' });
4 changes: 4 additions & 0 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ <h3 id="miscellaneous"> Miscellaneous </h3>
<td><img src='/beerpay/hashdog/scrapfy-chrome-extension.svg' alt=''/></td>
<td><code>https://img.shields.io/beerpay/hashdog/scrapfy-chrome-extension.svg</code></td>
</tr>
<tr><th> Code Sponsor: </th>
<td><img src='/codesponsor/YOURTOKEN/hopsoft/bg.svg' alt='Code Sponsor'/></td>
<td><code>https://img.shields.io/codesponsor/YOURTOKEN/hopsoft/bg.svg</code></td>
</tr>
<tr><th> Code Climate: </th>
<td><img src='/codeclimate/github/kabisaict/flow.svg' alt=''/></td>
<td><code>https://img.shields.io/codeclimate/github/kabisaict/flow.svg</code></td>
Expand Down