PullRequest Decorator for C# project in gitlab

Hi

I can’t find an example of a working pullrequest decorator with gitlab?
I’ve a c# project which runs the sonarscanner inside a docker container build which is triggered from the gitlab pipelines

All example I found are basically for github and it seems like its workinug there automatically?

Hey there.

If you already have the analysis running properly, as long as your project is bound Merge Request decoration should work automatically.

Do you see Pull Requests being analyzed in the SonarCloud UI?

unfortunately not.
I can see the branch under branches but pull requests is 0

image

Maybe it has to do with the build strategy. I integrated sonarcloud by running the sonarscanner cli in a docker build file (which also builds the whole application)


RUN dotnet sonarscanner begin \
  /k:"$SONAR_PROJECT_KEY" \
  /o:"$SONAR_OGRANIZAION_KEY" \
  /d:sonar.host.url="$SONAR_HOST_URL" \
  /d:sonar.login="$SONAR_TOKEN" \
  /d:sonar.branch.name="$BRANCH_NAME" \
  /d:sonar.coverageReportPaths="/sonarqubecoverage/SonarQube.xml" \
  /d:sonar.dotnet.excludeTestProjects=true \
  /v:"$APP_VERSION"

//THEN Build, Test 

RUN dotnet sonarscanner end /d:sonar.login="$SONAR_TOKEN"

my gitlab script looks like this


image: mcr.microsoft.com/dotnet/sdk:5.0

stages:
    - build
    - notify

variables:
  DOCKER_BUILDKIT: 1
  SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
  GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task

# Building app for dev server
build:
  stage: build
  image: docker:20.10
  services:
  - docker:20.10-dind
  script:
    - cd src
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
        - docker build --build-arg BRANCH_NAME=$CI_COMMIT_BRANCH --cache-from $CI_REGISTRY/myCompany/myImage:latest-dev -t $CI_REGISTRY/myCompany/myImage:latest-dev -t $CI_REGISTRY/myCompany/myImage:$CI_PIPELINE_IID-dev .

Hey there.

This is probably the issue. When you manually configure branch analysis, we flip auto-detection for branches/merge requests off. This means when you run a Merge Request build (I assume you run a build/analysis on each MR), the “wrong” kind of analysis is being performed.

I would suggest removing this, and relying on the presence of these environment variables being available in your scanner environment.

CI_COMMIT_REF_NAME //sonar.branch.name
CI_MERGE_REQUEST_SOURCE_BRANCH_NAME //sonar.pullrequest.branch
CI_MERGE_REQUEST_TARGET_BRANCH_NAME //sonar.pullrequest.base
CI_MERGE_REQUEST_IID //sonar.pullrequest.key
CI_COMMIT_SHA  //sonar.scm.revision

I changed it this way. But now it doesn’t recognize the branch when I push to a branch. I can see that
CI_COMMIT_REF_NAME contains the correct name.

When I set /d:sonar.branch.name="$CI_COMMIT_REF_NAME" manually the branchname is resolved correctly. So I assume that the env vars are set as expected. Do I have to do anything so they get collected?

I added the envs as args, passed them from the gitlab build and set them then as envs

ARG CI_COMMIT_REF_NAME
ARG CI_MERGE_REQUEST_SOURCE_BRANCH_NAME 
ARG CI_MERGE_REQUEST_TARGET_BRANCH_NAME 
ARG CI_MERGE_REQUEST_IID
ARG CI_COMMIT_SHA

ENV CI_COMMIT_REF_NAME=$CI_COMMIT_REF_NAME
ENV CI_MERGE_REQUEST_SOURCE_BRANCH_NAME=$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
ENV CI_MERGE_REQUEST_TARGET_BRANCH_NAME=$CI_MERGE_REQUEST_TARGET_BRANCH_NAME
ENV CI_MERGE_REQUEST_IID=$CI_MERGE_REQUEST_IID
ENV CI_COMMIT_SHA=$CI_COMMIT_SHA

Ah, I did forget one thing. The GITLAB_CI environment variable must be set to true (which kicks off detecting the other variables)

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