Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
84 changes: 59 additions & 25 deletions action/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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",
Expand All @@ -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

Expand Down Expand Up @@ -290,6 +302,44 @@ 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
# 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]:
"""Check each PR commit individually via commit-check --message.

Expand Down Expand Up @@ -500,9 +550,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,
Expand All @@ -525,24 +575,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
Expand Down
5 changes: 5 additions & 0 deletions action/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
28 changes: 24 additions & 4 deletions action/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading