Files
eng-standards-main/templates/ci_report.py
T
姚章浩 cb5985ff9e feat(apply): 应用机制 + 新工程生成器,让规范默认生效
问题:规范此前只是文档,只有"有人去读"一个渠道 —— 实测 7 个仓 0 个有
AGENTS.md 指针、分支保护全空,起新工程时不会自动带上规范。

落地三件:
- scripts/new-project.sh:一条命令生成 9 个默认合规文件(提交闸门、CI、
  本地门禁、指针、提交规范 docs)。已端到端测过:空目录成功、非空拒绝、
  生成物 YAML 合法、无构建入口时门禁 exit 1(不静默通过)、默认分支 main。
- templates/ 收进 ci_gate.py / ci_report.py 的逐字节副本(cmp 校验通过),
  正本仓自此自包含,生成器不再依赖别的仓。
- 05-应用机制.md:写清 Hermes 的实际注入机制与两个必须避开的坑
  (每会话只加载一个上下文文件且有 20k 上限;不要把规则放 ~/.hermes/AGENTS.md
  —— 它只在 cwd 恰好是 ~/.hermes 时加载)。
- README / 03 加索引与交叉引用。

顺带修一个自己踩的坑:生成出来的 scripts/verify.sh 日志夹具只建不删,
正是正本 §3.2-5 自己写的陷阱($TMPDIR 会攒到几万条)。改成成功时删除、
失败时保留并打印路径。
2026-10-10 14:44:50 +08:00

83 lines
2.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""把 CI 结果上报给主机侧 ci-bridge(bridge 负责落报告页 + 推微信)。"""
import json
import os
import subprocess
import urllib.request
def rd(path, default=""):
try:
with open(path, encoding="utf-8") as fh:
return fh.read().strip()
except Exception:
return default
def git(*args):
try:
return subprocess.run(["git"] + list(args), capture_output=True,
text=True).stdout.strip()
except Exception:
return ""
def main():
repo = os.environ.get("GITHUB_REPOSITORY", "unknown/unknown")
sha = rd("/tmp/ci-sha") or git("rev-parse", "HEAD")
branch = os.environ.get("GITHUB_REF_NAME") or \
os.environ.get("GITHUB_REF", "").replace("refs/heads/", "")
gate = rd("/tmp/ci-gate-status", "skipped")
build = rd("/tmp/ci-build-status", "failure")
status = "success" if (gate == "success" and build == "success") else "failure"
log = rd("/tmp/ci-build-tail")
if gate != "success":
log = "提交信息规范校验未通过\n" + log
try:
count = int(rd("/tmp/ci-commits-count", "1") or 1)
except Exception:
count = 1
rep = {
"repo": repo,
"sha": sha,
"branch": branch,
"actor": os.environ.get("GITHUB_ACTOR", ""),
"status": status,
"commit_subject": git("log", "-1", "--format=%s"),
"commit_count": count,
"duration_s": rd("/tmp/ci-duration", "?"),
"preview_url": rd("/tmp/ci-preview-url"),
"log": log[-3500:],
"steps": [
{"name": "提交信息规范校验", "status": gate},
{"name": "编译与自检", "status": build},
],
}
if rep["preview_url"]:
print("\U0001F310 网页预览: %s" % rep["preview_url"])
print("结果: %s | 构建: %s | 提交闸门: %s | 耗时: %ss"
% (status, build, gate, rep["duration_s"]))
base = os.environ.get("CI_BRIDGE_RESOLVED", "").rstrip("/")
if not base:
print("跳过上报:未找到可达的 ci-bridge")
return 0
data = json.dumps(rep, ensure_ascii=False).encode("utf-8")
req = urllib.request.Request(
base + "/api/ci/report", data=data, method="POST",
headers={"Content-Type": "application/json",
"X-CI-Token": os.environ.get("BRIDGE_TOKEN", "")})
try:
with urllib.request.urlopen(req, timeout=30) as resp:
print("\u2705 已上报 ci-bridge:", resp.read().decode()[:300])
except Exception as e:
print("\u274c 上报 ci-bridge 失败:", e)
return 0
if __name__ == "__main__":
raise SystemExit(main())