Sonar Cloud configured for Bitbucket Pipeline. PR branches are not displayed in the list of analyzed branches

Our organization just recently started using SonarCloud. I’ve already successfully configured it for Azure DevOps projects, but having troubles with Bitbucket Cloud Pipelines.

The project repo is Node.js/Typescript app, hosted in Bitbucket. Here’s the part of the pipeline YML definition that I am using (omitting unrelated parts):

...
  steps:
   - step: &react_build
        name: Build, Test and Analyze on SonarCloud
        caches: 
          - node
          - sonar
        size: 2x
        script:
          ...
          - /bin/bash ./deployment_scripts/react_build.sh dev
          - pipe: sonarsource/sonarcloud-scan:1.2.0
            variables:
              SONAR_TOKEN: ${SONAR_TOKEN}
              EXTRA_ARGS: '-Dsonar.organization="xxx" -Dsonar.projectKey="xxx_yyyapp" -Dsonar.sources=src -Dsonar.tests=src -Dsonar.test.inclusions="**/*.test.tsx,**/*.test.ts" -Dsonar.typescript.lcov.reportPaths=coverage/lcov.info'
...
    - step: &check-quality-gate-sonarcloud
        name: Check the Quality Gate on SonarCloud
        script:
          - pipe: sonarsource/sonarcloud-quality-gate:0.1.4

pipelines:
  pull-requests:
    '**':
      - step: *react_build
      - step: *check-quality-gate-sonarcloud

  branches:
    develop:
      - step: *react_build
      - step: *check-quality-gate-sonarcloud

The pipeline executes successfully on both develop and PR branch builds.
Sonar Cloud analysis runs without any errors in logs.
The PR and target branches are correctly identified according to pipeline logs, and variables $BITBUCKET_BRANCH and $BITBUCKET_PR_DESTINATION_BRANCH are passed to SonarCloud.

However, after successful build and quality gate pass, I always see PR branch analysis results displayed for develop branch, and no matter how many PRs I have gone through, I can only see develop branch analysis in SonarCloud. This breaks PR decoration as well, as SC has analysis results for develop branch, and PR branch results are just missing in SC.

To make it more clear, whenever I run PR build pipeline, SC data for develop branch gets overwritten with analysis data from that PR, so I can see new code smells and bugs introduced in PR, but in SC they are displayed for develop.

PR Provider is set to BitbucketCloud in settings, but as you can see in attached screenshot, only develop branch is available, and no dropdown with other PR branches is displayed.

I’ve probably repeated myself way too much here to explain the problem, but any help would be appreciated, as hours of Googling didn’t reveal any solutions.

Hey there.

Is BITBUCKET_PR_ID present when you run PR builds/analysis?

Hi Colin,

I can see that it is passed in logs. I’m trying to find the actual value passed, but in the mean time, here’s a background task ID and pipeline/scan logs stripped of any potentially sensitive information:

Background Task ID: AXivCG-aZPthslBZ54Jj
Pipeline Log: pipelineLog-1949.txt (15.3 KB)

Hey there.

I think that’s a good move, as based on the logs it’s being identified as neither a branch or a pull request analysis (just an analysis of the main branch). The autodetection relies on BITBUCKET_PR_ID not being empty (not saying there’s no potential there’s a bug, but lets investigate this angle first)

Hi Colin,
I’ve added echo commands to the pipeline YAML file, and it seems like the passed ID is correct (or at least, not empty). Here’s the fragment of the logs:

echo "BITBUCKET_PR_ID is $BITBUCKET_PR_ID"
`BITBUCKET_PR_ID is 560`

echo "BITBUCKET_PR_ID is $BITBUCKET_BRANCH"
`BITBUCKET_PR_ID is pipeline-test`

echo "BITBUCKET_PR_ID is $BITBUCKET_PR_DESTINATION_BRANCH"
`BITBUCKET_PR_ID is develop`

I’ve also temporarily added DEBUG=true to sonarsource/sonarcloud-scan:1.2.0 pipe, so got some extra information. Here’s the log file (again, cleaned up source files references and potentially sensitive info):
pipelineLog-1962.txt (141.6 KB)

Hey there.

I can’t seem to reproduce the issue. :confused: I always successfully get the pull request identified and autoconfigured.

INFO: Load branch configuration
INFO: Detected analysis for pull request '1' targeting 'master'
INFO: Auto-configuring pull request 1

I’ll phone a friend on this one.

1 Like

Hey. :slight_smile:

So it’s interesting, between our runs we both have lines like this:

INFO: Bitbucket Cloud Pipelines detected

But your runs are missing log messages like these:

INFO: Found an active CI vendor: 'Bitbucket Pipelines'
INFO: Detected project key 'colinmuellerbbc_sdfdsaf' from 'Bitbucket Cloud Pipelines'
INFO: Detected organization key 'colinmuellerbbc' from 'Bitbucket Cloud Pipelines'

