Problem with GitLab integration when analisys code in merge request

ALM used
GitLab

CI system used
GitLab CI

Scanner command used when applicable

variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
    GIT_DEPTH: "0"

sonarcloud-check:
  stage: Code quality + tests
  when: on_success
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script:
    - sonar-scanner
  only:
    - merge_requests
    - main
    - homolog

Languages of the repository
Python

Error observed (wrap logs/code around with triple quotes ``` for proper formatting)
ERROR: Error during SonarScanner execution
ERROR: Could not find a default branch to fall back on.
ERROR:
ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.

Steps to reproduce
I have followed the steps of documentation. The analyses for homolog and main branches works well, but for Merge Request (merge_requests) not.

I’ve been trying everything but I can’t display the analysis summary in the Merge Request of the GitLab.

Has anyone had this problem and can help me?

Hey there.

  • Is your project bound?
  • Does the branch you’re raising your pull request on have a sonar-project.properties file with the expected sonar.projectKey and sonar.organization?

Hi, Colin.

My project is Bound.

My sonar-project.properties is like this:

sonar.projectKey=a************a
sonar.organization=a************k
sonar.host.url=https://sonarcloud.io

I’ve been trying to put all information as possible into the sonnar-scanner command line, but I’m still having the same problem.

When the Merge Requet is approved for homolog or main branhces, the Sonar step is executed successfully, the problem is only in the merge_quests.

script:
    - sonar-scanner -X -Dsonar.branch.name="sonar-integration" -Dsonar.host.url="https://sonarcloud.io" -Dsonar.verbose=true -Dsonar.log.level=INFO -Dsonar.ws.timeout=60 -Dsonar.qualitygate.wait=true -Dsonar.qualitygate.timeout=1800

I solved part of the problem. Now merge_requests is being successfully by SonarCloud.

The other part of the problem is that the analysis summary is not showing up in the Merge Request.

My sonnar-scanner is like this:

script:
    - sonar-scanner -Drevision=$CI_COMMIT_REF_NAME -Dsonar.host.url=${SONAR_HOST_URL} -Dsonar.branch.name=${CI_COMMIT_REF_NAME} -Dsonar.projectName=${CI_PROJECT_NAME} -Dsonar.login=${SONAR_TOKEN} -Dsonar.gitlab.project_id=${CI_PROJECT_NAMESPACE}/$CI_PROJECT_ID 

And my sonar-project.properties

sonar.projectKey=a**********************s
sonar.organization=a**********************k
sonar.host.url=https://sonarcloud.io
sonar.python.version=3.8
sonar.language=py 
sonar.sources=.
sonar.qualitygate.wait=true
sonar.qualitygate.timeout=600
skipTests=true

Is it possible to display the analysis summary in the Merge Request? I think so, but I can’t find how to do it.

Totally solved.

Was needed add more three params:

-Dsonar.pullrequest.key=${CI_MERGE_REQUEST_IID} -Dsonar.pullrequest.base=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME} -Dsonar.pullrequest.branch=${CI_COMMIT_REF_NAME} 

Now, the SonarCloud does the analysis and returns the summary to merge request.

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