What this workflow does
This workflow checks GitLab merge requests automatically when someone adds a special review comment. It uses OpenAI GPT-4 to read code changes and give review feedback. This saves time and helps catch errors before code gets merged.
The workflow reads changed files, ignores renamed or deleted ones, and looks at the code differences. It splits changes into original and new code parts. Then AI gives a score and review comments. Finally, the workflow posts these comments back to GitLab in the merge request.
Who should use this workflow
This tool helps teams using GitLab who want faster, consistent code reviews. It suits developers or team leads who review lots of merge requests and want to cut review time.
The workflow works best if users can manage API tokens for GitLab and OpenAI and use n8n automation. It fits teams growing in size or merge request volume.
Tools and services used
- GitLab API: Gets merge request file changes and posts review comments.
- OpenAI GPT-4: Reads code diffs and creates review feedback.
- n8n Automation: Runs the workflow with nodes for webhook, HTTP requests, code parsing, and AI calls.
Beginner step-by-step: How to use this workflow in n8n
Import the workflow
- Download the workflow file using the Download button on this page.
- Inside the n8n editor, click the menu and select “Import from File”.
- Choose the downloaded workflow file to import.
Configure the workflow
- Add your GitLab Personal Access Token as a credential in the node that calls GitLab API.
- Add your OpenAI API Key in the OpenAI Chat Model node.
- If project or merge request IDs are hardcoded, update them for your repo or leave dynamic expressions intact.
- Check the Webhook node path matches the GitLab webhook URL configured in your GitLab project settings.
Test and activate
- Test the workflow by posting a comment with the trigger marker (like ‘+0’) on a merge request in GitLab.
- Verify the workflow runs, fetches diff changes, generates AI review, and posts a discussion comment back to GitLab.
- If all works, toggle the workflow ON to run automatically.
To run this in production use, consider self-host n8n for reliability and control.
Workflow inputs, processing, and outputs
Inputs
- GitLab webhook event triggered by a comment on a merge request.
- Comment text, merge request ID, project ID from the webhook payload.
Processing steps
- The Webhook node listens for comments on merge requests.
- An If node filters events for comments matching a review trigger marker (‘+0’).
- The HTTP Request node calls GitLab API to fetch changed files and diffs in the merge request.
- A Split Out node processes each changed file separately.
- An If node named “Skip File Changes” ignores renamed or deleted files and diffs that lack change chunks.
- A Code node called “Parse Last Diff Line” extracts line numbers for correct comment placement in GitLab.
- Another Code node separates the diff into original and new code lines.
- The Basic LLM Chain node sends the code parts and file path to an AI prompt that reviews the changes, scores them, and writes feedback.
- The OpenAI Chat Model node (GPT-4) generates the text review based on the prompt.
- A final HTTP Request node posts the AI review comment as a merge request discussion using GitLab API.
Outputs
- AI-generated code review comments directly posted to the GitLab merge request discussions.
- Faster, consistent feedback without manual review delays.
Edge cases and error handling
If the GitLab API returns “401 Unauthorized”, check the GitLab Personal Access Token is correct in the HTTP Request node headers.
If AI comments do not appear on the merge request, verify the comment body and positions sent to the final HTTP node follow GitLab API rules exactly.
If the workflow does not start on comment events, confirm the webhook in GitLab listens for merge request comment events and uses the correct URL.
Very large merge requests may need added logic to avoid API rate limits or split processing in batches. Current logic handles files one by one.
Customization ideas
- Change the review trigger comment text in the “Need Review” If node from ‘+0’ to something else like ‘please review’.
- Add filters by file extension in the “Skip File Changes” If node to review only specific code types (like .js or .py files).
- Edit the AI prompt in the Basic LLM Chain node to include team coding standards or security rules.
- Swap the OpenAI GPT-4o-mini model to another model for speed or cost control.
- Improve AI comment formatting by modifying the LLM prompt output template to add summaries or links.
Summary of results
✓ Saves hours of manual code review daily.
✓ Ensures consistent, expert-style AI feedback.
✓ Speeds up code deployments by cutting review delays.
✓ Reduces risk of bugs by catching issues early.
✓ Enables review scaling as team grows.
