Gitlab MR and branch analysis

which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
SonarQube 8.1, developer edition, analysing C++
Gitlab enterprise 12.6

what are you trying to achieve
I’m trying to set up analysis of merge requests such that you can see:

  • issues added/removed by the MR, change in coverage by the MR
  • all issues in the codebase for the MR branch, coverage for the codebase for the MR branch

what have you tried so far to achieve this
So far it seems like if you use pull request analysis, you get issues that were added by the MR (but not what was removed), and you also can’t see all issues for the MR branch (e.g. to compare with master and see whether you have removed issues).
Branch analysis I think shows you all the issues for that branch but I couldn’t see how you can run both branch analysis and pull request analysis at the same time? I think sonar-scanner autodetects which one to run based on whether certain environment variables exist but I don’t really want to have to run a pipeline for both a branch and a merge request just to get the sonar analysis for both.

Ideally with PR analysis you could just switch between metrics for new code (changed by the PR) and all code (state of the repository at the tip of the PR)…

Any advice or pointers?
Thanks
Nathanael

Welcome to the community forum!

Indeed, the scanner will detect env variable to know if the build is happening in a MR context, or a branch context. So if you want both, you have to run the analysis twice. You can add the “branch” to the job, as described in the gitlab pipeline documentation. Both job will be triggered when you push on a MR.