| 67 | |
| 68 | |
| 69 | class Reporter: |
| 70 | def __init__(self, issues: list, report_format: str): |
| 71 | self.issues = issues |
| 72 | self.format = report_format |
| 73 | |
| 74 | def generate(self) -> str: |
| 75 | if self.format == "json": |
| 76 | return self.to_json() |
| 77 | if self.format == "sarif": |
| 78 | return self.to_sarif() |
| 79 | if self.format == "html": |
| 80 | return self.to_html() |
| 81 | return self.to_console() |
| 82 | |
| 83 | |
| 84 | def to_console(self) -> str: |
| 85 | if not self.issues: |
| 86 | return "\nNo issues found." |
| 87 | |
| 88 | output = [] |
| 89 | severity_order = ["CRITICAL", "HIGH", "MEDIUM", "LOW"] |
| 90 | |
| 91 | issues_by_severity: dict[str, list] = {} |
| 92 | for issue in self.issues: |
| 93 | severity = _severity_key(issue) |
| 94 | issues_by_severity.setdefault(severity, []).append(issue) |
| 95 | |
| 96 | for severity in severity_order: |
| 97 | if severity not in issues_by_severity: |
| 98 | continue |
| 99 | |
| 100 | sorted_issues = sorted( |
| 101 | issues_by_severity[severity], |
| 102 | key=lambda i: (i.file_path, i.line_number), |
| 103 | ) |
| 104 | |
| 105 | output.append(f"\n{'='*60}") |
| 106 | output.append( |
| 107 | f" {severity} ({len(sorted_issues)} issue{'s' if len(sorted_issues) != 1 else ''})" |
| 108 | ) |
| 109 | output.append(f"{'='*60}") |
| 110 | |
| 111 | for issue in sorted_issues: |
| 112 | output.append( |
| 113 | f"\n[+] Rule ID: {issue.rule_id}\n" |
| 114 | f" Description: {issue.description}\n" |
| 115 | f" File: {issue.file_path}:{issue.line_number}\n" |
| 116 | f" Code: `{issue.code.strip()}`" |
| 117 | ) |
| 118 | |
| 119 | return "\n".join(output) |
| 120 | |
| 121 | # ------------------------------------------------------------------ # |
| 122 | # JSON # |
| 123 | # ------------------------------------------------------------------ # |
| 124 | |
| 125 | def to_json(self) -> str: |
| 126 | report = { |
no outgoing calls