Skip to content

About

AI-Codereview

Resources

Stars

4 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

使用 DeepSeek 在 git 提交时自动 Code Review

这个仓库里包含一个非常简单的示例,用于在 git 提交时自动调用 DeepSeek 模型,对本次提交的代码 diff 做审查并给出建议。

1. 环境准备

  • Node.js:本示例脚本使用 Node.js(>=16)运行。
  • DeepSeek API Key:假设你已经在 DeepSeek 控制台创建了 API Key。

推荐在项目根目录创建 .env.local 文件(不要提交到版本库,已加入 .gitignore):

快速开始:

  1. 复制 .env.example 为 .env.local:

    cp .env.example .env.local
  2. 编辑 .env.local,填入你的 DeepSeek API Key:

    DEEPSEEK_API_KEY=你的_API_Key

完整配置说明:

# 必需:从 https://platform.deepseek.com/ 获取你的 API Key
DEEPSEEK_API_KEY=你的_API_Key

# 可选:DeepSeek API 基础地址(默认:https://api.deepseek.com)
# DEEPSEEK_API_BASE=https://api.deepseek.com

# 可选:使用的模型名称(默认:deepseek-chat)
# DEEPSEEK_MODEL=deepseek-chat

# 可选:是否在严重问题时阻止提交(1 阻止 / 0 不阻止,默认:1)
# DEEPSEEK_BLOCK_ON_SEVERE=1

# 可选:自定义审查规则文件路径(默认:./rules.json)
# DEEPSEEK_RULES_PATH=./rules.json

脚本启动时会优先读取当前工作目录下的 .env.local,将其中的键值(如 DEEPSEEK_API_KEY)写入 process.env,如果环境变量中已经存在同名键,则以环境变量为准。

2. 安装依赖(可选)

当前示例仅使用 Node.js 内置模块(https 和 child_process),不需要额外 npm 依赖。

如果你希望发布为全局 CLI,可以在本仓库执行:

npm install -g .

之后就可以在任意仓库中直接使用:

deepseek-review

3. 在本仓库启用 pre-commit hook

在本仓库根目录执行一次:

git config core.hooksPath .githooks
chmod +x .githooks/pre-commit
chmod +x scripts/deepseek-review.js

之后,当你在这个仓库里执行:

git add .
git commit -m "test"

就会在真正提交前自动运行:

  • 调用 scripts/deepseek-review.js
  • 读取当前已暂存(staged)的 diff
  • 调用 DeepSeek 模型进行代码审查
  • 在终端输出审查结果(不会强制阻止提交)

4. 集成到你自己的项目

在你自己的项目里,可以把下面几个文件/脚本拷贝过去,并做适当调整:

  • scripts/deepseek-review.js:核心代码审查 CLI
  • .githooks/pre-commit:git hook 脚本(或改为 .git/hooks/pre-commit)
  • package.json 里的 bin 字段和 scripts.review 脚本(按需修改)

然后在你的项目中:

git config core.hooksPath .githooks   # 或者手动把脚本放进 .git/hooks
chmod +x .githooks/pre-commit
chmod +x scripts/deepseek-review.js

5. 行为说明与可定制点

  • 默认不阻止提交:即使发现问题,也只是给出审查报告,不会让 git commit 失败。
  • 如果你希望在发现“严重问题”时阻止提交,可以:
    • 在 scripts/deepseek-review.js 中解析模型返回内容,例如约定模型输出中出现 【严重】 时返回非 0 退出码;
    • 然后在 .githooks/pre-commit 里,当脚本退出码非 0 时 exit 1 终止提交。

你也可以根据实际需要:

  • 限制只在特定分支(比如 main / release)上启用该 hook;
  • 调整 prompt 内容,让 DeepSeek 更关注特定问题(安全 / 性能 / 代码规范等)。

About

AI-Codereview

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages