diff --git a/src/diff_risk_dashboard/report.py b/src/diff_risk_dashboard/report.py index c536ff5..01069ef 100644 --- a/src/diff_risk_dashboard/report.py +++ b/src/diff_risk_dashboard/report.py @@ -1,26 +1,40 @@ from __future__ import annotations -from collections.abc import Mapping from typing import Any _SEVERITIES = ["CRITICAL", "HIGH", "MEDIUM", "LOW", "INFO"] -def _get_counts(summary: Mapping[str, Any]) -> dict[str, int]: - counts: dict[str, int] = {} - by = summary.get("by_severity", {}) or {} - if isinstance(by, Mapping): - for sev in _SEVERITIES: - counts[sev] = int(by.get(sev, by.get(sev.lower(), 0)) or 0) - return counts +def _norm_counts(summary: dict[str, Any]) -> dict[str, int]: + src = summary.get("by_severity") or {} + by = {str(k).upper(): int(v) for k, v in src.items()} + return {s: int(by.get(s, 0)) for s in _SEVERITIES} -def to_markdown(summary: Mapping[str, Any]) -> str: - counts = _get_counts(summary) - total = int(summary.get("total", 0) or 0) - worst = str(summary.get("worst", "INFO") or "INFO").upper() - risk = str(summary.get("risk", summary.get("risk_level", "green")) or "green").lower() +def to_markdown(summary: dict[str, Any]) -> str: + counts = _norm_counts(summary) + total = int(summary.get("total", sum(counts.values()))) + worst = str(summary.get("worst") or summary.get("risk_level") or "INFO").upper() + risk = str(summary.get("risk_level", "green") or "green").lower() emoji = {"red": "🔴", "yellow": "🟡", "green": "🟢"}.get(risk, "🟢") + + zero = total == 0 or sum(counts.values()) == 0 + if zero: + # Zero-state: limpio y profesional + return "\n".join( + [ + f"# Diff Risk Dashboard {emoji} — No findings", + "", + "> ✅ No findings. All severities are 0.", + "", + "| Severity | Count |", + "|---|---:|", + "| **TOTAL** | **0** |", + "", + ] + ) + + # Caso normal: tabla completa (compatibilidad con tests/README) lines = [ f"# Diff Risk Dashboard {emoji} — Worst: **{worst}**", "", @@ -29,5 +43,10 @@ def to_markdown(summary: Mapping[str, Any]) -> str: ] for sev in _SEVERITIES: lines.append(f"| {sev} | {counts.get(sev, 0)} |") - lines += [f"| **TOTAL** | **{total}** |", "", "> Generated by diff-risk-dashboard CLI"] + lines.append(f"| **TOTAL** | **{total}** |") + lines.append("") return "\n".join(lines) + + +# Alias de compatibilidad +render_markdown = to_markdown