Skip to content

A workflow of Dify to auto code review merge requests from GitLab.

License

Notifications You must be signed in to change notification settings

zgldh/dify-gitlab-mr-reviewer

Repository files navigation

Dify GitLab MR Reviewer

Dify GitLab MR Reviewer is a tool designed to automate the code review process for GitLab Merge Requests (MRs). It integrates with the Dify workflow to provide intelligent code review suggestions, helping developers identify potential issues and improve code quality.

Features

  • Automated Code Review: Automatically reviews GitLab MRs and provides feedback.
  • Code Review email for push: Send email to the pusher when commits are pushed.
  • Integration with Dify: Utilizes the Dify workflow for advanced code analysis.
  • Docker Trigger Support: Easy deployment using Docker containers to trigger the workflow.
  • Logging and Monitoring: Detailed logging and monitoring to ensure robust operation.

Prerequisites

Before you begin, ensure you have the following installed:

  • GitLab (Version 17 or newer) This reviewer can be applied to free GitLab instance. You need a personal access token to an account with access (Role developer at least) to the repository.
  • Dify The AI workflow platform. You have to get the workflow URL and access token.
  • Docker To proxy the GitLab system hook requests to Dify

Installation

1. Get the GitLab Personal Access Token

Get the GitLab Personal Access Token Steps

Please note the token must have the api scope. This GitLab account will post comments when reviewing MRs. So please remember the GitLab account name.

The token will be used as GITLAB_PRIVATE_TOKEN and the account name will be used as GITLAB_REVIEWER_USERNAME.

2. Import the workflow into Dify

Select the file dify-workflow-zh.yml to import. Or you can import the English version dify-workflow-en.yml.

How to import the workflow

Then adjust your workflow environment variables.

Dify workflow environment variables

Sample:

GITLAB_HOST = https://gitlab.your-company.com
GITLAB_PRIVATE_TOKEN = <The token you retrived from the first step>
GITLAB_REVIEWER_USERNAME = <The GitLab account name you retrived from the first step>

The workflow is default using the Google Gemini 2.0 Flash Exp. You can change to any other LLM model.

3. Note down the URL and Key from Dify workflow

This is for autommatically trigger the workflow.
Get the workflow URL, it will be refered as DIFY_URL in following steps. It looks like:

https://your-dify-host/v1/workflows/run

And the API key from the workflow page, it will be refered as DIFY_API_KEY in follwoing steps: The Dify API Key

You will need the URL and API key to trigger the workflow.

4. Setup the trigger service.

Build the docker image:

cd trigger
docker build -t gitlab-mr-reviewer .

Run the docker container. The host should be accessible from the GitLab server.

docker run --name ai-code-review-trigger -d --restart always -p 4195:4195 -v $(pwd)/logs:/var/log/supervisor/ gitlab-mr-reviewer

Your GitLab server should be able to access this 4195 port. For example http://localhost:4195.

5. Setup a System Hook to GitLab

Go to your GitLab admin area, navigate to System hooks > Add new webhook.

Fill up the URL as http://localhost:4195/hook?dify=<DIFY_URL>

Fill up the Secret token as <DIFY_API_KEY> (it will be masked as ***)

Triggers should be Push events and Merge request events only.

Then click Add webhook to save the settings.

6. Done

That's all settings. You can try to create a new merge request, or push your commits, then let's see what the AI would say!

Troubleshooting

Q: Why I don't receive the code review email?
A: You have to go to your GitLab account settings, the profile page, and enable the Public Email option.

Contributing

Contributions are welcome! Please read the contributing guidelines to get started.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

A workflow of Dify to auto code review merge requests from GitLab.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published