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.