How to change the main branch in SonarQube?

Hi @OlivierK

Is it possible to set the main branch name to the actual default branch name (in azure devops), so that when default branch changes, it is automatically changed in Sonarqube.

Experiencing the same issue; am full admin using SonarQube ( SQ ) Enterprise 9.4 - build 54424. We have a repo which was imported into SQ - by default the master branch is empty, the develop branch is being used by the team.

SQ is now whining about ‘misconfiguration issues’ and pointing me to many different screens, global settings, help pages discussing what it was like in 7.4, Python workaround, etc, etc. Blah!

At first glance, this seems an easy problem to resolve - delete the master and rename / set develop as the master, or: ‘Dear SQ, please ignore this one and use that one. Thank you!’

Nope - unable to delete master, can only rename. Unable to rename develop, only delete.

Step-by-step instructions, explaining like I’m a small child or a golden retriever, and actually fixing the problem would be most welcome.


OK - Potential Solution

Note

  • I’m a ‘Full Admin’, there can be ‘grades of Admin’ so check which one you have
  • Use the Developer or Enterprise version of SQ, NOT the Community edition
  • Steps 1 and 2 fit my environment, just make sure you have the thing in SQ, it builds and reports some stats

Steps

  1. Import the project from Azure DevOPS ( Projects - Create Project - Azure DevOps )
  2. Put the generated project key in your Prepare analysis on SonarQube task
  3. In SonarQube, click the project name link from the main Projects screen
  4. From the subsequent panel, in the top right-hand corner, you should see a Project Settings dropdown ( No? You aren’t Admin )
  5. From the dropdown in step 4, select: Branches & Pull Requests
  6. Delete the develop branch using the Actions option ( this affects ONLY SQ, nothing else )
  7. Rename the main / master branch to develop
  8. Execute the build pipeline which performs the SQ Analysis as per step 2 above
  9. Go for a cup of tea or coffee

Refresh the Projects screen. For the project in question, I now see the analysis stats ( Bugs, Vulnerabilities, Hotspots Reviewed, etc) instead of a ‘blank hole’. Clicking the project name to drill down, I now see this:

image

Showing the develop branch as the default / main / master.

1 Like

Is it Possible to change Main branch to Development branch in

  • Community Edition
  • Version 9.4 (build 54424)
    because I’ve read some comments where they mentioned in community edition we can’t change Main branch.
    Please advise
1 Like

Workaround “delete & create” is not allowed by our QA.

We changes flag Detault in AZURE regularly, because we don’t have term “main” or “develop” branche. We have only “release/X.Y” branches and the default is that which have flag “Default” in AZURE.

I’ve changed default flag from release/0.7 to release/0.9 a few hours ago. After rebuild on AZURE I’ve this status in “sonarcloud” :

  • Main Branch is still release/0.7
  • but I see, that the version is 0.9 and changelog 6c8f5ad9 is also from release/0.9

@OlivierKo - I am in to the same stage, I have scanned my feature branch named ‘test123’ and I could see the right analysis, however it is reflecting in the UI as master, Say for example if I rename the branch name to ‘test123’ in UI by following the instruction which you have provided, tomorrow if I am actually running the scan on master branch, what will happen to my ‘test123’ will I still be able to scan both master and test123 branch?
Thank you!

Having to “rename” the master branch is very unintuitive. The branch is named master why would I rename it? Having to delete my “main” branch and rename the master branch is very confusing. Both branches exist, what I’m trying to do is to pick which one is the default not rename anything or delete anything.

From a UX perspective this situation is very confusing and it would be nice if the UI was updated to reflect a “selection” of which branch is the main branch.

Additionally, github itself stores metadata about which branch is the default branch and thus it would be extra nice if this was simply detected from github and updated automatically. I can’t imagine ever wanting the “main” branch to differ from the “default” branch. Additionally it would be nice to use the same verbage of “default” branch rather than “main” branch, since its also super confusing to have the “master” branch be the main branch but the main branch, which is actually the default branch is not the main branch… You see where this is going.

So to recap, feature requests:

  1. Change the UI to refer to the default branch as “default branch”, not “main branch”.
  2. Use the github api to automatically detect the “default branch” and automatically update it.
  3. Update the UI to no longer have a “rename” menu item on the default branch, tut instead add a “set as default” menu item to non-default branches.
3 Likes

Agree 100% with your comments Justin. It’s April 2023 and we are on 9.9 release this issue just got worse with the defaults changing to “main”. None of our new projects are getting analyzed without hand holding… The “Main Branch” tag switching needs to be much easier.

3 Likes

Hey @justinmchase and @dbrin .

Unfortunately, changing the main branch in SonarQube takes a lot of changes in the underlying database. We didn’t feel comfortable introducing these changes late in the v9.x series as the LTS got closer.

The first sprint to properly prepare for this feature has been completed, so it is definitely coming.

You should be able to change sonar.projectCreation.mainBranchName under the global Administration > General > Project Creation.

I think you make an interesting point about default vs. main branch, considering this is the verbiage used across all the DevOps Platforms we integrate with (I just checked).

Can I ask that you raise this specific point over at Product Manager for a Day - Sonar Community?

This is also interesting. I’ll make a note to raise the point when we get closer to actually implementing changing the main branch. Synchronization between the DevOps Platform and SonarQube is an important topic for us.

2 Likes

Perhaps we could have just a setting by project, which allow to choose the default branch ?
It will be the branch whose results will be presented by defautl in the UI, whatever its name is.

1 Like

Hi Colin,

Appreciate the information. Any reason why SonarQube wouldn’t simply display the name of the branch being analyzed? I recognize that it’s rather rare for the default branch for a repo to be anything other than “develop”, “main”, or “master”, but why is SonarQube taking a point-of-view? If a repo calls their default branch “abnormal-and-unintuitive-branch-name”, why doesn’t SonarQube show that?

I’m new here and may not properly understand the feature, but any additional guidance would be appreciated.

1 Like

Hey there.

I’m not quite sure I understand your point, but let me try:

From a functional point of view: SonarQube doesn’t really care about the name of the branch (although it sets a default if no information is provided during project onboarding). The issue here is really that sometimes the main branch of a project changes, or is misconfigured at first, and to preserve the history of the branch SonarQube doesn’t just “switch” and tack on measures for the new default branch to the old default branch.

From a technical point of view: since it was first implemented, the “main branch” of a project in SonarQube has been synonymous with the project itself. We have to separate those concepts, which takes quite a lot of work in the database. Once that is done, SonarQube will be much more flexible.

1 Like

Hi all,

So you know, we’re on track to deliver in SonarQube 10.2 the ability to choose a different, existing branch as the main branch.

 
:tada: :tada: :tada:
Ann

7 Likes

Hello, has this feature been released?

Yes @dbrin – in SonarQube v10.2 finally. The latest active version of v10.x is v10.6.

1 Like

Thank you!!! Confirmed.

Hello @ganncamp any idea where to find it in sonar Qube?

@shareef_hiasat_saj Searching “change main branch” in the SonarQube docs will take you pretty far.

Thanks @Colin i tried but am talking about sonar qube Cloud paid one, the link is for sonarqub server not cloud

Hi @shareef_hiasat_saj,

The link is for the SonarQube Server docs because the feature is available on SonarQube Server. But not on Cloud.

 
Ann

1 Like

i see thanks

i added vote to have it in future