Files
knowledge-governance/scripts/ci_negative_fixtures.sh
T
姚章浩 4f8ee0a8c5 feat(version): 变更记录改固定四类,破坏性变化带发布文档链接
按项目方给的规则重做 CHANGELOG 与检查器(本轮不推、不打 tag、不部署):

- 条目形状:四个自定义必填节(改了什么/兼容性与迁移影响/怎么验收/已知问题)
  → 固定四类 Added/Changed/Fixed/Security;分类可省略,**不许空栏目**;每个版本块
  至少一个非空分类;`[Unreleased]` 段不得写日期。
- 破坏性变化:固定写在 Changed 里、以 `**破坏性**` 开头,**必须链接发布文档**;
  仓内 `docs/….md` 链接**对账**(不存在 ⇒ 红)。写在别的分类里也判红。
- 结构判据补强:非法日期、重复版本、顺序必须严格递减、前导零;围栏代码块里的
  `##`/`###` 不算标题;**块正文以任意二级标题为界**(「## 能力基线(未发布)」这类
  不带方括号的标题原先不是边界,会把后文算成上一节的内容 ⇒ 空栏目被兜成"有内容",
  夹具真的抓到了这个假绿)。
- tag 一致性:远端专属清单 `.ci-remote-tags` → `ls-remote` → 本地 refs 三级来源;
  **没核对远端就不许说校验成功**(CI/预检用 `--require-remote`,拿不到清单即红),
  非正式 tag(如本地 `x`)不当作发布版本、不判红,没有 tag 不判红。
- `0.1.0` 定为**未发布的能力基线**(远端无 tag、无 Release):改成不带方括号的
  「## 能力基线(未发布)」,不补造 tag/发布日期/发布事实。
- 过程记录迁出:审查轮次与命令输出 → `docs/archive/CI_接入留档.md`(原文保留);
  未合并事项 → `docs/BACKLOG.md`;补 `docs/RELEASE_RUNBOOK.md`(破坏性变化的链接目标,
  同时修掉 CHANGELOG 里指向不存在文档的悬空引用)。
- 发版预检:无版本基线时把「首个正式版本号由人指定」列成阻塞项(不再要求先有基线才能跑);
  打印破坏性变化条数;远端 tag 查不到时明说未核对并阻塞;不再硬读已删除的『已知问题』节。
- traps trap-5 口径修正:不是"不用 tag",而是"构建时不要用 git describe 取版本号"。

验证:check_changelog --self-test 全过;反仓夹具 80/0(含五条真改文件的结构负例、
六条只在临时仓库里造 tag 的场景);make verify 15/15 success。
2026-10-13 14:59:35 +08:00

