Unset sonar.branch.name

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: