In SonarQube, I can see more than one Branch in sonarqube, but no pull requests. I have multiple successful analysis on the source branch. There are no warnings against the build analysis
In āAdministrationā>>āDevOps Platform Integrationā in the Bitbucket section, I have green checkmarks against both āQuality Gate status reportingā and āImport repositories from your DevOps platformā
In āProject Settingsā>>āDevOps Platform Integrationā I get āConfiguration Validā when I press āCheck Configurationā
My Jenkins job is a Multibranch Pipeline, and my Jenkinsfile includes a withSonarQubeEnv(installationName: 'SonarQube') section, the Jenkins build is successfully posting the results to SonarQube, and I can browse the correct branch in SonarQube and see the results
I modified my SonarScanner command line invocation to include the ā-Xā flag and I can see this:
This topic Sonarqube Pull Request Decoration - Analyses parameters - Get help / SonarQube - SonarSource Community states that the āDiscover pull requests from originā setting should be set in the Bitbucket Branch Source plugin, but that must be for a previous version, as itās not present on my installation. I have the āDiscover Branchesā setting checked, but the only other āDiscoverā¦ā options available are āDiscover other refsā and āDiscover tagsā
Since youāre using the latest version of SonarScanner for Jenkins, you should be seeing the fact that itās a PR analysis picked up automatically from your environment. However, what we see from your logs is that itās being treated as a branch analysis, not a PR analysis.
So I have to start with the dumb questions: the developer actually created a PR, right? And that PR is actually what triggered the analysis and is being analyzed?
Thanks for your reply. I have definitely created a PR, and bitbucket is showing a build status against it.
The Jenkins build is against the branch, the PR isnāt shown in Jenkins at all. I donāt know if itās because of the missing āDiscover pull requests from originā setting in the Bitbucket Branch Source plugin. I found something online (canāt find the page now) saying I should add pull-requests/*/from to Discover other refs in the Branch Source configuration, and now I have a separate job in Jenkins for the PR. This means Iād now need two separate builds, one for the branch and one for the PR. Additionally the PR build doesnāt build automatically when I do a new push to the branch (which is the source branch for the PR), for reasons Iāve yet to discover. Is this really the way itās meant to be set up?
When the Jenkins build for the PR finally completed, there was this in the log:
So Jenkins was still treating it as a branch rather than a PR, and SonarQube still does not have any awareness of any PRs on its GUI. Not sure where to go from here, any other ideas?
We did some work in the SonarScanner for Jenkins in the 8-series to get this all picked up automatically. I donāt think you need the Bitbucket Branch Source plugin (which isnāt maintained by us, so I donāt know anything about it). But I guess it wonāt interfereā¦?
The Scanner for Jenkins is looking for two environment variables to know itās a PR; $CHANGE_ID and $CHANGE_TARGET. And then it sets parameters sonar.pullrequet.key, sonar.pullrequest.branch, and sonar.pullrequest.base. So, can you check your environment to make sure those variables are set? Can you add -Dsonar.scanner.dumpToFile=[file path] to see what analysis parameters the scanner is working with?
Iāve done another run with dumpToFile, and none of the sonar.pullrequest variables are set. Iām not sure Jenkins knows about the PR, because itās building from the branch. I thought of using the Bitbucket REST API to get the PR info, but donāt know how to give Jenkins the credentials for it (even though it should already have them somewhere) , and that would have to be combined with with using git to get the PRs for the commit.
Any ideas how we can get Jenkins the info?
Thanks,
Phil
For Jenkins Pipeline jobs to function for pull requests on Bitbucket Server 7.x, you need to enable āCall Changes apiā option in the plugin configuration.
a. Bitbucket Server 7.x no longer automatically creates the required refs for pull requests. Calls to this new API are required to trigger ref creation.
Apologies, I should have mentioned that earlier; yes I found the same pages and have had āCall Changes APIā enabled from the start. Sorry we keep running up against issues!
So⦠all the pieces seem to be in place, in all the right versions. But we donāt have the environment vars that will trigger PR (versus branch) analysis.
I think itās time to see what env vars we do have. Can you add a printenv to your build script? Maybe in a couple places just to see if the list / values fluctuate?
Also, and since you seem to have already crossed all your tās this feels like a silly question, but. You did this part, right?
You need to configure your Multibranch Pipeline job correctly to avoid issues with Pull Request decoration. From your Multibranch Pipeline job in Jenkins, go to Configure > Branch Sources > Behaviors .
For Bitbucket and GitHub, under Discover pull requests from origin , make sure The current pull request revision is selected.
But I inserted a printenv at the start and end of the Build stage, and added a new stage before the Build stage that just does a sh 'printenv', and in neither of them could I see any CHANGE variables. Hereās the complete list of available variables (with their values stripped):
Within Docker all the variables were the same (with trivial differences to e.g. HOME) except that the following variables were no longer available:
HUDSON_COOKIE
LANG
LOGNAME
STAGE_NAME
USER
But Iām not convinced that these could affect the behaviour.
On that last point, as Iāve mentioned before my Jenkins Bitbucket Source Plugin doesnāt have the āDiscover pull requests from Originā option. Iāve added a small screenshot so you can see Iām not going crazy!
Thanks. Yes, itās from Configure > Branch Sources > Behaviors. All the sources Iāve found on various forums seem to expect it to be there too, but it just isnāt.
I hate to say this, but I think this is going to be on the Jenkins side. The env vars we need to run it as a PR analysis are missing, and you canāt find the option to make them available. Iāve done a little digging online & Iām not finding anything obvious. All your versions are reasonably up to date, so itās not likely to be that youāre missing an upgrade to make this appear.
All I can think is to check your browser console for errors when you should be seeing this āpull requests from originā option and youāre not, and to see what other Jenkins plugins are contributing to that dropdown that might be interfering.
Thanks for all your help, Iāll have to look at creating a post on the Jenkins forum to see if those with more experience with Jenkins to think about.