Skip to content

Commit 5155d89

Browse files
committed
fix: don't mark a subject red when only its author is wrong
When a commit failed only CC017 (an AI tool as its author), the diff showed its subject as a red line, as if the subject had to change. The subject is now red only when the advice is about it. An empty message also no longer shows the commit's author where its subject would be.
1 parent d68e05b commit 5155d89

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

‎main.py‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,12 +1144,18 @@ def _first_value(scope: ScopeResult) -> str:
11441144
11451145
The text the action piped in when it has it, then the whole message the
11461146
``message`` rule reports, then whichever rule names a value first. A PR
1147-
description has no such line: its first line does not identify it.
1147+
description has no such line: its first line does not identify it. An
1148+
AI rule's value is the tool or the author it found, never the text, so
1149+
it never stands in for a commit's subject.
11481150
"""
11491151
if scope.label == "PR description":
11501152
return ""
11511153
values = [scope.text, _message_value(scope)]
1152-
values.extend(str(check.get("value") or "") for check in scope.checks)
1154+
values.extend(
1155+
str(check.get("value") or "")
1156+
for check in scope.checks
1157+
if check.get("rule_id") not in AI_RULE_IDS
1158+
)
11531159
for value in values:
11541160
if value.strip():
11551161
return value.strip().splitlines()[0].strip()
@@ -1680,23 +1686,31 @@ def _hunk(scope: ScopeResult, status: str) -> list[str]:
16801686
"""
16811687
is_commit = bool(scope.sha) and scope.label.startswith("Commit")
16821688
lines = [f"@@ {scope.sha[:7] if is_commit else scope.label} @@"]
1689+
value = _first_value(scope)
16831690
removed: list[str] = []
16841691
added: list[str] = []
16851692
advice: list[str] = []
1693+
# Whether a piece of advice is about the value itself: a subject or a
1694+
# branch name that has to change. Advice about something else, such as
1695+
# an AI tool as the commit's author, leaves the subject as context.
1696+
about_value = False
16861697
for check in _findings(scope, status):
16871698
minus, plus = _line_edits(scope, check)
16881699
removed.extend(minus)
16891700
added.extend(plus)
16901701
if not (minus or plus):
16911702
advice.append(_advice(check))
1703+
checked = str(check.get("value") or "").strip()
1704+
about_value |= not checked or checked.splitlines()[0].strip() == value
16921705
if scope.raw_text and not scope.checks:
16931706
advice.append("the output could not be parsed, see the step log")
1707+
about_value = True
16941708
removed, added, advice = (list(dict.fromkeys(x)) for x in (removed, added, advice))
1695-
value = _first_value(scope)
16961709
if value and value not in removed:
1697-
# Advice is about the value itself, so it is what was found; a fix
1698-
# that only touches other lines leaves it as context.
1699-
lines.append(f"{'-' if advice else ' '} {value}")
1710+
# Advice about the value means it is what was found; a fix that only
1711+
# touches other lines, or advice about something else, leaves it as
1712+
# context.
1713+
lines.append(f"{'-' if about_value else ' '} {value}")
17001714
lines.extend(f"- {line}" for line in removed)
17011715
lines.extend(f"+ {line}" for line in added)
17021716
lines.extend(f"# {line}" for line in advice)

‎main_test.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,45 @@ def test_advice_is_about_the_value_so_the_value_is_red(self):
25592559
],
25602560
)
25612561

2562+
def test_an_ai_author_leaves_the_subject_as_context(self):
2563+
# CC017 is about who made the commit, not what its subject says.
2564+
scope = main.ScopeResult(
2565+
label="Commit 1/1",
2566+
sha=SHA_A,
2567+
text="fix: handle the empty case\n",
2568+
checks=[
2569+
make_check(
2570+
"message", "pass", "CC001", value="fix: handle the empty case"
2571+
),
2572+
make_check(
2573+
"ai_author",
2574+
"fail",
2575+
"CC017",
2576+
value="Claude <noreply@anthropic.com>",
2577+
suggest="Commit as yourself",
2578+
),
2579+
],
2580+
)
2581+
self.assertEqual(
2582+
self._hunks([scope]),
2583+
["@@ d87faca @@", " fix: handle the empty case", "# Commit as yourself"],
2584+
)
2585+
2586+
def test_an_ai_rules_value_never_stands_in_for_the_subject(self):
2587+
# An empty message has no subject; the author CC017 reports is not one.
2588+
scope = main.ScopeResult(
2589+
label="Commit 1/1",
2590+
sha=SHA_A,
2591+
checks=[
2592+
make_check("message", "pass", "CC001"),
2593+
make_check(
2594+
"ai_author", "pass", "CC017", value="Jane <jane@example.com>"
2595+
),
2596+
],
2597+
)
2598+
self.assertEqual(main._first_value(scope), "")
2599+
self.assertEqual(main._render_scopes([scope], False), [" ✔ d87faca"])
2600+
25622601
def test_two_rules_that_ask_for_the_same_line_show_it_once(self):
25632602
scope = ai_scope()
25642603
scope.checks.append({**scope.checks[0], "rule_id": "CC015"})

0 commit comments

Comments
 (0)