Sonar property for reference branch

Hi Team,

In new release 8.4.1, there is a new feature on the new code reference branch.


Is there any sonar property for setting the reference branch?

I would like to include this in sonar.properties file

Thanks,
Abhishek

Hi @aanvekar001 ,
Did you get a way for setting the Reference Branch dynamically?
I see no one from sonar has responded.

Welcome :slight_smile:

there’s no property (yet) i know of, using the latest version 8.6.0.39681
But you may use the web api api/new_code_periods/set as described in this post,
see point 3 for example.

Gilbert

@anon67236913

Thanks alot, we were exploring the same api.

Thanks
Sunil

Hi @dozinbatty I configured it from the portal itself, no one from sonar responded.

Thanks,
Abhishek

@anon67236913
We were able to set the reference_branch using api/new_code/set and it worked like a charm.
The only challenge we have is when a new branch is created in git and we are sending it for analysis, during that time sonar does not have branch created so we cannot set the reference branch for that scenario. The new branch is suppose to live only for one commit in git.
So the question is there a way we could create a branch in sonar with an api, then set the reference branch and then call the analysis.

Hi,

no need to create a branch via api, as it is automatically created via sonar.branch.name property
during the analysis.

We use it like that, i.e.
A generic Jenkins pipeline for Maven builds implemented as shared library and driven by
a yaml config file.

The pipeline checks if project already exists using api/projects/search , if not it is created.
https://<yoursonar>/api/projects/create?project=com.foo:foobar&name=foobar
Normally project is created automatically, it’s only because of the reference branch / new code setting.

If yaml has sonarMainBranch: xxx the main branch is renamed, otherwise default = master.
https://<yoursonar>/api/project_branches/rename?project=com.foo:foobar&name=xxx
It’s also checked if this branch exists in Git !

Main branch is set as reference branch for all branches.
https://<yoursonar>/api/new_code_periods/set?project=com.foo:foobar&type=REFERENCE_BRANCH&value=xxx

Finally you set new code for the main branch to previous version, as it can not reference itself.
https://<yoursonar>/api/new_code_periods/set?project=com.foo:foobar&branch=xxx&type=PREVIOUS_VERSION

This will result in

any new branch will automatically use the reference branch project setting

Gilbert

@anon67236913
Thanks alot.
We didn’t want to do the changes at the project level. But if that is what we have to do achieve the analysis, we will do.

Using a reference branch for the new code setting is only available at projekt and branch level.
see Defining New Code | SonarQube Docs for all details.

But I still can’t find a property to set this value in the build script. Only GUI is possible which is bad for automation.

2 Likes

Right now there is no property to use within your build.
The only way is via web api, as shown above in my posting.
You might wrap it as shared library if using Jenkins pipelines.

@anon67236913 this only works if you are using the same reference branch for all branches. There may be multiple “long lived”/“develop” branches which are used as reference branches. With a property, this would be no problem as in every branch’s Jenkinsfile the respective reference branch would be specified and passed on to the SQ analysis.
Such a property was available for SQ 7.x so its annoying and not understandable why this feature is not considered to be important anymore.

1 Like

Sure, this is just an example for a generic pipeline.
You may create a sharedlib with two branch parameters using api/new_code_periods/set
https://<sonarhost>/api/new_code_periods/set?project=$projectkey&branch=$branch&&type=REFERENCE_BRANCH&value=$otherbranch
And you’re right, i also didn’t understand why sonar.branch.target has been removed.

hi @dozinbatty
Did you get a chance to configure reference branch for new code dynamically or maybe automatically?

Cheers

1 Like

Its better to have new sonar property to pass reference branch during scan itself. This is flexible option. SonarQube team should work on it.

1 Like

Sure, i’ve already created a feature request, which got 8 votes but still no sonar.branch.target
or similar property.

Yes there’s a sonar property for reference branch
https://docs.sonarqube.org/latest/analysis/analysis-parameters/

Additionally, it would be great to be able to set the default on the instance level to be reference based.
Currently it’s not possible (at least in 8.9.x LTS)