GitLab MR analysis: docs, tests, optimization
Example workflows (ready to adapt) – GitLab MR analysis: docs, tests, optimization
This workflow automates GitLab Merge Request (MR) analysis by generating code documentation, unit tests, and optimization suggestions for each changed file. After processing, it posts a consolidated report as an MR comment and notifies your team in Slack. You can adapt this template to fit your project’s needs.
Workflow Diagram
A high-level flowchart of the MR analysis process:
flowchart TD
A[GitLab MR Trigger] --> B[Filter MR Opened]
B --> C{Get MR Data}
C -->|Commits| D[Get MR Commits]
C -->|Changes| E[Get MR Changes]
E --> F[Process Changed Files]
F --> G[Get File Content]
G --> H[Prepare File Data]
H --> I1[Generate Documentation]
H --> I2[Generate Tests]
H --> I3[Generate Optimization]
I1 & I2 & I3 --> J[Combine Analysis]
J --> K[Post MR Comment]
K --> L[Notify Team via Slack]
Nodes & Responsibilities
| Node | Type | Purpose |
|---|---|---|
| GitLab MR Trigger | GitLab Trigger – merge_requests | Listens for new or updated MRs in your GitLab project |
| Filter MR Opened | If | Ensures only opened MRs are processed |
| Get MR Commits | GitLab – getCommits | Fetches the commit history for the MR |
| Get MR Changes | GitLab – getChanges | Retrieves the list of files changed in the MR |
| Process Changed Files | Function – code | Extracts file metadata (path, new vs. modified, diff) and limits processing to a configurable number of files |
| Get File Content | GitLab – getFile | Downloads the raw file content from the source branch |
| Prepare File Data | Function – code | Decodes Base64 content, labels new vs. modified, and attaches MR context (project ID, MR IID) |
| Generate Documentation | DocuWriter.ai – codeDocumentation | Uses AI to generate comprehensive technical docs for the file |
| Generate Tests | DocuWriter.ai – codeTests | Produces unit test stubs or suggestions based on source code |
| Generate Optimization | DocuWriter.ai – codeOptimization | Proposes code improvements (performance, readability, security) |
| Combine Analysis | Function – code | Aggregates docs, tests, and optimization into a single Markdown report |
| Post MR Comment | GitLab – createNote | Posts the combined report as a comment on the MR |
| Notify Team via Slack | Slack – postMessage | Sends a Slack message with MR title, author, branches, and a link to view the report in GitLab |
Key Configuration Snippet
{
"name": "GitLab MR Documentation & Analysis",
"nodes": [
{
"id": "gitlab-mr-trigger",
"type": "n8n-nodes-base.gitlabTrigger",
"parameters": { "events": ["merge_requests"] },
"credentials": { "gitlabApi": { "name": "GitLab API" } }
},
{
"id": "filter-mr-opened",
"type": "n8n-nodes-base.if",
"parameters": {
"conditions": {
"string": [
{
"value1": "={{$json.object_attributes.action}}",
"operation": "equal",
"value2": "open"
}
]
}
}
},
{
"id": "get-mr-commits",
"type": "n8n-nodes-base.gitlab",
"parameters": {
"resource": "mergeRequest",
"operation": "getCommits",
"projectId": "={{$json.project.id}}",
"mergeRequestIid": "={{$json.object_attributes.iid}}"
}
},
{
"id": "prepare-file-data",
"type": "n8n-nodes-base.code",
"parameters": {
"jsCode": "// Decode and prepare source code..."
}
},
{
"id": "generate-documentation",
"type": "n8n-nodes-docuwriter-ai.docuWriter",
"parameters": {
"resource": "codeDocumentation",
"operation": "generate"
},
"credentials": { "docuWriterApi": { "name": "DocuWriter.ai API" } }
},
{
"id": "generate-tests",
"type": "n8n-nodes-docuwriter-ai.docuWriter",
"parameters": {
"resource": "codeTests",
"operation": "generate"
}
},
{
"id": "generate-optimization",
"type": "n8n-nodes-docuwriter-ai.docuWriter",
"parameters": {
"resource": "codeOptimization",
"operation": "optimize"
}
},
{
"id": "combine-analysis",
"type": "n8n-nodes-base.code",
"parameters": {
"jsCode": "// Combine docs, tests & optimizations..."
}
},
{
"id": "post-mr-comment",
"type": "n8n-nodes-base.gitlab",
"parameters": {
"resource": "mergeRequest",
"operation": "createNote"
}
},
{
"id": "notify-team",
"type": "n8n-nodes-base.slack",
"parameters": {
"channel": "#code-reviews",
"text": "🔍 Code analysis complete for GitLab MR"
}
}
]
}
(Adapt node IDs, branches, and credentials to your environment.)
Adapting the Workflow
- 🔧 Adjust Limits: Modify
Process Changed Filesto analyze more or fewer files. - 🌐 Customize Channels: Change Slack channels or add email notifications.
- 📋 Refine AI Prompts: Tweak
additionalInstructionsin DocuWriter.ai nodes to suit your project style. - 🔒 Secure Tokens: Ensure GitLab and DocuWriter.ai credentials are correctly stored in n8n.
Use this robust template to kickstart automated MR reviews in your GitLab projects!
Updated