The plugin shares SonarQube feedback with developers via Gerrit Code Review.
Jenkins version 2.249.3 or newer is required.
SonarQube report generation must happen within SonarQube Scanner wrapper.
On a non-pipeline job, you can enable SonarQube Scanner wrapper by
checking Prepare SonarQube Scanner environment
:
On a pipeline job, SonarQube Scanner wrapper is represented by withSonarQubeEnv
.
For example:
withSonarQubeEnv('my-sonar-installation-name') {
withMaven(maven: 'my-maven-installation-name') {
sh "mvn clean verify sonar:sonar -Dsonar.pullrequest.key=${env.GERRIT_CHANGE_NUMBER}-${env.GERRIT_PATCHSET_NUMBER} -Dsonar.pullrequest.base=${env.GERRIT_BRANCH} -Dsonar.pullrequest.branch=${env.GERRIT_REFSPEC}"
}
}
Gerrit Trigger plugin should be installed and configured.
Rest API should be configured in the Advanced section of Gerrit Trigger settings.
HTTP authentication data should be set up. Enable Code-Review and Enable Verified checkboxes should be checked on.
For complete guidance please see Gerrit Trigger Wiki page.
In case when the plugin is runing outside of a job with Gerrit Trigger the next environmental variables should be set:
GERRIT_NAME
- Gerrit server nameGERRIT_CHANGE_NUMBER
- Change numberGERRIT_PATCHSET_NUMBER
- Patchset numberInstall the plugin via Jenkins plugin manager. Gerrit Trigger plugin should be also installed and configured.
Sonar Gerrit plugin is intended to run as post-build action. Choose it from available post-build actions.
There are several settings allowing to customise the plugin for your needs.
This analysis strategy is based on https://docs.sonarqube.org/latest/analysis/pull-request/ .
The SonarQube instance must either have sonarqube-community-branch-plugin enabled or be of developer edition type.
In order to run a pull request scan, Sonar
requires the following mandatory properties:
Key | Recommended value template | Example |
---|---|---|
sonar.pullrequest.key | ${GERRIT_CHANGE_NUMBER}-${GERRIT_PATCHSET_NUMBER} | 250-1 |
sonar.pullrequest.base | ${GERRIT_BRANCH} | master |
sonar.pullrequest.branch | ${GERRIT_REFSPEC} | refs/changes/01/1/1 |
Example of Maven
target:
clean verify sonar:sonar -Dsonar.pullrequest.key=${GERRIT_CHANGE_NUMBER}-${GERRIT_PATCHSET_NUMBER} -Dsonar.pullrequest.base=${GERRIT_BRANCH} -Dsonar.pullrequest.branch=${GERRIT_REFSPEC}
sonarToGerrit(
inspectionConfig: [
analysisStrategy: pullRequest()
]
)
This analysis strategy is intended to work with report provided by SonarQube running on a project in preview mode. That means SonarQube report generation should be included to build.
If you use Maven, fill out "Goals and options" field in "Build" section of your Jenkins job:
clean verify sonar:sonar -Dsonar.analysis.mode=preview -Dsonar.report.export.path=sonar-report.json
Use setting "Project configuration" if only one SonarQube report is generated and static code analysis of the whole project is required.
Use setting "Sub-project configurations" to specify modules and paths for separate reports if modules are analysed separately or not every module needs to be analysed.
Settings:
target/sonar/sonar-report.json
sonarToGerrit(
inspectionConfig: [
analysisStrategy: previewMode(
sonarQubeInstallationName: 'My SonarQube Installation',
baseConfig: [
projectPath : '',
sonarReportPath: 'target/sonar/sonar-report.json',
autoMatch : true
]
// OR
//subJobConfigs : [
// [
// projectPath: 'module0',
// sonarReportPath: 'target/sonar/sonar-report.json'
// ],
// [
// projectPath: 'module1',
// sonarReportPath: 'target/module1/sonar/sonar-report.json'
// ]
//]
)
]
)
Filter is used to specify what issues will be affected in the output:
It is possible to filter issues by:
**/*
means any file path will match. Tested file paths always start with /
./
.Review settings contains of issue filter to specify issues to be commented and review template.
Posted review comments can be of one of the following types :
STANDARD
- The usual review comment that everyone use.ROBOT
- Available since Gerrit 2.14. Review comment type to be used by robots. See https://gerrit-review.googlesource.com/Documentation/config-robot-comments.html for more details.This section allows user to customise text, intended to use as review title and issue comment.
Starting with v. 2.1 it's become possible to specify a separate filter for score settings.
Please note: to use Gerrit category other than Default it is necessary to configure it in Gerrit. See details in Gerrit Documentation.
An example of settings to be added to the project.config for creating Sonar-Verified category:
[label "Sonar-Verified"]
function = MaxWithBlock
value = -1 Issues found
value = 0 No score
value = +1 Verified
defaultValue = 0
And access rights:
[access "refs/heads/*"]
label-Sonar-Verified = -1..+1 group Project Owners
label-Sonar-Verified = -1..+1 group Non-Interactive Users
To override the credentials used to post comments on the job level set up section "Override default HTTP credentials". ( Global credentials on the Gerrit Trigger Server level should be set up as well for Gerrit Trigger needs.)
This functionality works when Gerrit is configured with post server settings.
Options :
node {
stage('Build') {
// trigger build
git url: 'ssh://your_project_repo'
// Fetch the changeset to a local branch using the build parameters provided to the build by the Gerrit Trigger...
def changeBranch = "change-${env.GERRIT_CHANGE_NUMBER}-${env.GERRIT_PATCHSET_NUMBER}"
sh "git fetch origin ${env.GERRIT_REFSPEC}:${changeBranch}"
sh "git checkout ${changeBranch}"
try {
withSonarQubeEnv('my-sonar-installation') {
withMaven(maven: 'my-maven-installation') {
sh "mvn clean verify sonar:sonar -Dsonar.pullrequest.key=${env.GERRIT_CHANGE_NUMBER}-${env.GERRIT_PATCHSET_NUMBER} -Dsonar.pullrequest.base=${env.GERRIT_BRANCH} -Dsonar.pullrequest.branch=${env.GERRIT_REFSPEC}"
}
}
} finally {
sonarToGerrit(
inspectionConfig: [
analysisStrategy: pullRequest()
]
/* Optional parameters
, reviewConfig: [
commentType: 'STANDARD', // 'STANDARD' or 'ROBOT'
issueFilterConfig : [
severity : 'INFO',
newIssuesOnly : false,
changedLinesOnly: false,
includedPathsGlobPattern: null,
excludedPathsGlobPattern: null,
],
noIssuesTitleTemplate : 'SonarQube violations have not been found.',
someIssuesTitleTemplate: '<total_count> SonarQube violations have been found.',
issueCommentTemplate : '<severity> SonarQube violation:\n\n\n<message>\n\n\nRead more: <rule_url>'
omitDuplicateComments : 'If true, comments with the same content at the same place will be omitted by Gerrit. Defaults to false.'
],
scoreConfig: [
issueFilterConfig: [
severity : 'INFO',
newIssuesOnly : false,
changedLinesOnly: false,
includedPathsGlobPattern: null,
excludedPathsGlobPattern: null,
],
category : 'Code-Review',
noIssuesScore : 0,
issuesScore : -1
],
notificationConfig: [
noIssuesNotificationRecipient : 'NONE',
commentedIssuesNotificationRecipient: 'OWNER',
negativeScoreNotificationRecipient : 'OWNER'
],
authConfig: [
httpCredentialsId: 'b948c0ba-51a2-4eb7-b42b-71e6a77d7d34'
]*/
)
}
}
}
node {
stage('Build') {
// trigger build
git url: 'ssh://your_project_repo'
// Fetch the changeset to a local branch using the build parameters provided to the build by the Gerrit Trigger...
def changeBranch = "change-${env.GERRIT_CHANGE_NUMBER}-${env.GERRIT_PATCHSET_NUMBER}"
sh "git fetch origin ${env.GERRIT_REFSPEC}:${changeBranch}"
sh "git checkout ${changeBranch}"
try {
withSonarQubeEnv('my-sonar-installation') {
withMaven(maven: 'my-maven-installation') {
sh "mvn clean verify sonar:sonar -Dsonar.analysis.mode=preview -Dsonar.report.export.path=sonar-report.json"
}
}
} finally {
sonarToGerrit(
inspectionConfig: [
analysisStrategy: previewMode(
sonarQubeInstallationName: 'My SonarQube Installation',
baseConfig: [
projectPath : '',
sonarReportPath: 'target/sonar/sonar-report.json',
autoMatch : true
]
// OR
//subJobConfigs : [
// [
// projectPath: 'module0',
// sonarReportPath: 'target/sonar/sonar-report.json'
// ],
// [
// projectPath: 'module1',
// sonarReportPath: 'target/module1/sonar/sonar-report.json'
// ]
//]
)
]
/* Optional parameters
, reviewConfig: [
commentType: 'STANDARD', // 'STANDARD' or 'ROBOT'
issueFilterConfig : [
severity : 'INFO',
newIssuesOnly : false,
changedLinesOnly: false,
includedPathsGlobPattern: null,
excludedPathsGlobPattern: null,
],
noIssuesTitleTemplate : 'SonarQube violations have not been found.',
someIssuesTitleTemplate: '<total_count> SonarQube violations have been found.',
issueCommentTemplate : '<severity> SonarQube violation:\n\n\n<message>\n\n\nRead more: <rule_url>'
omitDuplicateComments : 'If true, comments with the same content at the same place will be omitted by Gerrit. Defaults to false.'
],
scoreConfig: [
issueFilterConfig: [
severity : 'INFO',
newIssuesOnly : false,
changedLinesOnly: false,
includedPathsGlobPattern: null,
excludedPathsGlobPattern: null,
],
category : 'Code-Review',
noIssuesScore : 0,
issuesScore : -1
],
notificationConfig: [
noIssuesNotificationRecipient : 'NONE',
commentedIssuesNotificationRecipient: 'OWNER',
negativeScoreNotificationRecipient : 'OWNER'
],
authConfig: [
httpCredentialsId: 'b948c0ba-51a2-4eb7-b42b-71e6a77d7d34'
]*/
)
}
}
}
This message occurres when RestAPIException is thrown by Gerrit API on attempt to post request.
Since version 1.0.7 it is possible to obtain a full stacktrace of the exception using a logger for
class org.jenkinsci.plugins.sonargerrit.SonarToGerritPublisher
The log will contain necessary information about the exception as follows:
In this version plugin settings has moved from Build Steps to Post Build Actions. User needs to reconfigure jobs, or settings will be erased to default.
Report issues and enhancements in the Issue tracker.
Refer to our contribution guidelines
Licensed under MIT, see LICENSE