80 lines
2.7 KiB
YAML
80 lines
2.7 KiB
YAML
name: Auto Review
|
|
|
|
on:
|
|
pull_request_target:
|
|
types: [opened, synchronize]
|
|
|
|
jobs:
|
|
review:
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
pull-requests: write
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Get PR diff
|
|
env:
|
|
GH_TOKEN: ${{ github.token }}
|
|
run: |
|
|
gh pr diff ${{ github.event.pull_request.number }} > /tmp/pr.diff
|
|
head -c 30000 /tmp/pr.diff > /tmp/pr_truncated.diff
|
|
|
|
- name: Review with DeepSeek
|
|
id: review
|
|
env:
|
|
DEEPSEEK_API_KEY: ${{ secrets.DEEPSEEK_API_KEY }}
|
|
PR_TITLE: ${{ github.event.pull_request.title }}
|
|
PR_BODY: ${{ github.event.pull_request.body }}
|
|
run: |
|
|
python3 - <<'PYEOF'
|
|
import os, json, urllib.request
|
|
|
|
diff = open("/tmp/pr_truncated.diff").read()
|
|
title = os.environ.get("PR_TITLE", "")
|
|
body = os.environ.get("PR_BODY", "")[:2000]
|
|
api_key = os.environ["DEEPSEEK_API_KEY"]
|
|
|
|
payload = json.dumps({
|
|
"model": "deepseek-chat",
|
|
"messages": [
|
|
{
|
|
"role": "system",
|
|
"content": "你是一个专业的代码审查员。请审查以下 Pull Request 的代码变更,给出简洁的中文评审意见。重点关注:1. 潜在的 bug 或逻辑错误 2. 安全问题 3. 性能问题 4. 代码风格和可读性。如果代码没有问题,简单说 LGTM 并给出简短总结。不要逐行重复代码。"
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": f"PR 标题: {title}\n\nPR 描述: {body}\n\n代码变更:\n{diff}"
|
|
}
|
|
],
|
|
"max_tokens": 2000
|
|
}).encode()
|
|
|
|
req = urllib.request.Request(
|
|
"https://api.deepseek.com/chat/completions",
|
|
data=payload,
|
|
headers={
|
|
"Authorization": f"Bearer {api_key}",
|
|
"Content-Type": "application/json",
|
|
},
|
|
)
|
|
|
|
with urllib.request.urlopen(req, timeout=120) as resp:
|
|
data = json.loads(resp.read())
|
|
|
|
review = data["choices"][0]["message"]["content"]
|
|
|
|
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
|
|
delim = "EOF_REVIEW"
|
|
f.write(f"comment<<{delim}\n{review}\n{delim}\n")
|
|
PYEOF
|
|
|
|
- name: Post review comment
|
|
env:
|
|
GH_TOKEN: ${{ github.token }}
|
|
REVIEW: ${{ steps.review.outputs.comment }}
|
|
run: |
|
|
printf '🤖 **Auto Review by DeepSeek**\n\n%s' "$REVIEW" > /tmp/comment.md
|
|
gh pr comment ${{ github.event.pull_request.number }} --body-file /tmp/comment.md
|