@@ -26,9 +26,7 @@ def _normalize(sev: str) -> str:
2626 return sev .strip ().upper ()
2727
2828
29- def _count_by_severity_from_list (
30- items : Iterable [Mapping [str , Any ]],
31- ) -> dict [str , int ]:
29+ def _count_by_severity_from_list (items : Iterable [Mapping [str , Any ]]) -> dict [str , int ]:
3230 counts : dict [str , int ] = {k : 0 for k in SEVERITIES }
3331 for it in items :
3432 raw = it .get ("severity" ) or it .get ("level" ) or ""
@@ -39,9 +37,6 @@ def _count_by_severity_from_list(
3937
4038
4139def _build_summary (apv_obj : object ) -> Summary :
42- # Soporta:
43- # 1) {"by_severity": {"HIGH": 1, ...}}
44- # 2) [{"severity": "HIGH"}, ...] o en "findings"/"items"
4540 counts : dict [str , int ] = {k : 0 for k in SEVERITIES }
4641
4742 if isinstance (apv_obj , Mapping ):
@@ -51,14 +46,18 @@ def _build_summary(apv_obj: object) -> Summary:
5146 kk = _normalize (str (k ))
5247 if kk in counts :
5348 try :
54- counts [kk ] += int (v ) # type: ignore[arg-type]
49+ counts [kk ] += int (v )
5550 except Exception :
5651 pass
52+
5753 items = apv_obj .get ("findings" ) or apv_obj .get ("items" )
5854 if isinstance (items , list ) and not any (counts .values ()):
59- counts = _count_by_severity_from_list (items ) # type: ignore[arg-type]
55+ items_m = [x for x in items if isinstance (x , Mapping )]
56+ counts = _count_by_severity_from_list (items_m )
57+
6058 elif isinstance (apv_obj , list ):
61- counts = _count_by_severity_from_list (apv_obj ) # type: ignore[arg-type]
59+ items_m = [x for x in apv_obj if isinstance (x , Mapping )]
60+ counts = _count_by_severity_from_list (items_m )
6261
6362 total = sum (counts .values ())
6463
@@ -98,12 +97,9 @@ def _render_table(summary: Summary, console: Console) -> None:
9897 )
9998 return
10099
101- if summary .risk_level == "red" :
102- title_dot = "🔴"
103- elif summary .risk_level == "yellow" :
104- title_dot = "🟡"
105- else :
106- title_dot = "🟢"
100+ title_dot = (
101+ "🔴" if summary .risk_level == "red" else "🟡" if summary .risk_level == "yellow" else "🟢"
102+ )
107103
108104 table = Table (
109105 title = f"Diff Risk Dashboard { title_dot } — Worst: { summary .worst } " ,
@@ -147,7 +143,7 @@ def _render_bars(summary: Summary) -> None:
147143 print (f"{ sev :<10} { c :>4} { pct :>3} % { '█' * int (pct / 4 )} " )
148144
149145
150- def main () -> None :
146+ def main () -> int :
151147 desc = "Diff Risk Dashboard (APV JSON -> summary)"
152148 p = argparse .ArgumentParser (prog = "diff_risk_dashboard" , description = desc )
153149 p .add_argument ("input" , help = "File path or inline JSON" )
@@ -193,18 +189,18 @@ def main() -> None:
193189
194190 elif args .format == "bar" :
195191 _render_bars (summary )
196-
197- else : # table
192+ else :
198193 console = Console (force_jupyter = False , force_terminal = None , soft_wrap = False )
199194 _render_table (summary , console )
200195
201- if not args .no_exit_by_risk :
202- if summary .risk_level == "red" :
203- raise SystemExit (2 )
204- if summary .risk_level == "yellow" :
205- raise SystemExit (1 )
206- raise SystemExit (0 )
196+ if args .no_exit_by_risk :
197+ return 0
198+ if summary .risk_level == "red" :
199+ return 2
200+ if summary .risk_level == "yellow" :
201+ return 1
202+ return 0
207203
208204
209205if __name__ == "__main__" :
210- main ()
206+ raise SystemExit ( main () )
0 commit comments