diff --git a/.github/workflows/coauthor-guard.yml b/.github/workflows/coauthor-guard.yml index 48344ad..d99c9e9 100644 --- a/.github/workflows/coauthor-guard.yml +++ b/.github/workflows/coauthor-guard.yml @@ -6,17 +6,29 @@ jobs: scan: runs-on: ubuntu-latest steps: + # No `ref:`. The default is the merge commit this workflow was read + # from, so scripts/check_coauthor.py comes from the same tree as the + # steps that call it. Checking out the head would run whatever copy the + # branch carries, and a branch cut before the script existed carries + # none. The scan names its range by sha and fetches both ends below, so + # it does not depend on what is checked out. - uses: actions/checkout@v4 with: fetch-depth: 0 - ref: ${{ github.event.pull_request.head.sha }} - - name: Fetch base commit + - name: Fetch the scanned range env: BASE_SHA: ${{ github.event.pull_request.base.sha }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} run: | set -euo pipefail git fetch --no-tags --depth=200 origin "${BASE_SHA}" + # The head by sha too: the merge commit checked out above normally + # carries it as a parent, but the scan must not rest on that. + git fetch --no-tags origin "${HEAD_SHA}" + # also need origin/main so the scan can exclude commits already on + # the mainline (we never re-flag released history we cannot rewrite) + git fetch --no-tags origin main - name: Scan commit messages for forbidden attribution env: @@ -27,9 +39,11 @@ jobs: range="${BASE_SHA}..${HEAD_SHA}" echo "Scanning commit range: ${range}" tmp="$(mktemp)" - git log --format='commit %H%n%B%n---END---' "${range}" > "${tmp}" - pattern='co-authored-by:[[:space:]]*.*(claude|anthropic|noreply@anthropic)|generated[[:space:]]+with[[:space:]]+\[?claude|generated[[:space:]]+by[[:space:]]+\[?anthropic' - if grep -iEn "${pattern}" "${tmp}"; then + # exclude commits already reachable from origin/main: only NEW commits + # introduced by this PR are subject to the guard. + git log --format='commit %H%n%B%n---END---' "${range}" --not origin/main > "${tmp}" + # The patterns live in the script, the same ones the body scan reads. + if ! python3 scripts/check_coauthor.py "${tmp}"; then echo "ERROR: commit message contains forbidden Claude/Anthropic attribution." echo "Fix: rebase the offending commit(s) and force-push the PR branch." exit 1 @@ -41,8 +55,11 @@ jobs: PR_BODY: ${{ github.event.pull_request.body }} run: | set -euo pipefail - if printf '%s' "${PR_BODY:-}" | grep -iE "(co-authored-by:[[:space:]]*.*(claude|anthropic)|generated[[:space:]]+with[[:space:]]+claude|generated[[:space:]]+by[[:space:]]+anthropic)" > /dev/null; then - echo "::error::PR body contains Claude/Anthropic co-author mention." + # Piped through the environment, never interpolated into the script. + if ! printf '%s' "${PR_BODY:-}" | python3 scripts/check_coauthor.py -; then + echo "::error::PR body contains Claude/Anthropic attribution." exit 1 fi echo "OK: no forbidden attribution found in PR body." + +# (re-trigger CI after PR-body cleanup) diff --git a/scripts/check_coauthor.py b/scripts/check_coauthor.py new file mode 100644 index 0000000..8762a0c --- /dev/null +++ b/scripts/check_coauthor.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +"""Refuse Claude/Anthropic attribution in commit messages and pull request bodies. + +No commit a pull request introduces, and no pull request body, may carry a +Claude or Anthropic attribution line. ``.github/workflows/coauthor-guard.yml`` +runs this script twice, over the messages of those commits and over the body, +so both scans read the patterns below and nothing else. + +WHY ONE SCRIPT. The two scans used to carry their own regexes inline in the +workflow, and they drifted apart: the body's lacked the optional bracket the +commit scan had, so the footer the tool appends to a pull request body +(a Markdown link, ``[Claude Code](...)``) passed the body scan, and neither +scan knew the session trailer the tool appends to a commit message. + +Scanning is line by line, as ``grep`` did, so a pattern never spans two lines. +Mentioning Claude in prose passes: what is refused is the shape of an +attribution (a trailer or the footer), not the name. + +Usage: + python3 scripts/check_coauthor.py [PATH ...] + +Each PATH is a text file; ``-``, or no PATH at all, reads standard input. +Every offending line is printed as ``source:line: text``. Exits 1 if there is +one, 0 if there is none. +""" + +from __future__ import annotations + +import argparse +import re +import sys +from pathlib import Path + +FORBIDDEN: tuple[re.Pattern[str], ...] = ( + # A co-author trailer naming Claude or Anthropic, in the name or the address. + re.compile(r"co-authored-by\s*:.*(claude|anthropic)", re.IGNORECASE), + # The session trailer, whatever the URL after it. + re.compile(r"claude-session\s*:", re.IGNORECASE), + # The footer, with or without the bracket of its Markdown link. + re.compile(r"generated\s+with\s+\[?claude", re.IGNORECASE), + re.compile(r"generated\s+by\s+\[?anthropic", re.IGNORECASE), + # A bare session link, the line that follows the footer in a pull request body. + re.compile(r"claude\.ai/code/session_", re.IGNORECASE), +) + + +def forbidden_lines(text: str) -> list[tuple[int, str]]: + """Return ``(line number, line)`` for every line carrying a forbidden attribution.""" + return [ + (number, line) + for number, line in enumerate(text.splitlines(), start=1) + if any(pattern.search(line) for pattern in FORBIDDEN) + ] + + +def _read(path: str) -> tuple[str, str]: + """Return ``(source name, text)``; undecodable bytes are replaced, never fatal.""" + if path == "-": + return "", sys.stdin.buffer.read().decode("utf-8", errors="replace") + return path, Path(path).read_text(encoding="utf-8", errors="replace") + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser( + description="Refuse Claude/Anthropic attribution in commit messages and PR bodies." + ) + parser.add_argument( + "paths", + nargs="*", + default=["-"], + help="text files to scan; '-' or nothing reads standard input", + ) + args = parser.parse_args(argv) + + found = 0 + for path in args.paths: + source, text = _read(path) + for number, line in forbidden_lines(text): + print(f"{source}:{number}: {line}") + found += 1 + + if found: + print(f"check_coauthor: {found} line(s) carry a forbidden Claude/Anthropic attribution.") + return 1 + print("check_coauthor: OK, no forbidden attribution.") + return 0 + + +if __name__ == "__main__": + sys.exit(main())