Sonarcloud CircleCI and Graldle failed with 'sonar.pullrequest.branch' is mandatory for a pull request analysis

When a pull request is opened, I get below error. I am using circleci and this is a gradle based project.

‘sonar.pullrequest.branch’ is mandatory for a pull request analysis

Through forums I seen some resolutions to set this values in gradle file. But what values needs to set? from where? is there a problem if the key is static? or how to dynamically set or get the key?

Hello @Vinod_plank,

We need the sonar.pullrequest.branch property to know which branch to analyze.

We resolve these properties automatically when your SonarCloud project is linked to your ALM (GitHub, Gitlab, …). Are you using an ALM?

If you are not able to link your project you could use the CIRCLE_BRANCH environment variable that CircleCI exposes when running a job.

Hope that helps,
Tom

Hello @TomVanBraband

Thanks for the reply. Yes, our project is connected with ALM. (Github). Still I get the error.

Additional info: Below is how the pipeline is:

  1. Have included the plugins and lines suggested by sonar in build.gradle file
  2. Included context and keys in the circleci
  3. Added the sonar suggested content to circleci config.yml file

Hello @Vinod_plank,

Sorry for the late reply, can you confirm that you see a Github icon next to your project name on your project overview page? It should look like the following screenshot: image.

If you can see the GitHub icon, could you send me the full output of the CircleCi job? You can do this in a private message if it contains sensitive information.

Thanks,
Tom

Yes, it is linked with Github and I can see the icons.

May I know how would I send you a personal message?

Edit:
Attaching the sonar run log: I removed confidential texts or kept some placeholders.
sonar.txt (6.4 KB)

I also made sure that the branch environment variables are present in preparing env var of circleci

Could you run that again but with the sonar.verbose property set to true? This should enable much more useful output.

Thanks,
Tom

I followed up with the @Vinod_plank in a private thread. The problem turned out to be that the token used to start the analysis did not have sufficient permissions. This is a bug, and it can be tracked here.

Im facing the same issue and it doesn’t seem to be fixed. Here is the output from a pull request CircleCi build

> Task :swagger-contract-tests-webflux:jacocoTestReport
> Task :swagger-contract-tests-webflux:check
> Task :codeCoverageReport
> Task :sonarqube
Failed to check if project 'springfox_springfox' is bound

> Task :sonarqube FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':sonarqube'.
> Parameter 'sonar.pullrequest.branch' is mandatory for a pull request analysis

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Is the solution to set the -D with the $CIRCLE_BRANCH environment variable?

You will need to do something like this in your build.gradle.kts (Assuming your are using kotlin version of gradle file )

val sonarToken = System.getenv("SONAR_TOKEN") 
val circleCiBranch =  System.getenv("CIRCLE_BRANCH")
val prNumber =  System.getenv("CIRCLE_PR_NUMBER")

sonarqube {
    properties {
        property("sonar.projectKey", "papi")
        property("sonar.organization", "whichdigital")
        property("sonar.host.url", "https://sonarcloud.io")
        property("sonar.login", "$sonarToken")
        if(!"master".equals(circleCiBranch)) {
            property("sonar.pullrequest.key", "$prNumber")
            property("sonar.pullrequest.branch", "$circleCiBranch")
            property("sonar.pullrequest.base", "master")
        }
        property("sonar.junit.reportPaths", "${buildDir}/code-coverage")
        property("sonar.jacoco.reportPaths", "${buildDir}/code-coverage/jacoco.xml")
    }
}

Where SONAR_TOKEN you will need to declare in your circleci build, based on your sonar project settings.