Skip to content

Commit c8e8695

Browse files
committed
refactor: unify job summary and PR comment rendering
Use a single report renderer for both surfaces with a plain '# Commit Check' title (dropping 'Policy Report') and identical content on success and failure. PR comment matching also accepts the old hyphenated title so existing comments are still updated rather than duplicated.
1 parent 1ab3be0 commit c8e8695

2 files changed

Lines changed: 38 additions & 35 deletions

File tree

‎main.py‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
from typing import Any
2121

2222
# Constants for message titles
23-
SUCCESS_TITLE = "# Commit-Check ✔️"
24-
FAILURE_TITLE = "# Commit-Check ❌"
23+
SUCCESS_TITLE = "# Commit Check ✔️"
24+
FAILURE_TITLE = "# Commit Check ❌"
2525
COMMIT_MESSAGE_DELIMITER = "\x00"
2626
RULES_URL = "https://commit-check.com/rules/"
2727

@@ -416,45 +416,41 @@ def _markdown_details(results: list[ScopeResult]) -> str:
416416
return "\n".join(sections)
417417

418418

419-
def render_job_summary(results: list[ScopeResult]) -> str:
420-
"""Create the Markdown body for the GitHub job summary."""
421-
header = "# Commit Check Policy Report"
419+
def render_report(results: list[ScopeResult], include_footer: bool = True) -> str:
420+
"""Render the Markdown report shared by the job summary and PR comment.
421+
422+
All checks passing collapses to a single success line; failures render
423+
a scope table with rule links plus collapsible failure details.
424+
"""
422425
if all(scope.status == "pass" for scope in results):
423426
scopes = "scope" if len(results) == 1 else "scopes"
424-
return f"{header}\n\n\u2705 **All checks passed** ({len(results)} {scopes})"
427+
return f"{SUCCESS_TITLE} All checks passed ({len(results)} {scopes})"
425428

426429
failures = _failure_count(results)
427430
unit = "failure" if failures == 1 else "failures"
428431
scopes = "scope" if len(results) == 1 else "scopes"
429432
lines = [
430-
header,
433+
FAILURE_TITLE,
431434
"",
432435
f"**{failures} {unit}** across {len(results)} {scopes}",
433436
"",
434437
_markdown_table(results),
435438
"",
436439
_markdown_details(results),
437-
"",
438-
f"_Rules reference: {RULES_URL}_",
439440
]
441+
if include_footer:
442+
lines.extend(["", f"_Rules reference: {RULES_URL}_"])
440443
return "\n".join(lines)
441444

442445

443-
def render_pr_comment(results: list[ScopeResult]) -> str:
444-
"""Create the Markdown body for the PR comment."""
445-
if all(scope.status == "pass" for scope in results):
446-
return f"{SUCCESS_TITLE} All checks passed"
446+
def render_job_summary(results: list[ScopeResult]) -> str:
447+
"""Create the Markdown body for the GitHub job summary."""
448+
return render_report(results, include_footer=True)
447449

448-
failures = _failure_count(results)
449-
unit = "failure" if failures == 1 else "failures"
450-
lines = [
451-
f"{FAILURE_TITLE} {failures} {unit}",
452-
"",
453-
_markdown_table(results),
454-
"",
455-
_markdown_details(results),
456-
]
457-
return "\n".join(lines)
450+
451+
def render_pr_comment(results: list[ScopeResult]) -> str:
452+
"""Create the Markdown body for the PR comment (same report as summary)."""
453+
return render_report(results, include_footer=True)
458454

459455

460456
def build_result_body(result_text: str | None) -> str:
@@ -610,6 +606,8 @@ def add_pr_comments(results: list[ScopeResult]) -> int:
610606
c
611607
for c in comments
612608
if c.body.startswith(SUCCESS_TITLE) or c.body.startswith(FAILURE_TITLE)
609+
# Match comments from older versions that used a hyphenated title.
610+
or c.body.startswith("# Commit-Check")
613611
]
614612

615613
if matching_comments:

‎main_test.py‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -631,11 +631,12 @@ def test_raw_text_fallback_is_printed(self):
631631
class TestRenderJobSummary(unittest.TestCase):
632632
def test_all_pass(self):
633633
body = main.render_job_summary([pass_scope("Branch")])
634-
self.assertIn("# Commit Check Policy Report", body)
635-
self.assertIn("✅ **All checks passed** (1 scope)", body)
634+
self.assertTrue(body.startswith(main.SUCCESS_TITLE))
635+
self.assertIn("All checks passed (1 scope)", body)
636636

637637
def test_failure_renders_table_with_rule_links(self):
638638
body = main.render_job_summary([fail_scope("Commit 1/1")])
639+
self.assertTrue(body.startswith(main.FAILURE_TITLE))
639640
self.assertIn("**1 failure** across 1 scope", body)
640641
self.assertIn("| Scope | Failed checks | Result |", body)
641642
self.assertIn(
@@ -653,16 +654,20 @@ def test_pass_scope_renders_checkmark(self):
653654

654655

655656
class TestRenderPrComment(unittest.TestCase):
656-
def test_all_pass_keeps_success_prefix(self):
657-
body = main.render_pr_comment([pass_scope("Branch")])
658-
self.assertTrue(body.startswith(main.SUCCESS_TITLE))
659-
self.assertIn("All checks passed", body)
660-
661-
def test_failure_keeps_failure_prefix_with_count(self):
662-
body = main.render_pr_comment([fail_scope("Commit 1/1")])
663-
self.assertTrue(body.startswith(main.FAILURE_TITLE))
664-
self.assertIn("1 failure", body)
665-
self.assertIn("| Scope | Failed checks | Result |", body)
657+
def test_all_pass_matches_job_summary(self):
658+
comment = main.render_pr_comment([pass_scope("Branch")])
659+
summary = main.render_job_summary([pass_scope("Branch")])
660+
self.assertEqual(comment, summary)
661+
self.assertTrue(comment.startswith(main.SUCCESS_TITLE))
662+
self.assertIn("All checks passed (1 scope)", comment)
663+
664+
def test_failure_matches_job_summary(self):
665+
comment = main.render_pr_comment([fail_scope("Commit 1/1")])
666+
summary = main.render_job_summary([fail_scope("Commit 1/1")])
667+
self.assertEqual(comment, summary)
668+
self.assertTrue(comment.startswith(main.FAILURE_TITLE))
669+
self.assertIn("**1 failure** across 1 scope", comment)
670+
self.assertIn("| Scope | Failed checks | Result |", comment)
666671

667672

668673
class TestBuildResultBody(unittest.TestCase):

0 commit comments

Comments
 (0)