|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from socketsecurity.config import CliConfig |
| 5 | +from socketsecurity.core.alert_selection import ( |
| 6 | + clone_diff_with_selected_alerts, |
| 7 | + select_diff_alerts, |
| 8 | +) |
| 9 | +from socketsecurity.core.classes import Diff |
| 10 | +from socketsecurity.core.messages import Messages |
| 11 | + |
| 12 | + |
| 13 | +@dataclass(frozen=True) |
| 14 | +class ReportReferences: |
| 15 | + full_scan_id: str |
| 16 | + report_url: Optional[str] |
| 17 | + diff_url: Optional[str] |
| 18 | + is_full_scan: bool |
| 19 | + |
| 20 | + def console_link(self) -> Optional[tuple[str, str]]: |
| 21 | + if self.is_full_scan: |
| 22 | + if self.report_url: |
| 23 | + return "Report Url", self.report_url |
| 24 | + if self.diff_url: |
| 25 | + return "Report Url", self.diff_url |
| 26 | + else: |
| 27 | + if self.diff_url: |
| 28 | + return "Diff Url", self.diff_url |
| 29 | + if self.report_url: |
| 30 | + return "Report Url", self.report_url |
| 31 | + return None |
| 32 | + |
| 33 | + |
| 34 | +def build_report_references(diff_report: Diff) -> ReportReferences: |
| 35 | + return ReportReferences( |
| 36 | + full_scan_id=getattr(diff_report, "id", ""), |
| 37 | + report_url=getattr(diff_report, "report_url", None), |
| 38 | + diff_url=getattr(diff_report, "diff_url", ""), |
| 39 | + is_full_scan=getattr(diff_report, "is_full_scan", False), |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def build_console_summary(diff_report: Diff, config: CliConfig) -> str: |
| 44 | + references = build_report_references(diff_report) |
| 45 | + is_full_scan = references.is_full_scan |
| 46 | + link = references.console_link() |
| 47 | + |
| 48 | + if is_full_scan and not getattr(diff_report, "alerts_fetched", False): |
| 49 | + lines = ["Full scan completed. Findings were not fetched for console output."] |
| 50 | + if link: |
| 51 | + lines.append(f"{link[0]}: {link[1]}") |
| 52 | + return "\n".join(lines) |
| 53 | + |
| 54 | + selected_alerts = select_diff_alerts( |
| 55 | + diff_report, |
| 56 | + strict_blocking=config.strict_blocking, |
| 57 | + ) |
| 58 | + has_new_alerts = len(selected_alerts) > 0 |
| 59 | + has_unchanged_alerts = ( |
| 60 | + config.strict_blocking |
| 61 | + and hasattr(diff_report, "unchanged_alerts") |
| 62 | + and len(diff_report.unchanged_alerts) > 0 |
| 63 | + ) |
| 64 | + |
| 65 | + if not has_new_alerts and not has_unchanged_alerts: |
| 66 | + if link: |
| 67 | + return f"No issues found\n{link[0]}: {link[1]}" |
| 68 | + return "No issues found" |
| 69 | + |
| 70 | + new_blocking = sum(1 for issue in diff_report.new_alerts if issue.error) |
| 71 | + new_warning = sum(1 for issue in diff_report.new_alerts if issue.warn) |
| 72 | + |
| 73 | + unchanged_blocking = 0 |
| 74 | + unchanged_warning = 0 |
| 75 | + if has_unchanged_alerts: |
| 76 | + unchanged_blocking = sum( |
| 77 | + 1 for issue in diff_report.unchanged_alerts if issue.error |
| 78 | + ) |
| 79 | + unchanged_warning = sum( |
| 80 | + 1 for issue in diff_report.unchanged_alerts if issue.warn |
| 81 | + ) |
| 82 | + |
| 83 | + selected_diff = clone_diff_with_selected_alerts(diff_report, selected_alerts) |
| 84 | + console_security_comment = Messages.create_console_security_alert_table( |
| 85 | + selected_diff |
| 86 | + ) |
| 87 | + |
| 88 | + blocking_label = "Blocking issues" if is_full_scan else "NEW blocking issues" |
| 89 | + warning_label = "Warning issues" if is_full_scan else "NEW warning issues" |
| 90 | + |
| 91 | + lines = ["Security issues detected by Socket Security:"] |
| 92 | + if new_blocking > 0: |
| 93 | + lines.append(f" - {blocking_label}: {new_blocking}") |
| 94 | + if new_warning > 0: |
| 95 | + lines.append(f" - {warning_label}: {new_warning}") |
| 96 | + if unchanged_blocking > 0: |
| 97 | + lines.append( |
| 98 | + f" - EXISTING blocking issues: {unchanged_blocking} " |
| 99 | + "(causing failure due to --strict-blocking)" |
| 100 | + ) |
| 101 | + if unchanged_warning > 0: |
| 102 | + lines.append(f" - EXISTING warning issues: {unchanged_warning}") |
| 103 | + if is_full_scan: |
| 104 | + lines.append( |
| 105 | + " Reported against the whole repository, with no baseline to compare " |
| 106 | + "against, so these do not affect the exit code." |
| 107 | + ) |
| 108 | + |
| 109 | + if link: |
| 110 | + lines.append(f"{link[0]}: {link[1]}") |
| 111 | + lines.append("") |
| 112 | + lines.append(str(console_security_comment)) |
| 113 | + return "\n".join(lines) |
| 114 | + |
| 115 | + |
| 116 | +def build_socket_json_report( |
| 117 | + diff_report: Diff, |
| 118 | + config: CliConfig, |
| 119 | +) -> dict[str, object]: |
| 120 | + references = build_report_references(diff_report) |
| 121 | + selected_alerts = select_diff_alerts( |
| 122 | + diff_report, |
| 123 | + strict_blocking=config.strict_blocking, |
| 124 | + ) |
| 125 | + selected_diff = clone_diff_with_selected_alerts(diff_report, selected_alerts) |
| 126 | + report = Messages.create_security_comment_json(selected_diff) |
| 127 | + legal_flag = getattr(config, "legal", False) |
| 128 | + repo = getattr(config, "repo", None) |
| 129 | + branch = getattr(config, "branch", None) |
| 130 | + commit_sha = getattr(config, "commit_sha", None) |
| 131 | + report["full_scan_id"] = references.full_scan_id |
| 132 | + report["diff_url"] = references.diff_url |
| 133 | + report["report_url"] = references.report_url |
| 134 | + report["repo"] = repo if isinstance(repo, str) or repo is None else None |
| 135 | + report["branch"] = branch if isinstance(branch, str) or branch is None else None |
| 136 | + report["commit_sha"] = ( |
| 137 | + commit_sha if isinstance(commit_sha, str) or commit_sha is None else None |
| 138 | + ) |
| 139 | + report["legal_mode"] = legal_flag if isinstance(legal_flag, bool) else False |
| 140 | + return report |
0 commit comments