|
3 | 3 | import argparse |
4 | 4 | import json |
5 | 5 | import sys |
| 6 | +from collections.abc import Mapping |
6 | 7 | from pathlib import Path |
7 | | - |
8 | | -from .core import Summary, summarize |
9 | | - |
10 | | - |
11 | | -def _print_table(summary: Summary) -> None: |
12 | | - bs = summary["by_severity"] |
13 | | - rows = [ |
14 | | - ("CRITICAL", bs["CRITICAL"]), |
15 | | - ("HIGH", bs["HIGH"]), |
16 | | - ("MEDIUM", bs["MEDIUM"]), |
17 | | - ("LOW", bs["LOW"]), |
18 | | - ("INFO", bs["INFO"]), |
19 | | - ] |
20 | | - print("\n=== Diff Risk Summary ===") |
21 | | - print(f"Total findings: {summary['total']}") |
22 | | - print("Severity counts:") |
23 | | - w = max(len(r[0]) for r in rows) |
24 | | - for name, cnt in rows: |
25 | | - print(f" {name:<{w}} : {cnt}") |
26 | | - print(f"Worst severity : {summary['worst']}") |
27 | | - print(f"Risk level : {summary['risk_level']}\n") |
28 | | - |
29 | | - |
30 | | -def main(argv: list[str] | None = None) -> int: |
31 | | - p = argparse.ArgumentParser(description="Diff Risk Dashboard (APV JSON -> summary)") |
32 | | - p.add_argument("apv_json", help="Path to ai-patch-verifier JSON") |
33 | | - args = p.parse_args(argv) |
34 | | - data = json.loads(Path(args.apv_json).read_text(encoding="utf-8")) |
35 | | - sm = summarize(data) |
36 | | - _print_table(sm) |
37 | | - return 2 if sm["risk_level"] == "red" else (1 if sm["risk_level"] == "yellow" else 0) |
38 | | - |
39 | | - |
40 | | -if __name__ == "__main__": |
41 | | - sys.exit(main()) |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +from .core import summarize_apv_json |
| 11 | +from .report import to_markdown |
| 12 | + |
| 13 | + |
| 14 | +def _exit_code(risk: str) -> int: |
| 15 | + return {"green": 0, "yellow": 1, "red": 2}.get(risk, 0) |
| 16 | + |
| 17 | + |
| 18 | +def _print_table(summary: Mapping[str, Any]) -> None: |
| 19 | + by = summary.get("by_severity", {}) or {} |
| 20 | + print("Severity\tCount") |
| 21 | + for sev in ["CRITICAL", "HIGH", "MEDIUM", "LOW", "INFO"]: |
| 22 | + print(f"{sev}\t{by.get(sev, by.get(sev.lower(), 0))}") |
| 23 | + print(f"TOTAL\t{summary.get('total', 0)}") |
| 24 | + |
| 25 | + |
| 26 | +def main() -> int: |
| 27 | + p = argparse.ArgumentParser( |
| 28 | + prog="diff_risk_dashboard", description="Diff Risk Dashboard (APV JSON -> summary)" |
| 29 | + ) |
| 30 | + p.add_argument("input", help="Path o texto JSON de ai-patch-verifier") |
| 31 | + p.add_argument( |
| 32 | + "-f", "--format", choices=["table", "json", "md"], default="table", help="Formato de salida" |
| 33 | + ) |
| 34 | + p.add_argument("-o", "--output", default="-", help="Archivo de salida; '-' = stdout") |
| 35 | + p.add_argument( |
| 36 | + "--no-exit-by-risk", action="store_true", help="No ajustar el exit code por nivel de riesgo" |
| 37 | + ) |
| 38 | + args = p.parse_args() |
| 39 | + |
| 40 | + summary: Mapping[str, Any] = summarize_apv_json(args.input) |
| 41 | + out: str | None = None |
| 42 | + if args.format == "json": |
| 43 | + out = json.dumps(summary, indent=2) |
| 44 | + elif args.format == "md": |
| 45 | + out = to_markdown(summary) |
| 46 | + else: |
| 47 | + _print_table(summary) |
| 48 | + |
| 49 | + if out is not None: |
| 50 | + if args.output == "-": |
| 51 | + print(out) |
| 52 | + else: |
| 53 | + Path(args.output).write_text(out, encoding="utf-8") |
| 54 | + print(f"Wrote {args.output}", file=sys.stderr) |
| 55 | + |
| 56 | + risk = str(summary.get("risk", summary.get("risk_level", "green"))) |
| 57 | + return 0 if args.no_exit_by_risk else _exit_code(risk) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": # pragma: no cover |
| 61 | + raise SystemExit(main()) |
0 commit comments