Commit cf30cd6812
Verified · cmc
Layout: unified · split
.qlty/.gitignore added +7
| @@ -0,0 +1,7 @@ | ||
| 1 | * | |
| 2 | !configs | |
| 3 | !configs/** | |
| 4 | !hooks | |
| 5 | !hooks/** | |
| 6 | !qlty.toml | |
| 7 | !.gitignore | |
.qlty/qlty.toml added +44
| @@ -0,0 +1,44 @@ | ||
| 1 | config_version = "0" | |
| 2 | ||
| 3 | exclude_patterns = [ | |
| 4 | "**/vendor/**", | |
| 5 | "**/node_modules/**", | |
| 6 | "**/testdata/**", | |
| 7 | "**/dist/**", | |
| 8 | "**/.build/**", | |
| 9 | "**/build/**", | |
| 10 | "**/*.min.*", | |
| 11 | "**/generated/**", | |
| 12 | ".claude/**", | |
| 13 | "**/Pods/**", | |
| 14 | ] | |
| 15 | ||
| 16 | test_patterns = [ | |
| 17 | "**/test/**", | |
| 18 | "**/tests/**", | |
| 19 | "**/Tests/**", | |
| 20 | "**/*_test.*", | |
| 21 | "**/test_*.*", | |
| 22 | "**/*.test.*", | |
| 23 | "**/*Tests.swift", | |
| 24 | "e2e/**", | |
| 25 | ] | |
| 26 | ||
| 27 | [[source]] | |
| 28 | name = "default" | |
| 29 | default = true | |
| 30 | ||
| 31 | [[plugin]] | |
| 32 | name = "osv-scanner" | |
| 33 | ||
| 34 | [[plugin]] | |
| 35 | name = "trufflehog" | |
| 36 | ||
| 37 | # Early returns are guard clauses, which keep the happy path unindented. | |
| 38 | [smells.return_statements] | |
| 39 | enabled = false | |
| 40 | ||
| 41 | # Total file complexity is the sum of its functions, which function_complexity | |
| 42 | # already checks. | |
| 43 | [smells.file_complexity] | |
| 44 | enabled = false | |
audit_report/diff.py +3 −237
| @@ -17,11 +17,9 @@ from __future__ import annotations | ||
| 17 | 17 | |
| 18 | 18 | from dataclasses import dataclass, field |
| 19 | 19 | from datetime import datetime, timezone |
| 20 | from html import escape | |
| 21 | 20 | |
| 22 | 21 | from .engine import FAIL, Finding |
| 23 | 22 | from .loader import Package |
| 24 | from .reporters.html import CSS as _CSS | |
| 25 | 23 | |
| 26 | 24 | ABSENT = "absent" # rule present in only one of the two packages |
| 27 | 25 | |
| @@ -153,240 +151,8 @@ def has_regression(diff: DiffReport, threshold: str) -> bool: | ||
| 153 | 151 | ) |
| 154 | 152 | |
| 155 | 153 | |
| 156 | # --------------------------------------------------------------------------- # | |
| 157 | # Rendering | |
| 158 | # --------------------------------------------------------------------------- # | |
| 159 | ||
| 160 | _CATEGORY_HEADING = { | |
| 161 | REGRESSED: "Regressions — a control is no longer supported", | |
| 162 | FIXED: "Fixed — a previously failing control now passes", | |
| 163 | DRIFTED: "Ongoing failures with changed evidence", | |
| 164 | CHANGED: "Other status changes", | |
| 165 | UNCHANGED: "Unchanged", | |
| 166 | } | |
| 167 | ||
| 168 | ||
| 169 | def _transition(delta: RuleDelta) -> str: | |
| 170 | return f"{_STATUS_LABEL.get(delta.old_status, delta.old_status)} → {_STATUS_LABEL.get(delta.new_status, delta.new_status)}" | |
| 171 | ||
| 172 | ||
| 173 | def _md_table(rows: list[dict[str, str]]) -> list[str]: | |
| 174 | if not rows: | |
| 175 | return [] | |
| 176 | shown = rows[:10] | |
| 177 | headers = list(shown[0].keys()) | |
| 178 | out = [ | |
| 179 | "| " + " | ".join(headers) + " |", | |
| 180 | "| " + " | ".join("---" for _ in headers) + " |", | |
| 181 | ] | |
| 182 | out += ["| " + " | ".join(str(r.get(h, "")) for h in headers) + " |" for r in shown] | |
| 183 | if len(rows) > len(shown): | |
| 184 | out.append(f"\n_+{len(rows) - len(shown)} more row(s) omitted._") | |
| 185 | return out | |
| 186 | ||
| 187 | ||
| 188 | def _render_delta_md(delta, category: str) -> list[str]: | |
| 189 | rule = delta.rule | |
| 190 | out = [ | |
| 191 | f"### {rule.title}", | |
| 192 | "", | |
| 193 | f"- **Rule:** `{rule.id}` · **Severity:** {rule.severity}", | |
| 194 | f"- **Controls:** {', '.join(rule.controls) or '—'}", | |
| 195 | f"- **Change:** {_transition(delta)}", | |
| 196 | ] | |
| 197 | if delta.new_reason: | |
| 198 | out.append(f"- **Now:** {delta.new_reason}") | |
| 199 | if category == REGRESSED and rule.remediation: | |
| 200 | out.append(f"- **Remediation:** {rule.remediation.strip()}") | |
| 201 | out.append("") | |
| 202 | if delta.evidence_added: | |
| 203 | out.append("**Newly failing rows:**") | |
| 204 | out.append("") | |
| 205 | out.extend(_md_table(delta.evidence_added)) | |
| 206 | out.append("") | |
| 207 | if delta.evidence_removed: | |
| 208 | out.append("**No longer failing rows:**") | |
| 209 | out.append("") | |
| 210 | out.extend(_md_table(delta.evidence_removed)) | |
| 211 | out.append("") | |
| 212 | return out | |
| 213 | ||
| 214 | ||
| 215 | def _render_md(diff: DiffReport) -> str: | |
| 216 | counts = diff.counts | |
| 217 | out: list[str] = [] | |
| 218 | out.append(f"# Evidence Drift — {diff.current.subject} ({diff.current.platform})") | |
| 219 | out.append("") | |
| 220 | out.append(f"- **Baseline:** `{diff.baseline.path.name}`") | |
| 221 | out.append(f"- **Current:** `{diff.current.path.name}`") | |
| 222 | out.append(f"- **Generated:** {diff.generated_at}") | |
| 223 | out.append( | |
| 224 | f"- **Drift:** {counts[REGRESSED]} regressed · {counts[FIXED]} fixed · " | |
| 225 | f"{counts[DRIFTED]} drifted · {counts[CHANGED]} changed · " | |
| 226 | f"{counts[UNCHANGED]} unchanged" | |
| 227 | ) | |
| 228 | out.append("") | |
| 229 | out.append( | |
| 230 | "> A *regression* means a setting moved into a state that no longer " | |
| 231 | "supports a control since the baseline. As always this is evidence, not " | |
| 232 | "a verdict." | |
| 233 | ) | |
| 234 | out.append("") | |
| 235 | ||
| 236 | for category in CATEGORY_ORDER: | |
| 237 | rows = diff.by_category(category) | |
| 238 | if not rows or category == UNCHANGED: | |
| 239 | continue | |
| 240 | out.append(f"## {_CATEGORY_HEADING[category]}") | |
| 241 | out.append("") | |
| 242 | for delta in rows: | |
| 243 | out.extend(_render_delta_md(delta, category)) | |
| 244 | ||
| 245 | unchanged = diff.counts[UNCHANGED] | |
| 246 | if unchanged: | |
| 247 | out.append(f"_{unchanged} rule(s) unchanged._") | |
| 248 | return "\n".join(out).rstrip() + "\n" | |
| 249 | ||
| 250 | ||
| 251 | def _html_table(rows: list[dict[str, str]], caption: str, cls: str) -> str: | |
| 252 | shown = rows[:10] | |
| 253 | headers = list(shown[0].keys()) | |
| 254 | head = "".join(f"<th>{escape(h)}</th>" for h in headers) | |
| 255 | body = "".join( | |
| 256 | "<tr>" + "".join(f"<td>{escape(str(r.get(h, '')))}</td>" for h in headers) + "</tr>" | |
| 257 | for r in shown | |
| 258 | ) | |
| 259 | return ( | |
| 260 | f"<table class='{cls}'><caption>{escape(caption)}</caption>" | |
| 261 | f"<thead><tr>{head}</tr></thead><tbody>{body}</tbody></table>" | |
| 262 | ) | |
| 263 | ||
| 264 | ||
| 265 | _DIFF_CSS = ( | |
| 266 | _CSS | |
| 267 | + """ | |
| 268 | .delta { border: 1px solid #e5e5e5; border-radius: 6px; padding: 1rem 1.1rem; margin: .8rem 0; } | |
| 269 | .delta.regressed { border-left: 4px solid #c1272d; } | |
| 270 | .delta.fixed { border-left: 4px solid #1a7f37; } | |
| 271 | .delta.drifted { border-left: 4px solid #d08700; } | |
| 272 | .delta.changed { border-left: 4px solid #bbb; } | |
| 273 | .delta h3 { margin: 0 0 .4rem; font-size: 1.05rem; } | |
| 274 | .transition { font-weight: 700; } | |
| 275 | table.added caption, table.removed caption { text-align: left; font-weight: 600; font-size: .85rem; padding: .2rem 0; } | |
| 276 | table.added caption { color: #c1272d; } table.removed caption { color: #1a7f37; } | |
| 277 | @media (prefers-color-scheme: dark) { | |
| 278 | .delta { border-color: #2d2e33; } | |
| 279 | table.added caption { color: #ff6b70; } table.removed caption { color: #4ac36a; } | |
| 280 | } | |
| 281 | """ | |
| 282 | ) | |
| 283 | ||
| 284 | ||
| 285 | def _render_delta_html(delta, category: str) -> str: | |
| 286 | rule = delta.rule | |
| 287 | body = [ | |
| 288 | f"<h3>{escape(rule.title)}</h3>", | |
| 289 | ( | |
| 290 | f"<dl><dt>Rule</dt><dd><code>{escape(rule.id)}</code> · " | |
| 291 | f"{escape(rule.severity)}</dd>" | |
| 292 | ), | |
| 293 | f"<dt>Controls</dt><dd>{escape(', '.join(rule.controls) or '—')}</dd>", | |
| 294 | f"<dt>Change</dt><dd class='transition'>{escape(_transition(delta))}</dd>", | |
| 295 | ] | |
| 296 | if delta.new_reason: | |
| 297 | body.append(f"<dt>Now</dt><dd>{escape(delta.new_reason)}</dd>") | |
| 298 | if category == REGRESSED and rule.remediation: | |
| 299 | body.append(f"<dt>Remediation</dt><dd>{escape(rule.remediation.strip())}</dd>") | |
| 300 | body.append("</dl>") | |
| 301 | if delta.evidence_added: | |
| 302 | body.append(_html_table(delta.evidence_added, "Newly failing rows", "added")) | |
| 303 | if delta.evidence_removed: | |
| 304 | body.append( | |
| 305 | _html_table(delta.evidence_removed, "No longer failing rows", "removed") | |
| 306 | ) | |
| 307 | return f"<div class='delta {category}'>{''.join(body)}</div>" | |
| 308 | ||
| 309 | ||
| 310 | def _render_html(diff: DiffReport) -> str: | |
| 311 | counts = diff.counts | |
| 312 | parts: list[str] = [] | |
| 313 | parts.append( | |
| 314 | f"<h1>Evidence Drift — {escape(diff.current.subject)} " | |
| 315 | f"({escape(diff.current.platform)})</h1>" | |
| 316 | ) | |
| 317 | parts.append( | |
| 318 | f"<p class='meta'>Baseline <code>{escape(diff.baseline.path.name)}</code> → " | |
| 319 | f"Current <code>{escape(diff.current.path.name)}</code> · " | |
| 320 | f"Generated {escape(diff.generated_at)}</p>" | |
| 321 | ) | |
| 322 | parts.append( | |
| 323 | "<p class='summary-pills'>" | |
| 324 | f"<span>{counts[REGRESSED]} regressed</span>" | |
| 325 | f"<span>{counts[FIXED]} fixed</span>" | |
| 326 | f"<span>{counts[DRIFTED]} drifted</span>" | |
| 327 | f"<span>{counts[CHANGED]} changed</span>" | |
| 328 | f"<span>{counts[UNCHANGED]} unchanged</span></p>" | |
| 329 | ) | |
| 330 | parts.append( | |
| 331 | "<p class='note'>A <strong>regression</strong> means a setting moved into " | |
| 332 | "a state that no longer supports a control since the baseline. As always " | |
| 333 | "this is evidence, not a verdict.</p>" | |
| 334 | ) | |
| 335 | ||
| 336 | for category in CATEGORY_ORDER: | |
| 337 | rows = diff.by_category(category) | |
| 338 | if not rows or category == UNCHANGED: | |
| 339 | continue | |
| 340 | parts.append(f"<h2>{escape(_CATEGORY_HEADING[category])}</h2>") | |
| 341 | for delta in rows: | |
| 342 | parts.append(_render_delta_html(delta, category)) | |
| 343 | ||
| 344 | if counts[UNCHANGED]: | |
| 345 | parts.append(f"<p class='meta'>{counts[UNCHANGED]} rule(s) unchanged.</p>") | |
| 346 | parts.append("<footer>Generated by audit-report · Audit Labs · evidence, not a verdict.</footer>") | |
| 347 | ||
| 348 | return ( | |
| 349 | "<!doctype html><html lang='en'><head><meta charset='utf-8'>" | |
| 350 | "<meta name='viewport' content='width=device-width, initial-scale=1'>" | |
| 351 | f"<title>Evidence Drift — {escape(diff.current.subject)}</title>" | |
| 352 | f"<style>{_DIFF_CSS}</style></head><body><main>{''.join(parts)}</main></body></html>\n" | |
| 353 | ) | |
| 354 | ||
| 355 | ||
| 356 | def _render_json(diff: DiffReport) -> str: | |
| 357 | import json as _json | |
| 358 | ||
| 359 | payload = { | |
| 360 | "subject": diff.current.subject, | |
| 361 | "platform": diff.current.platform, | |
| 362 | "baseline_package": diff.baseline.path.name, | |
| 363 | "current_package": diff.current.path.name, | |
| 364 | "generated_at": diff.generated_at, | |
| 365 | "summary": diff.counts, | |
| 366 | "deltas": [ | |
| 367 | { | |
| 368 | "id": d.rule.id, | |
| 369 | "title": d.rule.title, | |
| 370 | "severity": d.rule.severity, | |
| 371 | "controls": d.rule.controls, | |
| 372 | "category": d.category, | |
| 373 | "old_status": d.old_status, | |
| 374 | "new_status": d.new_status, | |
| 375 | "evidence_added": d.evidence_added, | |
| 376 | "evidence_removed": d.evidence_removed, | |
| 377 | } | |
| 378 | for d in diff.deltas | |
| 379 | ], | |
| 380 | } | |
| 381 | return _json.dumps(payload, indent=2) + "\n" | |
| 382 | ||
| 383 | ||
| 384 | _RENDERERS = {"md": _render_md, "html": _render_html, "json": _render_json} | |
| 385 | ||
| 386 | ||
| 387 | 154 | def render(diff: DiffReport, fmt: str) -> str: |
| 388 | 155 | """Render a diff in the named format ('md', 'html', or 'json').""" |
| 389 | try: | |
| 390 | return _RENDERERS[fmt](diff) | |
| 391 | except KeyError: | |
| 392 | raise ValueError(f"unknown format: {fmt!r}") from None | |
| 156 | from .reporters.diff import render as render_diff | |
| 157 | ||
| 158 | return render_diff(diff, fmt) | |
audit_report/reporters/diff.py added +251
| @@ -0,0 +1,251 @@ | ||
| 1 | """Renderers for diff mode: Markdown, HTML, and JSON.""" | |
| 2 | ||
| 3 | from __future__ import annotations | |
| 4 | ||
| 5 | import json | |
| 6 | from html import escape | |
| 7 | ||
| 8 | from ..diff import ( | |
| 9 | _STATUS_LABEL, | |
| 10 | CATEGORY_ORDER, | |
| 11 | CHANGED, | |
| 12 | DRIFTED, | |
| 13 | FIXED, | |
| 14 | REGRESSED, | |
| 15 | UNCHANGED, | |
| 16 | DiffReport, | |
| 17 | RuleDelta, | |
| 18 | ) | |
| 19 | from .html import CSS as _CSS | |
| 20 | ||
| 21 | _CATEGORY_HEADING = { | |
| 22 | REGRESSED: "Regressions — a control is no longer supported", | |
| 23 | FIXED: "Fixed — a previously failing control now passes", | |
| 24 | DRIFTED: "Ongoing failures with changed evidence", | |
| 25 | CHANGED: "Other status changes", | |
| 26 | UNCHANGED: "Unchanged", | |
| 27 | } | |
| 28 | ||
| 29 | ||
| 30 | def _transition(delta: RuleDelta) -> str: | |
| 31 | return f"{_STATUS_LABEL.get(delta.old_status, delta.old_status)} → {_STATUS_LABEL.get(delta.new_status, delta.new_status)}" | |
| 32 | ||
| 33 | ||
| 34 | def _md_table(rows: list[dict[str, str]]) -> list[str]: | |
| 35 | if not rows: | |
| 36 | return [] | |
| 37 | shown = rows[:10] | |
| 38 | headers = list(shown[0].keys()) | |
| 39 | out = [ | |
| 40 | "| " + " | ".join(headers) + " |", | |
| 41 | "| " + " | ".join("---" for _ in headers) + " |", | |
| 42 | ] | |
| 43 | out += ["| " + " | ".join(str(r.get(h, "")) for h in headers) + " |" for r in shown] | |
| 44 | if len(rows) > len(shown): | |
| 45 | out.append(f"\n_+{len(rows) - len(shown)} more row(s) omitted._") | |
| 46 | return out | |
| 47 | ||
| 48 | ||
| 49 | def _render_delta_md(delta, category: str) -> list[str]: | |
| 50 | rule = delta.rule | |
| 51 | out = [ | |
| 52 | f"### {rule.title}", | |
| 53 | "", | |
| 54 | f"- **Rule:** `{rule.id}` · **Severity:** {rule.severity}", | |
| 55 | f"- **Controls:** {', '.join(rule.controls) or '—'}", | |
| 56 | f"- **Change:** {_transition(delta)}", | |
| 57 | ] | |
| 58 | if delta.new_reason: | |
| 59 | out.append(f"- **Now:** {delta.new_reason}") | |
| 60 | if category == REGRESSED and rule.remediation: | |
| 61 | out.append(f"- **Remediation:** {rule.remediation.strip()}") | |
| 62 | out.append("") | |
| 63 | if delta.evidence_added: | |
| 64 | out.append("**Newly failing rows:**") | |
| 65 | out.append("") | |
| 66 | out.extend(_md_table(delta.evidence_added)) | |
| 67 | out.append("") | |
| 68 | if delta.evidence_removed: | |
| 69 | out.append("**No longer failing rows:**") | |
| 70 | out.append("") | |
| 71 | out.extend(_md_table(delta.evidence_removed)) | |
| 72 | out.append("") | |
| 73 | return out | |
| 74 | ||
| 75 | ||
| 76 | def _render_md(diff: DiffReport) -> str: | |
| 77 | counts = diff.counts | |
| 78 | out: list[str] = [] | |
| 79 | out.append(f"# Evidence Drift — {diff.current.subject} ({diff.current.platform})") | |
| 80 | out.append("") | |
| 81 | out.append(f"- **Baseline:** `{diff.baseline.path.name}`") | |
| 82 | out.append(f"- **Current:** `{diff.current.path.name}`") | |
| 83 | out.append(f"- **Generated:** {diff.generated_at}") | |
| 84 | out.append( | |
| 85 | f"- **Drift:** {counts[REGRESSED]} regressed · {counts[FIXED]} fixed · " | |
| 86 | f"{counts[DRIFTED]} drifted · {counts[CHANGED]} changed · " | |
| 87 | f"{counts[UNCHANGED]} unchanged" | |
| 88 | ) | |
| 89 | out.append("") | |
| 90 | out.append( | |
| 91 | "> A *regression* means a setting moved into a state that no longer " | |
| 92 | "supports a control since the baseline. As always this is evidence, not " | |
| 93 | "a verdict." | |
| 94 | ) | |
| 95 | out.append("") | |
| 96 | ||
| 97 | for category in CATEGORY_ORDER: | |
| 98 | rows = diff.by_category(category) | |
| 99 | if not rows or category == UNCHANGED: | |
| 100 | continue | |
| 101 | out.append(f"## {_CATEGORY_HEADING[category]}") | |
| 102 | out.append("") | |
| 103 | for delta in rows: | |
| 104 | out.extend(_render_delta_md(delta, category)) | |
| 105 | ||
| 106 | unchanged = diff.counts[UNCHANGED] | |
| 107 | if unchanged: | |
| 108 | out.append(f"_{unchanged} rule(s) unchanged._") | |
| 109 | return "\n".join(out).rstrip() + "\n" | |
| 110 | ||
| 111 | ||
| 112 | def _html_table(rows: list[dict[str, str]], caption: str, cls: str) -> str: | |
| 113 | shown = rows[:10] | |
| 114 | headers = list(shown[0].keys()) | |
| 115 | head = "".join(f"<th>{escape(h)}</th>" for h in headers) | |
| 116 | body = "".join( | |
| 117 | "<tr>" + "".join(f"<td>{escape(str(r.get(h, '')))}</td>" for h in headers) + "</tr>" | |
| 118 | for r in shown | |
| 119 | ) | |
| 120 | return ( | |
| 121 | f"<table class='{cls}'><caption>{escape(caption)}</caption>" | |
| 122 | f"<thead><tr>{head}</tr></thead><tbody>{body}</tbody></table>" | |
| 123 | ) | |
| 124 | ||
| 125 | ||
| 126 | _DIFF_CSS = ( | |
| 127 | _CSS | |
| 128 | + """ | |
| 129 | .delta { border: 1px solid #e5e5e5; border-radius: 6px; padding: 1rem 1.1rem; margin: .8rem 0; } | |
| 130 | .delta.regressed { border-left: 4px solid #c1272d; } | |
| 131 | .delta.fixed { border-left: 4px solid #1a7f37; } | |
| 132 | .delta.drifted { border-left: 4px solid #d08700; } | |
| 133 | .delta.changed { border-left: 4px solid #bbb; } | |
| 134 | .delta h3 { margin: 0 0 .4rem; font-size: 1.05rem; } | |
| 135 | .transition { font-weight: 700; } | |
| 136 | table.added caption, table.removed caption { text-align: left; font-weight: 600; font-size: .85rem; padding: .2rem 0; } | |
| 137 | table.added caption { color: #c1272d; } table.removed caption { color: #1a7f37; } | |
| 138 | @media (prefers-color-scheme: dark) { | |
| 139 | .delta { border-color: #2d2e33; } | |
| 140 | table.added caption { color: #ff6b70; } table.removed caption { color: #4ac36a; } | |
| 141 | } | |
| 142 | """ | |
| 143 | ) | |
| 144 | ||
| 145 | ||
| 146 | def _render_delta_html(delta, category: str) -> str: | |
| 147 | rule = delta.rule | |
| 148 | body = [ | |
| 149 | f"<h3>{escape(rule.title)}</h3>", | |
| 150 | ( | |
| 151 | f"<dl><dt>Rule</dt><dd><code>{escape(rule.id)}</code> · " | |
| 152 | f"{escape(rule.severity)}</dd>" | |
| 153 | ), | |
| 154 | f"<dt>Controls</dt><dd>{escape(', '.join(rule.controls) or '—')}</dd>", | |
| 155 | f"<dt>Change</dt><dd class='transition'>{escape(_transition(delta))}</dd>", | |
| 156 | ] | |
| 157 | if delta.new_reason: | |
| 158 | body.append(f"<dt>Now</dt><dd>{escape(delta.new_reason)}</dd>") | |
| 159 | if category == REGRESSED and rule.remediation: | |
| 160 | body.append(f"<dt>Remediation</dt><dd>{escape(rule.remediation.strip())}</dd>") | |
| 161 | body.append("</dl>") | |
| 162 | if delta.evidence_added: | |
| 163 | body.append(_html_table(delta.evidence_added, "Newly failing rows", "added")) | |
| 164 | if delta.evidence_removed: | |
| 165 | body.append( | |
| 166 | _html_table(delta.evidence_removed, "No longer failing rows", "removed") | |
| 167 | ) | |
| 168 | return f"<div class='delta {category}'>{''.join(body)}</div>" | |
| 169 | ||
| 170 | ||
| 171 | def _render_html(diff: DiffReport) -> str: | |
| 172 | counts = diff.counts | |
| 173 | parts: list[str] = [] | |
| 174 | parts.append( | |
| 175 | f"<h1>Evidence Drift — {escape(diff.current.subject)} " | |
| 176 | f"({escape(diff.current.platform)})</h1>" | |
| 177 | ) | |
| 178 | parts.append( | |
| 179 | f"<p class='meta'>Baseline <code>{escape(diff.baseline.path.name)}</code> → " | |
| 180 | f"Current <code>{escape(diff.current.path.name)}</code> · " | |
| 181 | f"Generated {escape(diff.generated_at)}</p>" | |
| 182 | ) | |
| 183 | parts.append( | |
| 184 | "<p class='summary-pills'>" | |
| 185 | f"<span>{counts[REGRESSED]} regressed</span>" | |
| 186 | f"<span>{counts[FIXED]} fixed</span>" | |
| 187 | f"<span>{counts[DRIFTED]} drifted</span>" | |
| 188 | f"<span>{counts[CHANGED]} changed</span>" | |
| 189 | f"<span>{counts[UNCHANGED]} unchanged</span></p>" | |
| 190 | ) | |
| 191 | parts.append( | |
| 192 | "<p class='note'>A <strong>regression</strong> means a setting moved into " | |
| 193 | "a state that no longer supports a control since the baseline. As always " | |
| 194 | "this is evidence, not a verdict.</p>" | |
| 195 | ) | |
| 196 | ||
| 197 | for category in CATEGORY_ORDER: | |
| 198 | rows = diff.by_category(category) | |
| 199 | if not rows or category == UNCHANGED: | |
| 200 | continue | |
| 201 | parts.append(f"<h2>{escape(_CATEGORY_HEADING[category])}</h2>") | |
| 202 | for delta in rows: | |
| 203 | parts.append(_render_delta_html(delta, category)) | |
| 204 | ||
| 205 | if counts[UNCHANGED]: | |
| 206 | parts.append(f"<p class='meta'>{counts[UNCHANGED]} rule(s) unchanged.</p>") | |
| 207 | parts.append("<footer>Generated by audit-report · Audit Labs · evidence, not a verdict.</footer>") | |
| 208 | ||
| 209 | return ( | |
| 210 | "<!doctype html><html lang='en'><head><meta charset='utf-8'>" | |
| 211 | "<meta name='viewport' content='width=device-width, initial-scale=1'>" | |
| 212 | f"<title>Evidence Drift — {escape(diff.current.subject)}</title>" | |
| 213 | f"<style>{_DIFF_CSS}</style></head><body><main>{''.join(parts)}</main></body></html>\n" | |
| 214 | ) | |
| 215 | ||
| 216 | ||
| 217 | def _render_json(diff: DiffReport) -> str: | |
| 218 | payload = { | |
| 219 | "subject": diff.current.subject, | |
| 220 | "platform": diff.current.platform, | |
| 221 | "baseline_package": diff.baseline.path.name, | |
| 222 | "current_package": diff.current.path.name, | |
| 223 | "generated_at": diff.generated_at, | |
| 224 | "summary": diff.counts, | |
| 225 | "deltas": [ | |
| 226 | { | |
| 227 | "id": d.rule.id, | |
| 228 | "title": d.rule.title, | |
| 229 | "severity": d.rule.severity, | |
| 230 | "controls": d.rule.controls, | |
| 231 | "category": d.category, | |
| 232 | "old_status": d.old_status, | |
| 233 | "new_status": d.new_status, | |
| 234 | "evidence_added": d.evidence_added, | |
| 235 | "evidence_removed": d.evidence_removed, | |
| 236 | } | |
| 237 | for d in diff.deltas | |
| 238 | ], | |
| 239 | } | |
| 240 | return json.dumps(payload, indent=2) + "\n" | |
| 241 | ||
| 242 | ||
| 243 | _RENDERERS = {"md": _render_md, "html": _render_html, "json": _render_json} | |
| 244 | ||
| 245 | ||
| 246 | def render(diff: DiffReport, fmt: str) -> str: | |
| 247 | """Render a diff in the named format ('md', 'html', or 'json').""" | |
| 248 | try: | |
| 249 | return _RENDERERS[fmt](diff) | |
| 250 | except KeyError: | |
| 251 | raise ValueError(f"unknown format: {fmt!r}") from None | |
audit_report/trend.py +14 −11
| @@ -34,17 +34,7 @@ def discover(parent: str | Path, subject: str | None = None) -> tuple[str, str, | ||
| 34 | 34 | if not directory.is_dir(): |
| 35 | 35 | raise ValueError(f"not a directory: {directory}") |
| 36 | 36 | |
| 37 | groups: dict[tuple[str, str], list[Path]] = {} | |
| 38 | for child in sorted(directory.iterdir()): | |
| 39 | if not child.is_dir() or not any(child.glob("*.csv")): | |
| 40 | continue | |
| 41 | platform, subj = detect_platform(child.name) | |
| 42 | if platform == "unknown": | |
| 43 | continue | |
| 44 | if subject and subj != subject: | |
| 45 | continue | |
| 46 | groups.setdefault((platform, subj), []).append(child) | |
| 47 | ||
| 37 | groups = _group_packages(directory, subject) | |
| 48 | 38 | if not groups: |
| 49 | 39 | raise ValueError( |
| 50 | 40 | f"no audit-tools packages found under {directory}" |
| @@ -65,6 +55,19 @@ def discover(parent: str | Path, subject: str | None = None) -> tuple[str, str, | ||
| 65 | 55 | return platform, subj, paths |
| 66 | 56 | |
| 67 | 57 | |
| 58 | def _group_packages(directory: Path, subject: str | None) -> dict[tuple[str, str], list[Path]]: | |
| 59 | """Group the package folders under *directory* by (platform, subject).""" | |
| 60 | groups: dict[tuple[str, str], list[Path]] = {} | |
| 61 | for child in sorted(directory.iterdir()): | |
| 62 | if not child.is_dir() or not any(child.glob("*.csv")): | |
| 63 | continue | |
| 64 | platform, subj = detect_platform(child.name) | |
| 65 | if platform == "unknown" or (subject and subj != subject): | |
| 66 | continue | |
| 67 | groups.setdefault((platform, subj), []).append(child) | |
| 68 | return groups | |
| 69 | ||
| 70 | ||
| 68 | 71 | @dataclass |
| 69 | 72 | class TrendRow: |
| 70 | 73 | """One rule's status across the timeline.""" |