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 1 commit
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/tc/Nickersoft/dql.svg'
},
{
title: 'bitHound',
previewUri: '/bithound/code/github/rexxars/sse-channel.svg'
Expand Down
145 changes: 142 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2568,7 +2568,146 @@ cache(function(data, match, sendBadge, request) {
});
}));

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

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

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

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

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=".*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] = 'not found';
sendBadge(format, badgeData);
}
});

});
}));


// Code Climate maintainability
Copy link
Member

Choose a reason for hiding this comment

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

Could this be merged with the coverage badge above as there is only a few lines difference.
example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call! I went ahead and merged the two :)

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

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

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

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

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-(.+)\.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 = 'yellowgreen';
} else if (score == 'D') {
badgeData.colorscheme = 'yellow';
} else {
badgeData.colorscheme = 'red';
}
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'not found';
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 +2739,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
37 changes: 37 additions & 0 deletions service-tests/codeclimate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'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 unknown repo')
.get('/maintainability/unknown/unknown.json')
.expectJSON({
name: 'maintainability',
value: 'unknown'
});

t.create('test coverage score')
.get('/tc/Nickersoft/dql.json')
Copy link
Member

Choose a reason for hiding this comment

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

For consistency with a couple other badges, could you change /tc/ to /c/ for coverage?

.expectJSONTypes(Joi.object().keys({
name: 'coverage',
value: Joi.equal('A', 'B', 'C', 'D', 'F', 'unknown')
}));

t.create('test coverage score for unknown repo')
.get('/tc/unknown/unknown.json')
.expectJSON({
name: 'coverage',
value: 'unknown'
});


module.exports = t;