ChatGPT软件代码评审AI 评分 · 待评分AI 辅助编辑·未发现明显提示词注入风险n8n.io

GitLab MR 自动代码审查与 AI 风险评估

GitLab 合并请求触发 → 提取代码 diff → Claude/GPT 分析风险 → 生成结构化报告 → 邮件与 MR 评论通知

经验丰富步骤流程提示词贴士
给谁
使用 n8n 的 DevOps/工程团队,希望自动化合并请求审查流程
你会得到
MR 创建或更新时自动获取代码变更,由 AI 评估风险等级、识别问题、给出修复建议与测试用例,并通过邮件和 GitLab 评论同步给开发与 QA 团队
你需要
n8n · http-request · code · ai-agent · structured-output · anthropic · gmail

硬约束

需要可访问的 n8n 实例(自托管或 Cloud)、GitLab API 凭证(gitlabApi)、Anthropic API 密钥(anthropicApi)或 OpenAI API 密钥、Gmail OAuth2 凭证(gmailOAuth2)。模板硬编码了示例项目与占位邮箱,导入后必须替换为实际项目路径、仓库名称、通知邮箱列表及有效 API 令牌。

他们做了什么

该 n8n 模板由 Quantana 开发并发布至 n8n 官方模板库。工作流监听 GitLab 合并请求的创建与更新事件,通过 GitLab API 拉取代码 diff,经 AI Agent 调用 Claude(或 GPT)进行结构化分析,输出风险等级、问题列表、修复建议、代码片段、diff 对比表与 QA 测试用例,最后将报告以 HTML 邮件发送给开发/QA 团队,并以评论形式回写到 GitLab MR。

效果如何

模板作者声明该工作流可实现:自动代码审查、在合并前识别安全与合规风险、与现有 GitLab CI/CD 流程集成、改善开发与 QA 协作。实际效果取决于 AI 模型对代码的理解能力与提示词中 HTML 格式要求的遵循程度。

值得知道

1. 模板 workflow.json 中存在多处硬编码占位符(YOUR_TOKEN_HERE、REPLACE_WITH_GMAIL_OAUTH_ID、REPLACE_WITH_GITLAB_CRED_ID、示例邮箱 [email protected] 及示例项目名),导入后必须全部替换,否则无法运行。2. AI Agent 节点实际配置为 claude-3-5-haiku-20241022,与模板标题中的 GPT-4o 存在差异;如需使用 GPT-4o,需手动更换模型节点与参数。3. 邮件节点使用 Gmail OAuth2,需提前完成 Google Cloud 应用授权流程。4. 本站未对该工作流进行实测,复现前建议先用测试 MR 验证完整链路。

