-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from 1 commit
4abadf0
96d1c97
5f67ecf
323cacd
1945b7e
f8ca682
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
badgeData.text[1] = 'not found'; | ||
sendBadge(format, badgeData); | ||
} | ||
}); | ||
|
||
}); | ||
})); | ||
|
||
|
||
// Code Climate maintainability | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`. | ||
|
@@ -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'; | ||
} | ||
|
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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency with a couple other badges, could you change |
||
.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; |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.