Sonarcloud pull request decoration stopped appearing

Summary

Pull request decoration has stopped working on some github projects in my organisation, despite the sonarcloud CI triggering successful analysis and the sonarcloud github app being installed in the organisation.

We made no code changes to trigger this Sonarcloud behaviour change, and pertinent github actions and sonarcloud credentials are identical between projects that work and don’t work with sonarcloud. Suggestions appreciated

DETAILS:

  • CI system: github actions
  • Languages: Predominantly Python, some JS
  • Error observed: Pull request decoration stopped appearing in Github PRs, despite Sonarcloud CI being triggered. We made no Sonarcloud code changes proximate to Sonarcloud errors.
  • CI code
jobs:
  pytest:
    name: pytest
    runs-on: ubuntu-latest

    steps:
      - name: checkout
        uses: actions/checkout@v2

      - name: Run Pytest
        run: make test-ci

      - name: Archive unit test reports
        uses: actions/upload-artifact@v1
        with:
          name: coverage.xml
          path: ./coverage.xml
  SonarCloud-scan:
    name: SonarCloud scan
    needs: pytest
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/download-artifact@v2
        with:
          name: coverage.xml
      - name: SonarCloud scan
        uses: sonarsource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

End result:
successful CI incl. sonarcloud scan

But, despite not changing our code and having the sonarcloud github app installed, we NO LONGER GET:

I suspect that sonarcloud is not identifying our pull requests as pull requests, although I am not sure why that would have suddenly occurred when none of our CI code changed.

Can you suggest how to restore the pull request decoration? Thanks.

Hey there.

I’ve sent you a private message to get some additional details.

Hi @Jonathan_B,

Your analysis is correct, in all the recent builds of your projects, the analysis is detected as a Short branch analysis and not as a Pull Request analysis.

To be detected as pull requests analysis, the action must be triggered on pull_request events, and not on push events (push events are fine for short and long branches, without pull requests opened on it).

You can see a configuration example here, on the README file:

This change happened on our side, not yours, that’s why the decoration stopped appearing without any change on your pipeline.

Do you think changing the trigger to pull request events would be possible?

Question: if I’m changing actions yml to:

name: CI Workflow
on:
  push:
  pull_request:
    types: [ opened, synchronize, reopened ]

jobs:
  pytest:
    {do stuff}
  SonarCloud-scan:
    name: SonarCloud scan
    needs: pytest
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/download-artifact@v2
        with:
          name: coverage.xml
      - name: SonarCloud scan
        uses: sonarsource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

I’m not really sure how the interaction works to make Sonarcloud trigger PR decoration. My understanding of the above file is that we return true on the push condition (as previously) so I’m not sure that the pull-request: condition being simultaneously met matters?

You should remove the branches you open pull requests on from the branches trigger, and keep only the branches without pull requests there.

For example, if you have 3 long branches, master, develop and release-6.5, you can configure them like this:

on:
  # Trigger analysis when pushing in master or pull requests, and when creating
  # a pull request.
  push:
    branches:
      # any branch except PRs
      - master
      - develop
      - release-*
  pull_request:
      types: [opened, synchronize, reopened]

The SonarScanner knows which trigger caused it to start. If it is triggered due to a push event, a branch analysis is configured. If it is due to a pull_request event, then a pull request analysis is configured, which is itself the trigger for the GitHub Summary decoration you miss.

Is it clearer?

1 Like

Thanks, I restored decoration but it is not reporting test coverage, even though the sonarcloud homepage has that information for the same analysis.

Do you know why? What do I need to change to fix this?

Hi,

Glad to hear that the decoration appears now.
About the coverage, if you look at the Summary page of that branch (on the left menu, click on “Pull requests”, then on your PR), is it showing coverage in the bottom left corner?

Thanks Claire,

I found the problem: the restore-sonarcloud-decoration PR only edited yml files and had no changes to tested .py files. Adding python lines restored expected behaviour.

Issue is now resolved!

See below:

No py change:

py change:

[Also I’ll flag that I need to update the test exclusions. Coverage is higher than 48% on the relevant bits of the codebase!]

1 Like

I’m happy to read that everything is working fine now!

Sadly, the solution seems to have created a new issue.

While pull requests are now being identified, the Main Branch on sonarcloud has stopped being updated so the comparison in each PR analysis is increasingly stale and running against a now six-day old record of dev (despite those PRs being merged into dev)

Any suggestions on the root cause here?

Hi @Jonathan_B ,

I think the name of your main branch is missing in the “push” triggers:

Aha, and now I understand why the push was needed. Issue appears resolved. Thanks!

PS. typo in prior post. Need branch: in the push args

name: Maintain SonarCloud
# This Github Action keeps SonarCloud operational.  That means facilitating PR decoration on all PRs, and keeping the
# SonarCloud `Main Branch` up-to-date for PRs to compare against
on:
  # For Sonarcloud PR decoration to work, trigger must be pull_request.
  # This is the most common way to trigger the sonarcloud flow.
  pull_request:
    types: [ opened, synchronize, reopened ]
  # For Sonarcloud to keep track of "main branch" to compare PRs against, we need to trigger SonarCloud on pushes to
  # that branch.
  push:
    branches:
      - develop
{do pytest and sonarcloud}
1 Like

Typo fixed, thanks!