From e1f9d2ead6738b9e4acd8fae1ccf709299774968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Wed, 12 Jun 2024 09:34:51 +0200 Subject: [PATCH] Audit: Better errors message --- .../module/audit/__init__.py | 8 ++++++- github_app_geo_project/module/audit/utils.py | 21 +++++++++++++++++-- github_app_geo_project/module/utils.py | 4 ++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/github_app_geo_project/module/audit/__init__.py b/github_app_geo_project/module/audit/__init__.py index 611b3f87e8e..ae9660cdda0 100644 --- a/github_app_geo_project/module/audit/__init__.py +++ b/github_app_geo_project/module/audit/__init__.py @@ -200,8 +200,9 @@ async def _process_snyk_dpkg( if python_version: _use_python_version(python_version) + logs_url = urllib.parse.urljoin(context.service_url, f"logs/{context.job_id}") result, body, short_message, new_success = await audit_utils.snyk( - branch, context.module_config.get("snyk", {}), local_config.get("snyk", {}) + branch, context.module_config.get("snyk", {}), local_config.get("snyk", {}), logs_url ) success &= new_success output_url = _process_error( @@ -211,6 +212,11 @@ async def _process_snyk_dpkg( [{"title": m.title, "children": [m.to_html("no-title")]} for m in result], ", ".join(short_message), ) + message: module_utils.Message = module_utils.HtmlMessage( + "Output" % output_url + ) + message.title = "Output URL" + _LOGGER.debug(message) if output_url is not None: short_message.append(f"[See also]({output_url})") finally: diff --git a/github_app_geo_project/module/audit/utils.py b/github_app_geo_project/module/audit/utils.py index 2fa734def7e..837e4a86447 100644 --- a/github_app_geo_project/module/audit/utils.py +++ b/github_app_geo_project/module/audit/utils.py @@ -23,7 +23,10 @@ async def snyk( - branch: str, config: configuration.SnykConfiguration, local_config: configuration.SnykConfiguration + branch: str, + config: configuration.SnykConfiguration, + local_config: configuration.SnykConfiguration, + logs_url: str, ) -> tuple[list[module_utils.Message], module_utils.Message | None, list[str], bool]: """ Audit the code with Snyk. @@ -285,6 +288,7 @@ async def snyk( high_vulnerabilities: dict[str, int] = {} fixable_vulnerabilities: dict[str, int] = {} + fixable_vulnerabilities_summary: dict[str, str] = {} for row in test_json: message = module_utils.HtmlMessage( "\n".join( @@ -329,6 +333,8 @@ async def snyk( title += " [Patch available]." else: title += "." + if vuln.get("fixedIn", []) or vuln.get("isUpgradable", False) or vuln.get("isPatchable", False): + fixable_vulnerabilities_summary[vuln["id"]] = title message = module_utils.HtmlMessage( "
\n".join( [ @@ -368,8 +374,19 @@ async def snyk( snyk_fix_success = snyk_fix_proc.returncode == 0 if snyk_fix_proc.returncode != 0: message.title = "Error while fixing the project" - _LOGGER.error(message) + _LOGGER.warning(message) result.append(message) + message = module_utils.HtmlMessage( + "
\n".join( + [ + *fixable_vulnerabilities_summary.values(), + f"{os.path.basename(os.getcwd())}:{branch}", + f"See logs: {logs_url}", + ] + ) + ) + message.title = f"Unable to fix {len(fixable_vulnerabilities)} vulnerabilities" + _LOGGER.error(message) else: message.title = "Snyk fix applied" _LOGGER.debug(message) diff --git a/github_app_geo_project/module/utils.py b/github_app_geo_project/module/utils.py index 7f48cfb7eae..b11db129118 100644 --- a/github_app_geo_project/module/utils.py +++ b/github_app_geo_project/module/utils.py @@ -308,6 +308,10 @@ def __str__(self) -> str: """Get the string representation.""" return self.to_plain_text() + def __repr__(self) -> str: + """Get the string representation.""" + return self.to_plain_text() + def to_plain_text(self) -> str: """Get the ANSI message.""" sanitizer = html_sanitizer.Sanitizer(