PR scanning & PR decoration with GitHub and Azure Pipelines

Dear Sonar community,

let me provide some more context next to the template being filled out underneath. Those are the acceptance criteria for us:

  • Run Quality Gate check on every PR and block PR from merging if Quality Gate is violated
  • Add test coverage result to SonarCloud and use test coverage within the Quality Gate

We are currently using manual configuration of the analysis:

  • Easy for the developers to configure exclusions, test directories etc. in the code
  • Ingesting code coverage results while pipeline is running to use the results as Quality Gate
  • No built-in support for Scala which is most of our stack - example is TypeScript though

We have been trying to add parameters such as:

sonar.pullrequest.base=master
sonar.pullrequest.branch=feature/my-new-feature
sonar.pullrequest.key=5
sonar.pullrequest.provider=GitHub
sonar.pullrequest.github.repository=my-company/my-repo

but it resulted in an error message:
A pull request analysis cannot have the branch analysis parameter ‘sonar-branch-name’

  • GitHub Enterprise Cloud as source control
  • Azure Pipelines for CI automation with the Sonar plugin available on the market place
  • Configuration of the pipeline in the project
          - template: Templates/SonarCloud-QualityScanning.yml@SonarCloud-QualityScanning
            parameters:
              sonarCloudProjectName: $(serviceName)
              sonarCloudProjectKey: ******_$(serviceName)
              sonarCloudPrepare: true
              sonarPreSteps: true
              sonarCloudAnalyze: true
              sonarCloudPublish: true
              sonarCloudSourceDir: ${{ parameters.sonarCloudSourceDir }}
              sonarCloudExtraProperties: |
                sonar.sourceEncoding=UTF-8
                sonar.exclusions=dist/**,node_modules/**,testui/**,flow-typed/**,coverage/**,**/services/**,**/locales/**,**/mock/**
                sonar.coverage.exclusions=**/__tests__/**,src/utils/tracking/*,src/setupTests.ts
                sonar.javascript.lcov.reportPaths=./coverage/lcov.info
                sonar.cpd.exclusions=**/__tests__/**

Code inside the template:

  - ${{ if eq(parameters.sonarCloudPrepare, true) }}:
      - task: SonarCloudPrepare@1
        displayName: 'SonarCloud: Prepare analysis configuration'
        inputs:
            SonarCloud: 'sonarcloud'
            organization: ${{ parameters.sonarCloudOrganization }}
            scannerMode: 'CLI'
            ${{ if eq(parameters.sonarCloudEnableConfigFile, true) }}:
              configMode: 'file'
              configFile: $(parameters.sonarCloudConfigFile)
            ${{ if ne(parameters.sonarCloudExtraProperties, '') }}:
                extraProperties: |
                  ${{ parameters.sonarCloudExtraProperties }}
            ${{ if eq(parameters.sonarCloudEnableConfigFile, false) }}:
                cliProjectKey: ${{ parameters.sonarCloudProjectKey }}
                cliProjectName: ${{ parameters.sonarCloudProjectName }}
                cliProjectVersion: ${{ parameters.sonarCloudProjectVersion }}
                cliSources: ${{ parameters.sonarCloudSourceDir }}
                configMode: 'manual'

  - ${{ if eq(parameters.sonarCloudAnalyze, true) }}:
      - task: SonarCloudAnalyze@1
        displayName: 'SonarCloud: Run analysis'

  - ${{ if eq(parameters.sonarCloudPublish, true) }}:
      - task: SonarCloudPublish@1
        displayName: 'SounarCloud: Publish results'
        inputs:
          pollingTimeoutSec: '300'
  • TypeScript Java Script
  • SonarCloud is not public
  • Two problems observed:
  1. Pull Requests are not scanned at all
  2. As a consequence we we also do not PR decoration
  • Create PR on a project
  • Using automated analysis

Hi @andre.ostermeier

Those parameter are set automatically if we detect that this is a PullRequest, so you shouldn’t need to set them on your own.

For the sonar.branch.name parameter being set, can you please check, with debug enabled, if the value of System.PullRequest.PullRequestId variable on Azure Pipeline is set and has a value ?

Thank you.
Mickaël

Hi,

you gave us the right hint.

The parameter was indeed empty and we added those lines on top of the Azure pipeline and then the parameter had the PR Id.

pr:
branches:
include:
- main

Thanks for you help,
Andre

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.