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