From 7155e4a17950ee1e44c6736070c8d9f7ee5c4b43 Mon Sep 17 00:00:00 2001 From: Xianpeng Shen <3353385+shenxianpeng@users.noreply.github.com> Date: Fri, 4 Dec 2026 09:35:24 +0000 Subject: [PATCH 1/2] feat: check the pull request description for an issue and its length (CC502, CC503) Run commit-check 2.26.0. A pull request run asks the CLI with --pr-body: with pr-body-ai-attribution it checks every description rule, and without it [pull_request] require_issue_reference and description_min_length turn the check on by themselves, as in commit-check-ci. The pull request's author is passed as git's author, so commit.ignore_authors skips a bot's description. The ignored-policy skip names "require". --- README.md | 20 ++++++++- action.yml | 2 +- action/cli.py | 82 +++++++++++++++++++++++++----------- action/context.py | 5 +++ action/render.py | 28 +++++++++++-- requirements.txt | 2 +- tests/cli_test.py | 99 +++++++++++++++++++++++++++++++++++++++----- tests/conftest.py | 19 +++++++++ tests/report_test.py | 14 +++++++ 9 files changed, 227 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index ad0fbf9..4e48467 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ runners need a few tools — see [Good to know](#good-to-know). | `files` | `false` | Check the files each commit changes against the `[files]` rules of `cchk.toml` (size, prohibited paths, path length). Needs `fetch-depth: 0`: a commit the clone holds without its parent is skipped | | `pr-title` | `false` | Check the pull request title, the one that matters for squash merges, against the message rules a one-line title can meet: every `message` rule except a body (CC011), a sign-off (CC012) and an AI author (CC017). Pull request events only | | `message-ai-attribution` | `false` | Check every commit against the AI rules of `cchk.toml` (`ai_attribution`) only, author included, without the format rules. Takes the place of `message` | -| `pr-body-ai-attribution` | `false` | Check the pull request description against the AI rules of `cchk.toml` (`ai_attribution`, and the AI declaration boxes of `[pull_request] ai_declaration_checkboxes`) only. Pull request events only | +| `pr-body-ai-attribution` | `false` | Check the pull request description against the AI rules of `cchk.toml` (`ai_attribution`, and the AI declaration boxes of `[pull_request] ai_declaration_checkboxes`), and the issue and length rules below. Pull request events only | | `ai-label` | `false` | Label the pull request when an AI tool wrote any of it (a commit, the description, or an agent's app that opened it): `true` for `ai-assisted`, or the label's name. Labels are only added, never removed. Needs `pull-requests: write`; skipped on fork pull requests under `pull_request` | | `pr-comments` | `false` | Post the report as a pull request comment, edited in place on later runs. Needs `pull-requests: write`; skipped on fork pull requests | | `job-summary` | `true` | Write the report to the job summary | @@ -91,7 +91,7 @@ runners need a few tools — see [Good to know](#good-to-know). > [!TIP] > `pull_request` does not fire when a title or description is edited. With -> `pr-title` or `pr-body-ai-attribution`, add `types: [opened, synchronize, reopened, edited]` +> `pr-title`, `pr-body-ai-attribution` or a description rule, add `types: [opened, synchronize, reopened, edited]` > to re-check an edited pull request. ## Configuration @@ -234,6 +234,22 @@ ai_declaration_checkboxes = ["No AI tools were used", "AI tools were used"] > # Tick the box that applies: "No AI tools were used" or "AI tools were used" > ``` +The description can also be asked to name an issue (CC502) and to say enough +in the author's own words (CC503). These need no input: set them in +`cchk.toml` and every pull request run checks them. + +```toml +[pull_request] +require_issue_reference = true +description_min_length = 50 +``` + +> ```diff +> @@ PR description @@ +> # Name the issue this change is for, e.g. "Fixes #123" +> # Say what the change does and why +> ``` + With `ai-label: true`, a pull request an AI tool wrote also gets the `ai-assisted` label. diff --git a/action.yml b/action.yml index 70972b5..7919453 100644 --- a/action.yml +++ b/action.yml @@ -46,7 +46,7 @@ inputs: required: false default: false pr-body-ai-attribution: - description: check the pull request description against the AI attribution rules (ai_attribution in cchk.toml) + description: check the pull request description against the AI attribution rules (ai_attribution in cchk.toml); [pull_request] require_issue_reference and description_min_length in cchk.toml are checked without it required: false default: false ai-label: diff --git a/action/cli.py b/action/cli.py index ad78dc5..4f4ec7e 100644 --- a/action/cli.py +++ b/action/cli.py @@ -91,7 +91,16 @@ def skipped_files_scope(reason: str, sha: str = "") -> model.ScopeResult: #: Why an AI-only scope ran no rule: under the default policy there is none to run. -AI_POLICY_IGNORED = 'ai_attribution is "ignore": set it to "forbid" or "disclose"' +AI_POLICY_IGNORED = ( + 'ai_attribution is "ignore": set it to "forbid", "disclose" or "require"' +) + +#: The rules that read a pull request description for its AI declaration. +DECLARATION_RULE_IDS = frozenset({"CC501"}) + +#: The description rules that turn the description check on by themselves, +#: as they do in commit-check-ci: an issue reference and a minimum length. +DESCRIPTION_RULE_IDS = frozenset({"CC502", "CC503"}) def skip_when_no_ai_rule_ran(scope: model.ScopeResult) -> model.ScopeResult: @@ -100,11 +109,14 @@ def skip_when_no_ai_rule_ran(scope: model.ScopeResult) -> model.ScopeResult: Under the default ``ai_attribution = "ignore"`` the CLI builds none of the AI rules, so it returns an empty list of checks. Shown as a pass, that read as "checked, nothing found" to someone who turned the input on and - forgot the policy. + forgot the policy. A description checked for an issue or a length too + keeps those verdicts, and gains the skip beside them. """ - if scope.checks or scope.raw_text: + ai_rules = AI_RULE_IDS | DECLARATION_RULE_IDS + if scope.raw_text or any(c.get("rule_id") in ai_rules for c in scope.checks): return scope scope.checks = [ + *scope.checks, { "rule_id": "CC013", "check": "ai_attribution", @@ -114,7 +126,7 @@ def skip_when_no_ai_rule_ran(scope: model.ScopeResult) -> model.ScopeResult: "suggest": "", "docs_url": "", "reason": AI_POLICY_IGNORED, - } + }, ] return scope @@ -290,6 +302,42 @@ def check_scope( return model.ScopeResult(label=label, raw_text=raw, sha=sha) +def run_description_check() -> list[model.ScopeResult]: + """Check the pull request description, when anything asks for it. + + ``pr-body-ai-attribution: true`` holds it to every rule a description can + meet (``--pr-body``). Without it, ``[pull_request] require_issue_reference`` + or ``description_min_length`` in ``cchk.toml`` are enough on their own, as + in commit-check-ci, with the AI attribution rules left off; the CLI is + asked, since it is what reads the config, and the scope is reported only + when one of them ran or the config could not be read. + + An empty description is checked too: it names no issue, has no AI + declaration, and says nothing. The pull request's author goes with it as + git's author, so ``commit.ignore_authors`` skips a bot's description. + """ + pr_body = context.get_pr_body() or "" + login = context.get_pr_author_login() + env = {"GIT_AUTHOR_NAME": login} if login else None + if inputs.PR_BODY_AI_ATTRIBUTION_ENABLED: + scope = check_scope( + "PR description", ["--pr-body"], input_text=pr_body, env=env + ) + name_the_line_changes(scope, pr_body) + return [skip_when_no_ai_rule_ran(scope)] + scope = check_scope( + "PR description", + ["--pr-body", "--ai-attribution", "ignore"], + input_text=pr_body, + env=env, + ) + if pr_body: + # Read for ai-label as well: the AI tools are found whatever the policy. + PR_BODY_AI_TOOLS[:] = scope.ai_tools + ran = any(c.get("rule_id") in DESCRIPTION_RULE_IDS for c in scope.checks) + return [scope] if ran or scope.raw_text else [] + + def run_pr_message_checks(pr_commits: list[model.Commit]) -> list[model.ScopeResult]: """Check each PR commit individually via commit-check --message. @@ -500,9 +548,9 @@ def run_commit_check() -> tuple[int, list[model.ScopeResult]]: Checks are evaluated in order: 1. PR title (when ``pr-title: true`` and in a PR event) - 2. PR description, AI rules only (when ``pr-body-ai-attribution: true`` and in a PR - event; with only ``ai-label`` set it is read for the label, and no - scope is reported) + 2. PR description (in a PR event, when ``pr-body-ai-attribution: true`` + or ``cchk.toml`` turns on CC502 or CC503; otherwise it is only read + for ``ai-label``, and no scope is reported) 3. Individual PR commit messages (when ``message: true`` and in a PR event; AI rules only with ``message-ai-attribution: true``) 4. The files each PR commit changes (when ``files: true``; outside a PR, @@ -525,24 +573,8 @@ def run_commit_check() -> tuple[int, list[model.ScopeResult]]: results.append(check_scope("PR title", ["--pr-title"], input_text=pr_title)) # ---- 2. PR description ------------------------------------------------ - if ( - inputs.PR_BODY_AI_ATTRIBUTION_ENABLED or inputs.ai_label_name() - ) and context.is_pr_event(): - pr_body = context.get_pr_body() - if inputs.PR_BODY_AI_ATTRIBUTION_ENABLED: - # An empty description is checked too: it has no AI declaration, - # which CC501 fails when [pull_request] ai_declaration_checkboxes - # names the boxes. The attribution rules pass empty text. - scope = check_scope( - "PR description", ["--ai-attribution-only"], input_text=pr_body or "" - ) - name_the_line_changes(scope, pr_body or "") - results.append(skip_when_no_ai_rule_ran(scope)) - elif pr_body: - scope = check_scope( - "PR description", ["--ai-attribution-only"], input_text=pr_body - ) - PR_BODY_AI_TOOLS[:] = scope.ai_tools + if context.is_pr_event(): + results.extend(run_description_check()) # ---- 3. Commit message checks ----------------------------------------- pr_commits: list[model.Commit] | None = None diff --git a/action/context.py b/action/context.py index 35b5eab..ddf0373 100644 --- a/action/context.py +++ b/action/context.py @@ -185,6 +185,11 @@ def get_pr_body() -> str | None: return body if isinstance(body, str) and body.strip() else None +def get_pr_author_login() -> str: + """The login of the pull request's author, e.g. ``dependabot[bot]``.""" + return str((get_pr_event().get("user") or {}).get("login") or "") + + def get_pr_opener_tool() -> str | None: """The AI tool that opened the pull request from its own app, if any.""" login = (get_pr_event().get("user") or {}).get("login") or "" diff --git a/action/render.py b/action/render.py index 29fc48b..ed91780 100644 --- a/action/render.py +++ b/action/render.py @@ -315,6 +315,27 @@ def _first_value(scope: model.ScopeResult) -> str: return "" +def _description_findings(scope: model.ScopeResult) -> str: + """What a passing description was found to hold, rule by rule. + + The AI attribution when an AI rule ran, the issue it references (CC502), + and ``filled in`` for its length (CC503): ``references #412, filled in``. + """ + ran = {str(check.get("rule_id") or "") for check in scope.checks} + parts = [] + if ran & (cli.AI_RULE_IDS | cli.DECLARATION_RULE_IDS) or not ( + ran & cli.DESCRIPTION_RULE_IDS + ): + tools = ", ".join(scope.ai_tools) + parts.append(f"AI attribution: {tools}" if tools else "no AI attribution") + for check in scope.checks: + if check.get("rule_id") == "CC502" and _found(check): + parts.append(f"references {_found(check)}") + if "CC503" in ran: + parts.append("filled in") + return ", ".join(parts) + + def _scope_value(scope: model.ScopeResult, max_len: int = 80) -> str: """``_first_value`` capped for a row of the listing. @@ -325,10 +346,9 @@ def _scope_value(scope: model.ScopeResult, max_len: int = 80) -> str: """ if scope.label == "PR description" and scope.status == "pass": # Its first line does not identify it; what was found does. - if scope.ai_tools: - return f"AI attribution: {', '.join(scope.ai_tools)}" - return "no AI attribution" - value = _first_value(scope) + value = _description_findings(scope) + else: + value = _first_value(scope) if len(value) <= max_len: return value cut = value[: max_len - 1] diff --git a/requirements.txt b/requirements.txt index 8bfcfb1..eca051a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ # Install commit-check CLI # For details please see: https://github.com/commit-check/commit-check -commit-check==2.25.0 +commit-check==2.26.0 # Interact with the GitHub API. PyGithub==2.10.0 diff --git a/tests/cli_test.py b/tests/cli_test.py index 289b499..1c4b820 100644 --- a/tests/cli_test.py +++ b/tests/cli_test.py @@ -818,13 +818,23 @@ def test_an_older_cli_reports_none(self): class TestRunCommitCheckPrBody(unittest.TestCase): + checks_the_description = True + def setUp(self): cli.PR_BODY_AI_TOOLS.clear() self.addCleanup(cli.PR_BODY_AI_TOOLS.clear) - def _run(self, pr_body_enabled, ai_label, body="Adds a parser."): + def _run( + self, + pr_body_enabled, + ai_label, + body="Adds a parser.", + checks=None, + login="octocat", + pr_event=True, + ): body_scope = model.ScopeResult( - label="PR description", checks=[], ai_tools=["OpenAI Codex"] + label="PR description", checks=list(checks or []), ai_tools=["OpenAI Codex"] ) with ( patch("action.inputs.PR_TITLE_ENABLED", False), @@ -834,18 +844,22 @@ def _run(self, pr_body_enabled, ai_label, body="Adds a parser."): patch("action.inputs.BRANCH_ENABLED", False), patch("action.inputs.AUTHOR_NAME_ENABLED", False), patch("action.inputs.AUTHOR_EMAIL_ENABLED", False), - patch("action.context.is_pr_event", return_value=True), + patch("action.context.is_pr_event", return_value=pr_event), patch("action.context.get_pr_body", return_value=body), + patch("action.context.get_pr_author_login", return_value=login), patch("action.cli.check_scope", return_value=body_scope) as mock_scope, patch("action.cli.run_other_checks", return_value=[]), ): _rc, results = cli.run_commit_check() return results, mock_scope - def test_pr_body_is_a_scope_checked_with_the_ai_rules_only(self): + def test_pr_body_is_a_scope_checked_with_every_description_rule(self): results, mock_scope = self._run(True, "false") mock_scope.assert_called_once_with( - "PR description", ["--ai-attribution-only"], input_text="Adds a parser." + "PR description", + ["--pr-body"], + input_text="Adds a parser.", + env={"GIT_AUTHOR_NAME": "octocat"}, ) self.assertEqual([r.label for r in results], ["PR description"]) @@ -855,41 +869,104 @@ def test_no_ai_rule_ran_reads_as_skipped_not_passed(self): self.assertEqual(results[0].status, "skip") self.assertEqual(results[0].skip_reason, cli.AI_POLICY_IGNORED) + def test_the_skip_sits_beside_the_description_rules_that_ran(self): + issue = {"rule_id": "CC502", "check": "issue_reference", "status": "pass"} + results, _ = self._run(True, "false", checks=[issue]) + self.assertEqual([c["rule_id"] for c in results[0].checks], ["CC502", "CC013"]) + self.assertEqual(results[0].checks[1]["reason"], cli.AI_POLICY_IGNORED) + + def test_the_policy_names_every_value_that_runs_the_rules(self): + self.assertIn('"require"', cli.AI_POLICY_IGNORED) + def test_a_rule_that_ran_keeps_its_verdict(self): checks = [{"rule_id": "CC013", "check": "ai_attribution", "status": "pass"}] scope = model.ScopeResult(label="PR description", checks=list(checks)) self.assertEqual(cli.skip_when_no_ai_rule_ran(scope).checks, checks) + def test_the_declaration_counts_as_an_ai_rule(self): + checks = [{"rule_id": "CC501", "check": "ai_declaration", "status": "pass"}] + scope = model.ScopeResult(label="PR description", checks=list(checks)) + self.assertEqual(cli.skip_when_no_ai_rule_ran(scope).checks, checks) + def test_unparsable_output_still_fails(self): scope = model.ScopeResult(label="PR description", raw_text="Error: bad config") self.assertEqual(cli.skip_when_no_ai_rule_ran(scope).status, "fail") + def test_the_description_rules_report_a_scope_on_their_own(self): + """require_issue_reference or description_min_length need no input.""" + failed = {"rule_id": "CC502", "check": "issue_reference", "status": "fail"} + results, mock_scope = self._run(False, "false", checks=[failed]) + mock_scope.assert_called_once_with( + "PR description", + ["--pr-body", "--ai-attribution", "ignore"], + input_text="Adds a parser.", + env={"GIT_AUTHOR_NAME": "octocat"}, + ) + self.assertEqual([r.label for r in results], ["PR description"]) + self.assertEqual(results[0].status, "fail") + + def test_the_length_rule_reports_a_scope_on_its_own(self): + short = { + "rule_id": "CC503", + "check": "description_min_length", + "status": "fail", + } + results, _ = self._run(False, "false", checks=[short]) + self.assertEqual([r.label for r in results], ["PR description"]) + + def test_no_description_rule_reports_no_scope(self): + """With none set, the description is not a scope: nothing was asked.""" + declared = {"rule_id": "CC501", "check": "ai_declaration", "status": "fail"} + for checks in ([], [declared]): + with self.subTest(checks=checks): + results, _ = self._run(False, "false", checks=checks) + self.assertEqual(results, []) + + def test_a_config_the_cli_cannot_read_is_reported(self): + broken = model.ScopeResult(label="PR description", raw_text="Error: bad config") + with ( + patch("action.inputs.PR_BODY_AI_ATTRIBUTION_ENABLED", False), + patch("action.context.get_pr_body", return_value="x"), + patch("action.context.get_pr_author_login", return_value=""), + patch("action.cli.check_scope", return_value=broken), + ): + self.assertEqual(cli.run_description_check(), [broken]) + def test_ai_label_alone_reads_it_but_reports_no_scope(self): results, mock_scope = self._run(False, "true") mock_scope.assert_called_once() self.assertEqual(results, []) self.assertEqual(cli.PR_BODY_AI_TOOLS, ["OpenAI Codex"]) - def test_off_by_default(self): - results, mock_scope = self._run(False, "false") + def test_outside_a_pull_request_nothing_is_read(self): + results, mock_scope = self._run(False, "false", pr_event=False) mock_scope.assert_not_called() self.assertEqual(results, []) def test_no_description_is_checked_as_empty_text(self): - """A description left empty has no AI declaration for CC501 to find.""" + """An empty description names no issue and has no AI declaration.""" results, mock_scope = self._run(True, "true", body=None) mock_scope.assert_called_once_with( - "PR description", ["--ai-attribution-only"], input_text="" + "PR description", + ["--pr-body"], + input_text="", + env={"GIT_AUTHOR_NAME": "octocat"}, ) self.assertEqual([r.label for r in results], ["PR description"]) def test_no_description_nothing_to_label(self): - results, mock_scope = self._run(False, "true", body=None) - mock_scope.assert_not_called() + results, _ = self._run(False, "true", body=None) self.assertEqual(results, []) + self.assertEqual(cli.PR_BODY_AI_TOOLS, []) + + def test_without_a_login_git_decides_the_author(self): + _results, mock_scope = self._run(True, "false", login="") + self.assertIsNone(mock_scope.call_args.kwargs["env"]) class TestDescriptionLineChanges(unittest.TestCase): + checks_the_description = True + BODY = "## Summary\nAdds a parser.\n\nLink to Devin run: https://app.devin.ai/sessions/1\n" def _scope(self, *checks): diff --git a/tests/conftest.py b/tests/conftest.py index 399aabb..29b55a5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,3 +8,22 @@ # main.py sits at the repository root, one level above this directory. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +import pytest + + +@pytest.fixture(autouse=True) +def _no_description_check(request, monkeypatch): + """Leave the PR description out of runs that test something else. + + A pull request run always asks the CLI whether the config turns on a + description rule (CC502, CC503). Tests of the other checks mock the CLI + for their own scopes, so that extra call would answer with their mocks; + a test class that checks the description sets + ``checks_the_description = True``. + """ + if getattr(request.cls, "checks_the_description", False): + return + from action import cli + + monkeypatch.setattr(cli, "run_description_check", list) diff --git a/tests/report_test.py b/tests/report_test.py index b718195..b35d277 100644 --- a/tests/report_test.py +++ b/tests/report_test.py @@ -1691,6 +1691,20 @@ def test_a_passing_description_says_what_was_found(self): scope.ai_tools = ["Claude Code"] self.assertEqual(render._scope_value(scope), "AI attribution: Claude Code") + def test_a_passing_description_names_the_issue_and_its_length(self): + issue = make_check("issue_reference", rule_id="CC502", value="#412") + length = make_check("description_min_length", rule_id="CC503", value="Drop it") + ai = make_check("ai_attribution", rule_id="CC013") + cases = [ + ([issue, length], "references #412, filled in"), + ([length], "filled in"), + ([ai, issue], "no AI attribution, references #412"), + ] + for checks, expected in cases: + with self.subTest(expected=expected): + scope = model.ScopeResult(label="PR description", checks=checks) + self.assertEqual(render._scope_value(scope), expected) + def test_each_author_scope_carries_the_author_it_checked(self): with ( patch( From b5e21085e1210df1f21904dbf02de08885f3746a Mon Sep 17 00:00:00 2001 From: Xianpeng Shen <3353385+shenxianpeng@users.noreply.github.com> Date: Fri, 4 Dec 2026 09:45:58 +0000 Subject: [PATCH 2/2] fix: leave the AI declaration (CC501) to pr-body-ai-attribution The CLI runs CC501 under --ai-attribution ignore too, so a description checked only for CC502/CC503 failed for want of declaration boxes. --- action/cli.py | 6 ++++-- tests/cli_test.py | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/action/cli.py b/action/cli.py index 4f4ec7e..862999c 100644 --- a/action/cli.py +++ b/action/cli.py @@ -334,8 +334,10 @@ def run_description_check() -> list[model.ScopeResult]: if pr_body: # Read for ai-label as well: the AI tools are found whatever the policy. PR_BODY_AI_TOOLS[:] = scope.ai_tools - ran = any(c.get("rule_id") in DESCRIPTION_RULE_IDS for c in scope.checks) - return [scope] if ran or scope.raw_text else [] + # The CLI runs the AI declaration (CC501) whatever the policy; like the + # AI rules, it waits for pr-body-ai-attribution. + scope.checks = [c for c in scope.checks if c.get("rule_id") in DESCRIPTION_RULE_IDS] + return [scope] if scope.checks or scope.raw_text else [] def run_pr_message_checks(pr_commits: list[model.Commit]) -> list[model.ScopeResult]: diff --git a/tests/cli_test.py b/tests/cli_test.py index 1c4b820..e386594 100644 --- a/tests/cli_test.py +++ b/tests/cli_test.py @@ -905,6 +905,14 @@ def test_the_description_rules_report_a_scope_on_their_own(self): self.assertEqual([r.label for r in results], ["PR description"]) self.assertEqual(results[0].status, "fail") + def test_the_declaration_waits_for_the_input(self): + """The CLI runs CC501 under "ignore" too: it is left out, like the AI rules.""" + declared = {"rule_id": "CC501", "check": "ai_declaration", "status": "fail"} + issue = {"rule_id": "CC502", "check": "issue_reference", "status": "pass"} + results, _ = self._run(False, "false", checks=[declared, issue]) + self.assertEqual([c["rule_id"] for c in results[0].checks], ["CC502"]) + self.assertEqual(results[0].status, "pass") + def test_the_length_rule_reports_a_scope_on_its_own(self): short = { "rule_id": "CC503",