Skip to content

Commit 5aa913a

Browse files
authored
fix: name the lines to change in the PR description instead of repeating it (#326)
Co-authored-by: Xianpeng Shen <3353385+shenxianpeng@users.noreply.github.com>
1 parent 7e971e9 commit 5aa913a

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

‎main.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,47 @@ def get_pr_opener_tool() -> str | None:
463463
return AI_AGENT_BOTS.get(str(login).lower())
464464

465465

466+
def lines_removed(text: str, fixed: str) -> list[str]:
467+
"""The non-blank lines of ``text`` that ``fixed`` no longer has, in order.
468+
469+
A line repeated in ``text`` counts once per copy ``fixed`` dropped.
470+
"""
471+
kept: dict[str, int] = {}
472+
for line in fixed.splitlines():
473+
kept[line] = kept.get(line, 0) + 1
474+
removed = []
475+
for line in text.splitlines():
476+
if kept.get(line, 0):
477+
kept[line] -= 1
478+
elif line.strip():
479+
removed.append(line.strip())
480+
return removed
481+
482+
483+
def name_the_line_changes(scope: ScopeResult, body: str) -> None:
484+
"""Name the lines to change instead of repeating the description.
485+
486+
For a message the CLI's ``fix`` is the corrected text, short enough to
487+
paste. A description can run to a page, and the summary, the comment and
488+
the annotation would each repeat it in full; the lines to delete or add
489+
are what its author needs.
490+
"""
491+
for check in scope.checks:
492+
fixed = check.get("fix") or ""
493+
if not fixed:
494+
continue
495+
removed = lines_removed(body, fixed)
496+
added = lines_removed(fixed, body)
497+
parts = []
498+
if removed:
499+
parts.append("remove " + "; ".join(f'"{line}"' for line in removed))
500+
if added:
501+
parts.append("add " + "; ".join(f'"{line}"' for line in added))
502+
if parts:
503+
check["suggest"] = "In the description, " + ", and ".join(parts)
504+
check["fix"] = ""
505+
506+
466507
#: The AI tools found in the pull request description when it was read for
467508
#: the label only (``ai-label`` without ``pr-body-ai-attribution``), so it is no scope.
468509
PR_BODY_AI_TOOLS: list[str] = []
@@ -814,6 +855,7 @@ def run_commit_check() -> tuple[int, list[ScopeResult]]:
814855
"PR description", ["--ai-attribution-only"], input_text=pr_body
815856
)
816857
if PR_BODY_AI_ATTRIBUTION_ENABLED:
858+
name_the_line_changes(scope, pr_body)
817859
results.append(scope)
818860
else:
819861
PR_BODY_AI_TOOLS[:] = scope.ai_tools

‎main_test.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3737,6 +3737,89 @@ def test_no_description_nothing_to_check(self):
37373737
self.assertEqual(results, [])
37383738

37393739

3740+
class TestDescriptionLineChanges(unittest.TestCase):
3741+
BODY = "## Summary\nAdds a parser.\n\nLink to Devin run: https://app.devin.ai/sessions/1\n"
3742+
3743+
def _scope(self, *checks):
3744+
return main.ScopeResult(label="PR description", checks=list(checks))
3745+
3746+
def test_lines_removed_keeps_order_and_counts_copies(self):
3747+
self.assertEqual(main.lines_removed("a\nb\na\n\nc", "a\nc"), ["b", "a"])
3748+
3749+
def test_forbid_names_the_line_to_delete(self):
3750+
scope = self._scope(
3751+
{
3752+
"rule_id": "CC013",
3753+
"suggest": "Remove the AI lines.",
3754+
"fix": "## Summary\nAdds a parser.",
3755+
}
3756+
)
3757+
main.name_the_line_changes(scope, self.BODY)
3758+
self.assertEqual(
3759+
scope.checks[0]["suggest"],
3760+
'In the description, remove "Link to Devin run: https://app.devin.ai/sessions/1"',
3761+
)
3762+
self.assertEqual(scope.checks[0]["fix"], "")
3763+
3764+
def test_disclose_names_the_line_to_add_and_the_one_it_replaces(self):
3765+
body = "Adds a parser.\n\nCo-Authored-By: Claude <noreply@anthropic.com>"
3766+
scope = self._scope(
3767+
{
3768+
"rule_id": "CC015",
3769+
"suggest": "x",
3770+
"fix": "Adds a parser.\n\nAssisted-by: Claude",
3771+
},
3772+
{"rule_id": "CC014", "suggest": "x", "fix": body + "\nAssisted-by: Claude"},
3773+
)
3774+
main.name_the_line_changes(scope, body)
3775+
self.assertEqual(
3776+
[c["suggest"] for c in scope.checks],
3777+
[
3778+
(
3779+
'In the description, remove "Co-Authored-By: Claude'
3780+
' <noreply@anthropic.com>", and add "Assisted-by: Claude"'
3781+
),
3782+
'In the description, add "Assisted-by: Claude"',
3783+
],
3784+
)
3785+
3786+
def test_a_check_without_a_fix_or_a_change_is_left_alone(self):
3787+
scope = self._scope(
3788+
{"rule_id": "CC013", "suggest": "s", "fix": ""},
3789+
{"rule_id": "CC013", "suggest": "s", "fix": "same"},
3790+
)
3791+
main.name_the_line_changes(scope, "same")
3792+
self.assertEqual(
3793+
scope.checks,
3794+
[
3795+
{"rule_id": "CC013", "suggest": "s", "fix": ""},
3796+
{"rule_id": "CC013", "suggest": "s", "fix": "same"},
3797+
],
3798+
)
3799+
3800+
def test_run_names_the_lines_for_a_reported_description(self):
3801+
scope = self._scope(
3802+
{
3803+
"rule_id": "CC013",
3804+
"status": "fail",
3805+
"suggest": "s",
3806+
"fix": "## Summary\nAdds a parser.",
3807+
}
3808+
)
3809+
with (
3810+
patch("main.PR_TITLE_ENABLED", False),
3811+
patch("main.PR_BODY_AI_ATTRIBUTION_ENABLED", True),
3812+
patch.dict(os.environ, {"AI_LABEL": "false"}),
3813+
patch("main.MESSAGE_ENABLED", False),
3814+
patch("main.is_pr_event", return_value=True),
3815+
patch("main.get_pr_body", return_value=self.BODY),
3816+
patch("main.check_scope", return_value=scope),
3817+
patch("main.run_other_checks", return_value=[]),
3818+
):
3819+
_rc, results = main.run_commit_check()
3820+
self.assertEqual(results[0].checks[0]["fix"], "")
3821+
3822+
37403823
class TestAiToolsFound(unittest.TestCase):
37413824
def setUp(self):
37423825
main.PR_BODY_AI_TOOLS.clear()

0 commit comments

Comments
 (0)