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

[CodeClimate] Fix coverage and maintainability score badges #1368

Merged
merged 1 commit into from
Dec 18, 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
68 changes: 33 additions & 35 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2774,70 +2774,68 @@ cache(function(data, match, sendBadge, request) {
// 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];
const isCoverage = match[1] === 'c';
const userRepo = match[2]; // eg, `kabisaict/flow`.
const format = match[3];
request({
method: 'GET',
uri: 'https://api.codeclimate.com/v1/repos?github_slug=' + userRepo,
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);
const badgeData = getBadgeData(isCoverage ? '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';
try {
if (!body.data || body.data.length === 0) {
badgeData.text[1] = 'not found';
sendBadge(format, badgeData);
return;
} else if (isCoverage && body.data[0].relationships.latest_default_branch_test_report.data == null
|| !isCoverage && body.data[0].relationships.latest_default_branch_snapshot.data == null) {
badgeData.text[1] = 'unknown';
sendBadge(format, badgeData);
return;
}

try {
var statusMatch = res.headers['content-disposition']
.match(/filename=".*(?:maintainability|test_coverage)-(.+)\.svg"/);
if (!statusMatch) {
badgeData.text[1] = 'unknown';
let apiUrl = `https://api.codeclimate.com/v1/repos/${body.data[0].id}/`;
if (isCoverage) {
apiUrl += `test_reports/${body.data[0].relationships.latest_default_branch_test_report.data.id}`;
} else {
apiUrl += `snapshots/${body.data[0].relationships.latest_default_branch_snapshot.data.id}`;
}
request(apiUrl, function(err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
return;
}

var score = statusMatch[1].replace('-', '.');
const parsedData = JSON.parse(buffer);
const score = isCoverage ? parsedData.data.attributes.rating.letter : parsedData.data.attributes.ratings[0].letter;
badgeData.text[1] = score;

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

});
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

Expand Down
30 changes: 8 additions & 22 deletions service-tests/codeclimate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ t.create('maintainability score')
.get('/maintainability/Nickersoft/dql.json')
.expectJSONTypes(Joi.object().keys({
name: 'maintainability',
value: Joi.equal('A', 'B', 'C', 'D', 'F', 'unknown')
value: Joi.equal('A', 'B', 'C', 'D', 'F')
}));

t.create('maintainability score for non-existent repo')
Expand All @@ -19,21 +19,11 @@ t.create('maintainability score for non-existent repo')
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')
value: Joi.equal('A', 'B', 'C', 'D', 'F')
}));

t.create('test coverage score for non-existent repo')
Expand All @@ -43,15 +33,11 @@ t.create('test coverage score for non-existent repo')
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' });

t.create('test coverage score for repo without test reports')
.get('/c/kabisaict/flow.json')
.expectJSON({
name: 'coverage',
value: 'unknown'
});

module.exports = t;