Skip to content

Commit

Permalink
Audit: Better errors message
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Jun 12, 2024
1 parent 919bb33 commit e1f9d2e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 7 additions & 1 deletion github_app_geo_project/module/audit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
"<a href='%s'>Output</a>" % output_url
)
message.title = "Output URL"
_LOGGER.debug(message)
if output_url is not None:
short_message.append(f"[See also]({output_url})")
finally:
Expand Down
21 changes: 19 additions & 2 deletions github_app_geo_project/module/audit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
"<br>\n".join(
[
Expand Down Expand Up @@ -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(
"<br>\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)
Expand Down
4 changes: 4 additions & 0 deletions github_app_geo_project/module/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e1f9d2e

Please sign in to comment.