Organization default main branch

Is there a way to set an organization default main branch? We have over 30 repos which will each be their own project (unless there’s a good way to include multiple repos per project), and the default branch is not master. I’d like to just auto-create the projects as the builds run, but it seems like I can’t unless a branch called master builds.

Hi @jbeeson and welcome to the community !

There’s currently no organization-wide settings that can be set for projects.

But in the mean time, we provide a web api to rename the default branch of a project, maybe you can create a batch around that.

You can check the doc here : https://sonarcloud.io/web_api/api/project_branches

HTH,
Mickaël

Hi Michael,

Thanks for the advice. I did end up diving into the API after posting this and figured it out. For anyone else that wants to do something similar, here’s the PowerShell I put into my pipelines before the SonarCloud tasks to make sure a project is initialized correctly before attempting to analyze a non-standard main branch:

$organization = ''
$pat = ''

$product = "$(productName)"
$repo = "$(Build.Repository.Name)"
$branch = "$(branchName)"

$token = [System.Text.Encoding]::UTF8.GetBytes($pat + ":")
$base64 = [System.Convert]::ToBase64String($token)
$headers = @{ Authorization = "Basic $base64" }

$baseUri = "https://sonarcloud.io/"
$project = "$product-$repo-$branch"
$calls = @(
    "api/projects/create?name=$project&project=$project&visibility=private&organization=$organization"
    "api/project_branches/rename?name=$branch&project=$project"
)

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
foreach ($call in $calls) {
    $fullUri = $baseUri + $call
    try {
        Invoke-RestMethod -Method Post -UseBasicParsing -Uri $fullUri -Headers $headers
    }
    catch {
        Write-Warning $_
    }
}
1 Like