610 lines
33 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 反向验收夹具:**只在隔离副本里跑**。它故意弄坏东西,断言门禁会红;再恢复,断言会绿。
#
# 为什么要有它:一个"永远返回 0 的校验器"比没有校验更糟 —— 它给人一种已经检查过的错觉
# (正本 §0-2)。所以每条判据都要有反例,而且反例要在**隔离副本**里做,
# 绝不往日常 main 上推故意失败的提交(正本 §10)。
#
# 用法(在副本里):
# cp -a <repo> /tmp/kg-neg && cd /tmp/kg-neg && CI_NEGATIVE_FIXTURE=1 bash scripts/ci_negative_fixtures.sh
#
# 两道保险,防止有人在本仓里直接跑:
# 1. 必须显式设 CI_NEGATIVE_FIXTURE=1
# 2. 本仓有 origin 指向真实 Gitea 时拒绝运行(副本里通常没有 origin)
set -uo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
if [ "${CI_NEGATIVE_FIXTURE:-}" != "1" ]; then
echo "❌ 拒绝运行:本脚本会故意弄坏文件。先设 CI_NEGATIVE_FIXTURE=1,并确认自己在隔离副本里。" >&2
exit 2
fi
if git remote get-url origin >/dev/null 2>&1; then
echo "❌ 拒绝运行:这个工作区有 origin 远端($(git remote get-url origin))—— 反向夹具只在无远端的副本里跑。" >&2
exit 2
fi
PY="${PYTHON:-python3}"
pass=0; fail=0
say() { printf '\n\033[1m== %s ==\033[0m\n' "$*"; }
ok() { pass=$((pass+1)); echo " ✅ $1"; }
no() { fail=$((fail+1)); echo " ❌ $1"; }
# expect <期望退出码> <名字> -- <命令...>
expect() {
local want="$1" name="$2"; shift 3
local out; out="$("$@" 2>&1)"; local got=$?
if [ "$got" = "$want" ]; then ok "$name(退出码 $got,符合预期)"
else no "$name:期望退出码 $want,实际 $got"; echo "$out" | tail -12 | sed 's/^/ /'; fi
}
[ -d web/node_modules ] || { echo "先装依赖:npm --prefix web ci" >&2; exit 2; }
say "0. 基线:现在应当全绿"
expect 0 "identity(事件 SHA = 当前 HEAD)" -- env GITHUB_SHA="$(git rev-parse HEAD)" "$PY" scripts/ci_checks.py --only identity
expect 0 "format" -- "$PY" scripts/ci_checks.py --only format
expect 0 "security-secrets" -- "$PY" scripts/ci_checks.py --only security-secrets
say "1. 格式错误必须红(Go / 前端各一条)"
printf 'package main\nfunc main( ){ println("x") }\n' > zz_fixture_bad.go
git add zz_fixture_bad.go
expect 1 "Go 未格式化 ⇒ format 红" -- "$PY" scripts/ci_checks.py --only format
git rm -q --cached zz_fixture_bad.go; rm -f zz_fixture_bad.go
printf 'export function f( x:number ){return x*2}\n' > web/src/zzFixture.ts
git -C "$ROOT" add web/src/zzFixture.ts 2>/dev/null || git add web/src/zzFixture.ts
expect 1 "前端未格式化(基线外的新文件)⇒ format 红" -- "$PY" scripts/ci_checks.py --only format
git rm -q --cached web/src/zzFixture.ts; rm -f web/src/zzFixture.ts
expect 0 "还原后 format 恢复绿" -- "$PY" scripts/ci_checks.py --only format
say "2. 前端测试失败必须让门禁红"
cat > web/src/zzFixture.test.ts <<'TS'
import { describe, expect, it } from 'vitest'
describe('fixture', () => {
it('故意失败', () => { expect(1).toBe(2) })
})
TS
expect 1 "vitest 有用例失败 ⇒ tests-web 红" -- "$PY" scripts/ci_checks.py --only tests-web
rm -f web/src/zzFixture.test.ts
expect 0 "删除后 tests-web 恢复绿" -- "$PY" scripts/ci_checks.py --only tests-web
say "3. 版本与变更记录契约"
cp CHANGELOG.md /tmp/kg-changelog.bak
python3 - <<'PY'
import pathlib
p = pathlib.Path('CHANGELOG.md')
t = p.read_text(encoding='utf-8').rstrip('\n')
# 追加一条"没有日期"的已发布版本标题(结构自检必须抓住)
p.write_text(t + '\n\n## [9.9.9]\n\n### Added\n\n- 没有日期的正式版本条目\n', encoding='utf-8')
PY
"$PY" scripts/ci_checks.py --only contracts > /tmp/kg-nodate.log 2>&1; rc=$?
if [ "$rc" -ne 0 ] && grep -q '缺少日期' /tmp/kg-nodate.log; then
ok "变更记录里的已发布版本缺日期 ⇒ contracts 红(原因就是缺日期)"
else
no "已发布版本缺日期没有判红,或红的原因不是缺日期(rc=$rc)"; tail -8 /tmp/kg-nodate.log | sed 's/^/ /'
fi
cp /tmp/kg-changelog.bak CHANGELOG.md
expect 0 "还原后 contracts 恢复绿" -- "$PY" scripts/ci_checks.py --only contracts
# 构建身份:本仓还没有正式发布过 —— 也必须能产出**开发**身份(不退化成"上一正式版本号")
pycheck() { # pycheck "<名字>" <<'PY' … PY
local name="$1"; local out; out="$("$PY" - 2>&1)"; local rc=$?
if [ "$rc" = 0 ]; then ok "$name"; else no "$name"; echo "$out" | tail -8 | sed 's/^/ /'; fi
}
pycheck "无正式基线时 --identity 给出 dev+g<短SHA>(不是正式版本号)" <<'PY'
import json, re, subprocess
p = subprocess.run(["python3", "scripts/version.py", "--identity"], capture_output=True, text=True)
if p.returncode:
print("❌ --identity 在无基线时失败:", (p.stderr or "").strip()[:200]); raise SystemExit(1)
d = json.loads(p.stdout)
okv = (d["release_version"] is None and d["build_kind"] == "development"
and re.fullmatch(r"dev\+g[0-9a-f]{12}", d["display_version"] or "")
and len(d["commit_sha"]) == 40)
print(("✅ " if okv else "❌ ") + f"显示版本 {d['display_version']}|release_version={d['release_version']}|{d['build_kind']}")
raise SystemExit(0 if okv else 1)
PY
pycheck "没有已发布版本时不许声明发布构建(--identity --release ⇒ rc=2)" <<'PY'
import subprocess
p = subprocess.run(["python3", "scripts/version.py", "--identity", "--release"], capture_output=True, text=True)
okv = p.returncode == 2 and "发布构建" in (p.stderr or "")
print(("✅ " if okv else "❌ ") + f"退出码 {p.returncode}|{(p.stderr or '').strip().splitlines()[:1]}")
raise SystemExit(0 if okv else 1)
PY
# 变异测试①:把契约那一处的注入**整个拿掉** ⇒ 版本用例会 SKIP ⇒ 契约必须红(跳过不算通过)
cp scripts/ci_checks.py /tmp/kg-cichecks.bak
python3 - <<'PY'
import pathlib
p = pathlib.Path('scripts/ci_checks.py')
t = p.read_text(encoding='utf-8')
needle = 'rc1, text = run(ldflags_for(ident))'
assert t.count(needle) == 1, f'契约注入锚点不唯一({t.count(needle)} 处),夹具失效'
p.write_text(t.replace(needle, 'rc1, text = run("-X knowledge-governance/internal/version.Nope=x")', 1),
encoding='utf-8')
PY
"$PY" scripts/ci_checks.py --only contracts > /tmp/kg-skip.log 2>&1; rc=$?
if [ "$rc" -ne 0 ] && grep -q 'SKIP' /tmp/kg-skip.log; then
ok "注入缺失 ⇒ 版本用例被 SKIP ⇒ contracts 红(确认走的是 SKIP 分支)"
else
no "注入缺失没有判红,或没走到 SKIP 分支(rc=$rc)"; tail -8 /tmp/kg-skip.log | sed 's/^/ /'
fi
cp /tmp/kg-cichecks.bak scripts/ci_checks.py
# 变异测试②:把"自称正式版 ⇒ 必须红"那条负例阉割成开发构建 ⇒ contracts 必须红(证明负例不是死的)
python3 - <<'PY'
import pathlib
p = pathlib.Path('scripts/ci_checks.py')
t = p.read_text(encoding='utf-8')
needle = 'rc2, _ = run(ldflags_for(ident, build_kind="release", release_version=bogus))'
assert t.count(needle) == 1, f'负例锚点不唯一({t.count(needle)} 处),夹具失效'
p.write_text(t.replace(needle, 'rc2, _ = run(ldflags_for(ident, build_kind="development"))', 1),
encoding='utf-8')
PY
expect 1 "负例被阉割(不再自称正式版)⇒ contracts 红(说明负例真的在起作用)" -- "$PY" scripts/ci_checks.py --only contracts
cp /tmp/kg-cichecks.bak scripts/ci_checks.py
expect 0 "还原后 contracts 恢复绿" -- "$PY" scripts/ci_checks.py --only contracts
rm -f /tmp/kg-changelog.bak
say "4. 依赖锁与例外文件"
mv web/package-lock.json /tmp/kg-lock.bak
expect 1 "缺 package-lock.json ⇒ deps-install 红(不退化无约束安装)" -- "$PY" scripts/ci_checks.py --only deps-install
mv /tmp/kg-lock.bak web/package-lock.json
mv security/dependency-exceptions.json /tmp/kg-exc.bak
expect 1 "例外登记表缺失 ⇒ security-deps 红(不静默放行)" -- "$PY" scripts/ci_checks.py --only security-deps
mv /tmp/kg-exc.bak security/dependency-exceptions.json
say "5. 合成密钥夹具(无真实秘密)必须被拦住"
python3 - <<'PY'
import pathlib
# 值在运行期拼出来:夹具文件里不留真命中文本
ak = "AKIA" + "Q7ZP2M4XR9TLB6VW"
pathlib.Path('zz_fixture_secret.txt').write_text(f"aws_access_key = \"{ak}\"\n", encoding='utf-8')
PY
git add zz_fixture_secret.txt
expect 1 "合成 AWS AK ⇒ security-secrets 红" -- "$PY" scripts/ci_checks.py --only security-secrets
git rm -q --cached zz_fixture_secret.txt; rm -f zz_fixture_secret.txt
say "6. 检出身份(事件 SHA 错配必须红)"
expect 1 "事件 SHA 与 HEAD 不一致 ⇒ identity 红" -- env GITHUB_SHA="0000000000000000000000000000000000000000" "$PY" scripts/ci_checks.py --only identity
expect 0 "事件 SHA 与 HEAD 一致 ⇒ identity 绿" -- env GITHUB_SHA="$(git rev-parse HEAD)" "$PY" scripts/ci_checks.py --only identity
say "7. 必需检查未运行不得报全绿(全量跑一次,故意让 format 红)"
printf 'package main\nfunc main( ){ println("x") }\n' > zz_fixture_bad.go
git add zz_fixture_bad.go
"$PY" scripts/ci_checks.py > /tmp/kg-neg-gate.log 2>&1; rc=$?
if [ "$rc" -ne 0 ]; then ok "全量门禁非 0(退出码 $rc)"; else no "全量门禁竟然返回 0"; fi
python3 - <<'PY' && ok "报告里后续必需项被记为 not_run,且总体为 failure" || no "报告的 not_run / overall 断言不成立"
import json, sys
d = json.load(open('ci-report.json'))
by = {c['id']: c['status'] for c in d['checks']}
assert by.get('format') == 'failure', by
later = [by.get(k) for k in ('tests-go', 'tests-web', 'contracts', 'security-deps')]
assert all(x == 'not_run' for x in later if x is not None), by
assert d['overall_status'] == 'failure', d['overall_status']
assert any('not_run' in p or '未运行' in p for p in d['problems']), d['problems']
PY
git rm -q --cached zz_fixture_bad.go; rm -f zz_fixture_bad.go
say "8. 上报:通知失败不改变质量结果;汇总不可用必须非 0"
python3 - "$(git rev-parse HEAD)" <<'PYPY'
import json, sys, pathlib
sys.path.insert(0, 'scripts')
from ci_checks import CHECK_MANIFEST, SUCCESS
sha = sys.argv[1]
# 夹具报告必须是**合规**的(真清单 + 完整 SHA + run_mode=full):上报脚本现在会独立核对,
# 不核对就会把"能跑通"误当成"结论可信"。
checks = [{"id": cid, "status": SUCCESS, "reason": "ok", "duration_s": 1, "required_in": list(req)}
for cid, req in CHECK_MANIFEST.items()]
rep = {"schema": "ci-report/1", "profile": "ci", "run_mode": "full", "commit_sha": sha,
"event_sha": sha, "overall_status": SUCCESS, "policy_revision": "fixture",
"manifest": {cid: list(req) for cid, req in CHECK_MANIFEST.items()},
"checks": checks, "duration_s": 2, "problems": []}
pathlib.Path("/tmp/kg-report-ok.json").write_text(json.dumps(rep, ensure_ascii=False), encoding="utf-8")
PYPY
CI_BRIDGE_RESOLVED="http://127.0.0.1:1" "$PY" .gitea/scripts/ci_report.py --report /tmp/kg-report-ok.json > /tmp/kg-notify.log 2>&1
rc=$?
if [ "$rc" = 0 ]; then ok "通知不可达时退出码仍是 0(质量结论不变)"; else no "通知失败把退出码带成了 $rc"; sed 's/^/ /' /tmp/kg-notify.log | tail -5; fi
if grep -q '"notification_status": "failed' /tmp/kg-report-ok.json; then ok "通知失败被单独标注(notification_status=failed)"; else no "没看到 notification_status 被标注"; fi
expect 1 "汇总报告缺失 ⇒ 上报脚本非 0(不能报绿)" -- "$PY" .gitea/scripts/ci_report.py --report /tmp/kg-does-not-exist.json
say "9. 必需/可选测试的区分(必需用例被跳过必须红)"
cp scripts/test-scope.json /tmp/kg-scope.bak
python3 - <<'PYPY'
import json, pathlib
p = pathlib.Path('scripts/test-scope.json')
d = json.loads(p.read_text(encoding='utf-8'))
# 删掉"整个 postgres 包可跳过"这条声明:该包里的 57 个跳过随即变成"必需用例被跳过"
d['optional'] = [e for e in d['optional']
if e.get('package') != 'knowledge-governance/internal/platform/postgres']
p.write_text(json.dumps(d, ensure_ascii=False, indent=2), encoding='utf-8')
PYPY
expect 1 "未声明的必需用例被跳过 ⇒ tests-go 红" -- "$PY" scripts/ci_checks.py --only tests-go
cp /tmp/kg-scope.bak scripts/test-scope.json
expect 0 "恢复声明后 tests-go 回到绿" -- "$PY" scripts/ci_checks.py --only tests-go
mv scripts/test-scope.json /tmp/kg-scope2.bak
expect 1 "测试范围声明文件缺失 ⇒ tests-go 红(不许默认放行)" -- "$PY" scripts/ci_checks.py --only tests-go
mv /tmp/kg-scope2.bak scripts/test-scope.json
say "10. 前端 pending/todo 必须红"
printf "import { describe, it } from 'vitest'\ndescribe('fixture', () => { it.todo('故意待办') })\n" > web/src/zzPending.test.ts
expect 1 "有 todo 待跑用例 ⇒ tests-web 红" -- "$PY" scripts/ci_checks.py --only tests-web
rm -f web/src/zzPending.test.ts
expect 0 "删除后恢复绿" -- "$PY" scripts/ci_checks.py --only tests-web
say "11. 上报前必须独立核对报告(不能只看 overall_status)"
python3 - "$(git rev-parse HEAD)" <<'PYPY'
import json, sys, pathlib
sys.path.insert(0, 'scripts')
from ci_checks import CHECK_MANIFEST, SUCCESS
sha = sys.argv[1]
# 合规夹具必须用**真清单**造:报告里的清单与实现不一致时,上报脚本本来就应该拦下来
checks = [{"id": cid, "status": SUCCESS, "reason": "ok", "duration_s": 1,
"required_in": list(req)} for cid, req in CHECK_MANIFEST.items()]
base = {"schema": "ci-report/1", "profile": "ci", "run_mode": "full", "commit_sha": sha,
"event_sha": sha, "overall_status": SUCCESS,
"manifest": {cid: list(req) for cid, req in CHECK_MANIFEST.items()}, "checks": checks}
cases = {
"ok": base,
"short-sha": {**base, "commit_sha": sha[:12]},
"missing-check": {**base, "checks": [c for c in checks if c["id"] != "tests-go"]},
"dup-check": {**base, "checks": checks + [checks[0]]},
"unknown-check": {**base, "checks": checks + [{"id": "zz", "status": SUCCESS}]},
"profile-lie": {**base, "profile": "local"},
"manifest-drift": {**base, "manifest": {k: list(v) for k, v in CHECK_MANIFEST.items() if k != "format"}},
}
for name, rep in cases.items():
pathlib.Path(f"/tmp/kg-rep-{name}.json").write_text(json.dumps(rep, ensure_ascii=False), encoding="utf-8")
PYPY
expect 0 "合规报告 ⇒ 上报脚本 0" -- env GITHUB_ACTIONS=1 GITHUB_SHA="$(git rev-parse HEAD)" "$PY" .gitea/scripts/ci_report.py --no-post --report /tmp/kg-rep-ok.json
for c in short-sha missing-check dup-check unknown-check profile-lie manifest-drift; do
expect 1 "报告缺陷 $c ⇒ 上报脚本非 0(不能报绿)" -- env GITHUB_ACTIONS=1 GITHUB_SHA="$(git rev-parse HEAD)" "$PY" .gitea/scripts/ci_report.py --no-post --report /tmp/kg-rep-$c.json
done
say "12. 提交闸门用「本次范围」,不用「最近 N 条」"
T="$(mktemp -d)"
(
cd "$T" || exit 1
git init -q -b main
export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t GIT_COMMITTER_EMAIL=t@t
mkdir -p "$T/no-hooks"
export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.hooksPath GIT_CONFIG_VALUE_0="$T/no-hooks"
git commit -q --allow-empty -F - <<'M'
feat(a): 第一条合规
M
git commit -q --allow-empty -F - <<'M'
update stuff
M
BAD="$(git rev-parse HEAD)"
git commit -q --allow-empty -F - <<'M'
fix(b): 第三条合规
M
"$PY" "$ROOT/.gitea/scripts/ci_gate.py" --range "$BAD..HEAD" > /tmp/kg-range1.log 2>&1
echo "clean=$?"
"$PY" "$ROOT/.gitea/scripts/ci_gate.py" --range "HEAD~2..HEAD" > /tmp/kg-range2.log 2>&1
echo "dirty=$?"
) > /tmp/kg-range.log 2>&1
grep -q '^clean=0$' /tmp/kg-range.log && ok "干净范围通过(历史里有违规也不误伤 ⇒ 不是固定最近 N 条)" || { no "干净范围竟然判红"; sed 's/^/ /' /tmp/kg-range.log; }
grep -q '^dirty=1$' /tmp/kg-range.log && ok "范围含违规提交 ⇒ 判红" || { no "含违规的范围没有判红"; sed 's/^/ /' /tmp/kg-range.log; }
rm -rf "$T"
say "13. 通用模板/生成器:调用同一门禁入口、默认不生成 CD"
STD="${ENG_STANDARDS_DIR:-$HOME/eng-standards}"
GEN="$(mktemp -d)"
if [ -x "$STD/scripts/new-project.sh" ]; then
bash "$STD/scripts/new-project.sh" "$GEN/svc" Weknora/svc runner-x > /tmp/kg-gen.log 2>&1
if [ -f "$GEN/svc/.gitea/workflows/deploy-production.yml" ]; then no "生成器默认生成了 CD 工作流"; else ok "生成器默认不生成 CD 工作流"; fi
[ -f "$GEN/svc/scripts/ci_checks.py" ] && ok "生成器带了权威门禁实现" || no "生成器没带门禁实现"
[ -f "$GEN/svc/scripts/verify.sh" ] && ok "生成器带了门禁入口 shim" || no "生成器没带门禁入口"
if grep -q 'gofmt -l\|npm run build\|go vet' "$GEN/svc/.gitea/workflows/build.yml"; then
no "模板工作流里还留着内联的语言检查(第二套逻辑)"
else ok "模板工作流只调用门禁入口(没有内联第二套)"; fi
if grep -q '^ deploy' "$GEN/svc/.gitea/workflows/build.yml"; then no "build.yml 里还有 deploy 作业"; else ok "build.yml 里没有 deploy 作业"; fi
else
no "找不到规范仓的生成器(设 ENG_STANDARDS_DIR 指向它)"
fi
rm -rf "$GEN"
say "14. 第二轮修正:预览发布顺序(真实 ci_preview.py + 假 bridge 数请求)"
if python3 scripts/ci_preview_negative_test.py >/tmp/kg-preview-neg.log 2>&1; then
ok "预览:坏报告一个 publish 请求都不发、合规报告正好发 1 次(10 分支)"
else
no "预览发布负例失败,详见 /tmp/kg-preview-neg.log"
fi
say "15. 第二轮修正:提交范围取法(11 分支)+ 显示范围异常"
if python3 .gitea/scripts/ci_gate.py --self-test >/tmp/kg-scope.log 2>&1; then
ok "提交范围:截断/空范围/新分支/手工运行/shallow 全部分支符合预期"
else
no "提交范围自检失败,详见 /tmp/kg-scope.log"
fi
say "16. 第二轮修正:报告核对可单独调用(--verify-only)"
RD="$(mktemp -d)"
python3 - "$RD/rep.json" <<'PY'
import json, sys
sys.path.insert(0, "scripts")
import ci_checks
good = {"schema": "ci-report/1", "profile": "ci", "run_mode": "full",
"commit_sha": "a" * 40, "event_sha": "a" * 40,
"manifest": {c: list(r) for c, r in ci_checks.CHECK_MANIFEST.items()},
"checks": [{"id": c, "status": "success", "reason": ""} for c in ci_checks.CHECK_MANIFEST],
"overall_status": "success"}
json.dump(good, open(sys.argv[1], "w"))
json.dump({**good, "checks": [c for c in good["checks"] if c["id"] != "tests-go"]}, open(sys.argv[1] + ".missing", "w"))
PY
GITHUB_ACTIONS=true GITHUB_SHA=$(printf 'a%.0s' {1..40}) python3 .gitea/scripts/ci_report.py --verify-only --report "$RD/rep.json" >/dev/null 2>&1 \
&& ok "合规报告:--verify-only 退出 0(可采信)" || no "合规报告竟然核不过"
GITHUB_ACTIONS=true GITHUB_SHA=$(printf 'a%.0s' {1..40}) python3 .gitea/scripts/ci_report.py --verify-only --report "$RD/rep.json.missing" >/dev/null 2>&1 \
&& no "缺项报告:--verify-only 竟然退出 0" || ok "缺项报告:--verify-only 非 0(不可采信 ⇒ 预览不会发)"
rm -rf "$RD"
say "17. 第二轮修正:检出脚本的协议保留"
if [ "$(GITHUB_SERVER_URL='https://gitea.zheromiku.top' GITHUB_REPOSITORY='Weknora/knowledge-governance' bash .gitea/scripts/ci_checkout.sh --print-remote 2>/dev/null)" = "https://gitea.zheromiku.top/Weknora/knowledge-governance.git" ]; then
ok "https 服务端 ⇒ remote 仍是 https(没有降级)"
else
no "https 被降级或拼接不对"
fi
if GITHUB_SERVER_URL='ftp://nope' GITHUB_REPOSITORY='Weknora/knowledge-governance' bash .gitea/scripts/ci_checkout.sh --print-remote >/dev/null 2>&1; then
no "未知协议竟然被接受"
else
ok "未知协议被拒绝"
fi
say "18. 第二轮修正:合并只认明确策略(真实 merge_pr + 假 Gitea)"
if [ -f "$STD/scripts/adopt-existing.py" ]; then
if python3 "$STD/scripts/adopt-existing.py" --self-test-chain >/tmp/kg-merge-chain.log 2>&1; then
ok "合并调用链:13 分支里只有'策略齐+必需 context 全 success+完整 SHA+base/state 对'才发 merge"
else
no "合并调用链负例失败,详见 /tmp/kg-merge-chain.log"
fi
grep -q "required-contexts" "$STD/scripts/adopt-existing.py" \
&& ok "接入器只从明确策略取必需 context(不再用观察到的状态)" \
|| no "接入器里找不到策略来源"
else
no "找不到规范仓的接入器"
fi
say "19. 第二轮修正:生成器带齐新脚本与策略文件"
if [ -x "$STD/scripts/new-project.sh" ]; then
GEN2="$(mktemp -d)"
bash "$STD/scripts/new-project.sh" "$GEN2/svc" Weknora/svc2 runner-y >/tmp/kg-gen2.log 2>&1
for f in .gitea/scripts/ci_preview.py .gitea/scripts/ci_checkout.sh .gitea/required-contexts.txt scripts/check_secrets.py scripts/test-scope.json; do
[ -f "$GEN2/svc/$f" ] && ok "生成器落了 $f" || no "生成器没落 $f"
done
if grep -qE '^[[:space:]]*build / build' "$GEN2/svc/.gitea/required-contexts.txt"; then
no "生成器预填了猜出来的 context(会把没跑过必需检查的 PR 放过去)"
else
ok "策略文件故意留空(填之前合并会被拒)"
fi
rm -rf "$GEN2"
fi
say "20. 完整调用链:空工作区 → bootstrap → 仓内脚本 → 提交范围闸门"
if python3 scripts/ci_chain_fixture.py >/tmp/kg-chain.log 2>&1; then
ok "空工作区能启动:bootstrap 检出到事件完整 SHA、协议保留、脚本检出后才就位(12 项断言)"
else
no "完整调用链夹具失败,详见 /tmp/kg-chain.log"; tail -20 /tmp/kg-chain.log | sed 's/^/ /'
fi
if grep -q '没有那个文件或目录\|127' /tmp/kg-chain.log && grep -q '空工作区里调用仓内脚本返回 127' /tmp/kg-chain.log; then
ok "旧写法(第一步直接调用仓内脚本)的 127 已被复现并钉住"
fi
say "21. 通用模板:Python 必须锁定依赖 + 隔离环境"
# ① 自检必须覆盖"锁定/隔离"这一族判据(不是只看文本里有没有 --system —— 那会被说明文字误伤)
if python3 "$STD/templates/ci-gate.py" --self-test >/tmp/kg-pylock-self.log 2>&1 \
&& grep -q "Python 锁定/隔离" /tmp/kg-pylock-self.log; then
ok "通用门禁自检覆盖了「Python 锁定/隔离(缺锁失败、从不用 --system)」"
else
no "通用门禁自检没有覆盖 Python 锁定/隔离"; tail -3 /tmp/kg-pylock-self.log | sed 's/^/ /'
fi
# ② 真跑:只有 requirements.txt、没有 uv.lock ⇒ deps-install 红,并要求先生成锁文件
PYF="$(mktemp -d)"; mkdir -p "$PYF/scripts"
cp "$STD/templates/ci-gate.py" "$PYF/scripts/ci_checks.py"
cp "$STD/templates/check-secrets.py" "$PYF/scripts/check_secrets.py"
printf '[project]\nname = "fixture"\nversion = "0.1.0"\n' > "$PYF/pyproject.toml"
printf 'pytest==8.0.0\n' > "$PYF/requirements.txt"
printf '{"optional": []}' > "$PYF/scripts/test-scope.json"
( cd "$PYF" && git init -q -b main && env -u GITHUB_SHA python3 scripts/ci_checks.py --only deps-install ) >/tmp/kg-py-lock.log 2>&1
if [ $? -ne 0 ] && grep -q 'uv lock' /tmp/kg-py-lock.log; then
ok "只有 requirements.txt、没有 uv.lock ⇒ deps-install 红,并要求先生成锁文件"
else
no "缺锁时没有明确失败(或提示里没有'先生成锁文件')"; tail -5 /tmp/kg-py-lock.log | sed 's/^/ /'
fi
# ③ 真跑:生成 uv.lock 之后 ⇒ 绿,且环境是**项目内 .venv**(隔离,不是系统解释器)
if command -v uv >/dev/null 2>&1; then
( cd "$PYF" && uv lock >/dev/null 2>&1 )
( cd "$PYF" && env -u GITHUB_SHA python3 scripts/ci_checks.py --only deps-install ) >/tmp/kg-py-lock2.log 2>&1
# 断言看的是**这一项本身**(报告里 deps-install=success),不是进程总退出码 ——
# 夹具仓故意没有提交,总退出码会因为"commit_sha 不是完整 40 位"而红,那是另一回事。
if grep -q '按锁文件安装完成' /tmp/kg-py-lock2.log && grep -q 'deps-install=success' /tmp/kg-py-lock2.log; then
ok "有 uv.lock ⇒ 按锁文件安装成功(uv sync --frozen),deps-install=success"
else
no "有 uv.lock 时 deps-install 没通过"; tail -6 /tmp/kg-py-lock2.log | sed 's/^/ /'
fi
ISO="$( cd "$PYF" && uv run --frozen python -c "import sys; print(sys.prefix != sys.base_prefix)" 2>/dev/null )"
if [ "$ISO" = "True" ]; then
ok "依赖装在项目内 .venv(sys.prefix != sys.base_prefix ⇒ 没装进系统环境)"
else
no "Python 环境没有隔离(得到 $ISO)"
fi
else
no "本机没有 uv:无法验证锁定/隔离(不许当通过)"
fi
rm -rf "$PYF"
say "22. 版本记录:CHANGELOG 结构与正式 tag 一致性"
CLK="$PY scripts/check_changelog.py"
# ① 正例:本仓现在的 CHANGELOG 必须合规;没有正式 tag 也不判红
expect 0 "CHANGELOG 结构合规(四类小节、无空栏目)" -- $CLK
expect 0 "没有正式 tag ⇒ 不判红(开发版本不打 tag 是正常的)" -- $CLK --tags
# ② 检查器自检:非法日期/自创栏目/空栏目/重复/顺序倒置/前导零/破坏性无链接/轻量 tag/未核对远端 逐条会红
if $PY scripts/check_changelog.py --self-test >/tmp/kg-cl-self.log 2>&1; then
ok "CHANGELOG 检查的 --self-test 全过(夹具里逐条证明会红)"
else
no "CHANGELOG 检查自检失败"; tail -6 /tmp/kg-cl-self.log | sed 's/^/ /'
fi
# ③ 真改本仓 CHANGELOG(五种形状各判一次,改完立刻恢复)
cp CHANGELOG.md /tmp/kg-changelog.bak
mut() { # mut <名字> <期望 grep> <期望退出码>;变异脚本从 stdin 进
local name="$1" want="$2" want_rc="$3"
if ! "$PY" - >/tmp/kg-mut-py.log 2>&1; then
no "$name:变异脚本失败(锚点不在了?)"; tail -3 /tmp/kg-mut-py.log | sed 's/^/ /'
cp /tmp/kg-changelog.bak CHANGELOG.md; return
fi
$CLK >/tmp/kg-cl-mut.log 2>&1; local rc=$?
if [ "$rc" = "$want_rc" ] && grep -q "$want" /tmp/kg-cl-mut.log; then ok "$name"
else no "$name(rc=$rc,期望 $want_rc 且含 [$want])"; tail -6 /tmp/kg-cl-mut.log | sed 's/^/ /'; fi
cp /tmp/kg-changelog.bak CHANGELOG.md
}
mut "自创栏目(### 已知问题)⇒ 红" "不属于允许的四类" 1 <<'PY'
import pathlib, re
p = pathlib.Path("CHANGELOG.md"); t = p.read_text(encoding="utf-8")
# ⚠️ 锚点必须是**行首真标题**:顶部说明里也出现过 "`### Added`" 这类行内引用,
# 用子串替换会改到说明文字(看起来"变异了"、结构没动)——夹具第一稿就这么假通过过。
assert len(re.findall(r"^### Fixed$", t, re.M)) == 1, "行首真标题 ### Fixed 不唯一"
t2 = re.sub(r"^### Fixed$", "### 已知问题", t, count=1, flags=re.M)
assert t2 != t, "没改成"
p.write_text(t2, encoding="utf-8")
PY
mut "空栏目(清空 Security 内容)⇒ 红" "是空栏目" 1 <<'PY'
import pathlib, re
p = pathlib.Path("CHANGELOG.md"); t = p.read_text(encoding="utf-8")
assert len(re.findall(r"^### Security$", t, re.M)) == 1, "行首真标题 ### Security 不唯一"
t2 = re.sub(r"(^### Security\n(?:\n)?)(?:- [^\n]*\n)+", r"\1", t, count=1, flags=re.M)
assert t2 != t, "没清掉 Security 的内容"
p.write_text(t2, encoding="utf-8")
PY
mut "破坏性变化没带发布文档链接 ⇒ 红" "没有链接" 1 <<'PY'
import pathlib, re
p = pathlib.Path("CHANGELOG.md"); t = p.read_text(encoding="utf-8")
assert len(re.findall(r"^### Changed$", t, re.M)) == 1, "行首真标题 ### Changed 不唯一"
t2 = re.sub(r"^### Changed$", "### Changed\n\n- **破坏性**:某接口下线,升级步骤见文档(故意不写链接)",
t, count=1, flags=re.M)
assert t2 != t, "没改成"
p.write_text(t2, encoding="utf-8")
PY
mut "破坏性变化链接的文档不存在 ⇒ 红" "链接的发布文档不存在" 1 <<'PY'
import pathlib, re
p = pathlib.Path("CHANGELOG.md"); t = p.read_text(encoding="utf-8")
assert len(re.findall(r"^### Changed$", t, re.M)) == 1, "行首真标题 ### Changed 不唯一"
t2 = re.sub(r"^### Changed$", "### Changed\n\n- **破坏性**:某接口下线,见 [迁移步骤](docs/NO_SUCH_DOC.md)",
t, count=1, flags=re.M)
assert t2 != t, "没改成"
p.write_text(t2, encoding="utf-8")
PY
mut "版本号带前导零 ⇒ 红" "不合法" 1 <<'PY'
import pathlib
p = pathlib.Path("CHANGELOG.md"); t = p.read_text(encoding="utf-8").rstrip("\n")
p.write_text(t + "\n\n## [01.0.0] - 2026-03-02\n\n### Fixed\n\n- x\n", encoding="utf-8")
PY
rm -f /tmp/kg-changelog.bak
expect 0 "本仓 CHANGELOG 恢复后仍是绿的" -- $CLK
# ④ tag 一致性与远端核对:**只在临时仓库里造 tag**,绝不污染本仓(正本 §6.6 注)
pycheck "tag 一致性与远端核对(临时仓库内 6 个场景)" <<'PY'
import os, pathlib, shutil, subprocess, tempfile
SAMPLE = """# Changelog
## [Unreleased]
### Added
- 还没发布
## [0.1.0] - 2026-10-10
### Changed
- **破坏性**:接口 `/old` 下线,升级步骤见 [发布说明](docs/RELEASE_RUNBOOK.md)
### Fixed
- 修了 X
"""
def git(d, *args):
return subprocess.run(["git", *args], cwd=d, capture_output=True, text=True)
def seed(d, changelog=SAMPLE):
(d / "scripts").mkdir(parents=True)
(d / "docs").mkdir()
shutil.copy("scripts/check_changelog.py", d / "scripts/check_changelog.py")
(d / "docs" / "RELEASE_RUNBOOK.md").write_text("# 发布说明\n", encoding="utf-8")
(d / "CHANGELOG.md").write_text(changelog, encoding="utf-8")
(d / "no-hooks").mkdir(exist_ok=True)
os.environ.update({"GIT_AUTHOR_NAME": "f", "GIT_AUTHOR_EMAIL": "f@f",
"GIT_COMMITTER_NAME": "f", "GIT_COMMITTER_EMAIL": "f@f",
"GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": "core.hooksPath",
"GIT_CONFIG_VALUE_0": str(d / "no-hooks")})
git(d, "init", "-q", "-b", "main")
git(d, "add", "-A")
git(d, "commit", "-q", "-m", "chore: 夹具提交")
git(d, "remote", "add", "origin", str(d / "nowhere.git")) # 没有远端 ⇒ 只能看本地 tag
def clk(d, *args):
return subprocess.run(["python3", str(d / "scripts/check_changelog.py"), *args],
cwd=d, capture_output=True, text=True)
cases = []
with tempfile.TemporaryDirectory() as tmp:
r = pathlib.Path(tmp) / "r"; r.mkdir(); seed(r)
cases.append(("无正式 tag ⇒ 绿", clk(r, "--tags").returncode == 0))
git(r, "tag", "-a", "v0.1.0", "-m", "release 0.1.0")
cases.append(("注释 tag 与提交内版本一致 ⇒ 绿", clk(r, "--tags").returncode == 0))
git(r, "tag", "-a", "v9.9.9", "-m", "release 9.9.9")
cases.append(("tag 与提交内版本不符 ⇒ 红", clk(r, "--tags").returncode == 1))
git(r, "tag", "-d", "v9.9.9")
git(r, "tag", "v0.2.0") # 轻量 tag
cases.append(("轻量 tag ⇒ 红", clk(r, "--tags").returncode == 1))
git(r, "tag", "-d", "v0.2.0")
p2 = clk(r, "--tags", "--require-remote")
cases.append(("拿不到远端清单且 --require-remote ⇒ 红(不许声称已核验)",
p2.returncode == 1 and "没核对远端" in (p2.stdout + p2.stderr)))
r2 = pathlib.Path(tmp) / "r2"; r2.mkdir()
seed(r2, SAMPLE.replace("## [Unreleased]", "## [Unreleased] - 2026-01-01"))
cases.append(("未发布段写了日期 ⇒ 红", clk(r2, "--tags").returncode == 1))
bad = [n for n, okv in cases if not okv]
for n, okv in cases:
print(("✅ " if okv else "❌ ") + n)
raise SystemExit(1 if bad else 0)
PY
say "23. 发版预检:只读、不给可发结论时不越界"
if $PY scripts/release_preflight.py --self-test >/tmp/kg-pf-self.log 2>&1; then
ok "预检自检全过(写类 git 命令与 POST/PUT/DELETE/PATCH 在预检路径上被拒)"
else
no "预检自检失败"; tail -6 /tmp/kg-pf-self.log | sed 's/^/ /'
fi
$PY scripts/release_preflight.py >/tmp/kg-pf.log 2>&1; rc=$?
if [ "$rc" -ne 0 ] && grep -q "现在\*\*不能\*\*发" /tmp/kg-pf.log; then
ok "工作区脏时预检**不给可发结论**(退出码 $rc)"
else
no "脏工作区下预检竟然给了可发结论(退出码 $rc)"; tail -6 /tmp/kg-pf.log | sed 's/^/ /'
fi
if grep -q "还没有已发布版本:首个正式版本号必须由人显式指定" /tmp/kg-pf.log; then
ok "没有版本基线时,预检把「首个版本号由人指定」列成阻塞项(不编一个版本号就发)"
else
no "预检没有把「首个版本号由人指定」列成阻塞项"; tail -6 /tmp/kg-pf.log | sed 's/^/ /'
fi
$PY scripts/release_preflight.py --no-api >/tmp/kg-pf-noapi.log 2>&1; rc=$?
if [ "$rc" -ne 0 ] && grep -q "未核对" /tmp/kg-pf-noapi.log; then
ok "显式不核对远端(--no-api)⇒ 不给可发结论,且明说「未核对」(退出码 $rc)"
else
no "不核对远端却给了可发结论(退出码 $rc)"; tail -6 /tmp/kg-pf-noapi.log | sed 's/^/ /'
fi
if grep -qE "git tag -a|--data-binary|POST" /tmp/kg-pf.log; then
no "预检输出里出现了写操作迹象"
else
ok "预检输出里没有任何写操作(只列事实与阻塞项)"
fi
# 预检必须**真的没打过 tag**:跑完之后不该多出任何 v* tag
if [ -z "$(git tag -l 'v*')" ]; then
ok "跑完预检后本仓没有多出任何正式 tag(无副作用)"
else
no "预检留下了 tag:$(git tag -l 'v*' | tr '\n' ' ')"
fi
printf '\n\033[1m════ 反向验收汇总 ════\033[0m\n ✅ 通过 %d | ❌ 失败 %d\n' "$pass" "$fail"
[ "$fail" -eq 0 ] || exit 1