|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import csv |
| 4 | +import json |
| 5 | +from dataclasses import asdict |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from heimdall.ci_policy import policy_summary |
| 9 | +from heimdall.config import HeimdallConfig |
| 10 | +from heimdall.evaluation.models import EvaluationResult |
| 11 | + |
| 12 | + |
| 13 | +def write_ci_reports(output_dir: str | Path, results: list[EvaluationResult], total_semgrep: int, config: HeimdallConfig) -> None: |
| 14 | + output = Path(output_dir) |
| 15 | + output.mkdir(parents=True, exist_ok=True) |
| 16 | + rows = [asdict(result) for result in results] |
| 17 | + (output / "ci_results.json").write_text(json.dumps(rows, indent=2, ensure_ascii=False), encoding="utf-8") |
| 18 | + _write_csv(output / "ci_results.csv", rows) |
| 19 | + _write_markdown(output / "ci_summary.md", results, total_semgrep, config) |
| 20 | + |
| 21 | + |
| 22 | +def _write_csv(path: Path, rows: list[dict]) -> None: |
| 23 | + fields = [ |
| 24 | + "alert_id", |
| 25 | + "severity", |
| 26 | + "vulnerability_type", |
| 27 | + "prediction", |
| 28 | + "classification", |
| 29 | + "final_decision", |
| 30 | + "evidence", |
| 31 | + "recommended_action", |
| 32 | + ] |
| 33 | + with path.open("w", encoding="utf-8", newline="") as handle: |
| 34 | + writer = csv.DictWriter(handle, fieldnames=fields) |
| 35 | + writer.writeheader() |
| 36 | + for row in rows: |
| 37 | + writer.writerow({field: _csv_value(row.get(field, "")) for field in fields}) |
| 38 | + |
| 39 | + |
| 40 | +def _write_markdown(path: Path, results: list[EvaluationResult], total_semgrep: int, config: HeimdallConfig) -> None: |
| 41 | + confirmed = [row for row in results if row.final_decision == "True Positive" or row.classification == "TP"] |
| 42 | + false_positive = [row for row in results if row.final_decision == "False Positive" or row.classification in {"TN", "FN"}] |
| 43 | + review = [row for row in results if row.final_decision == "Needs Review" or row.classification == "REVIEW"] |
| 44 | + high_critical = [row for row in confirmed if row.severity.lower() in {"high", "critical"}] |
| 45 | + policy = policy_summary(results, config) |
| 46 | + lines = [ |
| 47 | + "# Heimdall CI/CD Summary", |
| 48 | + "", |
| 49 | + f"Pipeline status: {'PASSED' if policy['passed'] else 'FAILED'}", |
| 50 | + "", |
| 51 | + "| Metric | Count |", |
| 52 | + "|---|---:|", |
| 53 | + f"| Total Semgrep findings | {total_semgrep} |", |
| 54 | + f"| Total validated findings | {len(results)} |", |
| 55 | + f"| Confirmed True Positives | {len(confirmed)} |", |
| 56 | + f"| False Positives | {len(false_positive)} |", |
| 57 | + f"| Needs Review | {len(review)} |", |
| 58 | + f"| High/Critical confirmed vulnerabilities | {len(high_critical)} |", |
| 59 | + "", |
| 60 | + "## Findings", |
| 61 | + "", |
| 62 | + "| Rule ID | Severity | File | Line | Heimdall Decision | Evidence | Recommended Action |", |
| 63 | + "|---|---|---|---:|---|---|---|", |
| 64 | + ] |
| 65 | + for result in results: |
| 66 | + metadata = result.metadata or {} |
| 67 | + file_path = metadata.get("file_path", "") |
| 68 | + line = metadata.get("line_number", "") |
| 69 | + lines.append( |
| 70 | + "| {rule} | {severity} | {file} | {line} | {decision} | {evidence} | {action} |".format( |
| 71 | + rule=_md(result.alert_id), |
| 72 | + severity=_md(result.severity), |
| 73 | + file=_md(str(file_path)), |
| 74 | + line=line, |
| 75 | + decision=_md(result.final_decision or result.prediction), |
| 76 | + evidence=_md((result.evidence or result.rationale)[:160]), |
| 77 | + action=_md((result.recommended_action or "Review finding.")[:160]), |
| 78 | + ) |
| 79 | + ) |
| 80 | + path.write_text("\n".join(lines), encoding="utf-8") |
| 81 | + |
| 82 | + |
| 83 | +def _csv_value(value: object) -> object: |
| 84 | + if isinstance(value, (dict, list)): |
| 85 | + return json.dumps(value, ensure_ascii=False) |
| 86 | + return value |
| 87 | + |
| 88 | + |
| 89 | +def _md(value: str) -> str: |
| 90 | + return value.replace("|", "\\|").replace("\n", " ") |
0 commit comments