Setting the "Reference branch" for branches created from dev branch

Hello,

We are using SonarQube Developer Edition v2025.1.2 (108896) ACTIVE MQR Mode and it’s working mostly great.

In on our projects, we have the following workflow:

  1. A feature branch is created from dev
  2. The developer works on its branch, using SonarQube (and other tools) along the way to make sure everything is up to standard.
  3. Once the feature is done, a MR is created via GitLab
  4. The MR is reviewed is merged in dev
  5. Once a “sprint” is done, dev is merged into qa
  6. Once a release is ready, qa is merged into main and a release tag/branch couple is created.

The default “New code” definition is thus fine for dev, qa, main and release branches but it’s counter productive for every other branch because the “new code” tab is only comparing two consecutive pushes when we really want to compare to dev

I could change the “New code” definition to use dev as the reference branch, but this would then only be appropriate for the feature branches and not for the ones mentioned above.

From what I gather from the documentation, I should be able to specify the “New code” definition per branch on my project, but it’s not clear how to do the above, especially if I want to do it from the sonar-project.properties file.

Thanks for your help.

Hey there.

Sounds like you want to use the sonar.newcode.referenceBranch analysis parameter.

Just to let you know, in the UI, under Project Settings > New Code, you can set specific New Code Periods per branch.

So, I would need to set the “reference branch” to dev at the project level, and then set a different setting for the specific branches: dev, qa, main and release/*.
But is there a way to have such a regex for the release branches? Going to settings every time a branch is created is not desirable, especially considering that the person creating the branch would not have the right to see/change the settings in SonarQube.

That access rights issue, is also the reason why I’m looking for a solution that works entirely from within the sonar-project.properties file.

You can set sonar.newcode.referenceBranch in a sonar-project.properties file. I was just letting you (and others) know where the per-branch settings are in the UI.

If you want to apply some logic based on branch name, you’d probably want to do that outside sonar-project.properties and in our build pipeline.

(in pseudo-code)

if [[ "$BRANCH_NAME" == release* ]]; then
  SONAR_REF_BRANCH=foo
else
  SONAR_REF_BRANCH=bar
fi
sonar-scanner -Dsonar.newcode.referenceBranch=$SONAR_REF_BRANCH

Thanks for the pseudo code.
What I’m not sure, though, is if it’s possible to use “reference branch” by default and “Previous version” for any branch I decide. If it’s via the CI code as shown above, it’s fine as well.