Change main branch in SonarQube 10.5.1 Developer Edition (API or UI)

I am working with SonarQube 10.5.1 Developer Edition for one of our projects. Our project currently has two branches named “main” [ MAIN BRANCH ] and trunk in SonarQube. However, our source code’s main branch has changed from main to trunk. Now, we want to analyze the quality gate based on the “trunk” branch, and we need to update SonarQube accordingly.

Here is the problem we’re facing:

Current Situation: The “main” [ MAIN BRANCH ] branch in SonarQube corresponds to what used to be “main,” but now We want to make “trunk” as the base and MAIN branch for the quality gate and comparison.

Attempted Solutions:

UI: We couldn’t find an option in the SonarQube UI (under project settings > branches) to rename or set the “trunk” as the new [ MAIN BRANCH ].
API Call: We tried executing a POST API call to rename the “main” [ MAIN BRANCH ] branch to “trunk”[ MAIN BRANCH ] as follows:

curl -X POST “https://sonarqube/api/project_branches/rename” --header “Authorization: squ_beaa9” -d “project=web” -d “name=trunk”
curl -X POST “https://sonarqube/api/project_branches/set_main” --header “Authorization: squ_beaa9” -d “project=web” -d “name=trunk”
We used multiple types of tokens (user, global, and project-specific), but the API did not return any response or error.
Objective: We want to ensure that when we create a PR, SonarQube compares it against the “trunk” branch (which should be the new [ MAIN BRANCH ]) for the quality gate, rather than the old “main” branch.

Could someone guide me on:

How to properly rename or reassign the “[ MAIN BRANCH ]” branch in SonarQube from main to trunk via the UI or API in SonarQube 10.5.1?
Any suggestions on how we can set the trunk branch as the base branch for quality gate comparisons?

Hey there.

Your curl command should look more like this.

curl -X POST 'https://sonarqube/api/project_branches/rename' --header 'Authorization: Bearer squ_token' -d 'project=web-redesign' -d 'name=trunk'

Not the difference in how the authorization header is set.

Authorization: Bearer squ_token rather than Authorization: squ_token as documented here

Hi,

I am trying to rename the main branch of a project (web) from “master” to “trunk” using the SonarQube API, but I keep receiving the following error:

curl -X POST ‘https://sonarqube/api/project_branches/rename’ --header ‘Authorization: Bearer squ_token’ -d ‘project=web’ -d ‘name=trunk’

{"errors":[{"msg":"Insufficient privileges"}]}

I have tried using a global permissions token, user token, and project analysis token, but I get the same response each time.

My user account is part of the DevOps and CloudOps groups, where I have access to everything. However, it seems like I may not have the required permissions to rename the branch.

Could anyone provide guidance on what permissions or settings need to be adjusted to rename the MIAN BRANCH to trunk successfully?

Thanks in advance for your help!

Your user will also need Administer permission on the project itself in order to change the main branch. (docs on project permissions)

This should also expose the Project Settings > Branches and Pull Requests area of the UI where the branch rename can be done in the UI. You’ll know the user has the right permissions if they can see this project-level area.