refactor: format the review comment in one module

This commit is contained in:
Abdessamad Derraz committed 2026-11-23 21:44:08 +01:00
1 parent b5457d222b
commit 2c274e947e
3 files changed
+54 -59

No files matched your search

+26 -5
View File
@@ -7,8 +7,16 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING:
from resolve.review import ValidationResult
# The terminal's word for each check status. Any other status is "info".
MARKERS = {"PASS": "ok", "FAIL": "fail", "WARN": "warn"}
# Each check status as the pull request comment and the terminal word it.
# Any other status is an informational note.
WORDS = {"PASS": ("Pass", "ok"), "FAIL": ("Fail", "fail"), "WARN": ("Warning", "warn")}
INFO = ("Info", "info")
def _was_read(r: ValidationResult) -> bool:
"""A path refused before it was read has no size or digest to show:
empty fields read as an empty file contributed."""
return bool(r.sha1)
def json_rows(results: list[ValidationResult]) -> list[dict]:
@@ -45,20 +53,33 @@ def markdown_report(results: list[ValidationResult]) -> str:
lines.append("")
for r in results:
lines.append(r.to_markdown())
lines.append(file_markdown(r))
lines.append("")
return "\n".join(lines)
def file_markdown(r: ValidationResult) -> str:
"""One file's block of the pull request comment."""
lines = [f"### {r.status} `{r.filename}`", "", f"- **Path**: `{r.filepath}`"]
if _was_read(r):
lines.append(f"- **Size**: {r.size:,} bytes")
lines.append(f"- **SHA1**: `{r.sha1}`")
lines.append(f"- **MD5**: `{r.md5}`")
lines.append(f"- **CRC32**: `{r.crc32}`")
lines.append("")
lines.extend(f"- {WORDS.get(s, INFO)[0]}: {m}" for s, m in r.checks)
return "\n".join(lines)
def text_lines(results: list[ValidationResult]) -> list[str]:
"""The terminal report, each file headed by a blank line."""
lines: list[str] = []
for r in results:
lines.extend(("", f"[{r.status}] {r.filepath}"))
if r.sha1:
if _was_read(r):
lines.append(f" SHA1: {r.sha1}")
lines.append(f" MD5: {r.md5}")
lines.append(f" Size: {r.size:,}")
lines.extend(f" [{MARKERS.get(s, 'info')}] {m}" for s, m in r.checks)
lines.extend(f" [{WORDS.get(s, INFO)[1]}] {m}" for s, m in r.checks)
return lines