Skip to content

Commit af141a7

Browse files
authored
fix: hold the PR title only to rules a title can meet (#337)
1 parent d68e05b commit af141a7

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎main.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,22 @@ def keep_ai_rules(scope: ScopeResult) -> ScopeResult:
502502
return skip_when_no_ai_rule_ran(scope)
503503

504504

505+
#: Rules about a commit rather than about a line of text: it has a body
506+
#: (CC011), a sign-off (CC012), an author that is no AI tool (CC017). A pull
507+
#: request title has neither body nor trailers nor an author of its own, so
508+
#: judged as a commit message it could never pass the first two, and the
509+
#: third would judge whatever git identity the runner has.
510+
COMMIT_ONLY_RULE_IDS = frozenset({"CC011", "CC012", "CC017"})
511+
512+
513+
def drop_commit_only_rules(scope: ScopeResult) -> ScopeResult:
514+
"""Narrow the PR title scope to the rules a one-line title can meet."""
515+
scope.checks = [
516+
c for c in scope.checks if c.get("rule_id") not in COMMIT_ONLY_RULE_IDS
517+
]
518+
return scope
519+
520+
505521
def get_pr_title() -> str | None:
506522
"""Read PR title from GitHub event payload."""
507523
if not is_pr_event():
@@ -972,7 +988,8 @@ def run_commit_check() -> tuple[int, list[ScopeResult]]:
972988
if PR_TITLE_ENABLED and is_pr_event():
973989
pr_title = get_pr_title()
974990
if pr_title:
975-
results.append(check_scope("PR title", ["--message"], input_text=pr_title))
991+
scope = check_scope("PR title", ["--message"], input_text=pr_title)
992+
results.append(drop_commit_only_rules(scope))
976993

977994
# ---- 2. PR description ------------------------------------------------
978995
if (PR_BODY_AI_ATTRIBUTION_ENABLED or ai_label_name()) and is_pr_event():

‎main_test.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,33 @@ def test_pr_title_check_runs_when_enabled(self):
10041004
"PR title", ["--message"], input_text="feat: a feature"
10051005
)
10061006

1007+
def test_pr_title_is_not_held_to_commit_only_rules(self):
1008+
# require_body and require_signed_off_by judge a commit; a title has
1009+
# no body or trailers, so it could never pass them.
1010+
title = main.ScopeResult(
1011+
label="PR title",
1012+
checks=[
1013+
make_check("message", "pass", "CC001", value="feat: add login"),
1014+
make_check("require_body", "fail", "CC011", value="feat: add login"),
1015+
make_check("require_signed_off_by", "fail", "CC012"),
1016+
make_check("ai_author", "fail", "CC017", value="Claude <x@y.z>"),
1017+
],
1018+
)
1019+
with (
1020+
patch("main.PR_TITLE_ENABLED", True),
1021+
patch("main.MESSAGE_ENABLED", False),
1022+
patch("main.BRANCH_ENABLED", False),
1023+
patch("main.AUTHOR_NAME_ENABLED", False),
1024+
patch("main.AUTHOR_EMAIL_ENABLED", False),
1025+
patch("main.is_pr_event", return_value=True),
1026+
patch("main.get_pr_title", return_value="feat: add login"),
1027+
patch("main.check_scope", return_value=title),
1028+
patch("main.run_other_checks", return_value=[]),
1029+
):
1030+
rc, results = main.run_commit_check()
1031+
self.assertEqual(rc, 0)
1032+
self.assertEqual([c["rule_id"] for c in results[0].checks], ["CC001"])
1033+
10071034
def test_pr_title_failure_propagates(self):
10081035
with (
10091036
patch("main.PR_TITLE_ENABLED", True),
@@ -4222,6 +4249,18 @@ def test_failing_message(self):
42224249
main.render_report([scope]),
42234250
)
42244251

4252+
def test_a_title_meets_only_the_rules_a_title_can(self):
4253+
with open(self.config, "w", encoding="utf-8") as f:
4254+
f.write("[commit]\nrequire_body = true\nrequire_signed_off_by = true\n")
4255+
rc, data = self._run("feat: add login page")
4256+
failed = {c["rule_id"] for c in data["checks"] if c["status"] == "fail"}
4257+
self.assertEqual(rc, 1)
4258+
self.assertEqual(failed, {"CC011", "CC012"})
4259+
scope = main.drop_commit_only_rules(
4260+
main.ScopeResult(label="PR title", checks=data["checks"])
4261+
)
4262+
self.assertEqual(scope.status, "pass")
4263+
42254264

42264265
class TestStderrIsNotPartOfTheJson(unittest.TestCase):
42274266
"""stdout is the JSON; stderr is what the CLI has to say to a person.

0 commit comments

Comments
 (0)