|
16 | 16 | Iterable, |
17 | 17 | Tuple, |
18 | 18 | ) |
19 | | - |
20 | 19 | import typer |
| 20 | +from pathlib import Path |
21 | 21 |
|
22 | 22 | stdout = print |
23 | 23 | stderr = partial(print, file=sys.stderr) |
@@ -101,6 +101,64 @@ def from_maven(report: str) -> Iterable[Issue]: |
101 | 101 | ) |
102 | 102 |
|
103 | 103 |
|
| 104 | +@dataclass(frozen=True) |
| 105 | +class SecurityIssue: |
| 106 | + file_name: str |
| 107 | + line: int |
| 108 | + column: int |
| 109 | + cwe: str |
| 110 | + test_id: str |
| 111 | + description: str |
| 112 | + references: tuple |
| 113 | + |
| 114 | + |
| 115 | +def from_json(report_str: str, prefix: Path) -> Iterable[SecurityIssue]: |
| 116 | + report = json.loads(report_str) |
| 117 | + issues = report.get("results", {}) |
| 118 | + for issue in issues: |
| 119 | + references = [] |
| 120 | + if issue["more_info"]: |
| 121 | + references.append(issue["more_info"]) |
| 122 | + if issue.get("issue_cwe", {}).get("link", None): |
| 123 | + references.append(issue["issue_cwe"]["link"]) |
| 124 | + yield SecurityIssue( |
| 125 | + file_name=issue["filename"].replace(str(prefix) + "/", ""), |
| 126 | + line=issue["line_number"], |
| 127 | + column=issue["col_offset"], |
| 128 | + cwe=str(issue["issue_cwe"].get("id", "")), |
| 129 | + test_id=issue["test_id"], |
| 130 | + description=issue["issue_text"], |
| 131 | + references=tuple(references) |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | +def issues_to_markdown(issues: Iterable[SecurityIssue]) -> str: |
| 136 | + template = cleandoc(""" |
| 137 | + {header}{rows} |
| 138 | + """) |
| 139 | + |
| 140 | + def _header(): |
| 141 | + header = "# Security\n\n" |
| 142 | + header += "|File|line/<br>column|Cwe|Test ID|Details|\n" |
| 143 | + header += "|---|:-:|:-:|:-:|---|\n" |
| 144 | + return header |
| 145 | + |
| 146 | + def _row(issue): |
| 147 | + row = "|" + issue.file_name + "|" |
| 148 | + row += f"line: {issue.line}<br>column: {issue.column}|" |
| 149 | + row += issue.cwe + "|" |
| 150 | + row += issue.test_id + "|" |
| 151 | + for element in issue.references: |
| 152 | + row += element + " ,<br>" |
| 153 | + row = row[:-5] + "|" |
| 154 | + return row |
| 155 | + |
| 156 | + return template.format( |
| 157 | + header=_header(), |
| 158 | + rows="\n".join(_row(i) for i in issues) |
| 159 | + ) |
| 160 | + |
| 161 | + |
104 | 162 | def security_issue_title(issue: Issue) -> str: |
105 | 163 | return f"🔐 {issue.cve}: {issue.coordinates}" |
106 | 164 |
|
@@ -159,7 +217,6 @@ def create_security_issue(issue: Issue, project="") -> Tuple[str, str]: |
159 | 217 | CVE_CLI = typer.Typer() |
160 | 218 | CLI.add_typer(CVE_CLI, name="cve", help="Work with CVE's") |
161 | 219 |
|
162 | | - |
163 | 220 | class Format(str, Enum): |
164 | 221 | Maven = "maven" |
165 | 222 |
|
@@ -257,6 +314,21 @@ def create( |
257 | 314 | stdout(format_jsonl(issue_url, issue)) |
258 | 315 |
|
259 | 316 |
|
| 317 | +class PPrintFormats(str, Enum): |
| 318 | + markdown = "markdown" |
| 319 | + |
| 320 | + |
| 321 | +@CLI.command(name="pretty-print") |
| 322 | +def json_issue_to_markdown( |
| 323 | + json_file: typer.FileText = typer.Argument(mode="r", help="json file with issues to convert"), |
| 324 | + path: Path = typer.Argument(default=Path("."), help="path to project root") |
| 325 | +) -> None: |
| 326 | + content = json_file.read() |
| 327 | + issues = from_json(content, path.absolute()) |
| 328 | + issues = sorted(issues, key=lambda i: (i.file_name, i.cwe, i.test_id)) |
| 329 | + print(issues_to_markdown(issues)) |
| 330 | + |
| 331 | + |
260 | 332 | def format_jsonl(issue_url: str, issue: Issue) -> str: |
261 | 333 | issue_json = asdict(issue) |
262 | 334 | issue_json["issue_url"] = issue_url.strip() |
|
0 commit comments