Azure devops pull request branch source and target names

I’m setting up Azure DevOps pull request decoration. After reading sonar documentation, I’ve added these properties to my sonar Prepare Analysis Configuration step:

sonar.pullrequest.key=$(System.PullRequest.PullRequestId)
sonar.pullrequest.branch=$(System.PullRequest.SourceBranch)
sonar.pullrequest.base=$(System.PullRequest.TargetBranch)

is this the intended approach? it seems like sonar expects values for branch name here like ‘develop’ or ‘feature/whatever’ but the values of pull request variables, like $(System.PullRequest.SourceBranch) are the full branch path ‘refs/heads/develop’. Sonar shows a warning that leads me to believe I’m not doing something correctly:

Could not find ref ‘refs/heads/develop’ in refs/heads or refs/remotes/origin. You may see unexpected issues and changes. Please make sure to fetch this ref before pull request analysis.

Are the above DevOps build variables I’m using the intended approach?

Thanks!

The branch is actually not important. The important one is sonar.pullrequest.base, and it had the value develop, which looks good like that.

You get the warning because the develop branch is not fetched. The scanner needs this branch fetched, so that it can compute the changed files and changed lines in commits that happened in your local branch relative to develop.

The solution is along the lines of git fetch origin develop before running the scanner.

I’m confused. none of those three azure devops variables will resolve to a branch name like develop. I was describing that each of them resolves to a full branch path like refs/heads/develop and this is what seems to be causing the warning. for example, these are the expanded values of those variables, taken from sonar logs:

sonar.pullrequest.key=144
sonar.pullrequest.branch=refs/heads/sonar_test
sonar.pullrequest.base=refs/heads/develop

Hi Brian,

I’m taking over this thread, and, just to let you know, depending on the location of your code (Currently are supported Azure Git, GitHub and Bitbucket Cloud), those properties will be filled for you at runtime, so there’s no need to override them in the additional properties.

Could you please remove them, and test again with a PR build (one that is triggered by the ‘Enable pull request validation’ trigger in the triggers tab of your build pipeline. if it’s not triggered by this particular setting, pull request decoration will not happen.

Thanks.

hey @mickaelcaro! thanks for jumping in.

I’m using Azure Git.

I removed all the sonar.pullrequest properties and do some more tests

just to confirm, I still need to set sonar.branch.name, correct? if I don’t, all my analysis end up under master in sonar.

maybe it helps to clarify, I have both a master and develop branch, with feature branches always being merged to develop and then releases being merged from develop to master

Branch name is filled automatically as well, if you are not ananlyzing the default branch of your repository.

then I must be doing something wrong. I just ran a build/analysis against my develop and the results are showing under master in sonar.

also, develop is my default branch. not master

Are you seing this kind of tree in SonarCloud ? (Go the Administration tab of your project, then Branches and Pull Requests)

If yes, is the “main branch” master as the picture shown ?

I think that this is not configurable upon first analysis, i’m going to check that.

In the mean time, you can rename the branch by clicking on the wheel under action column.

ok, I’m no longer setting any extra variables and things are looking better. my branch tree in sonar looks good and my pull requests are being annotated by sonar properly! thanks for your help @mickaelcaro!