-
Notifications
You must be signed in to change notification settings - Fork 142
Measure badges
In order to generate a badge that displays any measure related to a project or view analysed by your SonarQube instance, call the following REST webservice:
/api/badges/measure?key=<project or view key>&metric=<metric key>
The badge displays the name of the metric in the left part of the badge with the measured value in the right part of the badge.
If the measure impacts the quality gate of the project, the badge is colorized depending on the condition that has been set (requires version 3.0.0
.)
If no measure is available for the metric, or if no metric with this <metric key>
exists, then N/A
will be displayed in the right part of the badge.
Here are example live badges displaying the current value of some metrics measured on the SVG Badges plugin codebase taken from SonarQube.com:
As for quality gate status badges, the plugin generates by default badges with rounded corners. If you want to generate flat badges instead, add the optional parameter template
with value FLAT
as follows:
/api/badges/measure?key=<project or view key>&metric=<metric key>&template=FLAT
(requires version 3.0.0
.)
If you want the badge to be blinking if the displayed measure causes the quality gate's ERROR
status, add the optional parameter blinking
with value true
as follows:
/api/badges/measure?key=<project or view key>&metric=<metric key>&blinking=true
(requires version 3.0.0
.)
SonarQube's CoreMetrics class file lists all metric keys
that can be used. For even more details, see the SonarQube Metric Definitions. I have listed the ones you may need to display as a badge in the following table :
Metric key | Description | SonarQube version |
---|---|---|
lines | # of lines (including comments and empty lines) | >= 5.1 |
ncloc | # of lines of code (excluding comments and empty lines) | |
comment_lines_density | Percentage of comments | |
public_documented_api_density | Percentage of documented API | deprecated since SQ 6.2 |
function_complexity | Complexity by function | |
test_errors | # of tests that have stopped due to an error | |
test_failures | # of tests whose assertions have failed | |
skipped_tests | # of ignored tests | |
test_success_density | %age of tests that have succeeded | |
coverage | UT coverage %age | |
new_coverage | UT coverage %age on new code | |
it_coverage | IT coverage %age | < 6.2 |
new_it_coverage | IT coverage %age on new code | < 6.2 |
overall_coverage | Overall coverage %age | < 6.2 |
new_overall_coverage | Overall coverage %age on new code | < 6.2 |
duplicated_lines_density | lines duplication %age | |
new_duplicated_lines_density | new lines duplication %age | >= 6.1 |
blocker_violations | # of blocker issues | |
critical_violations | # of new critical issues | |
new_blocker_violations | # of blocker issues | |
new_critical_violations | # of new critical issues | |
code_smells | # of code smells | >= 5.5 |
new_code_smells | # of new code smells | >= 5.5 |
bugs | # of bugs | >= 5.5 |
new_bugs | # of new bugs | >= 5.5 |
vulnerabilities | # of vulnerabilities | >= 5.5 |
new_vulnerabilities | # of new vulnerabilities | >= 5.5 |
sqale_debt_ratio | technical debt ratio | >= 4.5 |
new_sqale_debt_ratio | new technical debt ratio | >= 5.2 |
new_maintainability_rating | new maintainability rating | >= 6.2 |
new_reliability_rating | new reliability rating | >= 6.2 |
new_security_rating | new security rating | >= 6.2 |
Note: Some metric keys
are not usable "as is" as they return values that need interpretation (such as time, effort or ratings.) This explains why I haven't added them to the table yet.
As for Quality Gate badges, you can use HTML or markdown as follows:
<a href="<serverBaseURL>/dashboard/index/<project or view key>"><img src="<serverBaseURL>/api/badges/measure?key=<project or view key>&metric=<core metric key>"/></a>
Example:
<a href="http://localhost:9000/dashboard/index/com.qualinsight.plugins.sonarqube:qualinsight-plugins-sonarqube-badges"><img src="http://localhost:9000/api/badges/measure?key=com.qualinsight.plugins.sonarqube:qualinsight-plugins-sonarqube-badges&metric=coverage"/></a>
[![Quality Gate](<serverBaseURL>/api/badges/gate?key=<project or view key>&metric=<core metric key>)](<serverBaseURL>/dashboard/index/<project or view key>)
Example:
[![Quality Gate](http://localhost:9000/api/badges/measure?key=com.qualinsight.plugins.sonarqube:qualinsight-plugins-sonarqube-badges&metric=coverage)](http://localhost:9000/dashboard/index/com.qualinsight.plugins.sonarqube:qualinsight-plugins-sonarqube-badges)