GitHub - CircleCI - Gradle - Branch + PR Analysis

My organization is currently trialing SonarCloud, and while most things have been working great, we’ve had some trouble around the branch and PR analysis on our private projects.

What we’re seeing is that unless we turn on the “Only build pull requests” settings we have intermittent PR decoration. We are fine with turning that setting on, but this precludes us from having branch analyses (since Circle CI won’t kick off on commit with that setting turned on).
With the setting turned off, it’s often a case of whether or not the PR already exists at build time that decides if PR decoration works.

Ideally, we would like to have both branch and PR analyses run, but we haven’t been able to get them both working reliably at the same time.

Is there something we can tweak to get both branch and PR analysis running at the same time?

Hi Neil, welcome to the community forum!

The scanner reads the environment variables set by CircleCI to either trigger branch or PR analysis. If CircleCI starts a build following a commit on a branch, and there is not yet a pull request open for that branch, then there will not be any environment variables that are related to a pullrequest availablein the build, so we will trigger a regular branch analysis.

When CircleCI starts a build following a commit on a branch for which there is a pull request open, the relevant environment variables are detected and a pullrequest analysis is triggered.

What you could do in your situation is to manually add the branch analysis when you are building a pull request. This could be done by detecting that CircleCI is building a PR commit (the environment variable CIRCLE_PULL_REQUEST is present), in that case you can run the scanner a second time, but this time you supply the parameter -Dsonar.branch.name=${CIRCLE_BRANCH}. Supplying this parameter will override the automatic configuration, and will trigger a regular branch analysis.

You can find more information regarding the CircleCI environment variables here.

Hope this helps,
Tom