61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
||
"""Validate that maintainer-radar saves a usable UTF-8 Markdown report."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import re
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
|
||
REQUIRED_MARKERS = (
|
||
"# 维护者值班摘要",
|
||
"**队列事实:**",
|
||
"**判定依据:**",
|
||
"## PR #",
|
||
"**响应 SLA:**",
|
||
"**等待方:**",
|
||
"**Reviewer 负载:**",
|
||
"**责任停滞:**",
|
||
"**安全运营优先级:**",
|
||
"**维护动作:**",
|
||
"MR-",
|
||
)
|
||
|
||
|
||
def validate_report(text: str) -> list[str]:
|
||
errors: list[str] = []
|
||
if "\ufffd" in text or "\x00" in text or "\x1b" in text:
|
||
errors.append("report contains invalid encoding or ANSI control characters")
|
||
for marker in REQUIRED_MARKERS:
|
||
if marker not in text:
|
||
errors.append(f"missing radar report marker: {marker}")
|
||
cjk_count = len(re.findall(r"[\u3400-\u9fff]", text))
|
||
if cjk_count < 60:
|
||
errors.append(f"radar narrative is incomplete: found {cjk_count} CJK characters, need at least 60")
|
||
return errors
|
||
|
||
|
||
def main() -> int:
|
||
parser = argparse.ArgumentParser(description="Validate a saved maintainer-radar report.")
|
||
parser.add_argument("--report", required=True, type=Path)
|
||
args = parser.parse_args()
|
||
try:
|
||
text = args.report.read_text(encoding="utf-8", errors="strict")
|
||
except (OSError, UnicodeError) as exc:
|
||
print(f"radar report validation failed: {exc}", file=sys.stderr)
|
||
return 2
|
||
errors = validate_report(text)
|
||
if errors:
|
||
print("radar report validation failed:", file=sys.stderr)
|
||
for error in errors:
|
||
print(f"- {error}", file=sys.stderr)
|
||
return 1
|
||
print(f"radar report validation passed: {args.report.resolve()}")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|