GitLab MR Code Review
Workflow
1. Read credentials and check token scope
Credentials: INLINECODE0
CODEBLOCK0
Required API scopes:
- -
api — required for posting inline comments - INLINECODE2 — sufficient for analysis only (no comment posting)
Always run token check first to know upfront whether comments can be posted:
CODEBLOCK1
Output includes "can_write": true/false. If false, skip step 6 and inform the user that the token needs the api scope to post comments. Do NOT proceed to analysis and then fail at step 6.
2. Fetch MR metadata and diff
CODEBLOCK2
INLINECODE6 returns a JSON array. Each entry contains new_path, old_path, diff (unified diff text), and boolean flags new_file, deleted_file, renamed_file.
Fallback: if the /diffs endpoint returns HTTP 500 (some self-hosted GitLab instances), the script automatically retries via /changes. No manual intervention needed.
3. Filter files
Use ignore_matcher.py to exclude files before analysis:
CODEBLOCK3
Default ignore patterns (always applied, even without credentials file):
*.min.js, *.min.css, *.lock, package-lock.json, pnpm-lock.yaml, INLINECODE21
Binary extensions (.png, .jar, .class, .map, etc.) are always skipped.
4. Analyze the diff
- - Analyze only modified lines (added/removed in the diff). Do not comment on unchanged context lines.
- If the total diff is large, process file-by-file and aggregate results.
- Read
references/review-guidelines.md for all review rules, severity definitions, and comment format.
Focus areas:
- - Java / Spring Boot — Clean Code, SOLID, transaction boundaries, lazy loading
- MongoDB — query correctness, index coverage, atomicity
- PostgreSQL — SQL correctness, isolation levels, index/schema migrations
- React / TypeScript — hooks correctness, type safety, XSS, stale closures
5. Structure the chat summary
Group findings by severity:
CODEBLOCK4
Decision options: Pass / Needs changes / Reject
- - Pass: no Critical or Major findings
- Needs changes: one or more Major findings, no Critical
- Reject: one or more Critical findings
6. Post inline comments to GitLab
Only execute this step if check-token (step 1) returned "can_write": true.
Write comments to a temp JSON file, then post via post_comments.py.
Never use python -c with inline comment bodies — backticks and special characters break shell escaping.
CODEBLOCK5 java\n// fix\n``"
}
]
EOF
# 2. Post via script
python scripts/post_comments.py <mr_url> /tmp/mr_comments.json
CODEBLOCK6
@@ -375,6 +375,8 @@ ← new file starts at line 375
unchanged line → 375
unchanged line → 376
unchanged line → 377
+ added line → 378 ← use this number
+ added line → 379
CODEBLOCK7
[SEVERITY] <one-line issue>
<2-4 sentence explanation referencing the diff.>
Suggestion:
`<language>
<corrected snippet>
CODEBLOCK8
**Constraints:**
- Do not auto-approve the MR.
- Do not add labels or trigger pipelines.
- Only post comment-type discussions (no approval API calls).
- If a line is not in the diff, the API returns an error — log it and continue with the next comment.
- On HTTP 403 insufficient_scope, the script stops immediately and prints a fix instruction. Do not retry.
## Behavior Rules
- Strict engineering tone. No emotional language. No generic praise.
- Analyze only the modified code in the diff. Do not speculate about code outside the diff.
- Do not log or persist source code content.
- Respect ignore patterns strictly.
- For large diffs: process per file, deduplicate similar findings across files before final output.
## References
- **Review rules, severity table, comment format**: references/review-guidelines.md`
- §2 Java & Spring Boot (Clean Code, transactions, N+1, concurrency)
- §3 MongoDB (queries, indexes, atomicity)
- §4 PostgreSQL (SQL correctness, isolation, migrations)
- §5 React & TypeScript (hooks, type safety, security)
- §6 SOLID & DDD alignment
- §7 Severity classification table
- §8 Inline comment format template
GitLab MR 代码审查
工作流程
1. 读取凭据并检查令牌范围
凭据文件:~/.openclaw/credentials/gitlab.json
json
{
token: glpat-xxx,
host: https://gitlab.com,
ignore_patterns: [.min.js, .lock, forms/*.json]
}
必需的 API 范围:
- - api — 用于发布内联评论
- read_api — 仅用于分析(不发布评论)
始终先运行令牌检查,以提前了解是否可以发布评论:
bash
python scripts/gitlabclient.py check-token url>
输出包含 can_write: true/false。如果为 false,则跳过步骤 6,并告知用户令牌需要 api 范围才能发布评论。不要先进行分析然后在步骤 6 失败。
2. 获取 MR 元数据和差异
bash
python scripts/gitlabclient.py fetch-mr url>
python scripts/gitlabclient.py fetch-diff url>
fetch-diff 返回一个 JSON 数组。每个条目包含 newpath、oldpath、diff(统一差异文本)以及布尔标志 newfile、deletedfile、renamed_file。
回退方案:如果 /diffs 端点返回 HTTP 500(某些自托管 GitLab 实例),脚本会自动通过 /changes 重试。无需手动干预。
3. 过滤文件
在分析前使用 ignore_matcher.py 排除文件:
python
from ignorematcher import filterdiffs
reviewable = filterdiffs(alldiffs) # 合并默认模式 + 凭据中的 ignore_patterns
默认忽略模式(即使没有凭据文件也始终应用):
.min.js、.min.css、.lock、package-lock.json、pnpm-lock.yaml、forms/.json
二进制扩展名(.png、.jar、.class、.map 等)始终跳过。
4. 分析差异
- - 仅分析修改的行(差异中添加/删除的行)。不要对未更改的上下文行发表评论。
- 如果总差异较大,则逐个文件处理并汇总结果。
- 阅读 references/review-guidelines.md 了解所有审查规则、严重性定义和评论格式。
重点关注领域:
- - Java / Spring Boot — 整洁代码、SOLID 原则、事务边界、懒加载
- MongoDB — 查询正确性、索引覆盖、原子性
- PostgreSQL — SQL 正确性、隔离级别、索引/模式迁移
- React / TypeScript — hooks 正确性、类型安全、XSS、过期闭包
5. 组织聊天摘要
按严重性分组结果:
代码审查 — (<源分支> → <目标分支>)
严重
- - UserService.java:42 — 事务包裹 HTTP 调用;网络 I/O 期间持有数据库锁。
主要
- - OrderRepository.java:87 — N+1 问题:在循环中调用 findRolesByUserId。应使用批量查询。
次要
- - PaymentDto.java:15 — 字段名 val 描述性不足。
决策:需要修改
决策选项:通过 / 需要修改 / 拒绝
- - 通过:无严重或主要发现
- 需要修改:一个或多个主要发现,无严重发现
- 拒绝:一个或多个严重发现
6. 向 GitLab 发布内联评论
仅当 check-token(步骤 1)返回 can_write: true 时才执行此步骤。
将评论写入临时 JSON 文件,然后通过 post_comments.py 发布。
切勿使用 python -c 和内联评论正文——反引号和特殊字符会破坏 shell 转义。
bash
1. 将所有发现写入 JSON 文件
cat > /tmp/mr_comments.json << EOF
[
{
file_path: src/main/UserService.java,
line: 42,
body: [严重] 事务包裹 HTTP 调用...\n\n建议:\njava\n// 修复\n
}
]
EOF
2. 通过脚本发布
python scripts/post
comments.py url> /tmp/mr_comments.json
如何从差异块确定正确的行号:
@@ -375,6 +375,8 @@ ← 新文件从第 375 行开始
未更改行 → 375
未更改行 → 376
未更改行 → 377
+ 添加的行 → 378 ← 使用此编号
+ 添加的行 → 379
从 @@ -X,Y +A,B @@ 中的 +A 值开始计算新文件行。
每条评论正文格式(来自 references/review-guidelines.md §8):
[严重性] <一行问题描述>
<2-4 句解释,引用差异内容。>
建议:
<语言>
<修正后的代码片段>
约束条件:
- - 不要自动批准 MR。
- 不要添加标签或触发流水线。
- 仅发布评论类型的讨论(不调用批准 API)。
- 如果某行不在差异中,API 会返回错误——记录错误并继续处理下一条评论。
- 如果遇到 HTTP 403 insufficient_scope,脚本立即停止并打印修复说明。不要重试。
行为规则
- - 严格的工程语气。不使用情绪化语言。不进行泛泛的表扬。
- 仅分析差异中的修改代码。不要推测差异之外的代码。
- 不要记录或持久化源代码内容。
- 严格遵守忽略模式。
- 对于大型差异:逐个文件处理,在最终输出前跨文件去重相似发现。
参考资料
- - 审查规则、严重性表、评论格式:references/review-guidelines.md
- §2 Java 和 Spring Boot(整洁代码、事务、N+1 问题、并发)
- §3 MongoDB(查询、索引、原子性)
- §4 PostgreSQL(SQL 正确性、隔离、迁移)
- §5 React 和 TypeScript(hooks、类型安全、安全性)
- §6 SOLID 和 DDD 对齐
- §7 严重性分类表
- §8 内联评论格式模板