Unset sonar.branch.name

Hey,

In TFS On premise, using git, we’ve a PR build that looks at the incoming changes and queues the warranted builds (UI/Backend/Both/None).
The triggered build gets the sonar.branch.name field populated, and no analysis info is attached to the PR.
During the Analysis Preparation we tried setting

sonar.pullrequest.base=(baseBranchRef) sonar.pullrequest.key=(PullRequestId)

with those values being passed in as parameters.

The code analysis run fails with this error:

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

Is there a way to unset or not set the sonar.branch.name ?

1 Like

Hi,

Welcome to the community!

Can you make sure you’re on the latest version of the integration pieces in TFS? If you’re not adding that parameter, then it’s probably being “helpfully” inserted for you - my guess is by an old version of the TFS/ADO integration.

 
HTH,
Ann

Can you confirm a newer version would not do it? tbh, setting the branch name feels like the right behavior when you queue the build from a branch.
But it would be good to be able to unset it, if you queue the build programatically.

Hi,

Well since I’m only guessing at where it’s coming from in the first place… no. :smile:

But
a) it’s worth a shot
b) it’s generally best to be on the latest version of utilities unless you have a concrete reason not to be.

 
:smiley:
Ann

@mavs877 Did you ever find a solution for this?

@ganncamp We’re running into the same issue. Here’s our use case:

We use Azure DevOps for builds and BitBucket for pull requests.

When we open a pull request, BitBucket posts a webhook to Azure DevOps, which kicks off our build, and we set these parameters:

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

The problem is that the Azure DevOps’ SonarQube plugin is automatically setting sonar.branch.name, so then all four of these settings are set, and the analysis fails:

sonar.pullrequest.key=$(comes_from_incoming_webhook)
sonar.pullrequest.base=$(comes_from_incoming_webhook)
sonar.pullrequest.branch=$(Build.SourceBranch)
sonar.branch.name=[automatically set by the Azure DevOps' SonarQube plugin. Can we disable or override this?]

Is there any way to remove sonar.branch.name after the plugin automatically sets it?

Thanks :slightly_smiling_face:

It turns out that the Prepare analysis on SonarQube task exports an environment variable named SONARQUBE_SCANNER_PARAMS with configuration for the SonarQube scanner.

This means that there’s a super hacky way to remove sonar.branch.name: After the Prepare analysis on Sonarqube task, add a PowerShell script that:

  1. Retrieves the SONARQUBE_SCANNER_PARAMS environment variable
  2. Parses the value as JSON
  3. Removes the sonar.branch.name key
  4. Reserializes the configuration as a JSON string
  5. Overrides SONARQUBE_SCANNER_PARAMS with the new value

e.g.

# The Azure DevOps' SonarQube plugin automatically adds `sonar.branch.name` for us, but we don't want this to be set, since we're analyzing a pull request. See https://community.sonarsource.com/t/unset-sonar-branch-name/24169/5

$sonarQubeScannerParams = $($Env:SONARQUBE_SCANNER_PARAMS | ConvertFrom-Json)

$sonarQubeScannerParams.PSObject.Properties.Remove('sonar.branch.name')

$Env:SONARQUBE_SCANNER_PARAMS = $($sonarQubeScannerParams | ConvertTo-Json)

This works, but it would be nice if the Azure DevOps’ SonarQube task had a checkbox controlling whether sonar.branch.name (or the various sonar.pullrequest.* parameters) were automatically configured :slightly_smiling_face:

Wait, that still didn’t work. Hm. Will take another look at this tomorrow, God willing :slightly_smiling_face:

Hi,

This thread had lain dormant for 2.5+ years before you resurrected it. A lot can change in that time. If you’re still experiencing similar problems, it’s worth creating a brand new thread that includes all the details from your situation.

 
Ann