Skip to content

Commit a617797

Browse files
authored
feat: say why checks were skipped in the report (#300)
Co-authored-by: Xianpeng Shen <3353385+shenxianpeng@users.noreply.github.com>
1 parent 400525e commit a617797

2 files changed

Lines changed: 111 additions & 12 deletions

File tree

‎main.py‎

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ def status(self) -> str:
146146
return "skip"
147147
return "pass"
148148

149+
@property
150+
def skip_reason(self) -> str:
151+
"""Why a skipped scope did not run, or ``""`` when nobody said.
152+
153+
Newer commit-check releases name the reason on each skipped check (e.g.
154+
"author dependabot[bot] is in [commit].ignore_authors"). A bare
155+
"skipped" left the reader unable to tell a deliberate bypass from a
156+
broken setup.
157+
"""
158+
if self.status != "skip":
159+
return ""
160+
return next((c["reason"] for c in self.checks if c.get("reason")), "")
161+
149162
@property
150163
def failures(self) -> list[dict[str, str]]:
151164
"""The checks that failed in this scope."""
@@ -362,6 +375,7 @@ def skipped_author_scope(flag: str) -> ScopeResult:
362375
"error": "",
363376
"suggest": "",
364377
"docs_url": "",
378+
"reason": "the pull request's head commit could not be resolved",
365379
}
366380
],
367381
)
@@ -843,7 +857,8 @@ def _render_scopes(scopes: list[ScopeResult], include_docs: bool) -> list[str]:
843857
if scope.status == "skip":
844858
# Deliberately not a ✔. Nothing was validated here, and a tick
845859
# claiming otherwise is what made a bypassed policy look enforced.
846-
lines.append(f" ⊘ {label} (skipped)")
860+
reason = scope.skip_reason
861+
lines.append(f" ⊘ {label} (skipped{f': {reason}' if reason else ''})")
847862
continue
848863
if scope.status == "pass":
849864
value = _scope_value(scope)
@@ -988,6 +1003,8 @@ def render_step_log(results: list[ScopeResult]) -> None:
9881003
)
9891004
if total and skipped == total:
9901005
print("\u2298 commit-check: all checks skipped, nothing was validated")
1006+
if _skip_reasons(results):
1007+
print(_skip_reasons(results))
9911008
elif warned or skipped:
9921009
passed = total - skipped - warned
9931010
tail = []
@@ -998,6 +1015,8 @@ def render_step_log(results: list[ScopeResult]) -> None:
9981015
print(
9991016
f"\u2714 commit-check: {passed} of {total} checks passed, {', '.join(tail)}"
10001017
)
1018+
if _skip_reasons(results):
1019+
print(_skip_reasons(results))
10011020
else:
10021021
print("\u2714 commit-check: all checks passed")
10031022

@@ -1031,6 +1050,22 @@ def _skip_count(results: list[ScopeResult]) -> int:
10311050
return sum(1 for scope in results if scope.status == "skip")
10321051

10331052

