-
-
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
add [Sonar] badges for various test metrics #3571
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
87e1425
feat: add SonarTests badges
calebcartwright 99d1851
chore: remove commented out line
calebcartwright 8b32b94
Merge branch 'master' into sonar-tests-badge
calebcartwright 1c25b37
Merge branch 'master' into sonar-tests-badge
calebcartwright ac02b4d
Merge branch 'master' into sonar-tests-badge
calebcartwright f3431cb
Merge branch 'master' into sonar-tests-badge
calebcartwright 9e86182
Merge branch 'master' into sonar-tests-badge
calebcartwright ca57f88
refactor: update SonarTestsSummary to override transform
calebcartwright fdcd73f
Merge branch 'master' into sonar-tests-badge
calebcartwright dd74c91
Merge branch 'master' into sonar-tests-badge
calebcartwright File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
'use strict' | ||
|
||
const { | ||
testResultQueryParamSchema, | ||
renderTestResultBadge, | ||
documentation: testResultsDocumentation, | ||
} = require('../test-results') | ||
const { metric: metricCount } = require('../text-formatters') | ||
const SonarBase = require('./sonar-base') | ||
const { | ||
documentation, | ||
keywords, | ||
patternBase, | ||
queryParamSchema, | ||
getLabel, | ||
} = require('./sonar-helpers') | ||
|
||
class SonarTestsSummary extends SonarBase { | ||
static get category() { | ||
return 'build' | ||
} | ||
|
||
static get route() { | ||
return { | ||
base: 'sonar', | ||
pattern: `${patternBase}/tests`, | ||
queryParamSchema: queryParamSchema.concat(testResultQueryParamSchema), | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Sonar Tests', | ||
namedParams: { | ||
protocol: 'http', | ||
host: 'sonar.petalslink.com', | ||
component: 'org.ow2.petals:petals-se-ase', | ||
}, | ||
queryParams: { | ||
sonarVersion: '4.2', | ||
compact_message: null, | ||
passed_label: 'passed', | ||
failed_label: 'failed', | ||
skipped_label: 'skipped', | ||
}, | ||
staticPreview: this.render({ | ||
passed: 5, | ||
failed: 1, | ||
skipped: 0, | ||
total: 6, | ||
isCompact: false, | ||
}), | ||
keywords, | ||
documentation: ` | ||
${documentation} | ||
${testResultsDocumentation} | ||
`, | ||
}, | ||
] | ||
} | ||
|
||
static get defaultBadgeData() { | ||
return { | ||
label: 'tests', | ||
} | ||
} | ||
|
||
static render({ | ||
passed, | ||
failed, | ||
skipped, | ||
total, | ||
passedLabel, | ||
failedLabel, | ||
skippedLabel, | ||
isCompact, | ||
}) { | ||
return renderTestResultBadge({ | ||
passed, | ||
failed, | ||
skipped, | ||
total, | ||
passedLabel, | ||
failedLabel, | ||
skippedLabel, | ||
isCompact, | ||
}) | ||
} | ||
|
||
transformTestResults({ json, sonarVersion }) { | ||
const { | ||
tests: total, | ||
skipped_tests: skipped, | ||
test_failures: failed, | ||
} = this.transform({ | ||
json, | ||
sonarVersion, | ||
}) | ||
|
||
return { | ||
total, | ||
passed: total - (skipped + failed), | ||
failed, | ||
skipped, | ||
} | ||
} | ||
|
||
async handle( | ||
{ protocol, host, component }, | ||
{ | ||
sonarVersion, | ||
compact_message: compactMessage, | ||
passed_label: passedLabel, | ||
failed_label: failedLabel, | ||
skipped_label: skippedLabel, | ||
} | ||
) { | ||
const json = await this.fetch({ | ||
sonarVersion, | ||
protocol, | ||
host, | ||
component, | ||
metricName: 'tests,test_failures,skipped_tests', | ||
}) | ||
const { total, passed, failed, skipped } = this.transformTestResults({ | ||
json, | ||
sonarVersion, | ||
}) | ||
return this.constructor.render({ | ||
passed, | ||
failed, | ||
skipped, | ||
total, | ||
isCompact: compactMessage !== undefined, | ||
passedLabel, | ||
failedLabel, | ||
skippedLabel, | ||
}) | ||
} | ||
} | ||
|
||
class SonarTests extends SonarBase { | ||
static get category() { | ||
return 'build' | ||
} | ||
|
||
static get route() { | ||
return { | ||
base: 'sonar', | ||
pattern: `${patternBase}/:metric(total_tests|skipped_tests|test_failures|test_errors|test_execution_time|test_success_density)`, | ||
queryParamSchema, | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Sonar Test Count', | ||
pattern: `${patternBase}/:metric(total_tests|skipped_tests|test_failures|test_errors)`, | ||
namedParams: { | ||
protocol: 'http', | ||
host: 'sonar.petalslink.com', | ||
component: 'org.ow2.petals:petals-log', | ||
metric: 'total_tests', | ||
}, | ||
queryParams: { | ||
sonarVersion: '4.2', | ||
}, | ||
staticPreview: this.render({ | ||
metric: 'total_tests', | ||
value: 131, | ||
}), | ||
keywords, | ||
documentation, | ||
}, | ||
{ | ||
title: 'Sonar Test Execution Time', | ||
pattern: `${patternBase}/test_execution_time`, | ||
namedParams: { | ||
protocol: 'https', | ||
host: 'sonarcloud.io', | ||
component: 'swellaby:azure-pipelines-templates', | ||
}, | ||
queryParams: { | ||
sonarVersion: '4.2', | ||
}, | ||
staticPreview: this.render({ | ||
metric: 'test_execution_time', | ||
value: 2, | ||
}), | ||
keywords, | ||
documentation, | ||
}, | ||
{ | ||
title: 'Sonar Test Success Rate', | ||
pattern: `${patternBase}/test_success_density`, | ||
namedParams: { | ||
protocol: 'https', | ||
host: 'sonarcloud.io', | ||
component: 'swellaby:azure-pipelines-templates', | ||
}, | ||
queryParams: { | ||
sonarVersion: '4.2', | ||
}, | ||
staticPreview: this.render({ | ||
metric: 'test_success_density', | ||
value: 100, | ||
}), | ||
keywords, | ||
documentation, | ||
}, | ||
] | ||
} | ||
|
||
static get defaultBadgeData() { | ||
return { | ||
label: 'tests', | ||
} | ||
} | ||
|
||
static render({ value, metric }) { | ||
let color = 'blue' | ||
let label = getLabel({ metric }) | ||
let message = metricCount(value) | ||
|
||
if (metric === 'test_failures' || metric === 'test_errors') { | ||
color = value === 0 ? 'brightgreen' : 'red' | ||
} else if (metric === 'test_success_density') { | ||
color = value === 100 ? 'brightgreen' : 'red' | ||
label = 'tests' | ||
message = `${value}%` | ||
} | ||
|
||
return { | ||
label, | ||
message, | ||
color, | ||
} | ||
} | ||
|
||
async handle({ protocol, host, component, metric }, { sonarVersion }) { | ||
const json = await this.fetch({ | ||
sonarVersion, | ||
protocol, | ||
host, | ||
component, | ||
// We're using 'tests' as the metric key to provide our standard | ||
// formatted test badge (passed, failed, skipped) that exists for other | ||
// services. Therefore, we're exposing 'total_tests' to the user, and | ||
// need to map that to the 'tests' metric which sonar uses to represent the | ||
// total number of tests. | ||
// https://docs.sonarqube.org/latest/user-guide/metric-definitions/ | ||
metricName: metric === 'total_tests' ? 'tests' : metric, | ||
}) | ||
const metrics = this.transform({ json, sonarVersion }) | ||
return this.constructor.render({ | ||
value: metrics[metric === 'total_tests' ? 'tests' : metric], | ||
metric, | ||
}) | ||
} | ||
} | ||
|
||
module.exports = [SonarTestsSummary, SonarTests] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 there a reason to make
transform()
andtransformTestResults()
as 2 seperate methods here?Seems like it would be more natural to just override
transform()
and call the function in the parent class withsuper.transform()
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.
Good idea!