# Setting up integration at project level

This section explains how to set up various GitLab integration features for a given project.

## Reporting your quality gate status in GitLab for unbound projects <a href="#reporting-quality-gate-status" id="reporting-quality-gate-status"></a>

On SonarQube projects [importing-repos](https://docs.sonarsource.com/sonarqube-server/10.6/devops-platform-integration/gitlab-integration/importing-repos "mention"), SonarQube automatically sets up the report of your quality gate status and analysis metrics directly to your GitLab pull requests. For unbound projects, you must set up the quality gate status report manually as explained below (The integration of SonarQube with GitLab must be properly [global-setup](https://docs.sonarsource.com/sonarqube-server/10.6/devops-platform-integration/gitlab-integration/global-setup "mention")).

To report your quality gate status in GitLab for unbound projects:

1. In the SonarQube UI page of your project, select **Project Settings > General Settings > DevOps Platform Integration**.
2. Set:
   * **Configuration name**: The name of your GitLab instance’s Configuration record set in [global-setup](https://docs.sonarsource.com/sonarqube-server/10.6/devops-platform-integration/gitlab-integration/global-setup "mention") (Ask your system admin.).
   * **Project ID**: Your GitLab project ID (found in GitLab).

## Preventing pull request merges when the quality gate fails <a href="#preventing-pull-request-merge" id="preventing-pull-request-merge"></a>

In GitLab, you can block pull requests from being merged if it is failing the quality gate. To do this:

1. In your GitLab repository, go to *Your project* **> Settings > Merge requests.**
2. In the **Merge Checks** section, select **Pipelines must succeed**.

More information about GitLab’s External status checks can be found in the [GitLab Documentation](https://docs.gitlab.com/ee/user/project/merge_requests/status_checks.html#block-merges-of-merge-requests-unless-all-status-checks-have-passed).

## Reporting vulnerabilities in GitLab <a href="#reporting-vulnerabilities" id="reporting-vulnerabilities"></a>

This feature is available starting in [Developer Edition](https://www.sonarsource.com/plans-and-pricing/developer/) and requires GitLab Ultimate and GitLab CI/CD.

### Report overview <a href="#report-overview" id="report-overview"></a>

SonarQube can provide feedback about security vulnerabilities inside the GitLab interface itself. The security issues found by SonarQube will appear on the **Gitlab > Vulnerability** report page.

Initially, all issues of type Vulnerability marked **Open** on SonarQube are marked as **Needs triage** on GitLab. When you update the status of an issue in SonarQube, it is also updated in GitLab. Updating the status of an issue in Gitlab does not update it in SonarQube.

{% hint style="info" %}
If issues in GitLab appear duplicated after a modification, users should use the **Activity > Still detected** filter.
{% endhint %}

<details>

<summary>Correspondence of statuses</summary>

Because the available statuses on the two systems are not exactly the same, the following logic is used to manage the transitions:

| In SonarQube, a transition to | Results in this in GitLab |
| ----------------------------- | ------------------------- |
| Open                          | Needs triage              |
| Confirmed (deprecated)        | Confirm                   |
| Accepted                      | Dismiss                   |
| Fixed                         | Resolved                  |

</details>

<details>

<summary>Severity mapping</summary>

The following table presents the mapping of the severity levels between SonarQube and GitLab.

| Severity level in SonarQube | Is mapped to, in GitLab |
| --------------------------- | ----------------------- |
| High                        | High                    |
| Medium                      | Medium                  |
| Low                         | Low                     |

</details>

### Setting up the report <a href="#setting-up-the-report" id="setting-up-the-report"></a>

The report is set up through your GitLab CI/CD pipeline. The user starting the analysis in the pipeline must have the Browse permission on your project (see [security](https://docs.sonarsource.com/sonarqube-server/10.6/instance-administration/security "mention")). This user corresponds to the SonarQube account used to generate the analysis token in [adding-analysis-to-gitlab-ci-cd](https://docs.sonarsource.com/sonarqube-server/10.6/devops-platform-integration/gitlab-integration/adding-analysis-to-gitlab-ci-cd "mention").

Proceed as follows:

* Add a vulnerability report stage to your `.gitlab-ci.yml` file, as follows:

<details>

<summary>SonarScanner for Gradle</summary>

```css-79elbk
stages:
    - sonarqube-check
    - vulnerability-report

sonarqube-check:
  stage: sonarqube-check
  image: gradle:8.2.0-jdk17-jammy
  variables:
    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
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: gradle sonar
  allow_failure: true
  only:
    - merge_requests
    - master
    - main
    - develop

vulnerability-report:
  stage: vulnerability-report
  script:
    - 'curl -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/issues/gitlab_sast_export?projectKey=<projectKey>&branch=${CI_COMMIT_BRANCH}&pullRequest=${CI_MERGE_REQUEST_IID}" -o gl-sast-sonar-report.json'  # Replace <projectKey> with your project key

  allow_failure: true
  only:
    - merge_requests
    - master
    - main
    - develop
  artifacts:
    expire_in: 1 day
    reports:
      sast: gl-sast-sonar-report.json
  dependencies:
    - sonarqube-check
```

</details>

<details>

<summary>SonarScanner for Maven</summary>

```css-79elbk
stages:
    - sonarqube-check
    - vulnerability-report

sonarqube-check:
  stage: sonarqube-check
  image: maven:3.9.3-eclipse-temurin-17
  variables:
    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
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: 
    - mvn verify sonar:sonar
  allow_failure: true
  only:
    - merge_requests
    - master
    - main
    - develop

vulnerability-report:
  stage: vulnerability-report
  script:
    - 'curl -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/issues/gitlab_sast_export?projectKey=<projectKey>&branch=${CI_COMMIT_BRANCH}&pullRequest=${CI_MERGE_REQUEST_IID}" -o gl-sast-sonar-report.json' # Replace <projectKey> with your project key
  allow_failure: true
  only:
    - merge_requests
    - master
    - main
    - develop
  artifacts:
    expire_in: 1 day
    reports:
      sast: gl-sast-sonar-report.json
  dependencies:
    - sonarqube-check
```

</details>

<details>

<summary>SonarScanner CLI</summary>

```css-79elbk
stages:
    - sonarqube-check
    - vulnerability-report

sonarqube-check:
  stage: sonarqube-check
  image: 
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  variables:
    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
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: 
    - sonar-scanner
  allow_failure: true
  only:
    - merge_requests
    - master
    - main
    - develop

vulnerability-report:
  stage: vulnerability-report
  script:
    - 'curl -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/issues/gitlab_sast_export?projectKey=<projectKey>&branch=${CI_COMMIT_BRANCH}&pullRequest=${CI_MERGE_REQUEST_IID}" -o gl-sast-sonar-report.json'  # Replace <projectKey> with your project key
  allow_failure: true
  only:
    - merge_requests
    - master
    - main
    - develop
  artifacts:
    expire_in: 1 day
    reports:
      sast: gl-sast-sonar-report.json
  dependencies:
    - sonarqube-check
```

</details>

<details>

<summary>SonarScanner for .NET</summary>

```css-79elbk
stages:
    - sonarqube-check
    - vulnerability-report

sonarqube-check:
  stage: sonarqube-check
  image: mcr.microsoft.com/dotnet/sdk:latest
  variables:
    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
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: 
      - "apt-get update"
      - "apt-get install --yes openjdk-17-jre"
      - "dotnet tool install --global dotnet-sonarscanner"
      - "export PATH=\"$PATH:$HOME/.dotnet/tools\""
      - "dotnet sonarscanner begin /k:\"projectKey" /d:sonar.token=\"$SONAR_TOKEN\" /d:\"sonar.host.url=$SONAR_HOST_URL\" "  # Replace "projectKey" with your project key
      - "dotnet build"
      - "dotnet sonarscanner end /d:sonar.token=\"$SONAR_TOKEN\""
  allow_failure: true
  only:
    - merge_requests
    - master
    - main
    - develop

vulnerability-report:
  stage: vulnerability-report
  script:
    - 'curl -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/issues/gitlab_sast_export?projectKey=<projectKey>&branch=${CI_COMMIT_BRANCH}&pullRequest=${CI_MERGE_REQUEST_IID}" -o gl-sast-sonar-report.json'  # Replace <projectKey> with your project key
  allow_failure: true
  only:
    - merge_requests
    - master
    - main
    - develop
  artifacts:
    expire_in: 1 day
    reports:
      sast: gl-sast-sonar-report.json
  dependencies:
    - sonarqube-check
```

</details>
