jenkinssonarqubejenkins-pipelinegerritgerrit-trigger

Jenkins Gerrit reporting values plugin removes Code-review's value


I have a Pipeline script with two steps.

If the SonarQube finds warnings, it reports them back to Gerrit as comments and set the Code-review-1. The next stage is the UnitTest and if it is OK the Pipeline will be successful and the Jenkins should report back to Gerrit Verified+1. BUT, when Jenkins reports the Verified+1 then it removes the Code-review-1.

Related part of my Pipeline script:

....
    steps {
        withSonarQubeEnv('Managed SonarQube') {
            sh '''./sonar_runner.sh preview'''
            sonarToGerrit(
                inspectionConfig: [
                    serverURL: env.'SONAR_HOST_URL',
                    baseConfig: [
                        sonarReportPath: '.scannerwork/sonar-report.json',
                        autoMatch: true
                    ]
                ],
                scoreConfig: [
                    issueFilterConfig: [
                        severity: 'MINOR',
                        newIssuesOnly: false,
                        changedLinesOnly: false
                    ],
                    category: 'Code-Review',
                    noIssuesScore: 0,
                    issuesScore: -1
                ]
            )
        }
        stage('UnitTest') {
            steps {
                ansiColor('xterm') {
                    sh '''./unittest.sh'''
                }
      ....

My "Gerrit Reporting Values" section:

Gerrit Reporting Values section

My Gerrit history:

Gerrit history

My final result:

Final result

My question:

How can I set the the Code-review-1 and Verified+1 in one running? How can I avoid that Gerrit removes the Code-review-1 when reports Verified+1? I am open to GUI solution as well as Pipeline.

EDIT:

It is not option to change the global config of Gerrit plugin. I have to solve it on Jenkins job level. Is it possible?


Solution

  • First of all, as I mentioned in my question the global Gerrit config change and new Gerrit server were not option for me. I needed to solve this problem on Jenkins job level.

    I have found a "solution" which is rather a work-around, but it works.

    Step 0:

    If you check the STDOUT of SonarQube in the Jenkins console log, you can see a specific line which indicates the number of issues which are effected of score calculation. This line is: Issues to be involved in score calculation: X. It means you can know if there is effected issues or not based on this line.

    Jenkins console log

    Step 1:

    You should check the Jenkins console log and find the number of issues which are involved in score calculation. You can see below my implementation for it. If there is issue (The RegEx value is not zero) then this stage should set the build result to UNSTABLE.

    stage('Results') {
                steps {
                    script{
                            for(String line : currentBuild.getRawBuild().getLog(30)){
                                def find_pattern = (line =~ /^Issues to be involved in score calculation: [1-9]/)
                                if(find_pattern){
                                    echo line
                                    echo "Sonar has found warnings in changed lines. Build goes to UNSTABLE."
                                    currentBuild.result = "UNSTABLE"
                                }
                            }
                        }
    

    Example output of how it works:

    Report has loaded and contains 1021 issues
    Issues to be commented: 1
    Issues to be involved in score calculation: 1
    Review has been sent
    [Pipeline] }
    [Pipeline] // withSonarQubeEnv
    [Pipeline] }
    [Pipeline] // ansiColor
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (Results)
    [Pipeline] script
    [Pipeline] {
    [Pipeline] echo
    Issues to be involved in score calculation: 1
    [Pipeline] echo
    Sonar has found warnings in changed lines. Build goes to UNSTABLE.
    

    Step 2:

    Configure the Gerrit Reporting Values block to report back the both values (CR and Verified labels) back to Gerrit in case of UNSTABLE Build result.

    enter image description here