1053+
def _skip_reasons(results: list[ScopeResult]) -> str:
1054+
"""One sentence naming why checks were skipped, or ``""`` if unknown.
1055+
1056+
The verdict line is all most readers see, so it stays short: the config
1057+
section is dropped ("[commit].ignore_authors" and "[branch].ignore_authors"
1058+
both read "ignore_authors"), which folds the usual bot case into one
1059+
reason. The per-scope lines in the collapsed block keep the section.
1060+
"""
1061+
reasons = list(
1062+
dict.fromkeys(
1063+
re.sub(r"\[\w+\]\.", "", s.skip_reason) for s in results if s.skip_reason
1064+
)
1065+
)
1066+
return f"Skipped because {'; '.join(reasons)}." if reasons else ""
1067+
1068+
10341069
def _warn_count(results: list[ScopeResult]) -> int:
10351070
"""Number of scopes that carry at least one warning.
10361071
@@ -1211,17 +1246,22 @@ def _scope_value(scope: ScopeResult, max_len: int = 60) -> str:
12111246
#
12121247
# ⊘ **All 5 checks skipped** — nothing was validated
12131248
#
1249+
# Skipped because author dependabot[bot] is in ignore_authors.
1250+
#
12141251
# ```text
12151252
# Commit message
1216-
# ⊘ PR title (skipped)
1217-
# ⊘ Commit 1/1 (skipped)
1253+
# ⊘ PR title (skipped: author dependabot[bot] is in [commit].ignore_authors)
1254+
# ⊘ Commit 1/1 (skipped: author dependabot[bot] is in [commit].ignore_authors)
12181255
# Branch
1219-
# ⊘ Branch (skipped)
1256+
# ⊘ Branch (skipped: author dependabot[bot] is in [branch].ignore_authors)
12201257
# Author
1221-
# ⊘ Author name (skipped)
1222-
# ⊘ Author email (skipped)
1258+
# ⊘ Author name (skipped: author dependabot[bot] is in [commit].ignore_authors)
1259+
# ⊘ Author email (skipped: author dependabot[bot] is in [commit].ignore_authors)
12231260
# ```
12241261
#
1262+
# The reason comes from commit-check; an older CLI sends none, and the line
1263+
# reads "(skipped)" with no "Skipped because" sentence.
1264+
#
12251265
# A skipped scope deliberately carries no ✔ and no checked value: nothing was
12261266
# examined, so there is no value to report and no pass to claim. When only some
12271267
# scopes skip, the verdict reads "✅ **3 of 5 checks passed**, 2 skipped".
@@ -1387,6 +1427,8 @@ def render_report(results: list[ScopeResult]) -> str:
13871427
# checks passed" here is the defect this branch exists to prevent.
13881428
lines.append(f"⊘ **All {total} {unit} skipped** — nothing was validated")
13891429
lines.append("")
1430+
if _skip_reasons(results):
1431+
lines.extend([_skip_reasons(results), ""])
13901432
elif warned or skipped:
13911433
passed = total - skipped - warned
13921434
tail = []
@@ -1396,6 +1438,8 @@ def render_report(results: list[ScopeResult]) -> str:
13961438
tail.append(f"{skipped} skipped")
13971439
lines.append(f"✅ **{passed} of {total} {unit} passed**, {', '.join(tail)}")
13981440
lines.append("")
1441+
if _skip_reasons(results):
1442+
lines.extend([_skip_reasons(results), ""])
13991443
if warned:
14001444
lines.extend([_markdown_table(results, "warn", "Warnings"), ""])
14011445
else:

‎main_test.py‎

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ def test_pr_author_checks_are_skipped_on_a_shallow_clone(self):
10111011
"error": "",
10121012
"suggest": "",
10131013
"docs_url": "",
1014+
"reason": "the pull request's head commit could not be resolved",
10141015
}
10151016
],
10161017
)
@@ -2693,12 +2694,12 @@ def test_a_comment_without_an_author_is_not_a_bot(self):
26932694
self.assertEqual(main._find_own_comments([orphan]), (None, []))
26942695

26952696

2696-
def skip_scope(label: str = "PR title") -> main.ScopeResult:
2697+
def skip_scope(label: str = "PR title", reason: str = "") -> main.ScopeResult:
26972698
"""A scope whose every rule declined to run (e.g. author in ignore_authors)."""
2698-
return main.ScopeResult(
2699-
label=label,
2700-
checks=[make_check("message", status="skip", rule_id="CC001", value="")],
2701-
)
2699+
check = make_check("message", status="skip", rule_id="CC001", value="")
2700+
if reason:
2701+
check["reason"] = reason
2702+
return main.ScopeResult(label=label, checks=[check])
27022703

27032704

27042705
class TestSkippedScopes(unittest.TestCase):
@@ -2765,6 +2766,57 @@ def test_skipped_scope_reports_no_checked_value(self):
27652766
body = main.render_report([skip_scope("PR title")])
27662767
self.assertIn(" ⊘ PR title (skipped)", body)
27672768

2769+
def test_skip_reason_is_shown_on_the_scope_and_under_the_verdict(self):
2770+
"""A skip says why, so a deliberate bypass reads as one."""
2771+
commit = "author dependabot[bot] is in [commit].ignore_authors"
2772+
branch = "author dependabot[bot] is in [branch].ignore_authors"
2773+
results = [
2774+
skip_scope("PR title", reason=commit),
2775+
skip_scope("Commit 1/1", reason=commit),
2776+
skip_scope("Branch", reason=branch),
2777+
]
2778+
body = main.render_report(results)
2779+
self.assertIn(
2780+
"⊘ **All 3 checks skipped** — nothing was validated\n\n"
2781+
"Skipped because author dependabot[bot] is in ignore_authors.\n",
2782+
body,
2783+
)
2784+
self.assertIn(f" ⊘ PR title (skipped: {commit})", body)
2785+
self.assertIn(f" ⊘ Branch (skipped: {branch})", body)
2786+
2787+
def test_skip_reason_follows_a_partial_skip_verdict(self):
2788+
reason = "author dependabot[bot] is in [branch].ignore_authors"
2789+
results = [pass_scope("PR title"), skip_scope("Branch", reason=reason)]
2790+
body = main.render_report(results)
2791+
self.assertIn(
2792+
"✅ **1 of 2 checks passed**, 1 skipped\n\n"
2793+
"Skipped because author dependabot[bot] is in ignore_authors.\n",
2794+
body,
2795+
)
2796+
2797+
def test_skip_reason_reaches_the_step_log(self):
2798+
reason = "author dependabot[bot] is in [commit].ignore_authors"
2799+
with patch("sys.stdout", new_callable=io.StringIO) as out:
2800+
main.render_step_log([skip_scope("PR title", reason=reason)])
2801+
self.assertIn(f" ⊘ PR title (skipped: {reason})", out.getvalue())
2802+
self.assertIn(
2803+
"Skipped because author dependabot[bot] is in ignore_authors.",
2804+
out.getvalue(),
2805+
)
2806+
2807+
def test_skip_reason_follows_a_partial_skip_in_the_step_log(self):
2808+
reason = "author dependabot[bot] is in [branch].ignore_authors"
2809+
results = [pass_scope("PR title"), skip_scope("Branch", reason=reason)]
2810+
with patch("sys.stdout", new_callable=io.StringIO) as out:
2811+
main.render_step_log(results)
2812+
self.assertIn(
2813+
"Skipped because author dependabot[bot] is in ignore_authors.",
2814+
out.getvalue(),
2815+
)
2816+
2817+
def test_a_passing_scope_has_no_skip_reason(self):
2818+
self.assertEqual(pass_scope("Branch", value="main").skip_reason, "")
2819+
27682820
def test_older_engine_without_skip_is_unaffected(self):
27692821
"""Back-compat: engines that only emit pass/fail render as before."""
27702822
results = [pass_scope("Branch", value="main")]
@@ -3171,6 +3223,8 @@ def test_step_log_all_skipped_claims_no_pass(self):
31713223
"fix",
31723224
"docs_url",
31733225
}
3226+
#: Keys newer CLIs add on top: ``reason`` says why a check was skipped.
3227+
OPTIONAL_CHECK_KEYS = {"reason"}
31743228
STATUSES = {"pass", "fail", "warn", "skip"}
31753229

31763230

@@ -3233,7 +3287,8 @@ def _run(self, message: str) -> tuple[int, dict]:
32333287
self.assertIn("checks", data)
32343288
self.assertTrue(data["checks"], "CLI reported no checks")
32353289
for check in data["checks"]:
3236-
self.assertEqual(set(check), CHECK_KEYS, check)
3290+
self.assertLessEqual(CHECK_KEYS, set(check), check)
3291+
self.assertLessEqual(set(check), CHECK_KEYS | OPTIONAL_CHECK_KEYS, check)
32373292
self.assertIn(check["status"], STATUSES, check)
32383293
self.assertRegex(check["rule_id"], r"^CC\d{3}$")
32393294
self.assertTrue(

0 commit comments

Comments
 (0)