关于 gemini-review
一项使用 Google Gemini CLI 进行代码审查的技能,涵盖交互式、无头模式及托管代码库工作流。可审查分支变更,也可在 CI 或 Pull Request 中自动运行审查。
主要功能
- 01通过 code-review 扩展交互式审查
- 02无头 CLI 审查及 JSON 输出
- 03GitHub 应用与 Action 工作流
- 04GitLab CI 审查配置
- 05通过 GEMINI.md 提供项目上下文
适用场景
- 01检查分支变更中的错误和安全问题
- 02自动审查 Pull Request
- 03在 CI 流水线中运行代码审查
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ alinaqi/ maggy --skill gemini-review安装后在 Claude Code 中输入/gemini-review确认已加载。
详细介绍
Google Gemini 代码审查技能
使用 Google 的 Gemini CLI 和 Gemini 2.5 Pro 进行代码审查——它拥有高达 1M token 的上下文窗口,可一次性分析整个代码仓库。
来源: Gemini CLI | Code Review Extension | Gemini Code Assist | GitHub Action
为什么使用 Gemini 进行代码审查?
| 功能 | 优势 |
|---|---|
| Gemini 2.5 Pro | 代码推理能力处于业界领先水平 |
| 1M token 上下文 | 可容纳整个代码仓库,无需分块 |
| 免费层级 | 使用 Google 账号每天可发送 1,000 次请求 |
| 输出一致 | 格式简洁,结构清晰可预测 |
| 原生支持 GitHub | Gemini Code Assist 应用可自动审查 PR |
基准测试表现
| 基准测试 | 得分 | 说明 |
|---|---|---|
| SWE-Bench Verified | 63.8% | Agentic 编码基准测试 |
| Qodo PR Benchmark | 56.3% | PR 审查质量 |
| LiveCodeBench v5 | 70.4% | 代码生成 |
| WebDev Arena | #1 | Web 开发 |
安装
前置条件
# Check Node.js version (requires 20+)
node --version
# Install Node.js 20 if needed
# macOS
brew install node@20
# Or via nvm
nvm install 20
nvm use 20
安装 Gemini CLI
# Via npm (recommended)
npm install -g @google/gemini-cli
# Via Homebrew (macOS)
brew install gemini-cli
# Or run without installing
npx @google/gemini-cli
# Verify installation
gemini --version
安装 Code Review Extension
# Requires Gemini CLI v0.4.0+
gemini extensions install https://github.com/gemini-cli-extensions/code-review
# Verify extension
gemini extensions list
身份验证
选项 1:Google 账号(推荐)
免费层级:每天 1,000 次请求,每分钟 60 次请求
# Run gemini and follow browser login
gemini
# Select: "Login with Google Account"
# Opens browser for OAuth
这样即可使用 Gemini 2.5 Pro 的完整 1M token 上下文窗口。
选项 2:Gemini API 密钥
免费层级:每天 100 次请求
# Get API key from https://aistudio.google.com/apikey
# Set environment variable
export GEMINI_API_KEY="your-api-key"
# Or add to shell profile
echo 'export GEMINI_API_KEY="your-api-key"' >> ~/.zshrc
# Run Gemini
gemini
选项 3:Vertex AI(企业版)
# For Google Cloud projects
export GOOGLE_API_KEY="your-api-key"
export GOOGLE_GENAI_USE_VERTEXAI=true
export GOOGLE_CLOUD_PROJECT="your-project-id"
gemini
交互式代码审查
使用 Code Review Extension
# Start Gemini CLI
gemini
# Run code review on current branch
/code-review
该扩展会分析:
- 当前分支上的代码变更
- 识别质量问题
- 提出修复建议
手动审查提示词
# In interactive mode
gemini
# Then ask:
> Review the changes in this branch for bugs and security issues
> Analyze src/api/users.ts for potential vulnerabilities
> What are the code quality issues in the last 3 commits?
无头模式(自动化)
基本用法
# Simple prompt execution
gemini -p "Review the code changes for bugs and security issues"
# With JSON output (for parsing)
gemini -p "Review the changes" --output-format json
# Stream JSON events (real-time)
gemini -p "Review and fix issues" --output-format stream-json
# Specify model
gemini -m gemini-2.5-pro -p "Deep code review of this PR"
完整 CI/CD 示例
# Get diff and review
git diff origin/main...HEAD > diff.txt
gemini -p "Review this code diff for:
1. Security vulnerabilities
2. Performance issues
3. Code quality problems
4. Missing error handling
Diff:
$(cat diff.txt)
" --output-format json > review.json
会话跟踪
# Track token usage and costs
gemini -p "Review changes" --session-summary metrics.json
# View metrics
cat metrics.json
GitHub 集成
选项 1:Gemini Code Assist 应用(最简单)
从 GitHub Marketplace 安装:
- 前往 GitHub Marketplace → Gemini Code Assist
- 点击“Install”并选择代码仓库
- 打开 PR 时会自动进行审查
PR 评论中的命令:
/gemini review # Request code review
/gemini summary # Get PR summary
/gemini help # Show available commands
配额:
- 免费:每天 33 个 PR
- 企业版:每天 100+ 个 PR
选项 2:GitHub Action
# .github/workflows/gemini-review.yml
name: Gemini Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install Gemini CLI
run: npm install -g @google/gemini-cli
- name: Run Review
env:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
run: |
# Get diff
git diff origin/${{ github.base_ref }}...HEAD > diff.txt
# Run Gemini review
gemini -p "Review this pull request diff for bugs, security issues, and code quality problems. Be specific about file names and line numbers.
$(cat diff.txt)" > review.md
- name: Post Review Comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const review = fs.readFileSync('review.md', 'utf8');
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `## 🤖 Gemini Code Review\n\n${review}`
});
选项 3:官方 GitHub Action
# .github/workflows/gemini-review.yml
name: Gemini Code Review
on:
pull_request:
types: [opened, synchronize]
issue_comment:
types: [created]
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
issues: write
steps:
- uses: actions/checkout@v4
- name: Run Gemini CLI
uses: google-github-actions/run-gemini-cli@v1
with:
gemini_api_key: ${{ secrets.GEMINI_API_KEY }}
prompt: "Review this pull request for code quality, security issues, and potential bugs."
评论中的按需命令:
@gemini-cli /review
@gemini-cli explain this code change
@gemini-cli write unit tests for this component
GitLab CI/CD
# .gitlab-ci.yml
gemini-review:
image: node:20
stage: review
script:
- npm install -g @google/gemini-cli
- |
gemini -p "Review the merge request changes for bugs, security issues, and code quality" > review.md
- cat review.md
artifacts:
paths:
- review.md
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
variables:
GEMINI_API_KEY: $GEMINI_API_KEY
配置
全局配置
# ~/.gemini/settings.json
{
"model": "gemini-2.5-pro",
"theme": "dark",
"sandbox": true
}
项目配置(GEMINI.md)
在项目根目录中创建 GEMINI.md 文件,以提供项目专属上下文:
# Project Context for Gemini
## Tech Stack
- TypeScript with strict mode
- React 18 with hooks
- FastAPI backend
- PostgreSQL database
## Code Review Focus Areas
1. Type safety - ensure proper TypeScript types
2. React hooks rules - check for dependency array issues
3. SQL injection - verify parameterized queries
4. Authentication - check all endpoints have proper auth
## Conventions
- Use camelCase for variables
- Use PascalCase for components
- All API errors should use AppError class
CLI 速查
# Interactive
gemini # Start interactive mode
/code-review # Run code review extension
# Headless
gemini -p "prompt" # Single prompt, exit
gemini -p "prompt" --output-format json # JSON output
gemini -m gemini-2.5-flash -p "prompt" # Use faster model
# Extensions
gemini extensions list # List installed
gemini extensions install URL # Install extension
gemini extensions update # Update all
# Key Flags
--output-format json # Structured output
--output-format stream-json # Real-time events
--session-summary FILE # Track metrics
-m MODEL # Select model
对比:Claude、Codex 与 Gemini
| 方面 | Claude | Codex CLI | Gemini CLI |
|---|---|---|---|
| 设置 | 无(内置) | npm + OpenAI API | npm + Google 账号 |
| 模型 | Claude | GPT-5.2-Codex | Gemini 2.5 Pro |
| 上下文 | 对话 | 每次审查均为全新上下文 | 1M tokens(超大!) |
| 免费层级 | 不适用 | 有限 | 每天 1,000 次 |
| 最适合 | 快速审查 | 高准确率 | 大型代码库 |
| 原生支持 GitHub | 否 | @codex | Gemini Code Assist |
何时使用各引擎
| 场景 | 推荐引擎 |
|---|---|
| 快速流程内审查 | Claude |
| 关键安全审查 | Codex(检测率 88%) |
| 大型代码库(100+ 个文件) | Gemini(1M 上下文) |
| 免费自动审查 | Gemini |
| 多角度审查 | 三者全部(双引擎/三引擎) |
故障排除
| 问题 | 解决方案 |
|---|---|
gemini: command not found | npm install -g @google/gemini-cli |
Node.js version error | 升级到 Node.js 20+ |
Authentication failed | 重新运行 gemini 并再次登录 |
Extension not found | gemini extensions install https://github.com/gemini-cli-extensions/code-review |
Rate limited | 等待或升级到 Vertex AI |
Hangs in CI | 确保未设置 DEBUG 环境变量 |
反面模式
- 跳过身份验证设置 - 始终在 CI/CD 前完成配置
- 在日志中使用 API 密钥 - 使用机密管理
- 忽略上下文限制 - 即使有 1M 个令牌,超大型单体仓库也会受到限制
- 在每次提交时运行 - 仅在 PR 上运行以节省配额
- 未设置项目上下文 - 添加 GEMINI.md 以获得更好的审查
评论
支持 Markdown