fix(catalog): 截断超长 PR 正文避免超过 GitHub 65536 字符上限 (#422)

## 根因
在 [run
46796970818](https://github.com/desirecore/market/actions/runs/46796970818)
中,`gamedev-agent-skills` 包含大量合集子项,每个子项生成的诊断表格行汇总后超过 GitHub PR 正文 65,536
字符上限,导致 `git push` 成功后 `gh pr create` 返回 exit 1,进而留下无 PR 的孤儿分支并在次日触发
`git push` non-fast-forward 失败。

## 修复
- `scripts/catalog/propose_upstream_updates.py`:新增
`_truncate_pr_body`(上限 60,000 字符,截断处提示完整逐项结果见本次 Workflow 的
`audit-results` artifact),统一应用于新建 PR 与自动草稿转 Ready 两条路径。
- `scripts/catalog/test_propose_upstream_updates.py`:新增单测
`test_oversized_pr_body_is_truncated_below_github_limit`(17 tests OK)。
This commit is contained in:
mashagua authored and GitHub committed 2026-12-02 11:13:39 +08:00
1 parent 2d96fc9c41
commit 55588fd33f
2 files changed
+30 -2

No files matched your search

+23 -2
View File
@@ -160,6 +160,21 @@ def write_coverage(rows, destination):
destination.with_suffix('.md').write_text("\n".join(summary) + "\n") 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: 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.""" """Return True only if every commit and file on the Draft PR is from the automated proposer."""
allowed_headlines = { allowed_headlines = {
@@ -267,11 +282,13 @@ def promote_verified_draft_pr(
body = Path(tmp) / "body.md" body = Path(tmp) / "body.md"
body.write_text( body.write_text(
_truncate_pr_body(
f"`{item_id}` 上游快照:`{result['ref']}` → `{result['head']}`\n\n" f"`{item_id}` 上游快照:`{result['ref']}` → `{result['head']}`\n\n"
"已有草稿 PR 已按最新 `main` 策略与专项复核记录重审通过,已同步 `main` 并转为 Ready;审核身份和记录明确标为机器。已运行目录与本条目元数据校验。\n" "已有草稿 PR 已按最新 `main` 策略与专项复核记录重审通过,已同步 `main` 并转为 Ready;审核身份和记录明确标为机器。已运行目录与本条目元数据校验。\n"
"不执行上游代码,不代表软件安全认证或真实运行验收,不自动合并。非结构化依赖和行为变化仍需结合 PR 差异复核。\n\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" "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", + "```json\n" + json.dumps(audit, ensure_ascii=False, indent=2) + "\n```\n"
),
encoding="utf-8", encoding="utf-8",
) )
run("gh", "pr", "edit", str(pr_number), "--repo", repository, "--body-file", str(body), cwd=checkout) 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" body = Path(tmp) / "body.md"
findings = json.dumps(audit, ensure_ascii=False, indent=2) if audit else reason findings = json.dumps(audit, ensure_ascii=False, indent=2) if audit else reason
body.write_text( body.write_text(
_truncate_pr_body(
f"`{item_id}` 上游快照:`{result['ref']}` → `{result['head']}`\n\n" f"`{item_id}` 上游快照:`{result['ref']}` → `{result['head']}`\n\n"
+ ("自动来源、许可与结构化元数据核验通过;审核身份和记录明确标为机器。已运行目录与本条目元数据校验。\n" + ("自动来源、许可与结构化元数据核验通过;审核身份和记录明确标为机器。已运行目录与本条目元数据校验。\n"
if audit else "自动核验未通过,保留旧审核回执与草稿,不能据此发布新版。\n") if audit else "自动核验未通过,保留旧审核回执与草稿,不能据此发布新版。\n")
+ "不执行上游代码,不代表软件安全认证或真实运行验收,不自动合并。非结构化依赖和行为变化仍需结合 PR 差异复核。\n\n" + "不执行上游代码,不代表软件安全认证或真实运行验收,不自动合并。非结构化依赖和行为变化仍需结合 PR 差异复核。\n\n"
+ "Automated source/license/metadata verification, not a security certification or runtime acceptance. No auto-merge.\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") + ("```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, command = ["gh", "pr", "create", "--repo", repository, "--base", "main", "--head", branch,
"--title", f"chore(market): 更新 {item_id} 上游快照", "--body-file", str(body)] "--title", f"chore(market): 更新 {item_id} 上游快照", "--body-file", str(body)]
if not audit: if not audit:
@@ -242,6 +242,13 @@ class CandidateTests(unittest.TestCase):
self.assertNotIn(('gh', 'pr', 'ready'), events) self.assertNotIn(('gh', 'pr', 'ready'), events)
self.assertIn('校验未通过', cov[0]['reason']) 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__": if __name__ == "__main__":
unittest.main() unittest.main()