步骤

  1. 配置 GitLab 触发器

    在 n8n 中添加 GitLab Trigger 节点,连接 GitLab API 凭证,选择 merge_requests 事件,指定要监控的仓库(owner/repository)。保存后 n8n 将自动生成并注册 webhook。

  2. 提取 MR 代码 diff

    添加 HTTP Request 节点(Extract Diff),配置为调用 GitLab API:GET /api/v4/projects/{project}/merge_requests/{iid}/changes。使用表达式从触发器数据动态获取项目路径与 MR IID,Header 中填入 GitLab API Token。

  3. 校验变更非空

    添加 If 节点(If Some Change),检查 diff 返回的 changes 数组长度是否大于 0。若为空则终止流程,避免对无变更 MR 执行分析。

  4. AI 分析与结构化输出

    配置 AI Agent 节点,连接 Anthropic Chat Model(或替代模型)。节点使用 define 模式,内置完整 tool schema 要求 AI 返回 JSON:RiskLevel、Summary、Recommendations、Issues、URL、DiffTable、TestCases。将 diff 内容与 MR URL 作为 user message 传入。下游连接 Auto-fixing Output Parser 与 Structured Output Parser 确保输出格式合规。

    运行提示词 · 来源原文。n8n AI Agent 节点的 Parameters > Text(define 模式),作为完整 tool-calling 配置与消息内容

    ={
      "model": "claude-3-5-haiku-20241022",
      "max_tokens": 1000,
      "temperature": 0.7,
      "tools": [
        {
          "name": "record_summary",
          "description": "Record a structured summary of a git diff using well-defined JSON.",
          "input_schema": {
            "type": "object",
            "properties": {
              "RiskLevel": {
                "type": "string",
                "description": "Overall risk assessment of the changes: High/Medium/Low."
              },
              "Summary": {
                "type": "string",
                "description": "One-line summary of the git diff analysis"
              },
              "Recommendations": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "Recommendation": {
                      "type": "string",
                      "description": "Specific recommendation in HTML format."
                    },
                    "CodeSnippet": {
                      "type": "string",
                      "description": "Relevant code full snippet formatted in HTML."
                    }
                  },
                  "required": ["Recommendation", "CodeSnippet"]
                }
              },
              "Issues": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "File": {
                      "type": "string",
                      "description": "HTML-formatted name of the file where the issue exists."
                    },
                    "PotentialIssue": {
                      "type": "string",
                      "description": "HTML-formatted description of the issue."
                    },
                    "Severity": {
                      "type": "string",
                      "description": "Severity of the issue: High/Medium/Low in HTML."
                    }
                  },
                  "required": ["File", "PotentialIssue", "Severity"]
                }
              },
              "URL": {
                "type": "string",
                "description": "Link to the repository in HTML."
              },
              "DiffTable": {
                "type": "object",
                "properties": {
                  "FileName": {
                    "type": "string",
                    "description": "HTML-formatted name of the file."
                  },
                  "Changes": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "Line": {
                          "type": "string",
                          "description": "HTML-formatted line number."
                        },
                        "Before": {
                          "type": "string",
                          "description": "Code before the change formatted in HTML."
                        },
                        "After": {
                          "type": "string",
                          "description": "Code after the change formatted in HTML."
                        }
                      },
                      "required": ["Line", "Before", "After"]
                    }
                  }
                }
              },
              "TestCases": {
                "type": "array",
                "description": "List of test cases the QA team must check.",
                "items": {
                  "type": "object",
                  "properties": {
                    "TestFor": {
                      "type": "string",
                      "description": "Description of the functionality or area to test."
                    },
                    "Steps": {
                      "type": "string",
                      "description": "HTML-formatted steps to test the functionality."
                    },
                    "ExpectedOutcome": {
                      "type": "string",
                      "description": "HTML-formatted description of the expected result of the test."
                    }
                  },
                  "required": ["TestFor", "Steps", "ExpectedOutcome"]
                }
              }
            },
            "required": ["RiskLevel", "Summary", "Recommendations", "Issues", "URL", "DiffTable", "TestCases"]
          }
        }
      ],
      "tool_choice": {
        "type": "tool",
        "name": "record_summary"
      },
      "messages": [
        {
          "role": "user",
          "content": [
            {
              "type": "text",
              "text": "Analyze the following git diff, highlight potential issues, provide recommendations with code snippets (using HTML formatting for all values), and include the URL. Ensure proper risk evaluation. Mark issues that may break the build or reveal a security risk as High':"
            },
            {
              "type": "text",
              "text": "{{ $json.changes.map(change => JSON.stringify(change.diff).slice(1, -1).replace(/[\r]/g, '')).join(' ') }}"
            },
            {
              "type": "text",
              "text": "URL: {{ $json.web_url }}"
            },
            {
              "type": "text",
              "text": "Provide a table of test cases for the QA team under the 'TestCases' field."
            }
          ]
        }
      ]
    }
  5. 生成通知名单

    添加 Code 节点(Distribution List Generator),用 JavaScript 根据项目名映射 dev/qa 邮箱列表,合并全局列表与提交者邮箱,去重后输出逗号分隔的字符串。必须将示例中的占位项目名与邮箱替换为实际团队配置。

  6. 发送邮件与 MR 评论

    并行配置两个节点:1)Gmail 节点(Send to DL),接收 Distribution List Generator 的 emails 输出,发送 HTML 格式报告邮件,主题包含风险等级与项目名;2)HTTP Request 节点(Comment Back on MR),POST 到 GitLab MR notes API,将 AI 输出渲染为 Markdown 表格评论。两者均需替换凭证与项目路径表达式。

可复制提示词

搭建指导 · 本站建议版本。搭建助手启动词,用于导入模板后的配置核对与测试指引

这一段是提示词,页面不会在这里插入广告。

本模板为 n8n 官方工作流,导入后请按以下顺序检查:
1. 替换 GitLab Trigger 中的 owner/repository 为实际仓库;
2. 在 Extract Diff 与 Comment Back on MR 的 Header 中填入有效 GitLab API Token(非占位符);
3. 配置 Anthropic API 凭证(或改用 OpenAI);
4. 在 Distribution List Generator 中替换项目-邮箱映射为实际团队地址;
5. 配置 Gmail OAuth2 凭证并更新邮件主题变量;
6. 创建测试 MR 验证触发、分析、邮件与评论是否正常工作。

AI Agent 节点使用 define 模式,提示词为完整 JSON 参数(含 tool schema 与 messages),需在 n8n 节点内直接粘贴,不可作为聊天消息发送。

来源

GitLab merge request review & risk analysis with Claude/GPT AI · vishalquantana · n8n.io · 2025-05-12

留言与反馈

试过这个方法吗?分享效果、遇到的问题或改进建议。留言审核后公开,请勿填写敏感信息。

正在加载…

    相关用例