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] Added missing try-catch block #1613

Merged
merged 2 commits into from
Apr 1, 2018
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
59 changes: 32 additions & 27 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2716,34 +2716,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);
Copy link
Member

Choose a reason for hiding this comment

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

If the inner request call throws after its callback parameter returns, it leads to us writing the response twice, but I can't think of a neat solution apart from counting throws.

I guess it is pretty edge-case, huh? ☺

}
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;