So there’s some detection that’s working and some not.

Let me suggest two things:

  1. Can you check in on these other environment variables as well?

BITBUCKET_REPO_OWNER_UUID
BITBUCKET_REPO_UUID
BITBUCKET_BRANCH
BITBUCKET_COMMIT

  1. Try removing -Dsonar.organization="xxx -Dsonar.projectKey="xxx_yyyapp" from your EXTRA_ARGS configuration. Those analysis parameters should not be needed because the sonarcloud-scan pipe is used, as it’s to find those by itself when the project is bound

Hi Colin!
Thanks for looking into this! I’ll try to search on why those log messages can be missing.

  1. Variables seem to be fine:

echo “BITBUCKET_REPO_OWNER_UUID is $BITBUCKET_REPO_OWNER_UUID”
BITBUCKET_REPO_OWNER_UUID is {93e3831f-79a8-4993-ab91-8faeca480d0e}

echo “BITBUCKET_REPO_UUID is $BITBUCKET_REPO_UUID”
BITBUCKET_REPO_UUID is {56a6cd28-a845-4a0e-9a03-511a81a18026}

echo “BITBUCKET_BRANCH is $BITBUCKET_BRANCH”
BITBUCKET_BRANCH is pipeline-test

echo “BITBUCKET_COMMIT is $BITBUCKET_COMMIT”
BITBUCKET_COMMIT is 4d33dee4066d

  1. If I remove those parameters from extra args, I get error:

15:52:24.619 ERROR: Error during SonarScanner execution
java.lang.IllegalStateException: Unable to load component class org.sonar.scanner.scan.ProjectLock
at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:52)
at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:678)
at org.sonar.core.platform.ComponentContainer.getComponentByType(ComponentContainer.java:273)
at org.sonar.scanner.scan.ProjectScanContainer.doBeforeStart(ProjectScanContainer.java:163)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:121)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:126)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:123)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:58)
at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:52)
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
at com.sun.proxy.$Proxy0.execute(Unknown Source)
at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
at org.sonarsource.scanner.cli.Main.execute(Main.java:112)
at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
at org.sonarsource.scanner.cli.Main.main(Main.java:61)
Caused by: java.lang.IllegalStateException: Unable to load component class org.sonar.api.batch.fs.internal.DefaultInputProject
at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:52)
at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:632)
at org.picocontainer.parameters.BasicComponentParameter$1.resolveInstance(BasicComponentParameter.java:118)
at org.picocontainer.parameters.ComponentParameter$1.resolveInstance(ComponentParameter.java:136)
at org.picocontainer.injectors.SingleMemberInjector.getParameter(SingleMemberInjector.java:78)
at org.picocontainer.injectors.ConstructorInjector$CtorAndAdapters.getParameterArguments(ConstructorInjector.java:309)
at org.picocontainer.injectors.ConstructorInjector$1.run(ConstructorInjector.java:335)
at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)
at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.getComponentInstance(AbstractInjectionFactory.java:56)
at org.picocontainer.behaviors.AbstractBehavior.getComponentInstance(AbstractBehavior.java:64)
at org.picocontainer.behaviors.Stored.getComponentInstance(Stored.java:91)
at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:50)
... 22 more
Caused by: java.lang.IllegalStateException: Unable to load component class org.sonar.api.batch.bootstrap.ProjectReactor
at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:52)
at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:632)
at org.picocontainer.parameters.BasicComponentParameter$1.resolveInstance(BasicComponentParameter.java:118)
at org.picocontainer.parameters.ComponentParameter$1.resolveInstance(ComponentParameter.java:136)
at org.picocontainer.injectors.SingleMemberInjector.getParameter(SingleMemberInjector.java:78)
at org.picocontainer.injectors.SingleMemberInjector.getMemberArguments(SingleMemberInjector.java:61)
at org.picocontainer.injectors.MethodInjector.getMemberArguments(MethodInjector.java:100)
at org.picocontainer.injectors.MethodInjector$2.run(MethodInjector.java:112)
at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
at org.picocontainer.injectors.MethodInjector.decorateComponentInstance(MethodInjector.java:120)
at org.picocontainer.injectors.CompositeInjector.decorateComponentInstance(CompositeInjector.java:58)
at org.picocontainer.injectors.Reinjector.reinject(Reinjector.java:142)
at org.picocontainer.injectors.ProviderAdapter.getComponentInstance(ProviderAdapter.java:96)
at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:50)
... 36 more
Caused by: You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.organization
15:52:24.621 ERROR:
15:52:24.621 ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.
✖ SonarCloud analysis failed.

Hey @kirillito

Thanks for the update – sorry for the delay. Is your repository bound to SonarCloud on the BitBucket side (I am guessing the answer is no).

Hi @Colin ,
Yes, it is bound