Run code analysis task not using the full branch name

Hello,

Not sure if this is a sonarcloud or a vsts issue but I’ve noticed that the “Run code analysis” task is not using the full branch name anymore.

Take this example: the branch name is story/1234-NameOfTheStory. In sonarcloud the branch is reported as 1234-NameOfTheStory. Somehow it picks the string after the /.

2018-07-19T10:02:07.2138351Z INFO: Branch name: 1234-NameOfTheStory, type: short living

To make matters even more confusing, everything works perfectly fine If I run the definition manually. I can only see this type of behaviour only when the build runs as a result of a trigger being fired.

Kind regards,
Daniel

I noticed a similar thing with ours, I think the forward slashes cause problems.

My workaround was to just rename the branches for when passed to SonarCloud, for example in the advanced settings box of the prepare analysis task you could put:

sonar.branch.name=story-$(Build.SourceBranchName)

This will name all your branches in SonarCloud as story-1234-NameOfTheStory and you can set the appropriate regex from there.

Daniel,

The SonarQube Scanner for TFS/VSTS extension was updated recently (v4.3.2) to use the Build.SourceBranch variable instead of Build.SourceBranchName, the former of which preserves the full branch name while the latter only uses the last path segment in the ref.

Could it be possible that, perhaps by chance, when the builds have run because of a trigger being fired, that build agent is using an older (4.3.1 or earlier) version of the extension? Check your build logs.

You can also set in your prepare analysis task…

sonar.branch.name=${Build.SourceBranch}

But I would check what version of the extension is being used (note that is different than the version of SonarQube Scanner for MSBuild).

Colin

Thx a lot for your quick answers. Will give it a shot and come back with the results.

Cheers,
Daniel

Adding sonar.branch.name=${Build.SourceBranch} did not do the trick. The analysis results went in the master branch. I’ll try the Peter’s solution as a workaround.

@Colin - for some reason the agent is using 4.3.1. Not sure I can update it as it’s managed by Microsoft.

2018-07-19T16:49:46.8286754Z SonarScanner for MSBuild 4.3.1
2018-07-19T16:49:46.8287981Z Using the .NET Framework version of the Scanner for MSBuild

Cheers,
Daniel

Tried Peter’s solution and this is what I found in the log file :frowning:

2018-07-19T20:24:45.0833539Z INFO: Branch name: story-, type: long living

For some reason ${Build.SourceBranch} is not evaluated correctly.

Regards,
Daniel

Finally nailed it. It turns out that vsts does not like pascal case. It works just fine when doing this: sonar.branch.name=story/$(build.sourcebranchname).

Thank you all for being so helpful.

Cheers,
Daniel

Daniel, are you on TFS or VSTS? Using the SonarCloud extension or the SonarQube extension?

For further reference, the bug you are talking about was fixed in the latest version of both extensions (see VSTS-165).