-
Notifications
You must be signed in to change notification settings - Fork 20
540 lines (482 loc) · 26.6 KB
/
Copy pathdeepseek-review.yml
File metadata and controls
540 lines (482 loc) · 26.6 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# =============================================================================
# MFABD2 — DeepSeek PR 自动评审
# 路径建议:.github/workflows/deepseek-review.yml
# 依赖密钥:repository secret DEEPSEEK_API_KEY
# 可选变量:repository variable
# - DEEPSEEK_MODEL 默认 deepseek-v4-pro(也可填 deepseek-v4-flash)
# - DEEPSEEK_BASE_URL 默认 https://api.deepseek.com
# - DEEPSEEK_THINKING 默认 "true"(仅对 v4-pro 生效;flash 不开思考更便宜)
# =============================================================================
name: DeepSeek PR Reviewer
on:
# pull_request:
# types: [opened, synchronize, reopened, ready_for_review]
workflow_dispatch:
inputs:
pr_number:
description: '需要审查的 PR 编号'
required: true
type: string
permissions:
pull-requests: write
contents: read
# 同一 PR 新推 commit 时取消上一次还在跑的评审,避免重复消耗额度
concurrency:
group: deepseek-review-${{ github.event.pull_request.number || inputs.pr_number }}
cancel-in-progress: true
jobs:
review:
# 草稿 PR 跳过(手动 dispatch 仍允许)
if: |
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request' && github.event.pull_request.draft == false)
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: 检出代码
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: 设置 Python 3.11
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: pip
- name: 安装依赖
run: pip install --quiet "openai>=1.40" "requests>=2.31"
- name: 缓存官方文档(24h 复用)
uses: actions/cache@v4
with:
path: .deepseek_docs_cache
key: maafw-docs-${{ github.run_id }}
restore-keys: |
maafw-docs-
- name: 执行 AI 评审并发布评论
env:
DEEPSEEK_API_KEY: ${{ secrets.DEEPSEEK_API_KEY }}
DEEPSEEK_MODEL: ${{ vars.DEEPSEEK_MODEL || 'deepseek-v4-pro' }}
DEEPSEEK_BASE_URL: ${{ vars.DEEPSEEK_BASE_URL || 'https://api.deepseek.com' }}
DEEPSEEK_THINKING: ${{ vars.DEEPSEEK_THINKING || 'true' }}
DEEPSEEK_MAX_TOKENS: ${{ vars.DEEPSEEK_MAX_TOKENS || '16384' }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }}
REPO: ${{ github.repository }}
EVENT_NAME: ${{ github.event_name }}
MAX_DIFF_CHARS: '60000' # 给模型的 diff 最大字符数
PER_DOC_CHARS: '12000' # 每篇官方文档最大注入字符数
run: |
python << 'PYEOF'
# -*- coding: utf-8 -*-
"""
MFABD2 PR 自动评审脚本(GitHub Actions 内联)
逻辑分四段:拉 PR diff → 按需拉文档 → 调 DeepSeek → 发/更新评论
"""
import hashlib
import json
import os
import pathlib
import sys
import time
from typing import Optional
import requests
from openai import OpenAI
# --------------------- 0. 环境变量 --------------------- #
api_key = os.environ["DEEPSEEK_API_KEY"]
model = os.environ.get("DEEPSEEK_MODEL", "deepseek-v4-pro").strip()
base_url = os.environ.get("DEEPSEEK_BASE_URL", "https://api.deepseek.com").strip()
thinking = os.environ.get("DEEPSEEK_THINKING", "true").lower() == "true"
gh_token = os.environ["GITHUB_TOKEN"]
pr_number = os.environ.get("PR_NUMBER", "").strip()
repo = os.environ["REPO"]
event = os.environ.get("EVENT_NAME", "manual")
max_diff_chars = int(os.environ.get("MAX_DIFF_CHARS", "60000"))
per_doc_chars = int(os.environ.get("PER_DOC_CHARS", "12000"))
max_tokens = int(os.environ.get("DEEPSEEK_MAX_TOKENS", "16384"))
if not pr_number:
print("❌ 未检测到 PR 编号,工作流结束。")
sys.exit(0)
# 隐藏标记,用来在 PR 评论列表里找回上一次的 bot 评论并就地更新
BOT_MARKER = "<!-- mfabd2-deepseek-reviewer-bot:v1 -->"
GH_HEADERS = {
"Authorization": f"token {gh_token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": "mfabd2-deepseek-reviewer",
}
# --------------------- 1. 通用:带退避的 HTTP --------------------- #
def http(method: str, url: str, *, max_retries: int = 4, **kwargs) -> requests.Response:
kwargs.setdefault("timeout", 30)
last = None
for attempt in range(max_retries):
try:
r = requests.request(method, url, **kwargs)
if r.status_code in (429, 500, 502, 503, 504):
raise RuntimeError(f"HTTP {r.status_code}: {r.text[:200]}")
return r
except Exception as e:
last = e
wait = 2.0 * (2 ** attempt)
print(f"⚠️ 请求失败 {url} ({attempt+1}/{max_retries}): {e},{wait:.1f}s 后重试")
time.sleep(wait)
raise RuntimeError(f"请求最终失败 {url}: {last}")
# --------------------- 2. 拉取 PR 元信息 + 改动文件(分页) --------------------- #
# 先拉 PR 改动,再据此决定该加载哪些文档(按需加载,省 token)
pr_url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}"
pr_resp = http("GET", pr_url, headers=GH_HEADERS)
if pr_resp.status_code != 200:
print(f"❌ 拉取 PR 信息失败: HTTP {pr_resp.status_code} -- {pr_resp.text[:300]}")
sys.exit(1)
pr_meta = pr_resp.json()
pr_title = pr_meta.get("title", "")
pr_body = (pr_meta.get("body") or "")[:1500]
files_changed = []
page = 1
while True:
r = http("GET", f"{pr_url}/files", headers=GH_HEADERS,
params={"per_page": 100, "page": page})
if r.status_code != 200:
print(f"⚠️ /files 分页 {page} 失败 HTTP {r.status_code}")
break
chunk = r.json()
if not chunk:
break
files_changed.extend(chunk)
if len(chunk) < 100:
break
page += 1
if not files_changed:
print("ℹ️ PR 无文件变更,结束。")
sys.exit(0)
# 改动分类(提前到这里,供文档触发器和后续 focus 共用)
changed_set = {f["filename"] for f in files_changed}
pipeline_jsons = [x for x in changed_set
if x.startswith("assets/resource/") and x.endswith(".json") and "pipeline" in x]
other_resource_jsons = [x for x in changed_set
if x.startswith("assets/resource/") and x.endswith(".json")
and x not in pipeline_jsons]
python_files = [x for x in changed_set if x.startswith("agent/") and x.endswith(".py")]
interface_changed = "assets/interface.json" in changed_set
print(f"🗂️ 改动分类:pipeline={len(pipeline_jsons)} 其他JSON={len(other_resource_jsons)} "
f"py={len(python_files)} interface={'是' if interface_changed else '否'}")
# --------------------- 3. 按需加载官方文档 --------------------- #
# 每篇文档配 (URL, 触发条件);触发条件返回 False 时跳过下载,节省 ~3k-5k token。
# always=True 表示无条件加载(流水线协议是基础协议,多数 PR 都涉及)。
DOC_SPECS = {
"任务流水线协议": {
"url": "https://raw.githubusercontent.com/MaaXYZ/MaaFramework/main/docs/zh_cn/3.1-%E4%BB%BB%E5%8A%A1%E6%B5%81%E6%B0%B4%E7%BA%BF%E5%8D%8F%E8%AE%AE.md",
"trigger": lambda: bool(pipeline_jsons or other_resource_jsons or python_files),
"trigger_desc": "存在 pipeline JSON / 资源 JSON / Python 改动",
},
"ProjectInterfaceV2 协议": {
"url": "https://raw.githubusercontent.com/MaaXYZ/MaaFramework/main/docs/zh_cn/3.3-ProjectInterfaceV2%E5%8D%8F%E8%AE%AE.md",
"trigger": lambda: interface_changed,
"trigger_desc": "interface.json 被修改",
},
"Python 绑定 README": {
"url": "https://raw.githubusercontent.com/MaaXYZ/MaaFramework/main/source/binding/Python/README.md",
"trigger": lambda: bool(python_files),
"trigger_desc": "存在 agent/*.py 改动",
},
}
cache_dir = pathlib.Path(".deepseek_docs_cache")
cache_dir.mkdir(parents=True, exist_ok=True)
ONE_DAY = 86400
doc_blocks = []
loaded_names, skipped_names = [], []
for name, spec in DOC_SPECS.items():
if not spec["trigger"]():
print(f"⏭️ 跳过文档 [{name}]:未触发(条件:{spec['trigger_desc']})")
skipped_names.append(name)
continue
url = spec["url"]
cache_file = cache_dir / (hashlib.sha256(url.encode()).hexdigest() + ".md")
text = None
if cache_file.exists() and (time.time() - cache_file.stat().st_mtime) < ONE_DAY:
text = cache_file.read_text(encoding="utf-8", errors="replace")
print(f"📦 命中缓存: {name}")
else:
try:
r = http("GET", url, headers={"User-Agent": "mfabd2-reviewer"})
if r.status_code == 200:
text = r.text
cache_file.write_text(text, encoding="utf-8")
print(f"✅ 下载文档: {name}")
else:
print(f"⚠️ 文档 {name} HTTP {r.status_code}")
except Exception as e:
print(f"⚠️ 文档 {name} 失败: {e}")
if text:
doc_blocks.append(f"【{name}】\n{text[:per_doc_chars]}")
loaded_names.append(name)
doc_knowledge = ("\n\n---\n\n".join(doc_blocks)
if doc_blocks
else "(本次 PR 改动不涉及需引用的协议文档,模型将依赖训练知识 + 项目通用规范进行评审)")
print(f"📚 文档汇总:加载 {len(loaded_names)}/{len(DOC_SPECS)} 篇,"
f"跳过 {len(skipped_names)} 篇")
# 跳过二进制 / 资产文件(不送给模型读 base64)
SKIP_EXT = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".ico",
".zip", ".rar", ".7z", ".gz", ".tar", ".whl", ".exe", ".dll",
".pdf", ".mp3", ".mp4", ".wav", ".onnx", ".bin", ".so", ".dylib"}
def is_binary(name: str) -> bool:
n = name.lower()
return any(n.endswith(ext) for ext in SKIP_EXT)
# 按文件级拼接 diff,按字符预算控制总量
diff_chunks, total = [], 0
truncated, skipped_bin = False, []
for f in files_changed:
fn = f.get("filename", "")
status = f.get("status", "")
patch = f.get("patch")
if is_binary(fn):
skipped_bin.append(fn)
continue
if not patch:
diff_chunks.append(f"diff --git a/{fn} b/{fn}\n[status={status}, patch unavailable]")
continue
block = f"diff --git a/{fn} b/{fn}\n{patch}"
if total + len(block) > max_diff_chars:
truncated = True
break
diff_chunks.append(block)
total += len(block)
if not diff_chunks:
print("ℹ️ 全部变更均为二进制资源,无文本可审。")
sys.exit(0)
diff_content = "\n\n".join(diff_chunks)
if truncated:
diff_content += "\n\n[... 因长度限制,剩余 diff 已截断 ...]"
# --------------------- 4. 项目侧重点识别 --------------------- #
# 改动分类已在 section 2 末尾完成;此处仅根据分类组装 focus 文本
focus = []
if pipeline_jsons:
focus.append(
"- **流水线 JSON**:核对 next / on_error / interrupt 跳转是否成环或失联;"
"recognition 类型与所需参数(如 OCR 的 expected/replace、TemplateMatch 的 template/threshold)是否齐备;"
"regex 字段是 JSON 字符串需双重转义;"
"`doc` 字段(开发者注释)必须与实际 action / next 行为一致,否则视为陈旧注释。"
)
if other_resource_jsons:
focus.append(
"- **资源 JSON(非流水线)**:核对结构、字段类型、命名规范,避免无效字段或拼写错误。"
)
if python_files:
focus.append(
"- **Agent Python**:检查 `@AgentServer.custom_action` 注册名/类名是否与 Pipeline 的 "
"`action: Custom` + `custom_action` 字段匹配;"
"`custom_action_param` 抵达时是 **JSON 字符串**,必须 `json.loads()` 解包;"
"Custom Action 内不应硬编码 ROI、过滤词、阈值等业务参数(数据-逻辑分离原则)—— "
"这些应该来自 Pipeline 节点;"
"MaaResource / MaaController / MaaTasker 的生命周期与异常兜底;"
"`RecognitionDetail` 应使用 `filtered_results` 而非 `all_results`,以应用置信度阈值。"
)
if interface_changed:
focus.append(
"- **interface.json**:对照《工程接口 V2》核对 task / option / resource / controller 等字段、"
"类型、必填项;新增任务是否同时在 pipeline 中提供入口节点。"
)
if not focus:
focus.append("- 通用审查:代码风格、潜在 Bug、逻辑错误、异常兜底、命名一致性。")
# --------------------- 5. 组装 Prompt --------------------- #
system_prompt = (
"你是 MaaFramework(MAA)项目的资深代码审查专家,正在审查 MFABD2 项目(基于 MaaFW 的 "
"Brown Dust 2 自动化)。请严格以用户提供的官方文档内容为准;与你内置知识冲突时以文档为准。"
"用中文回复,语气专业、具体;引用文件名和代码片段时必须基于 diff 中真实存在的内容,"
"**禁止臆造行号或代码**;无法从 diff/文档中得到支持的结论必须显式标注「不确定」。"
)
# 注:以下"项目通用规范"部分把你团队的工程约定固化进 prompt,
# 这是把 AI 评审从"通用代码审查"提升为"项目内代码审查"的关键。
user_prompt = f"""\
# 一、官方文档(节选,请优先依据)
{doc_knowledge}
---
# 二、本次 PR 元信息
- 仓库: `{repo}`
- PR #{pr_number}: **{pr_title}**
- 描述(截断 1500 字符):
{pr_body or "(PR 描述为空)"}
# 三、本次审查重点(基于改动文件路径自动识别)
{chr(10).join(focus)}
# 四、MFABD2 项目通用规范(必须检查)
1. **doc 字段一致性**:流水线 JSON 中的 `doc` 是开发者业务注释,必须与 `action` / `next` / `recognition` 等实际字段语义一致,不一致时按"陈旧注释"标记。
2. **Custom Action 双向对齐**:Pipeline 中 `action: Custom` + `custom_action: "X"` 必须能在 `agent/` 下找到对应 `@AgentServer.custom_action("X")` 注册的实现;反之 Python 中注册的 action 名也应被某个 Pipeline 节点引用,否则视为孤儿。
3. **正则限制**:MaaFW 使用 C++ `std::regex`,**对 lookbehind / lookahead 支持不稳定**(已踩坑:`(\\d+)\\s*[/:|\\-~]\\s*\\1(?!\\d)` 出现误匹配)。审查中遇到 `(?=...)` `(?!...)` `(?<=...)` `(?<!...)` 应建议改为锚点写法(`^...$`)。
4. **数据-逻辑分离**:CustomAction 的 Python 代码不应出现硬编码 ROI 坐标、OCR 过滤词、阈值等业务参数;这些应通过 Pipeline 节点的 `custom_action_param` 传入。
5. **OCR 噪声过滤**:优先使用 Pipeline OCR 节点的 `replace` 字段在引擎层过滤,而不是在 Python 后处理中处理。
6. **JSON 中的正则**:所有 `regex` 字段必须按 JSON 字符串规则转义(反斜杠双写)。
7. **filtered vs all**:`RecognitionDetail` 取识别结果时使用 `filtered_results`(已应用阈值),不要用 `all_results`。
# 五、输出格式(请严格遵守 Markdown 结构)
## 概览
(1–3 句话总结这次 PR 在做什么、整体质量如何)
## 阻塞性问题(必须修改)
若无写"无"。每条必须包含:
- **文件**:`path/to/file:相关 hunk`
- **问题**:客观描述
- **原因**:引用上面文档第 X 节 / 项目规范第 X 条
- **建议**:具体怎么改
## 建议改进(非阻塞)
同上格式。
## 疑问 / 需要作者确认
列出无法仅凭 diff 判断、需要作者补充上下文的点。
---
# 六、本次 PR 的 unified diff
```diff
{diff_content}
```
"""
if skipped_bin:
user_prompt += "\n\n> ⓘ 以下二进制/资源文件未参与本次代码审查:" + ", ".join(skipped_bin[:30])
if len(skipped_bin) > 30:
user_prompt += f" 等 {len(skipped_bin)} 个文件"
# --------------------- 6. 调用 DeepSeek --------------------- #
# 6.1 预飞:先用 requests 直接探测 api.deepseek.com 是否可达;
# 若网络层有问题,可避免后续 OpenAI SDK 抛出含糊的 "Connection error."
import socket
import urllib.parse as _urlparse
host = _urlparse.urlparse(base_url).hostname or "api.deepseek.com"
print(f"🔍 预飞检查:解析 {host} ...")
try:
ip = socket.gethostbyname(host)
print(f" DNS OK → {ip}")
except Exception as e:
print(f"❌ DNS 失败:{e}(runner 出网受限或 DeepSeek 域名解析异常)")
sys.exit(1)
try:
probe = requests.get(f"{base_url.rstrip('/')}/v1/models",
headers={"Authorization": f"Bearer {api_key}"},
timeout=15)
print(f" HTTPS 探测 → HTTP {probe.status_code} "
f"(200=可达且鉴权OK; 401=可达但 Key 无效; 402=余额不足)")
if probe.status_code == 401:
print("⚠️ API Key 鉴权失败;请检查 secret DEEPSEEK_API_KEY 是否正确填写。")
elif probe.status_code == 402:
print("⚠️ 账户余额不足(DeepSeek 预付费);请前往 platform.deepseek.com 充值。")
except Exception as e:
print(f"❌ HTTPS 探测失败:{type(e).__name__}: {e}")
print(" 可能原因:runner 出网被防火墙拦截 / TLS 握手失败 / DeepSeek 临时不可用。")
sys.exit(1)
client = OpenAI(api_key=api_key, base_url=base_url, timeout=180.0, max_retries=0)
print(f"🤖 调用模型 `{model}` (thinking={thinking}) ...")
# v4-pro 默认开思考;flash 也支持但开了会变慢,按 env 决定
create_kwargs = dict(
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
max_tokens=max_tokens,
)
if thinking and "v4" in model:
# 注:v4 系列开 thinking 模式后 temperature/top_p 会被忽略
create_kwargs["reasoning_effort"] = "high"
create_kwargs["extra_body"] = {"thinking": {"type": "enabled"}}
else:
create_kwargs["temperature"] = 0.2
try:
import openai as _openai_mod # 用于精确 except
last_err = None
completion = None
for attempt in range(3):
try:
completion = client.chat.completions.create(**create_kwargs)
break
except _openai_mod.APIConnectionError as e:
last_err = e
cause = getattr(e, "__cause__", None)
print(f"⚠️ 连接错误 (尝试 {attempt+1}/3): {e!r}")
if cause:
print(f" 根因: {type(cause).__name__}: {cause}")
if attempt < 2:
time.sleep(2.0 * (2 ** attempt))
except _openai_mod.AuthenticationError as e:
print(f"❌ 鉴权失败:{e}")
print(" 请检查 secret DEEPSEEK_API_KEY 是否设置且正确。")
sys.exit(1)
except _openai_mod.PermissionDeniedError as e:
print(f"❌ 权限不足:{e}")
print(f" `{model}` 可能未对你的账户开放,或余额不足。"
"建议把 vars.DEEPSEEK_MODEL 改为 `deepseek-chat` 试试。")
sys.exit(1)
except _openai_mod.NotFoundError as e:
print(f"❌ 模型不存在:{e}")
print(f" 当前模型名 `{model}` 在 DeepSeek API 中不可用;"
"请改用 `deepseek-v4-pro` / `deepseek-v4-flash` / `deepseek-chat`。")
sys.exit(1)
except _openai_mod.BadRequestError as e:
print(f"❌ 请求格式错误:{e}")
sys.exit(1)
if completion is None:
raise last_err or RuntimeError("DeepSeek 调用失败:未知原因")
reply = (completion.choices[0].message.content or "").strip()
finish_reason = getattr(completion.choices[0], "finish_reason", "")
usage = getattr(completion, "usage", None)
if usage:
print(f"📊 token: prompt={usage.prompt_tokens}, "
f"completion={usage.completion_tokens}, total={usage.total_tokens}, "
f"finish_reason={finish_reason}")
# ⚠️ 截断检测:若 finish_reason='length',输出在 max_tokens 处被硬切,需提醒
truncated_by_length = (finish_reason == "length")
if truncated_by_length:
print(f"⚠️ 响应在 max_tokens={max_tokens} 处被截断;"
f"请在仓库 Variables 中调高 DEEPSEEK_MAX_TOKENS(建议 24000-32000)。")
reply += (
"\n\n---\n\n"
f"> ⚠️ **本次评审输出在 `max_tokens={max_tokens}` 处被截断,结论可能不完整。**\n"
f"> 请在仓库 *Settings → Variables* 中调高 `DEEPSEEK_MAX_TOKENS` 后重新触发评审。"
)
except Exception as e:
import traceback
print(f"❌ DeepSeek 调用最终失败:{type(e).__name__}: {e}")
cause = getattr(e, "__cause__", None)
if cause:
print(f" 根因: {type(cause).__name__}: {cause}")
print(" 完整堆栈:")
traceback.print_exc()
sys.exit(1)
if not reply:
print("⚠️ 模型返回为空,跳过评论。")
sys.exit(0)
# --------------------- 7. 发布或更新评论(去重) --------------------- #
comment_body = (
f"{BOT_MARKER}\n"
f"### 🤖 DeepSeek 自动评审报告\n"
f"**模型**:`{model}` "
f"**触发**:`{event}` "
f"**Diff 截断**:{'是' if truncated else '否'} "
f"**输出截断**:{'⚠️ 是' if truncated_by_length else '否'} "
f"**改动文件**:{len(files_changed)} 个(其中 {len(skipped_bin)} 个二进制已跳过)\n\n"
f"{reply}\n\n"
f"---\n"
f"_本评论由 GitHub Actions + DeepSeek 自动生成;最终判断以人工审查为准。_"
)
comments_url = f"https://api.github.com/repos/{repo}/issues/{pr_number}/comments"
existing_id: Optional[int] = None
page = 1
while True:
r = http("GET", comments_url, headers=GH_HEADERS,
params={"per_page": 100, "page": page})
if r.status_code != 200:
break
items = r.json()
for c in items:
if BOT_MARKER in (c.get("body") or ""):
existing_id = c["id"]
break
if existing_id or len(items) < 100:
break
page += 1
if existing_id:
patch_url = f"https://api.github.com/repos/{repo}/issues/comments/{existing_id}"
r = http("PATCH", patch_url, headers=GH_HEADERS, json={"body": comment_body})
if r.status_code == 200:
print(f"✅ 已更新现有评论 (id={existing_id})")
else:
print(f"❌ 更新评论失败 [{r.status_code}]: {r.text[:300]}")
sys.exit(1)
else:
r = http("POST", comments_url, headers=GH_HEADERS, json={"body": comment_body})
if r.status_code == 201:
print("✅ 已发布新评论")
else:
print(f"❌ 发布评论失败 [{r.status_code}]: {r.text[:300]}")
sys.exit(1)
PYEOF