Automated GitLab Code Review with AI - n8n Workflow

Use this powerful n8n workflow to automate sophisticated code reviews in GitLab Merge Requests using OpenAI (GPT-4o mini). Filter review triggers and post detailed AI feedback directly on the specific diff lines.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • DevOps engineers looking to streamline code quality checks.

  • Development teams seeking immediate, scalable feedback on MRs.

  • Users who need advanced, customized n8n templates for Git platforms.

  • Companies implementing AI into their Continuous Integration processes.

Overview

Maintaining high code quality requires rigorous review, but manual processes can be slow and inconsistent. This specialized n8n workflow solves this by integrating a powerful AI model directly into your GitLab development loop. When a specific trigger comment (like '+0') is left on a Merge Request discussion, this n8n automation takes over. It fetches the latest code changes, processes the file diffs, and sends both the original and new code segments to OpenAI.

The AI, acting as a senior expert, analyzes the changes, assigns a score, and provides a formatted, stern review proposal (accept or reject). Finally, the n8n node posts this detailed review back to GitLab as a discussion thread, precisely pinned to the relevant lines of the code change. This n8n workflow stands as an excellent example of using custom n8n templates to achieve complex, real-time developer tooling.

How it Works

This automation begins with the configured n8n trigger:


  1. Trigger Activation: The Webhook n8n trigger listens for incoming POST requests from GitLab (typically configured for merge request comments).

  2. Filter Trigger: The Need Review n8n node acts as a gatekeeper, checking if the comment body matches the defined trigger word (e.g., +0). If matched, the workflow continues.

  3. Fetch Changes: The Get Changes n8n node uses an HTTP request to query the GitLab API for the detailed changes (diffs) within the relevant Merge Request.

  4. Split Processing: The Split Out n8n node separates the overall response into individual items, one for each modified file change.

  5. Filter Diffs: The Skip File Changes n8n node ensures that only files with actual code differences (excluding renamed or deleted files) proceed to analysis.

  6. Coordinate Calculation: The Parse Last Diff Line n8n node uses custom JavaScript to determine the precise line numbers (lastNewLine, lastOldLine) needed by GitLab to anchor the discussion comment correctly.

  7. Code Preparation: The subsequent Code n8n node parses the raw Git diff, structuring it into originalCode and newCode payloads suitable for the LLM.

  8. AI Analysis: The Basic LLM Chain utilizes the configured OpenAI Chat Model (GPT-4o mini). It sends the file path and separated code segments, prompting the AI to perform a senior-level code review, providing a score and an Accept/Reject decision.

  9. Post Review: The final Post Discussions n8n node sends an HTTP POST request back to the GitLab API. It uses the LLM output and the calculated line coordinates to create a new discussion comment, completing the automated review loop.

Installation Guide

To deploy this powerful n8n workflow, follow these steps:


  1. Import: Copy the provided JSON and import it directly into your n8n instance.

  2. Credentials Setup:

OpenAI: Configure your OpenAI API Key credentials within the OpenAI Chat Model n8n node.
GitLab Tokens: Locate the two httpRequest n8n nodes (Get Changes and Post Discussions). Replace the placeholder GitLab URL with your actual instance URL and configure the PRIVATE-TOKEN header using a valid GitLab Access Token (ensure it has merge request write permissions).

  1. Webhook Configuration:

In the Webhook n8n trigger node, copy the URL provided (it will be unique to your instance).
Navigate to your GitLab project settings, set up a webhook, and paste the n8n trigger URL. Configure the webhook to trigger on 'Comment' or 'Discussion' events.

  1. Customization: Review the Need Review n8n node and the sticky notes to customize the trigger word (+0 in this template) and refine the AI's system prompt in the Basic LLM Chain n8n node as necessary.

Node Details

Webhook (n8n trigger): Serves as the starting point, listening for incoming events from GitLab when a discussion note is created or updated.
Need Review (If n8n node): Essential logic gate. Checks the incoming note content to see if it matches the specific review command (+0), ensuring the n8n workflow is only executed when requested.
Get Changes (HTTP Request n8n node): Retrieves the specific diff information for the GitLab Merge Request using the project and MR IDs passed by the initial trigger.
Split Out (n8n node): Transforms the API response to handle each file change individually, enabling sequential processing by the AI.
Parse Last Diff Line & Code (Code n8n nodes): These nodes contain custom JavaScript logic to meticulously parse the complex Git diff format. They extract the exact originalCode and newCode segments and calculate the positional coordinates required for GitLab to place the AI comment correctly.
Basic LLM Chain (n8n node): Defines the large language model prompt, instructing the AI to act as a rigorous code reviewer, demanding an Accept/Reject decision and a score.
OpenAI Chat Model (n8n node): Connects the chain to the GPT-4o mini model using established OpenAI credentials for high-speed, intelligent analysis.
Post Discussions (HTTP Request n8n node): The final action. It constructs a complex API payload using all gathered data (LLM output, file paths, line numbers) to post the automated AI review as an official discussion thread in the GitLab Merge Request.

Related n8n Workflows

Free

Nodes: 8 Nodes
Updated: December 26 2025
View all
Created by
assert
assert

Featured*