Unset sonar.branch.name in Azure DevOps pipeline

We’re running Azure DevOps Server 2020 Update 1.1 with SonarQube extension version 5.8.0

We have a hook from BitBucket Server that kicks off a pipeline in Azure DevOps. The hook sets variables in the Azure DevOps pipeline to indicate which BitBucket PR we’re building, the source branch, and the destination branch.

In the SonarQube task, we set these variables:

sonar.pullrequest.key=$(PullRequestId)
sonar.pullrequest.base=$(PullRequestBaseBranch)
sonar.pullrequest.branch=$(Build.SourceBranch)

But then we get this error:

##[error]Caused by: A pull request analysis cannot have the branch analysis parameter ‘sonar.branch.name’

The problem is that the SonarQube task always sets sonar.branch.name. Normally, this is desired behaviour, but in our case, we’d like to manually set the SonarQube pull request parameters and have sonar.branch.name not be set.

Is this possible, or is these some way to unset sonar.branch.name?

Thanks! :slightly_smiling_face:

Hi,

Is that you’re analyzing actually a PR, or is it really a branch you want to analyze as a PR?

 
Ann

It’s an actual PR, but the PR is in BitBucket while the build is on Azure DevOps.

Hi,

Thanks for clarifying. I’ve flagged this for more expert attention.

 
Ann

Thanks, Ann :slightly_smiling_face:

@ganncamp BTW, is it possible to do the second thing that you mentioned: Analyze a branch as a PR?

If so, that might also suit our use case :slightly_smiling_face:

Hi,

Sure. A PR is just a branch with a few extra steps…

 
Ann

@ganncamp Thanks, Ann :slightly_smiling_face: I think we’re talking past each other a bit, so I’ll wait for the expert attention on the original question. Thanks again for your replies! :slightly_smiling_face:

Hi @ganncamp, just following up on this :slightly_smiling_face:

Hi,

Does this help?

 
Ann

Hi Ann,

That’s my own post :wink:

It looks like we’re going in circles, so I probably should raise this issue via our official support channels with SonarQube :slightly_smiling_face:

1 Like

Hi @Bosch-Eli-Black

No need to set these variables if you are using the SonarQube Azure DevOps extension, it will take care of this automatically.

For the sonar.branch.name, unfortunately if the default branch of your repo is something else that “master” or “refs/heads/master”, we will not detect it, as we don’t currently have a mechanism to fetch the real one from your BB server.

HTH,
Mickaël

Hi @mickaelcaro,

Thanks for the reply :slightly_smiling_face:

I’d like to explain our situation a bit more:

When we open a pull request in BitBucket Server, we run an Azure DevOps pipeline that builds the branch and commit of the BitBucket pull request.

When we build in Azure DevOps, we’d like to tell SonarQube that we’re building for a pull request, not a branch.

The problem is that the SonarQube Azure DevOps extension automatically configures the build as a branch build, instead of a pull request build, because it doesn’t know that the reason that we kicked off the Azure DevOps pipeline is because a pull request was opened in BitBucket :slightly_smiling_face:

Do you have any suggestions for working around this?

Ideally, we’d like to be able to tell the SonarQube Azure DevOps extension to not automatically set the sonar.branch.name variable.

Thanks :slightly_smiling_face:

Which kind of pipeline are you using ? YAML ? Or “classic editor” ?
When you say “we run an Azure DevOps pipeline”, isn’t it automatically triggered ?

@mickaelcaro No, it’s not automatically triggered: The pull request is created in BitBucket Server, and we invoke the Azure DevOps build pipeline via a POST webhook.

The pipeline is using the classic editor, but we could switch to YAML if that would let us work around this issue :slightly_smiling_face:

Ok, that’s why this is not really working as expected.

You can configure the automatic trigger of a PR buil in the “triggers” section of your classic pipeline, ie :

The pre-requisites to that though is that you have to setup a connection between your BB server and Azure, when creating your pipeline, there are multiple options on “where you code is”, choosing bitbucket there will allow you to setup that connection.

We don’t really support manual configuration for these specific use cases (analyzing a PR) over the extension itself.

Hope that helps,
Miclaël

@mickaelcaro Wow, that screenshot looks like exactly what we’re looking for! It appears that Azure DevOps supports triggers for BitBucket Cloud but not BitBucket Server, though :confused:

(See the note at Build code from on-premises Bitbucket server - Azure Pipelines | Microsoft Learn “PR triggers are not available with on-premises Bitbucket repositories”)

What are the chances of getting an `Automatically configure source branch/PR" option added to the Azure DvOps BitBucket extension? If we had that option and could turn it off, we could set the PR configuration ourselves :slightly_smiling_face:

Ah, truly sorry about this ! I’m afraid if Microsoft doesn’t support it, It will be hard to do it on our side since we are relying on their webhooks/trigger.

I’m afraid you will probably have to use the Sonar analysis itself, without the extension, and detect on the pipeline if you are within a PR or not to fill in the correct properties. Is that something doable for you ?

Mickaël

Ya, let me give that a try. Thanks :slightly_smiling_face:

@mickaelcaro Is there any way to get the path to the SonarQube executable? (e.g. D:\_vstsAgent\fe0vmc18237-SL1\_wk\_tasks\SonarQubePrepare_15bsadfca1-basd2f-4a2a-a403-89b77a063157\5.8.0\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe)