Skip to content

Commit

Permalink
Added support for new Code Climate scores (#1236)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickersoft authored and paulmelnikow committed Nov 6, 2017
1 parent 6788a31 commit 632c70b
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,14 @@ const allBadgeExamples = [
title: 'Code Climate',
previewUri: '/codeclimate/issues/github/me-and/mdf.svg'
},
{
title: 'Code Climate',
previewUri: '/codeclimate/maintainability/Nickersoft/dql.svg'
},
{
title: 'Code Climate',
previewUri: '/codeclimate/c/Nickersoft/dql.svg'
},
{
title: 'bitHound',
previewUri: '/bithound/code/github/rexxars/sse-channel.svg'
Expand Down
76 changes: 73 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,77 @@ cache(function(data, match, sendBadge, request) {
});
}));

// Code Climate integration
// New Code Climate scores (coverage + maintainability)
camp.route(/^\/codeclimate\/(c|maintainability)\/(.+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var type = match[1] === 'c' ? 'test_coverage' : 'maintainability';
var userRepo = match[2]; // eg, `kabisaict/flow`.
var format = match[3];
request({
method: 'GET',
uri: 'https://api.codeclimate.com/v1/repos?github_slug=' + userRepo,
json: true
}, function (err, res, body) {
var badgeData = getBadgeData(match[1] === 'c' ? 'coverage' : 'maintainability', data);

if (err != null) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
return;
}

if (!body.data || body.data.length == 0 || !body.data[0].hasOwnProperty('attributes')) {
badgeData.text[1] = 'not found';
sendBadge(format, badgeData);
return;
}

var options = {
method: 'HEAD',
uri: 'https://api.codeclimate.com/v1/badges/' + body.data[0].attributes.badge_token + '/' + type,
};

request(options, function(err, res) {
if (err != null) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
return;
}

try {
var statusMatch = res.headers['content-disposition']
.match(/filename=".*(?:maintainability|test_coverage)-(.+)\.svg"/);
if (!statusMatch) {
badgeData.text[1] = 'unknown';
sendBadge(format, badgeData);
return;
}

var score = statusMatch[1].replace('-', '.');
badgeData.text[1] = score;

if (score == 'A') {
badgeData.colorscheme = 'brightgreen';
} else if (score == 'B') {
badgeData.colorscheme = 'green';
} else if (score == 'C') {
badgeData.colorscheme = 'yellow';
} else if (score == 'D') {
badgeData.colorscheme = 'orange';
} else {
badgeData.colorscheme = 'red';
}
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});

});
}));

// // Code Climate integration
camp.route(/^\/codeclimate\/(.+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var userRepo = match[1]; // eg, `github/kabisaict/flow`.
Expand Down Expand Up @@ -2589,9 +2659,9 @@ cache(function(data, match, sendBadge, request) {
} else if (score > 3) {
badgeData.colorscheme = 'green';
} else if (score > 2) {
badgeData.colorscheme = 'yellowgreen';
} else if (score > 1) {
badgeData.colorscheme = 'yellow';
} else if (score > 1) {
badgeData.colorscheme = 'orange';
} else {
badgeData.colorscheme = 'red';
}
Expand Down
57 changes: 57 additions & 0 deletions service-tests/codeclimate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

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

const t = new ServiceTester({ id: 'codeclimate', title: 'Code Climate' })

t.create('maintainability score')
.get('/maintainability/Nickersoft/dql.json')
.expectJSONTypes(Joi.object().keys({
name: 'maintainability',
value: Joi.equal('A', 'B', 'C', 'D', 'F', 'unknown')
}));

t.create('maintainability score for non-existent repo')
.get('/maintainability/unknown/unknown.json')
.expectJSON({
name: 'maintainability',
value: 'not found'
});

t.create('maintainability score without content-disposition')
.get('/maintainability/Nickersoft/dql.json')
.intercept(nock => nock('https://api.codeclimate.com')
.get('/v1/repos')
.query({ github_slug: 'Nickersoft/dql' })
.reply(200, { data: [{ attributes: { badge_token: '78ac0fa85c83fea5213a' } }] })
.head('/v1/badges/78ac0fa85c83fea5213a/maintainability')
.reply(200))
.expectJSON({ name: 'maintainability', value: 'invalid' });

t.create('test coverage score')
.get('/c/Nickersoft/dql.json')
.expectJSONTypes(Joi.object().keys({
name: 'coverage',
value: Joi.equal('A', 'B', 'C', 'D', 'F', 'unknown')
}));

t.create('test coverage score for non-existent repo')
.get('/c/unknown/unknown.json')
.expectJSON({
name: 'coverage',
value: 'not found'
});

t.create('test coverage score without content-disposition')
.get('/c/Nickersoft/dql.json')
.intercept(nock => nock('https://api.codeclimate.com')
.get('/v1/repos')
.query({ github_slug: 'Nickersoft/dql' })
.reply(200, { data: [{ attributes: { badge_token: '78ac0fa85c83fea5213a' } }] })
.head('/v1/badges/78ac0fa85c83fea5213a/test_coverage')
.reply(200))
.expectJSON({ name: 'coverage', value: 'invalid' });


module.exports = t;

0 comments on commit 632c70b

Please sign in to comment.