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
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.
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.
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?
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:
Retrieves the SONARQUBE_SCANNER_PARAMS environment variable
Parses the value as JSON
Removes the sonar.branch.name key
Reserializes the configuration as a JSON string
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
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.