ci: runner-envs + fork-aware checkout (review any PR)
AI Code Review / review (pull_request) Failing after 1s

This commit is contained in:
gitea-bot
2026-07-31 08:08:40 +08:00
parent e896e7d172
commit b8be62d6d0
+17 -34
View File
@@ -1,6 +1,6 @@
# AI 代码审查:每次 PR 触发,用 Claude Code 调用智谱 GLM(glm-5.2) 审查 diff
# 把结论评论到 PR。走 GLM 的 Anthropic 兼容端点,无需访问 Anthropic 官方。
# 注意:来自 fork 的跨仓库 PR 会被 Gitea 隐藏密钥,无法审查——请在团队仓库内开同仓库 PR
# AI 代码审查:每次 PR 触发(含 fork 跨仓库 PR,用 Claude Code 调用智谱 GLM(glm-5.2) 审查 diff
# 把结论评论到 PR。ANTHROPIC_API_KEY / REVIEW_PAT 由 runner 的 envs 注入(不经 Gitea secret
# fork PR 也能拿到);MAIN_API 用组织变量
name: AI Code Review
on:
pull_request:
@@ -11,46 +11,34 @@ jobs:
runs-on: ubuntu-latest
env:
ANTHROPIC_BASE_URL: https://open.bigmodel.cn/api/anthropic
ANTHROPIC_API_KEY: ${{ secrets.ZHIPU_API_KEY }}
ANTHROPIC_MODEL: glm-5.2
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1"
DISABLE_AUTOUPDATER: "1"
IS_SANDBOX: "1" # CI 容器以 root 运行,需此项才能用 --dangerously-skip-permissions
IS_SANDBOX: "1"
# ANTHROPIC_API_KEY 和 REVIEW_PAT 不要写在这里 —— 由 runner envs 注入
steps:
- name: 检出代码
# 用 http.extraHeader 传凭据,避免 PAT 出现在 clone URL(防日志/报错泄漏)
env:
REVIEW_PAT: ${{ secrets.REVIEW_PAT }}
- name: 检出代码(兼容 fork PR:克隆 head 仓库)
run: |
if [ -z "$REVIEW_PAT" ]; then
echo "::warning::REVIEW_PAT 为空:通常是来自 fork 的跨仓库 PR(Gitea 会隐藏密钥)。请在团队仓库内开同仓库 PR。本次跳过审查。"
echo "SKIP_REVIEW=1" >> "$GITHUB_ENV"
exit 0
fi
git config --global http.extraHeader "Authorization: Basic $(printf 'oauth2:%s' "$REVIEW_PAT" | base64 -w0)"
git clone "http://gitea:3000/${{ gitea.repository }}.git" .
git checkout "${{ gitea.event.pull_request.head.ref }}"
git clone "${{ gitea.event.pull_request.head.repo.clone_url }}" .
git checkout "${{ gitea.event.pull_request.head.sha }}"
- name: 生成 PR diff
if: env.SKIP_REVIEW != '1'
- name: 生成 PR diff(从 base 仓库取目标分支做对比)
run: |
BASE="${{ gitea.event.pull_request.base.ref }}"
git fetch origin "$BASE"
git diff "origin/$BASE...HEAD" > pr.diff
BASE_REPO="${{ gitea.event.pull_request.base.repo.clone_url }}"
BASE_REF="${{ gitea.event.pull_request.base.ref }}"
git remote add base "$BASE_REPO"
git fetch base "$BASE_REF"
git diff "base/$BASE_REF...HEAD" > pr.diff
echo "diff 行数: $(wc -l < pr.diff)"
- name: 安装 Claude Code(已装则跳过)
if: env.SKIP_REVIEW != '1'
run: |
if command -v claude >/dev/null 2>&1; then
echo "claude 已存在,跳过安装"
else
npm install -g @anthropic-ai/claude-code --registry=https://registry.npmmirror.com
if command -v claude >/dev/null 2>&1; then echo "claude 已存在,跳过安装"
else npm install -g @anthropic-ai/claude-code --registry=https://registry.npmmirror.com
fi
- name: 运行 AI 审查
if: env.SKIP_REVIEW != '1'
# Claude Code 会自动读取本仓库 CLAUDE.md 与 .claude/skills/*
run: |
claude -p "$(cat .claude/review-prompt.md)" \
--model glm-5.2 \
@@ -59,17 +47,12 @@ jobs:
--max-turns 15 > review.md || true
- name: 把审查结论评论到 PR
if: env.SKIP_REVIEW != '1'
env:
REVIEW_PAT: ${{ secrets.REVIEW_PAT }}
API: ${{ vars.MAIN_API }}
REPO: ${{ gitea.repository }}
PR: ${{ gitea.event.pull_request.number }}
run: |
if [ -s review.md ]; then
# 写到临时文件再 --data @,避免超长 review 触发 ARG_MAX
node -e 'let d="";process.stdin.on("data",c=>d+=c).on("end",()=>process.stdout.write(JSON.stringify({body:d})))' < review.md > /tmp/body.json
curl -fsS -X POST "$API/repos/$REPO/issues/$PR/comments" \
curl -fsS -X POST "$API/repos/${{ gitea.repository }}/issues/${{ gitea.event.pull_request.number }}/comments" \
-H "Authorization: token $REVIEW_PAT" \
-H "Content-Type: application/json" \
--data @/tmp/body.json