|
15 | 15 | Iterable, |
16 | 16 | Tuple, |
17 | 17 | ) |
18 | | - |
| 18 | +from noxconfig import PROJECT_CONFIG |
19 | 19 | import typer |
20 | 20 |
|
21 | 21 | stdout = print |
@@ -100,6 +100,43 @@ def from_maven(report: str) -> Iterable[Issue]: |
100 | 100 | ) |
101 | 101 |
|
102 | 102 |
|
| 103 | +def from_json(report: str) -> Iterable[Issue]: |
| 104 | + report = json.loads(report) |
| 105 | + issues = report.get("results", {}) |
| 106 | + for issue in issues: |
| 107 | + references = [] |
| 108 | + if issue["more_info"]: |
| 109 | + references.append(issue["more_info"]) |
| 110 | + if issue.get("issue_cve", {}).get("link", None): |
| 111 | + references.append(issue["issue_cve"]["link"]) |
| 112 | + if issue.get("issue_cwe", {}).get("link", None): |
| 113 | + references.append(issue["issue_cwe"]["link"]) |
| 114 | + yield Issue( |
| 115 | + cve=str(issue.get("issue_cve", {}).get("id", "")), |
| 116 | + cwe=str(issue.get("issue_cwe", {}).get("id", "")), |
| 117 | + description=issue["issue_text"], |
| 118 | + coordinates=issue["filename"].replace( |
| 119 | + str(PROJECT_CONFIG.root) + "/", "" |
| 120 | + ) + f":{issue["line_number"]}:{issue["col_offset"]}:", |
| 121 | + references=tuple(references) |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +def issues_to_markdown(issues: Iterable[Issue]) -> str: |
| 126 | + markdown_str = "" |
| 127 | + markdown_str += "# Security\n\n" |
| 128 | + markdown_str += "|File|Cve|Cwe|Details|\n" |
| 129 | + markdown_str += "|---|:-:|:-:|---|\n" |
| 130 | + for issue in issues: |
| 131 | + row = "|" + issue.coordinates + "|" |
| 132 | + row += issue.cve + "|" |
| 133 | + row += issue.cwe + "|" |
| 134 | + for element in issue.references: |
| 135 | + row += element + " ,<br>" |
| 136 | + markdown_str += row[:-5] + "|\n" |
| 137 | + return markdown_str |
| 138 | + |
| 139 | + |
103 | 140 | def security_issue_title(issue: Issue) -> str: |
104 | 141 | return f"🔐 {issue.cve}: {issue.coordinates}" |
105 | 142 |
|
@@ -149,15 +186,16 @@ def create_security_issue(issue: Issue, project="") -> Tuple[str, str]: |
149 | 186 | raise ex |
150 | 187 |
|
151 | 188 | std_err = result.stderr.decode("utf-8") |
152 | | - std_out = result.stdout.decode("utf-8") |
| 189 | + std_out = result.stdout.decode("utf-8r") |
153 | 190 |
|
154 | 191 | return std_err, std_out |
155 | 192 |
|
156 | 193 |
|
157 | 194 | CLI = typer.Typer() |
158 | 195 | CVE_CLI = typer.Typer() |
| 196 | +PP_CLI = typer.Typer() |
159 | 197 | CLI.add_typer(CVE_CLI, name="cve", help="Work with CVE's") |
160 | | - |
| 198 | +CLI.add_typer(PP_CLI, name="prettyprint", help="Prints pretty") |
161 | 199 |
|
162 | 200 | class Format(str, Enum): |
163 | 201 | Maven = "maven" |
@@ -256,6 +294,15 @@ def create( |
256 | 294 | stdout(format_jsonl(issue_url, issue)) |
257 | 295 |
|
258 | 296 |
|
| 297 | +@PP_CLI.command(name="markdown") |
| 298 | +def json_issue_to_markdown( |
| 299 | + json_file: str = typer.Argument(help="json file with issues to convert"), |
| 300 | +) -> None: |
| 301 | + with open(json_file, "r") as file: |
| 302 | + issues_ = from_json(file.read()) |
| 303 | + print(issues_to_markdown(issues_)) |
| 304 | + |
| 305 | + |
259 | 306 | def format_jsonl(issue_url: str, issue: Issue) -> str: |
260 | 307 | issue_json = asdict(issue) |
261 | 308 | issue_json["issue_url"] = issue_url.strip() |
|
0 commit comments