真实 Runner 首跑(run #16, ci-trial-20261012)发现:新分支推送时 Gitea 给 total_commits=0、commits 列表被截断(5 条),而 Git 范围 12 条;原先'声明条数 != Git 范围就拒绝'让门禁第一次真实运行就红,且与代码质量无关。改成 Git 范围是唯一权威:total_commits>0 才交叉核对(保留'声明6/列表1/Git2 ⇒ 拒绝'的判据),<=0 或缺失视为未声明并以 Git 范围为准,同时把'负载条数不可信、列表被截断'写进来源说明。--self-test 增加该形状(15 分支);traps 22 记录教训:负例要照真实负载的形状造。
This commit is contained in:
@@ -189,6 +189,12 @@ def resolve_scope(explicit="", env=None, cwd=None):
|
||||
declared = int(declared_raw)
|
||||
except (TypeError, ValueError):
|
||||
return [], src, f"事件负载 total_commits 不是整数:{declared_raw!r}"
|
||||
# ⚠️ 实测(真实 Runner):**新分支推送时 Gitea 会给 `total_commits=0`** ——
|
||||
# 那不是"声明 0 条",而是"这次没声明条数"。只有当它 > 0 时才拿来跟 Git 范围比对;
|
||||
# 否则以 Git 范围为准(并把"负载没声明/列表被截断"写进来源里)。
|
||||
if declared <= 0:
|
||||
declared = None
|
||||
if declared is not None:
|
||||
if declared != len(commits):
|
||||
return [], src, (
|
||||
f"事件声明 {declared} 条提交,但 Git 范围算出 {len(commits)} 条"
|
||||
@@ -200,8 +206,8 @@ def resolve_scope(explicit="", env=None, cwd=None):
|
||||
else:
|
||||
src += "(与事件列表一致)"
|
||||
elif len(listed) != len(commits):
|
||||
src += (f"(⚠️ 事件列表 {len(listed)} 条、Git 范围 {len(commits)} 条"
|
||||
f" ⇒ 以 Git 范围为准)")
|
||||
src += (f"(⚠️ 事件负载没有可靠的条数声明(total_commits={declared_raw!r}),"
|
||||
f"列表给了 {len(listed)} 条、Git 范围 {len(commits)} 条 ⇒ 以 Git 范围为准)")
|
||||
return commits, src, ""
|
||||
|
||||
# ── ② 负载里有 commits 但没有 before:用列表,但**没有 Git 范围可交叉核对**,要写明 ──
|
||||
@@ -216,7 +222,9 @@ def resolve_scope(explicit="", env=None, cwd=None):
|
||||
declared = int(declared_raw)
|
||||
except (TypeError, ValueError):
|
||||
return [], "", f"事件负载 total_commits 不是整数:{declared_raw!r}"
|
||||
if declared != len(listed):
|
||||
if declared <= 0:
|
||||
declared = None # 与推送路径同一条口径:<=0 是"没声明",不是"声明 0 条"
|
||||
elif declared != len(listed):
|
||||
return [], "", (f"事件声明 {declared} 条提交、列表只给 {len(listed)} 条,"
|
||||
f"而这次又没有 before 可以算 Git 范围 —— 列表被截断且无法交叉核对,"
|
||||
f"不许拿截断后的列表去核")
|
||||
@@ -376,6 +384,20 @@ def self_test() -> int:
|
||||
if err or len(commits) != 1 or "commits[]" not in src:
|
||||
problems.append(f"事件负载正常时没有按 commits[] 取:{err or (len(commits), src)}")
|
||||
|
||||
# ⑫a **真实事件形状**:新分支推送时 Gitea 给 total_commits=0、列表被截断,
|
||||
# 必须以 Git 范围为准通过(真实 Runner 上就是这里红的)
|
||||
with open(evf, "w", encoding="utf-8") as fh:
|
||||
json.dump({"before": "0" * 40, "after": good2, "total_commits": 0,
|
||||
"commits": [{"id": good2, "message": "fix(b): 第三条合规"}],
|
||||
"repository": {"default_branch": "main"}}, fh)
|
||||
git("update-ref", "refs/remotes/origin/main", good1, cwd=d)
|
||||
commits, src, err = resolve_scope(env={"GITHUB_EVENT_PATH": evf, "GITHUB_ACTIONS": "true"}, cwd=d)
|
||||
if err or len(commits) != 2:
|
||||
problems.append(f"total_commits=0(真实新分支事件形状)时没有以 Git 范围为准:{err or len(commits)}")
|
||||
elif "没有可靠的条数声明" not in src:
|
||||
problems.append(f"没有写明负载条数不可信:{src}")
|
||||
git("update-ref", "-d", "refs/remotes/origin/main", cwd=d)
|
||||
|
||||
# ⑫ 本地一个远端都没有 ⇒ 只核 HEAD,并写明(不许退化成"整部历史")
|
||||
commits, src, err = resolve_scope(env={}, cwd=d)
|
||||
if err or len(commits) != 1 or "只核 HEAD" not in src:
|
||||
@@ -448,10 +470,10 @@ def self_test() -> int:
|
||||
for p in problems:
|
||||
print(f" · {p}", file=sys.stderr)
|
||||
return 1
|
||||
print("✅ 提交闸门自检通过:范围取法 14 个分支(显式范围 / Git 范围 before..after / "
|
||||
"负载声明与列表不一致 / 列表截断仍以 Git 为准 / 空范围拒绝 / 新分支 / 无 before 的列表 / "
|
||||
"列表提交不在检出里 / after 与 HEAD 核对 / 手工运行只核 HEAD 并写明 / "
|
||||
"无远端只核 HEAD / 新分支以默认分支为基准(含同名分支回归)/ 未推送提交)")
|
||||
print("✅ 提交闸门自检通过:范围取法 15 个分支(显式范围 / Git 范围 before..after / "
|
||||
"负载声明与列表不一致 / 列表截断仍以 Git 为准 / **total_commits=0 的真实事件形状** / "
|
||||
"空范围拒绝 / 新分支以默认分支为基准(含同名分支回归)/ 列表提交不在检出里 / "
|
||||
"after 与 HEAD 核对 / 手工运行与无远端时只核 HEAD 并写明 / 未推送提交)")
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
+6
-1
@@ -127,7 +127,12 @@ python3 -c "import json;d=json.load(open('ci-report.json'));print(d['overall_sta
|
||||
再跑提交范围闸门。正例(检出到事件 SHA、协议保留、合规新分支通过)与负例
|
||||
(旧写法 127、取不到 SHA 失败、含违规提交的新分支判红且**红在那条提交上**)共 12 项断言。
|
||||
|
||||
未推送、未合并、未部署;仍未在真实 Runner 上跑过(需要短命试验分支授权)。
|
||||
- **第一次真实 Runner 首跑又抓到一处**:新分支推送时 Gitea 的 `total_commits=0`、`commits` 列表被截断(5 条),
|
||||
而 Git 范围是 12 条 —— 原先"声明条数 ≠ Git 范围就拒绝"的严判让门禁在真实 CI 上第一次就红了,且与代码质量无关。
|
||||
改成:**Git 范围是唯一权威**,`total_commits > 0` 才交叉核对,`<=0`/缺失视为"未声明"并把
|
||||
"负载条数不可信、列表被截断"写进来源说明(`--self-test` 增加对应分支,见 traps 22)。
|
||||
|
||||
|
||||
|
||||
## [0.1.0] - 2026-10-10
|
||||
|
||||
|
||||
@@ -319,3 +319,21 @@ YAML 里只调用它,不再内联这套拼接。
|
||||
|
||||
**修法**:`ci_gate.resolve_scope()`;`ci_checkout.sh --post-checkout` 负责把默认分支引用取下来。
|
||||
`--self-test` 里有专门的回归用例(`origin/<本分支> == after` 时必须仍算出那几条提交)。
|
||||
|
||||
---
|
||||
|
||||
### trap-22|把事件里的 `total_commits` 当成"声明条数"
|
||||
|
||||
**症状**:新分支推送的真实事件里 Gitea 给的是 `total_commits=0`、`commits` 列表被截断(实测 5 条),
|
||||
而 Git 范围算出 12 条。严判"声明 ≠ Git 范围"⇒ 门禁在**第一次真实 CI 就红了**,
|
||||
红的原因与代码质量无关(本地夹具用的是"声明 6 条/列表 1 条"的自造形状,覆盖不到这一种)。
|
||||
|
||||
**根因**:`total_commits` 在 Gitea 的语义不是"这次推送的提交条数",**新分支推送时就是 0**
|
||||
(等于"没声明")。把它当权威声明去比对,等于用一个不存在的约束判红。
|
||||
|
||||
**判据**:**Git 范围是唯一权威**。`total_commits > 0` 时才拿来交叉核对(不一致 ⇒ 拒绝,
|
||||
覆盖"声明 6 条 / 列表 1 条 / Git 2 条"那一类);`<=0` 或缺失 ⇒ 视为未声明,
|
||||
以 Git 范围为准,并把"负载条数不可信、列表被截断"**写进来源说明**里。
|
||||
|
||||
**修法**:`ci_gate.resolve_scope()`(`--self-test` 里有专门的 `total_commits=0` 分支)。
|
||||
教训:负例要**照真实事件负载的形状**造,不能只造自己想的形状。
|
||||
|
||||
Reference in New Issue
Block a user