How to enable Decorative Pull Requests

We have Bitbucket Server 6.2.7 with Jenkins 2.227 and SonarQube 8.2 Developer license.

I have tried to following the directions for enabling Decorative Pull requests and I have now sent through the branch name using /d:[sonar.branch.name]=${env.BRANCH_NAME} where is the environment variable Jenkins generates sending the branch names as seen below. What am I missing for SonarQube to recognise a PR as a Pull request I could not see this in the user guide?

Hi @Rob_Duncan,

I suggest you upgrade to SQ 8.3, that simplifies the Jenkins setup: parameters for scanner and PR decoration are autodetected, you will just have to launch the scanner without any parameters.

You will need the Bitbucket Source branch plugin and create a multibranch pipeline job in Jenkins, you will find more information in the doc here: https://docs.sonarqube.org/latest/analysis/jenkins/

By the way, sonar.branch.name parameters are used to analyse branches, not Pull Requests, you can find more information here to analyse PR : https://docs.sonarqube.org/latest/analysis/pull-request/

Cheers

Thank you @aurelie. It will take us some time to arrange an upgrade so I looked into the PR parameters which I missed in the user guide. Seeing these helped click the idea that the branch name should be only sent for a non PR and send the PR parameters for PR’s only.

So I tried and managed to get this to work:

//Detect if a PR or not
if(env.BRANCH_NAME.contains('PR-')){
    sonarSwitches = "/d:sonar.pullrequest.key=${env.CHANGE_ID} /d:sonar.pullrequest.branch=${env.CHANGE_BRANCH} /d:sonar.pullrequest.base=${env.CHANGE_TARGET}"
} else {
    sonarSwitches = "/d:sonar.branch.name=${env.BRANCH_NAME}"
}

And then passed this into the sonarScan call:

withSonarQubeEnv('sonarqube-prod-server') {
    bat "SonarScanner.MSBuild.exe begin /k:\"${sonarprojectkey}\" ${sonarSwitches} "
    bat "\"${tool 'msbuildhome'}\\MSBuild.exe\" \"${env.WORKSPACE}\" /t:Rebuild"
    bat "SonarScanner.MSBuild.exe end"
}

Thank you very much - the little pointers helped make this work! I am glad it is going to get easier in the next version - much appreciated.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.