Introduction
OtterWise works with GitLab.com. You sign in with GitLab, enable your projects, and upload coverage from your GitLab CI/CD pipeline with the bash uploader.
OtterWise then posts coverage on your merge requests and sets commit statuses. Self-managed GitLab is not supported yet.
Prerequisites
- An OtterWise account that you created with Sign in with GitLab
- A GitLab project that you enabled in OtterWise
- A test suite that can generate coverage reports (see the supported formats)
Important
To enable a project, you must be a Maintainer or Owner of the project. OtterWise creates a project webhook, and GitLab allows this only for these roles.
Setting Up the Integration
Step 1: Add the OtterWise Token as a CI/CD Variable
- Open your project in GitLab
- Go to Settings > CI/CD > Variables
- Click Add variable
- Key:
OTTERWISE_TOKEN - Value: Your OtterWise token (available in your repository settings in OtterWise)
- Select Mask variable to hide the value in job logs
- Leave Protect variable off, unless your pipelines run on protected branches only. A protected variable is not available to merge request pipelines on other branches.
Step 2: Add the Upload to Your Pipeline
Add the uploader after your tests, in the same job. The uploader reads OTTERWISE_TOKEN from the environment.
It also detects the project, branch, commit and merge request number from the GitLab CI variables.
- bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh)
The job image must have bash, curl, git and jq. If your coverage file is not in a standard location,
add --file path/to/coverage.xml.
Step 3: Set a Git Depth of 2
By default, GitLab clones with a depth of 20. To calculate the diff of a change, OtterWise needs at least 2 commits.
Set GIT_DEPTH to "2". The diff is calculated in your pipeline, so your code is not sent to OtterWise for it.
Complete .gitlab-ci.yml Example
This example is for a PHP project that uses PHPUnit.
stages:
- test
variables:
GIT_DEPTH: "2"
# Run one pipeline per change: a merge request pipeline when a merge
# request is open, and a branch pipeline for all other pushes.
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never
- if: $CI_COMMIT_BRANCH
test:
stage: test
image: php:8.3-cli
before_script:
- apt-get update && apt-get install -y git unzip curl jq
- pecl install pcov && docker-php-ext-enable pcov
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install --prefer-dist --no-progress
script:
- vendor/bin/phpunit --coverage-clover=build/logs/clover.xml
- bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --file build/logs/clover.xml
For other languages, see the language guides, for example Node.js, Python and Go.
Merge Request Pipelines and Branch Pipelines
GitLab has two kinds of pipelines. OtterWise needs both:
- Branch pipelines run when you push to a branch. They give OtterWise the coverage of your default branch, which it uses as the base for comparison.
- Merge request pipelines run for an open merge request. They give OtterWise the coverage of the change, so it can comment on the merge request.
The workflow:rules in the example above start a merge request pipeline when a merge request is open, and a branch pipeline when it is not.
Without these rules, GitLab can run both for the same push. You then upload the same coverage twice.
Monorepo Support
To upload coverage separately for each part of a monorepo, use the --component flag:
- bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --file backend/coverage/clover.xml --component backend - bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --file frontend/coverage/lcov.info --component frontend
For all options, see the Bash Uploader documentation.
Status Checks and "Pipelines must succeed"
When status checks are on, OtterWise sets up to three commit statuses
on the commit: Code Coverage: PR (or Code Coverage: Commit), Code Coverage: Patch
and Code Coverage: Change. GitLab shows each status as a job in an "external" stage of the pipeline for that commit.
A status fails when the coverage is below the limits in your OtterWise repository settings ("Minimum coverage", "Minimum patch coverage" and "Decrease threshold"). In GitLab, a failed external job makes the full pipeline fail, also when all your own jobs passed.
Important
If your project has Settings > Merge requests > Merge checks > Pipelines must succeed turned on, a failed OtterWise status blocks the merge request.
- To make coverage a required check: turn on "Pipelines must succeed" in GitLab, and set the limits in your OtterWise repository settings.
- To show coverage but not block merges: turn off "Use status checks" in your OtterWise repository settings. The merge request comment still shows the coverage. You can also keep the statuses and make them always pass: set "Minimum coverage" and "Minimum patch coverage" to 0, and "Decrease threshold" to 100.
On GitHub, a failed status blocks a pull request only if you add it as a required check in the branch protection rules. GitLab has no setting for each status: "Pipelines must succeed" applies to the full pipeline.
Permissions and Code Access
When you sign in with GitLab, OtterWise asks for the read_user and api scopes.
GitLab has no smaller scope that can set commit statuses, write merge request comments and create project webhooks.
Important
The api scope includes read access to the code of the projects that you can access.
This is different from GitHub, where OtterWise does not ask for access to private code.
The "no code access" statements on our website apply to GitHub only.
OtterWise uses the scope for these tasks:
- To list your groups, projects and your access level to them
- To read merge requests, and to write the coverage comment and the commit statuses
- To create one webhook in each project that you enable
- To read a file or a diff from GitLab when you open it in the code viewer, or when a coverage page or API request needs the changed lines
The coverage upload does not send your code. The uploader makes the diff in your pipeline and removes the code from it before the upload. To remove the access, revoke OtterWise in your GitLab user settings under Applications.
Differences from GitHub
GitLab has no app or bot account like the OtterWise GitHub App. This causes some differences:
- Comments and statuses use your account. Merge request comments and commit statuses are posted as the user who enabled the project. If that user loses access to the project, sign in with another user and enable the project again.
- Statuses show as an external job. The commit status appears as an "external" job in the pipeline. A failed status makes the pipeline fail. See Status Checks and "Pipelines must succeed".
- No line annotations. Annotations in the diff are available for GitHub only.
- No hosted runners. OtterWise hosted runners (Raft) are available for GitHub only.
- No browser extension. The browser extension is available for GitHub only.
- Groups and subgroups. Each GitLab group or subgroup is one OtterWise organization. Your personal namespace is also one organization.
- Some changes sync late. GitLab.com has no webhooks for project rename, delete, archive, visibility changes or membership changes. OtterWise picks these up when you sign in, or when you click "Refresh data".
- Internal projects are private. OtterWise handles projects with the "internal" visibility as private projects.
- Pull requests are merge requests. OtterWise shows merge requests as pull requests, with the merge request number (IID).
- Code access. The GitLab
apiscope includes read access to your code. See Permissions and Code Access. - Code viewer. The code viewer uses your GitLab sign-in token. See Revoking Code Viewer for the difference from GitHub.
- Self-managed GitLab. Self-managed GitLab is not supported yet. Only GitLab.com works.
Troubleshooting
- Authentication errors: Check that
OTTERWISE_TOKENis set. If the variable is protected, make sure the pipeline runs on a protected branch or tag. - Coverage file not found: Check the path of the coverage file, and pass it with
--file. - Git diff issues: Check that
GIT_DEPTHis"2"or more. - No merge request comment: Check that the pipeline is a merge request pipeline, and that the user who enabled the project still has access.
- Cannot enable the project: You must be a Maintainer or Owner of the project.
- The pipeline fails although all jobs passed: An OtterWise status failed. See Status Checks and "Pipelines must succeed".