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