diff --git a/scripts/catalog/propose_upstream_updates.py b/scripts/catalog/propose_upstream_updates.py index d75a137..a4f6f79 100644 --- a/scripts/catalog/propose_upstream_updates.py +++ b/scripts/catalog/propose_upstream_updates.py @@ -160,6 +160,21 @@ def write_coverage(rows, destination): destination.with_suffix('.md').write_text("\n".join(summary) + "\n") +MAX_PR_BODY_CHARS = 60000 + + +def _truncate_pr_body(text: str, limit: int = MAX_PR_BODY_CHARS) -> str: + """Keep PR body safely below GitHub's 65,536-character API limit.""" + if len(text) <= limit: + return text + notice = ( + "\n\n---\n" + "> 审核详情过长已截断(超过 GitHub PR 正文字符上限);完整逐项结果请见本次 Workflow 的 `audit-results` artifact。\n" + "> Truncated to stay within GitHub's PR body character limit; see the workflow `audit-results` artifact for complete per-item findings.\n" + ) + return text[: max(0, limit - len(notice))].rstrip() + notice + + def _is_automated_draft_only(item_id: str, pr_details: dict) -> bool: """Return True only if every commit and file on the Draft PR is from the automated proposer.""" allowed_headlines = { @@ -267,11 +282,13 @@ def promote_verified_draft_pr( body = Path(tmp) / "body.md" body.write_text( - f"`{item_id}` 上游快照:`{result['ref']}` → `{result['head']}`\n\n" - "已有草稿 PR 已按最新 `main` 策略与专项复核记录重审通过,已同步 `main` 并转为 Ready;审核身份和记录明确标为机器。已运行目录与本条目元数据校验。\n" - "不执行上游代码,不代表软件安全认证或真实运行验收,不自动合并。非结构化依赖和行为变化仍需结合 PR 差异复核。\n\n" - "Automated source/license/metadata re-verification passed under latest main; synced with main and promoted from draft. Not a security certification or runtime acceptance. No auto-merge.\n\n" - + "```json\n" + json.dumps(audit, ensure_ascii=False, indent=2) + "\n```\n", + _truncate_pr_body( + f"`{item_id}` 上游快照:`{result['ref']}` → `{result['head']}`\n\n" + "已有草稿 PR 已按最新 `main` 策略与专项复核记录重审通过,已同步 `main` 并转为 Ready;审核身份和记录明确标为机器。已运行目录与本条目元数据校验。\n" + "不执行上游代码,不代表软件安全认证或真实运行验收,不自动合并。非结构化依赖和行为变化仍需结合 PR 差异复核。\n\n" + "Automated source/license/metadata re-verification passed under latest main; synced with main and promoted from draft. Not a security certification or runtime acceptance. No auto-merge.\n\n" + + "```json\n" + json.dumps(audit, ensure_ascii=False, indent=2) + "\n```\n" + ), encoding="utf-8", ) run("gh", "pr", "edit", str(pr_number), "--repo", repository, "--body-file", str(body), cwd=checkout) @@ -422,12 +439,16 @@ def main() -> None: body = Path(tmp) / "body.md" findings = json.dumps(audit, ensure_ascii=False, indent=2) if audit else reason body.write_text( - f"`{item_id}` 上游快照:`{result['ref']}` → `{result['head']}`\n\n" - + ("自动来源、许可与结构化元数据核验通过;审核身份和记录明确标为机器。已运行目录与本条目元数据校验。\n" - if audit else "自动核验未通过,保留旧审核回执与草稿,不能据此发布新版。\n") - + "不执行上游代码,不代表软件安全认证或真实运行验收,不自动合并。非结构化依赖和行为变化仍需结合 PR 差异复核。\n\n" - + "Automated source/license/metadata verification, not a security certification or runtime acceptance. No auto-merge.\n\n" - + ("```json\n" + str(findings) + "\n```\n" if audit else str(findings) + "\n"), encoding="utf-8") + _truncate_pr_body( + f"`{item_id}` 上游快照:`{result['ref']}` → `{result['head']}`\n\n" + + ("自动来源、许可与结构化元数据核验通过;审核身份和记录明确标为机器。已运行目录与本条目元数据校验。\n" + if audit else "自动核验未通过,保留旧审核回执与草稿,不能据此发布新版。\n") + + "不执行上游代码,不代表软件安全认证或真实运行验收,不自动合并。非结构化依赖和行为变化仍需结合 PR 差异复核。\n\n" + + "Automated source/license/metadata verification, not a security certification or runtime acceptance. No auto-merge.\n\n" + + ("```json\n" + str(findings) + "\n```\n" if audit else str(findings) + "\n") + ), + encoding="utf-8", + ) command = ["gh", "pr", "create", "--repo", repository, "--base", "main", "--head", branch, "--title", f"chore(market): 更新 {item_id} 上游快照", "--body-file", str(body)] if not audit: diff --git a/scripts/catalog/test_propose_upstream_updates.py b/scripts/catalog/test_propose_upstream_updates.py index 2df9809..a04209f 100644 --- a/scripts/catalog/test_propose_upstream_updates.py +++ b/scripts/catalog/test_propose_upstream_updates.py @@ -242,6 +242,13 @@ class CandidateTests(unittest.TestCase): self.assertNotIn(('gh', 'pr', 'ready'), events) self.assertIn('校验未通过', cov[0]['reason']) + def test_oversized_pr_body_is_truncated_below_github_limit(self): + huge = "x" * 80000 + truncated = proposer._truncate_pr_body(huge) + self.assertLessEqual(len(truncated), proposer.MAX_PR_BODY_CHARS) + self.assertLess(len(truncated), 65536) + self.assertIn("audit-results", truncated) + if __name__ == "__main__": unittest.main()