fix(branch): 分支与评审口径改回原稿 —— main 直推,PR 只是建议

问题:我把原稿 §1.1 的「**所有改动直接进 main**」丢掉了,另外加了三条原稿没有的
口径,还全程以"正本 §1.x"的名义引用,没标出这是我加的:
  ① §1.2「业务代码必须 PR、高风险路径必须异人评审」
  ② §1.3「main 必须设分支保护:禁直推 + 必须 PR + 必须状态检查通过」
  ③ 把分支保护列进 03 的 P0 并顺手在一个仓上开了
后果是实打实的:kg 的 main 被锁成禁直推,使用者"本地 main 攒一批再推"的习惯
被迫改成走 PR —— 被规范的名义逼着换了工作方式。

改法(按原稿口径恢复):
- §1.1 恢复「所有改动直接进 main」;需要评审时才开短命分支 + PR
- §1.2 改成「什么时候开 PR(**建议**,不是门禁)」,并写明早期版本这么错过,
  防止下一个人"顺手加严"
- §1.3 改成「分支保护:**默认不开**」,写清它容易被忽略的代价:
  「必须状态检查通过」只门控 PR 合并,**直推 main 时管不住**(CI 推完再跑)
- 03 第 1 项 → 「已定:默认不开」;05 应用机制 / 06 接入文档 / new-project.sh 同步
- scripts/adopt-existing.py:改成 **--with-protection 才动保护**(原来是 --no-protection
  才不动,默认开)——默认值反了这本身就是在替使用者做决定
- 技能 eng-standards 同步(代理真正读到的是它,不同步等于改了文档没用)

平台侧:kg 的分支保护已 DELETE;7 个仓 branch_protections 全部为 []

验证:py_compile 通过;DRY-RUN 默认输出"默认不开";加 --with-protection 才出现保护计划;
正本仓已无"必须走 PR / 禁止直接推送 / 必须设分支保护"等旧措辞。
This commit is contained in:
姚章浩
2026-10-10 18:04:21 +08:00
parent c26288b108
commit b87a102e34
6 changed files with 67 additions and 41 deletions
+19 -12
View File
@@ -1,11 +1,14 @@
#!/usr/bin/env python3
"""把一个**既有** Gitea 工程接入工程规范(正本 §8 的可执行版)。
python3 adopt-existing.py <owner/repo> [--apply] [--no-protection]
python3 adopt-existing.py <owner/repo> [--apply] [--with-protection]
默认**只体检 + 打印计划,不动任何东西**。加 --apply 才真的改。
为什么入口分成两边(体检 / 执行):
**分支保护默认不开**(原稿口径:所有改动直接进 main,正本 §1.3)。
只有加 --with-protection 才动它。
为什么开保护要单独一个开关、而且还得分两步入(体检 / 执行):
"强制状态检查"开错了会把 PR **永久锁死** —— 检查永远没人报,PR 永远合不了。
所以必须先体检判定该仓的工作流**能不能覆盖 PR 头提交**,判定通过才允许开。
@@ -262,9 +265,9 @@ def protection_payload(rep, allow, ctxs):
"enable_status_check": bool(allow), "status_check_contexts": ctxs if allow else []}
def set_protection(g: Gitea, repo: str, rep: dict, apply: bool, allow: bool, ctxs, no_protection: bool):
if no_protection:
return "按 --no-protection 跳过"
def set_protection(g: Gitea, repo: str, rep: dict, apply: bool, allow: bool, ctxs, with_protection: bool):
if not with_protection:
return "默认不开(原稿口径:直推 main,正本 §1.3);要开请加 --with-protection"
plan = f"保护 {rep['default_branch']}(禁直推 + 必须 PR;强制状态检查 {'开 ' + str(ctxs) if allow else '不开'})"
if not apply:
return "计划:" + plan
@@ -284,7 +287,7 @@ def main():
sys.exit(__doc__)
repo = args[0]
apply = "--apply" in sys.argv
no_protection = "--no-protection" in sys.argv
with_protection = "--with-protection" in sys.argv
g = Gitea()
print(f"Gitea: {g.base} 仓库: {repo} 模式: "
@@ -311,7 +314,7 @@ def main():
print("\n── 计划 ──")
print(" 1. AGENTS.md : " + add_pointer(g, repo, rep, False))
print(" 2. 分支保护 : " + set_protection(g, repo, rep, False, allow, ctxs, no_protection))
print(" 2. 分支保护 : " + set_protection(g, repo, rep, False, allow, ctxs, with_protection))
if not apply:
print("\n(DRY-RUN:什么都没改。确认计划无误后加 --apply 重跑)")
@@ -319,17 +322,21 @@ def main():
print("\n── 执行 ──")
print(" AGENTS.md : " + add_pointer(g, repo, rep, True))
print(" 分支保护 : " + set_protection(g, repo, rep, True, allow, ctxs, no_protection))
print(" 分支保护 : " + set_protection(g, repo, rep, True, allow, ctxs, with_protection))
_, bp = g.api(f"/repos/{repo}/branch_protections")
for b in L(bp):
d = D(b)
print(f" 读回: {d.get('branch_name')} enable_push={d.get('enable_push')} "
f"status_check={d.get('enable_status_check')} ctx={d.get('status_check_contexts')}")
print("\n ⚠️ 还差一步反向自检(必须做,别跳):")
print(f" t=$(mktemp -d); git clone {g.base}/{repo}.git $t; cd $t \\")
print(f" && git commit --allow-empty -m 'chore(probe): 不该进 main' \\")
print(f" && git push origin {rep['default_branch']}")
print(" 判据:必须被拒 —— Not allowed to push to protected branch")
if with_protection:
print(f" t=$(mktemp -d); git clone {g.base}/{repo}.git $t; cd $t \\")
print(" && git commit --allow-empty -m 'chore(probe): 不该进 main' \\")
print(f" && git push origin {rep['default_branch']}")
print(" 判据:必须被拒 —— Not allowed to push to protected branch")
else:
print(" 推一个故意违规的提交 → 本机 hook 必须红、CI 的 gate 必须红")
print(" (直推也拦得住:gate 在 CI 里,不是靠 PR 才有的)")
if __name__ == "__main__":
+1 -1
View File
@@ -176,7 +176,7 @@ cat <<EOF
3) 挂仓库级 Runner + bridge 变量(幂等):
bash /srv/aiball/runner/attach_repo.sh $REPO $RUNNER
4) main 分支保护(网页:Settings → Branches):禁止直接推送 + 必须 PR + 必须 CI 通过
4) (可选)需要"谁都不能直推"时才开分支保护;**默认不开**(正本 §1.3)
5) **反向自检**(正本 §3.2-3):推一个故意违规的提交,确认 gate 红、且 build 没跑