|
1 | 1 | from __future__ import annotations |
2 | | -from typing import Dict, Any |
| 2 | + |
3 | 3 | import json |
4 | | -from collections import Counter |
5 | | - |
6 | | -def summarize_apv_json(path: str) -> Dict[str, Any]: |
7 | | - ''' |
8 | | - Expect a JSON array or object containing findings with a 'predicted_risk' |
9 | | - field in {'low','medium','high'} (interface compatible with ai-patch-verifier output). |
10 | | - ''' |
11 | | - with open(path, "r", encoding="utf-8") as f: |
12 | | - data = json.load(f) |
13 | | - |
14 | | - items = data if isinstance(data, list) else data.get("findings", []) |
15 | | - risks = [str(i.get("predicted_risk", "")).lower() for i in items] |
16 | | - counts = Counter(risks) |
17 | | - total = sum(counts.values()) |
18 | | - return { |
19 | | - "total": total, |
20 | | - "by_severity": { |
21 | | - "high": counts.get("high", 0), |
22 | | - "medium": counts.get("medium", 0), |
23 | | - "low": counts.get("low", 0), |
24 | | - }, |
| 4 | +from collections.abc import Iterable |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any, Literal, TypedDict, cast |
| 7 | + |
| 8 | +Severity = Literal["CRITICAL", "HIGH", "MEDIUM", "LOW", "INFO"] |
| 9 | + |
| 10 | + |
| 11 | +class Finding(TypedDict, total=False): |
| 12 | + severity: str |
| 13 | + predicted_risk: str |
| 14 | + title: str |
| 15 | + score: float |
| 16 | + |
| 17 | + |
| 18 | +class Summary(TypedDict): |
| 19 | + total: int |
| 20 | + by_severity: dict[str, int] # incluye claves lower y UPPER |
| 21 | + worst: Severity |
| 22 | + risk_level: Literal["red", "yellow", "green"] |
| 23 | + |
| 24 | + |
| 25 | +_SEV_ORDER: dict[Severity, int] = { |
| 26 | + "CRITICAL": 4, |
| 27 | + "HIGH": 3, |
| 28 | + "MEDIUM": 2, |
| 29 | + "LOW": 1, |
| 30 | + "INFO": 0, |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def _norm_sev(s: str | None) -> Severity: |
| 35 | + if not s: |
| 36 | + return "INFO" |
| 37 | + s = s.strip().upper() |
| 38 | + if s in _SEV_ORDER: |
| 39 | + return s # type: ignore[return-value] |
| 40 | + if s in {"CRIT"}: |
| 41 | + return "CRITICAL" |
| 42 | + if s in {"MED", "MODERATE"}: |
| 43 | + return "MEDIUM" |
| 44 | + if s in {"WARN", "WARNING"}: |
| 45 | + return "LOW" |
| 46 | + return "INFO" |
| 47 | + |
| 48 | + |
| 49 | +def _extract_raw_sev(f: Finding) -> str | None: |
| 50 | + return f.get("severity") or f.get("predicted_risk") |
| 51 | + |
| 52 | + |
| 53 | +def _iter_findings(obj: Any) -> Iterable[Finding]: |
| 54 | + if isinstance(obj, dict): |
| 55 | + cand = obj.get("findings", obj.get("results", [])) |
| 56 | + if isinstance(cand, list): |
| 57 | + for x in cand: |
| 58 | + if isinstance(x, dict): |
| 59 | + yield cast(Finding, x) |
| 60 | + return |
| 61 | + if isinstance(obj, list): |
| 62 | + for x in obj: |
| 63 | + if isinstance(x, dict): |
| 64 | + yield cast(Finding, x) |
| 65 | + |
| 66 | + |
| 67 | +def summarize(obj: Any) -> Summary: |
| 68 | + counts_uc: dict[Severity, int] = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0, "INFO": 0} |
| 69 | + total = 0 |
| 70 | + for f in _iter_findings(obj): |
| 71 | + sev = _norm_sev(_extract_raw_sev(f)) |
| 72 | + counts_uc[sev] += 1 |
| 73 | + total += 1 |
| 74 | + |
| 75 | + worst: Severity = "INFO" |
| 76 | + if counts_uc["CRITICAL"] > 0: |
| 77 | + worst = "CRITICAL" |
| 78 | + elif counts_uc["HIGH"] > 0: |
| 79 | + worst = "HIGH" |
| 80 | + elif counts_uc["MEDIUM"] > 0: |
| 81 | + worst = "MEDIUM" |
| 82 | + elif counts_uc["LOW"] > 0: |
| 83 | + worst = "LOW" |
| 84 | + |
| 85 | + if worst in {"CRITICAL", "HIGH"}: |
| 86 | + risk: Literal["red", "yellow", "green"] = "red" |
| 87 | + elif worst == "MEDIUM": |
| 88 | + risk = "yellow" |
| 89 | + else: |
| 90 | + risk = "green" |
| 91 | + |
| 92 | + by_lc = { |
| 93 | + "critical": counts_uc["CRITICAL"], |
| 94 | + "high": counts_uc["HIGH"], |
| 95 | + "medium": counts_uc["MEDIUM"], |
| 96 | + "low": counts_uc["LOW"], |
| 97 | + "info": counts_uc["INFO"], |
| 98 | + } |
| 99 | + by_uc = { |
| 100 | + "CRITICAL": counts_uc["CRITICAL"], |
| 101 | + "HIGH": counts_uc["HIGH"], |
| 102 | + "MEDIUM": counts_uc["MEDIUM"], |
| 103 | + "LOW": counts_uc["LOW"], |
| 104 | + "INFO": counts_uc["INFO"], |
25 | 105 | } |
| 106 | + by_sev: dict[str, int] = {**by_lc, **by_uc} |
| 107 | + return {"total": total, "by_severity": by_sev, "worst": worst, "risk_level": risk} |
| 108 | + |
| 109 | + |
| 110 | +def summarize_apv_json(text_or_path: str | bytes) -> Summary: |
| 111 | + """Acepta JSON (str/bytes) o ruta a archivo JSON.""" |
| 112 | + if isinstance(text_or_path, bytes): |
| 113 | + payload = text_or_path.decode("utf-8", errors="strict") |
| 114 | + else: |
| 115 | + p = Path(text_or_path) |
| 116 | + payload = p.read_text(encoding="utf-8") if p.exists() else text_or_path |
| 117 | + data = json.loads(payload) |
| 118 | + return summarize(data) |
0 commit comments