Bug? SonarQube does not recognise correct main branch from CI

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)

Developer Edition SonarQube Self hosted
GitLab v.15.1
org.sonarsource.scanner.maven (Java projects)
docker image sonarsource/sonar-scanner-cli 4.6 (Python projects)

  • what are you trying to achieve

We want SonarQube to automatically set the MAIN_BRANCH to main for new projects pushed to sonar via our CI, not master as we no longer use master.

SonarQube picks up the correct MAIN_BRANCH if the project is added to SonarQube FIRST, but this is not possible for us as our projects start with a default CI template that pushes to sonar, so as soon as the project is created in gitlab the bug will occur.

Given it works by manually adding the project, but not when automatically, we’d like to register this as a bug.

It creates risk for us because the subsequent analysis on the the main branch is not treated as the MAIN_BRANCH for calculating differences with previous versions, and that we have to delete the analysis history in order to fix the problem. There needs to be a manual process to detect this error has happened and needs time and effort to resolve.

This functionality works elsewhere in sonarqube, so we know it is possible, and it causes us risk of incorrect analysis and manual effort so I think this should be considered a bug.

  • what have you tried so far to achieve this

Setting the project id in sonarqube manually after project creation does not correct the problem.

Setting sonar.gitlab.project_id=${CI_PROJECT_ID} in the CI pipeline does not correct the problem.

Limited options to try as maven plugin and/or gitlab do a lot automatically.

1 Like

Hello @AnkitSharmaETX,

If you add your project into SonarQube via the first analysis, your main branch will not be detected automatically and it remains master.

Indeed, it works if you import your project on SonarQube UI from the DevOps platform that you configured. SonarQube will automatically create the project with the main branch name you have on the DevOps platform.

1 Like

Hello @AnkitSharmaETX ,

When creating a SonarQube project “on the fly” as an outcome of an analysis, the first branch analysed on a project will be considered to be the main branch (if no sonar.pullrequest.branch | sonar.branch.name) provided.

I gues that in your case, you don’t start in your CI by analyzing the main branch? Also, could you please share the arguments your are passing to the scanner?