Skip to content

Commit

Permalink
[CodeClimate] Added missing try-catch block (#1613)
Browse files Browse the repository at this point in the history
* Added missing try-catch block

* Added tests to cover malformed responses
  • Loading branch information
PyvesB authored and espadrine committed Apr 1, 2018
1 parent c5884b1 commit dd35739
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
59 changes: 32 additions & 27 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2710,34 +2710,39 @@ cache(function(data, match, sendBadge, request) {
return;
}

const parsedData = JSON.parse(buffer);
if (type === 'coverage' && isAlternativeFormat) {
const score = parsedData.data.attributes.rating.letter;
badgeData.text[1] = score;
badgeData.colorscheme = letterScoreColor(score);
} else if (type === 'coverage') {
const percentage = parseFloat(parsedData.data.attributes.covered_percent);
badgeData.text[1] = percentage.toFixed(0) + '%';
badgeData.colorscheme = coveragePercentageColor(percentage);
} else if (type === 'issues') {
const count = parsedData.data.meta.issues_count;
badgeData.text[1] = count;
badgeData.colorscheme = colorScale([1, 5, 10, 20], ['brightgreen', 'green', 'yellowgreen', 'yellow', 'red'])(count);
} else if (type === 'technical debt') {
const percentage = parseFloat(parsedData.data.attributes.ratings[0].measure.value);
badgeData.text[1] = percentage.toFixed(0) + '%';
badgeData.colorscheme = colorScale([5, 10, 20, 50], ['brightgreen', 'green', 'yellowgreen', 'yellow', 'red'])(percentage);
} else if (type === 'maintainability' && isAlternativeFormat) {
// maintainability = 100 - technical debt
const percentage = 100 - parseFloat(parsedData.data.attributes.ratings[0].measure.value);
badgeData.text[1] = percentage.toFixed(0) + '%';
badgeData.colorscheme = colorScale([50, 80, 90, 95], ['red', 'yellow', 'yellowgreen', 'green', 'brightgreen'])(percentage);
} else if (type === 'maintainability') {
const score = parsedData.data.attributes.ratings[0].letter;
badgeData.text[1] = score;
badgeData.colorscheme = letterScoreColor(score);
try {
const parsedData = JSON.parse(buffer);
if (type === 'coverage' && isAlternativeFormat) {
const score = parsedData.data.attributes.rating.letter;
badgeData.text[1] = score;
badgeData.colorscheme = letterScoreColor(score);
} else if (type === 'coverage') {
const percentage = parseFloat(parsedData.data.attributes.covered_percent);
badgeData.text[1] = percentage.toFixed(0) + '%';
badgeData.colorscheme = coveragePercentageColor(percentage);
} else if (type === 'issues') {
const count = parsedData.data.meta.issues_count;
badgeData.text[1] = count;
badgeData.colorscheme = colorScale([1, 5, 10, 20], ['brightgreen', 'green', 'yellowgreen', 'yellow', 'red'])(count);
} else if (type === 'technical debt') {
const percentage = parseFloat(parsedData.data.attributes.ratings[0].measure.value);
badgeData.text[1] = percentage.toFixed(0) + '%';
badgeData.colorscheme = colorScale([5, 10, 20, 50], ['brightgreen', 'green', 'yellowgreen', 'yellow', 'red'])(percentage);
} else if (type === 'maintainability' && isAlternativeFormat) {
// maintainability = 100 - technical debt
const percentage = 100 - parseFloat(parsedData.data.attributes.ratings[0].measure.value);
badgeData.text[1] = percentage.toFixed(0) + '%';
badgeData.colorscheme = colorScale([50, 80, 90, 95], ['red', 'yellow', 'yellowgreen', 'green', 'brightgreen'])(percentage);
} else if (type === 'maintainability') {
const score = parsedData.data.attributes.ratings[0].letter;
badgeData.text[1] = score;
badgeData.colorscheme = letterScoreColor(score);
}
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
sendBadge(format, badgeData);
});
} catch(e) {
badgeData.text[1] = 'invalid';
Expand Down
23 changes: 23 additions & 0 deletions services/codeclimate/codeclimate.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,27 @@ t.create('maintainability letter for repo without snapshots')
value: 'unknown'
});

t.create('malformed response for outer user repos query')
.get('/maintainability/angular/angular.js.json')
.intercept(nock => nock('https://api.codeclimate.com')
.get('/v1/repos?github_slug=angular/angular.js')
.reply(200, {
data: [{}] // No relationships in the list of data elements.
}))
.expectJSON({
name: 'maintainability',
value: 'invalid'
});

t.create('malformed response for inner specific repo query')
.get('/maintainability/angular/angular.js.json')
.intercept(nock => nock('https://api.codeclimate.com', {allowUnmocked: true})
.get('/v1/repos/526933117e00a40567008444/snapshots/5ab4427859c16c0001003cad')
.reply(200, {})) // No data.
.networkOn() // Combined with allowUnmocked: true, this allows the outer user repos query to go through.
.expectJSON({
name: 'maintainability',
value: 'invalid'
});

module.exports = t;

0 comments on commit dd35739

Please sign in to comment.