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

Added support for new Code Climate scores #1236

Merged
merged 6 commits into from
Nov 6, 2017
Merged
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
8 changes: 8 additions & 0 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,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 @@ -2568,7 +2568,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the "project not found" case? Is it possible to test that more explicitly above, so you can reserve the catch for "something went wrong" and return invalid instead of not found?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not exactly sure... I copied these code blocks from the existing CodeClimate blocks. Could you elaborate on when "not found" vs. "invalid" vs. "unknown" should be displayed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • When someone is asking for a nonexistent project, we should display something really clear and helpful like project not found, which tells them they have spelled something wrong, the project isn't public, etc.
  • On the other hand unknown would mean CodeClimate has the project but for some reason can't answer the question, like some kind of error occurred when trying to compute it.
  • And invalid is like a 500 error: something went wrong on our side or their side when we tried to fetch the information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I gotcha. I tried to fixed the errors to be more appropriate, so take a look when you can.

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 @@ -2600,9 +2670,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;