Integrations

Review merge requests with MegaLens on GitLab

MegaLens checks your code before you merge it. The review runs in GitLab CI on your own runner, the machine that runs your pipeline jobs.

What appears on a merge request

A push to an eligible merge request starts a Standard review. When GitLab provides a line position, MegaLens posts strong findings as comments next to the changed lines. The other findings go in one collapsible summary note, which MegaLens updates when you run the same review tier again.

Open Copy fix prompt under a finding to copy a prompt into your coding tool. For a Deep review, open the pipeline and click the play button for megalens-review-deep. A project member with permission to run that job has to start it.

Set it up in five steps

You need GitLab 18.1 or later and a trusted runner that runs jobs in containers. The runner must allow Bubblewrap to isolate the review process. A shell executor ignores the container image. On GitLab.com, Project Access Tokens require Premium or Ultimate. They are available on all tiers if you manage your own GitLab instance or use GitLab Dedicated.

  1. Create a MegaLens token

    Open your MegaLens settings. Choose Create Action token for GitLab CI. The Editor token is for your editor. If your settings have only one token option, choose Generate Token.

    Accept the data flow notice if prompted. Copy the token for step 3. The full token is shown once.

  2. Create a GitLab Project Access Token

    In your GitLab project, open Settings, then Access Tokens. Create a Project Access Token with the Reporter role and the api scope. The review needs permission to read changes and post notes.

    This token also has other Reporter permissions, such as reading source code and creating issues. On your own GitLab instance, a token created by an internal user can also read other projects that have Internal visibility.

  3. Add both CI/CD variables

    In your project, open Settings, then CI/CD, then Variables. Mark both variables Masked and Protected and set these environment scopes.

    MEGALENS_TOKEN
    Use your MegaLens token. Set the environment scope to megalens-review.
    MEGALENS_GITLAB_TOKEN
    Use your GitLab token. Set the environment scope to megalens-*.

    Set Minimum role to use pipeline variables to No one allowed.

  4. Let protected merge requests run the review

    Protect both branches in the merge request. The source branch contains your changes. The target branch is where you plan to merge them. The person who starts the pipeline needs permission to push or merge to the target branch.

    As a Maintainer or Owner, enable Allow merge request pipelines to access protected variables and runnersin your project's CI/CD settings. Keep pipelines from forks disabled in the parent project. MegaLens reviews merge requests between branches in the same project.

  5. Add the pipeline configuration

    Open .gitlab-ci.yml at the top of your repository. Add the template to include and make sure your project file enables pipelines for merge requests.

    GitLab CI configuration
    include:
      - remote: https://raw.githubusercontent.com/megalens/review-action/34c21108a831e9424ed2d48d0ff448407eba8600/templates/megalens-review.yml
        inputs:
          gitlab_api_url: https://gitlab.com/api/v4
          image: registry.gitlab.com/megalens1/megalens-runner@sha256:0b35a5ee8834184e53f6fe63cf46d8c38708a5a015bf8e948084ccbf74332921
          runner_url: https://raw.githubusercontent.com/megalens/review-action/34c21108a831e9424ed2d48d0ff448407eba8600/dist/gitlab.mjs
          runner_sha256: '60c3c0ddd4afbf4a407f933deb0ac07d29cf976c68ed5e61385906b7163b0693'
    
    workflow:
      rules:
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
        - if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
          when: never
        - if: '$CI_COMMIT_BRANCH'

    This example is for GitLab.com. If your file already has include or workflow entries, add to those entries. For another GitLab instance, set gitlab_api_url to its HTTPS API address ending in /api/v4.

What it costs

A Standard review uses 1 credit and a Deep review uses 8. Your first 3 Standard reviews are free. If a review fails because of us, it costs 0 credits.

A change that is too large for one review uses 0 credits. See pricing for plans and pay as you go.

What leaves your runner

The runner sends the change, the pieces of nearby code the reviewer reads, and the review instructions over an encrypted connection to MegaLens. MegaLens passes that material to the AI model provider. Your full repository stays on the runner.

MegaLens also receives the project identifier, the IDs of the two commits being compared, and the review tier. The runner uses the GitLab token to read the merge request and post the review through your GitLab instance. The review engine tries to remove recognizable credentials from code it reads.