▶ 听文章
■ 停止
语速
0.75x 1.0x 1.25x 1.5x 2.0x
大模型会磨洋工,需要另一个 agent 或 AI 来监督。
最近在项目中集成了 OpenCode 的 AI 代码审查功能,使用智谱 AI 的 GLM-5 模型自动 review PR 代码。本文记录完整的集成过程和踩过的坑。
背景 项目需要一个自动化的代码审查机制:
当 PR 创建或更新时,自动触发 AI 审查 AI 只提供 review 建议,不修改代码 将审查结果以 comment 形式添加到 PR 中 技术选型 OpenCode OpenCode 是一个开源的 AI coding agent,支持:
多种 LLM 提供商(OpenAI、Anthropic、智谱等) GitHub/GitLab 集成 在 CI 环境中运行 GLM-5 Coding Plan 选择智谱 AI 的 GLM Coding Plan 原因:
专为 AI 编程优化,支持 OpenCode 价格实惠($3/月起) 国内访问稳定 TL;DR 如果只是想快速引入 code review,不需要处理权限、凭据等配置,可以直接使用封装好的 action。这是最快引入 code review 的方法,review 结果是中文:
1
2
3
4
5
6
7
8
- name : Run OpenCode review
uses : Svtter/opencode-actions/review@v1
with :
github-token : ${{ secrets.GITHUB_TOKEN }}
# only one is enough.
zhipu-api-key : ${{ secrets.ZHIPU_API_KEY }}
opencode-go-api-key : ${{ secrets.OPENCODE_GO_API_KEY }}
只需要配置对应的 secret 即可使用。
配置详解 1. 确定模型格式 OpenCode 的模型格式为 provider_id/model_id。对于智谱 GLM-5:
配置项 值 环境变量 ZHIPU_API_KEYModel 格式 zhipuai-coding-plan/glm-5
2. 创建 Workflow 文件 创建 .github/workflows/opencode-review.yml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
name : OpenCode PR Review
on :
pull_request :
types : [ opened, synchronize, reopened, ready_for_review]
jobs :
review :
name : AI Code Review
# 仅在 PR 准备就绪时运行(跳过 draft PR)
if : github.event.pull_request.draft == false
runs-on : self-hosted
permissions :
id-token : write
contents : read
pull-requests : write
issues : write
steps :
- name : Checkout repository
uses : actions/checkout@v6
with :
persist-credentials : true
token : ${{ secrets.GITHUB_TOKEN }}
- name : Run OpenCode Review
uses : anomalyco/opencode/github@latest
env :
ZHIPU_API_KEY : ${{ secrets.ZHIPU_API_KEY }}
GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
with :
model : zhipuai-coding-plan/glm-5
use_github_token : true
prompt : |
Review this pull request (read-only mode, DO NOT modify any code):
Please check:
- Code quality issues
- Potential bugs or logic errors
- Code style consistency
- Security concerns
- Performance issues
- Suggest improvements
Please respond in Chinese. DO NOT modify any code, only provide review comments.
3. 配置 GitHub Secrets 在仓库 Settings → Secrets and variables → Actions 中添加:
踩坑记录 坑 1: 权限不足 (403 Forbidden) 错误信息 :
1
Resource not accessible by integration - https://docs.github.com/rest/issues/comments#create-an-issue-comment
原因 :permissions 配置为 read,无法写入 PR comment。
解决 :将权限改为 write:
1
2
3
permissions :
pull-requests : write # 不是 read
issues : write # 不是 read
坑 2: Git 凭据问题 错误信息 :
1
fatal: could not read Username for 'https://github.com': No such device or address
原因 :checkout 时 persist-credentials: false 导致后续 git 操作无法认证。
解决 :
1
2
3
4
- uses : actions/checkout@v6
with :
persist-credentials : true # 不是 false
token : ${{ secrets.GITHUB_TOKEN }}
坑 3: Draft PR 不应触发审查 Draft PR 还在开发中,不需要 AI 审查,浪费资源。
解决 :添加条件判断:
1
2
3
jobs :
review :
if : github.event.pull_request.draft == false
工作流程 1
2
3
4
5
6
7
8
9
10
11
12
13
PR opened/updated
↓
检查是否为 Draft PR
↓ (非 Draft)
GitHub Actions 触发
↓
OpenCode 下载并分析 diff
↓
GLM-5 生成审查意见
↓
自动添加 PR comment
↓
开发者查看建议,手动决定是否采纳
效果展示 配置完成后,每次 PR 更新都会自动触发 AI 审查,审查结果会以 comment 形式出现在 PR 页面。
审查内容包括:
代码质量问题 潜在 bug 风格一致性 安全隐患 性能问题 改进建议 成本估算 使用 GLM Coding Plan:
基础套餐 $3/月 中等项目每月约 10-20 次 PR,成本可控 总结 通过 OpenCode + GLM-5 实现自动代码审查:
配置简单 :一个 workflow 文件即可成本低 :GLM Coding Plan 价格友好效果好 :GLM-5 在编程任务上表现优秀安全 :只读模式,不会修改代码参考链接 相关文章