From e910bddfdbe8fec9f4cd95dfc29cb1571845d31f Mon Sep 17 00:00:00 2001 From: showxu <10173746+showxu@users.noreply.github.com> Date: Tue, 27 Oct 2026 15:22:15 +0800 Subject: [PATCH 1/2] fix(policy): check pull requests by title and added content Members squash-merge with the PR title as the subject and an empty body, so pull-request commits never reach the default branch. Pull-request policy now checks the title, added lines and paths only; commit verification, committer identity, subjects and attribution trailers stay on default-branch pushes and dispatch. Merge commits that update a pull request and external contributors' commits pass, as the organization standard describes. --- CHANGELOG.md | 2 ++ README.md | 6 +++- scripts/check-policy.py | 8 ++++- tests/test_policy.py | 72 ++++++++++++++++++++++++++++++----------- 4 files changed, 67 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e32908b..3237667 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- `policy.yml` checks pull requests by their title, added lines and paths only. Commit verification, committer identity, Conventional Commit subjects and attribution trailers are checked on default-branch pushes and dispatch, so merge commits that update a pull request and external contributors' commits pass. + ## 0.1.1 - `policy.yml` accepts the agents CLI's tracked instruction files: `.agent/instructions/`, `.agent/agents.json` and `.agent/agents.resolved`. Other `.agent/` paths, including private directories nested under the units, stay private execution files. The declaration and lock may name agent IDs such as `codex` without an allowed product term. diff --git a/README.md b/README.md index 14bb600..3a9e9ad 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ for Swift packages, and publication of a package template. Required member check | `demo` | Builds the release executable and checks a fresh terminal recording through the presentation action's recording-only mode. | | `commands` | Runs each repository command in its own job in the member checkout. | | `result` | Requires every declared job to succeed. Only undeclared optional jobs may be skipped; failures and cancellation fail the aggregate. | -| `policy.yml` / `policy` | Checks commit verification/identity, Conventional Commit messages and PR title, attribution, added lines, private paths and permitted product terms. Validates version declarations when `.github/release.json` exists. | +| `policy.yml` / `policy` | On pull requests, checks the PR title, added lines, private paths and permitted product terms. On default-branch pushes and dispatch, also checks each commit's verification, committer identity, Conventional Commit subject and attribution. Validates version declarations when `.github/release.json` exists. | | `swift-package-release.yml` / `verify`, `publish` | Verifies the accepted commit and signed annotated tag; validates version/changelog; publishes notes without assets. Only `publish` writes contents. | | `organization.yml` / `organization` | Compares catalog membership, About descriptions and, in online mode, declared repository settings. All API requests are reads. | @@ -116,6 +116,10 @@ It does not exempt attribution or private execution paths. The optional Dependabot exception permits only its exact signoff after GitHub authenticates the verified bot author and web-flow signer. Policy retains the family owner identity `10173746+showxu@users.noreply.github.com` and GitHub's web committer. +Pull-request policy does not inspect individual commits: members squash-merge +with the PR title as the subject and an empty body, so merge commits that +update a pull request and external contributors' commits pass, and the +default-branch push checks the squash commit. ## Calling release diff --git a/scripts/check-policy.py b/scripts/check-policy.py index f1b1ebf..89943ca 100755 --- a/scripts/check-policy.py +++ b/scripts/check-policy.py @@ -219,7 +219,13 @@ def check(event, event_name, *, allow_terms=frozenset(), dependabot_exception=Fa comparison = base else: comparison = base - commits = [head] if base is None else git("rev-list", "--reverse", base + ".." + head).splitlines() + if event_name == "pull_request": + # Members squash-merge with the PR title as the subject and an empty + # body, so pull-request commits never reach the default branch. The + # default-branch push checks the squash commit's message and identity. + commits = [] + else: + commits = [head] if base is None else git("rev-list", "--reverse", base + ".." + head).splitlines() if event_name == "push": if base is None: print(f"::notice::Policy push: validating only {head}; previous default-branch tip unavailable or absent") diff --git a/tests/test_policy.py b/tests/test_policy.py index 45483a9..d6dcf79 100644 --- a/tests/test_policy.py +++ b/tests/test_policy.py @@ -177,15 +177,14 @@ def test_default_branch_push_checks_entire_range_without_pr_title(self): self.assertIn('validating 2 commits in ' + self.base + '..' + head, output) @mock.patch.dict(os.environ, {'GH_TOKEN': 'test-token'}) - def test_authenticated_dependabot_update_passes_on_pr_push_and_dispatch(self): + def test_authenticated_dependabot_update_passes_on_push_and_dispatch(self): head = self.dependency_update() - events = ((self.event(head), 'pull_request'), - (self.push_event(head, self.base), 'push'), + events = ((self.push_event(head, self.base), 'push'), ({'repository': {'full_name': 'owner/member'}, 'ref': 'main'}, 'workflow_dispatch')) for event, name in events: with self.subTest(event=name), mock.patch.dict(os.environ, {'GITHUB_SHA': head}), mock.patch.object( policy, 'urlopen', return_value=github_response(dependabot_metadata(head)) - ) as get: + ) as get, redirect_stdout(StringIO()): self.assertEqual(policy.check(event, name), []) request = get.call_args.args[0] self.assertEqual(request.full_url, 'https://api.github.com/repos/owner/member/commits/' + head) @@ -209,8 +208,8 @@ def test_same_signoff_fails_without_authenticated_dependabot_authorship(self): with self.subTest(metadata=changed), mock.patch.object( policy, 'urlopen', return_value=github_response({**dependabot_metadata(head), **changed}) ): - self.assertEqual(policy.check(self.event(head), 'pull_request'), - [head + ': forbidden attribution trailer']) + findings, _ = self.check_push(self.push_event(head, self.base)) + self.assertEqual(findings, [head + ': forbidden attribution trailer']) def test_commit_message_carriage_return_does_not_match_exact_signoff(self): (self.root / 'file').write_text('updated dependency\n') @@ -219,40 +218,75 @@ def test_commit_message_carriage_return_does_not_match_exact_signoff(self): 'chore(deps): update dependency\n\n' + policy.DEPENDABOT_SIGNOFF + '\r\n') head = self.git('rev-parse', 'HEAD') with mock.patch.object(policy, 'authenticated_dependabot_commit') as authenticate: - self.assertEqual(policy.check(self.event(head), 'pull_request'), - [head + ': forbidden attribution trailer']) + findings, _ = self.check_push(self.push_event(head, self.base)) + self.assertEqual(findings, [head + ': forbidden attribution trailer']) authenticate.assert_not_called() @mock.patch.dict(os.environ, {'GH_TOKEN': 'test-token'}) - def test_dependabot_pr_does_not_exempt_another_authors_commit(self): + def test_dependabot_exception_does_not_extend_to_another_authors_commit(self): first = self.dependency_update() self.git('config', 'user.name', 'Contributor') self.git('config', 'user.email', 'contributor@example.invalid') (self.root / 'file').write_text('another dependency update\n') second = self.commit('chore(deps): update another dependency\n\n' + policy.DEPENDABOT_SIGNOFF) other = {**dependabot_metadata(second), 'author': {'login': 'contributor', 'type': 'User'}} - event = self.event(second) - event['pull_request']['user'] = {'login': 'dependabot[bot]', 'type': 'Bot'} with mock.patch.object(policy, 'urlopen', side_effect=[ github_response(dependabot_metadata(first)), github_response(other) ]) as get: - self.assertEqual(policy.check(event, 'pull_request'), [second + ': forbidden attribution trailer']) + findings, _ = self.check_push(self.push_event(second, self.base)) + self.assertEqual(findings, [second + ': forbidden attribution trailer']) self.assertEqual(get.call_count, 2) @mock.patch.dict(os.environ, {'GH_TOKEN': 'test-token'}) def test_dependabot_authentication_fails_closed_when_unavailable_or_malformed(self): head = self.dependency_update() + event = self.push_event(head, self.base) with mock.patch.object(policy, 'urlopen', side_effect=URLError('unavailable')): with self.assertRaises(URLError): - policy.check(self.event(head), 'pull_request') + self.check_push(event) with mock.patch.object(policy, 'urlopen', return_value=github_response([])): - self.assertTrue(policy.check(self.event(head), 'pull_request')) + self.assertTrue(self.check_push(event)[0]) with mock.patch.object(policy, 'urlopen', return_value=BytesIO(b'not JSON')): with self.assertRaises(ValueError): - policy.check(self.event(head), 'pull_request') + self.check_push(event) with mock.patch.dict(os.environ, {}, clear=True): with self.assertRaises(KeyError): - policy.check(self.event(head), 'pull_request') + self.check_push(event) + + def test_pull_request_checks_title_and_added_lines_but_not_commits(self): + self.git('checkout', '-q', '-b', 'topic') + (self.root / 'topic').write_text('contributed content\n') + self.git('add', '.') + self.git('-c', 'user.name=Contributor', '-c', 'user.email=contributor@example.invalid', 'commit', '-q', + '-m', 'wip\n\nCo-authored-by: Someone \n' + 'Signed-off-by: Contributor ') + self.git('checkout', '-q', 'main') + (self.root / 'base-only').write_text('base change\n') + self.base = self.commit('chore: update base') + self.git('checkout', '-q', 'topic') + self.git('merge', '-q', '--no-ff', '--no-edit', 'main') + head = self.git('rev-parse', 'HEAD') + self.assertTrue(self.git('log', '-1', '--format=%s', head).startswith('Merge branch')) + with mock.patch.object(policy, 'api_commit', side_effect=AssertionError('commits must not be inspected')): + self.assertEqual(_check(self.event(head), 'pull_request', verify_commits=True), []) + findings = _check(self.event(head, 'Merge branch main'), 'pull_request', verify_commits=True) + self.assertEqual(findings, ['PR title: expected a Conventional Commit subject (' + policy.TYPES + ')']) + + def test_default_branch_push_requires_verified_commits_by_github_or_owner(self): + records = {} + for label, email, verified in (('owner', policy.OWNER_EMAIL, True), ('github', 'noreply@github.com', True), + ('foreign', 'contributor@example.invalid', True), + ('unsigned', policy.OWNER_EMAIL, False)): + (self.root / 'file').write_text(label + ' change\n') + sha = self.commit('fix: record ' + label + ' change') + records[sha] = {'commit': {'committer': {'email': email}, 'verification': {'verified': verified}}} + foreign, unsigned = list(records)[2:] + self.git('update-ref', 'refs/remotes/origin/main', unsigned) + with mock.patch.object(policy, 'api_commit', side_effect=lambda _, sha: records[sha]), \ + redirect_stdout(StringIO()): + findings = _check(self.push_event(unsigned, self.base, 'main'), 'push', verify_commits=True) + self.assertEqual(findings, [foreign + ': unexpected committer identity', + unsigned + ': commit is not Verified by GitHub']) def test_new_branch_does_not_rescan_legacy_ancestors(self): (self.root / 'file').write_text('new content\n') @@ -350,11 +384,11 @@ def test_new_orphan_feature_branch_checks_all_its_commits(self): self.assertTrue(any('whitespace error' in finding for finding in findings)) self.assertIn(self.base + '..' + head, output) - def test_title_commit_whitespace_and_conflict_failures(self): + def test_title_whitespace_and_conflict_failures(self): (self.root / 'file').write_text('bad whitespace \n<<<<<<< branch\n') - head = self.commit('Invalid subject\n\nGenerated-by: tool') + head = self.commit('fix: add conflicting content') findings = policy.check(self.event(head, 'Bad title'), 'pull_request') - for reason in ('PR title', 'Conventional Commit', 'attribution', 'whitespace', 'conflict marker'): + for reason in ('PR title', 'whitespace', 'conflict marker'): self.assertTrue(any(reason in line for line in findings), reason) def test_pr_merge_base_ignores_independent_base_branch_additions(self): From 677e140a8b07c19a7440dba0be9db12d1bbfcc7b Mon Sep 17 00:00:00 2001 From: showxu <10173746+showxu@users.noreply.github.com> Date: Tue, 27 Oct 2026 15:31:36 +0800 Subject: [PATCH 2/2] fix(policy)!: drop the Dependabot exception and document dispatch Pull requests are no longer checked commit by commit, and default-branch squash commits have empty bodies, so the Dependabot signoff exception no longer applies anywhere. Remove the input, its environment variable, the authentication helper and its tests. README and CHANGELOG state that manual dispatch checks only the selected commit, and that callers must squash-merge with the PR title and an empty body. Tests assert added-line findings on pull requests and that the original author is not restricted on default-branch pushes. --- .github/workflows/policy.yml | 5 - .github/workflows/source-policy.yml | 1 - CHANGELOG.md | 3 +- README.md | 21 +++-- scripts/check-policy.py | 48 +--------- tests/test_policy.py | 139 ++++------------------------ 6 files changed, 35 insertions(+), 182 deletions(-) diff --git a/.github/workflows/policy.yml b/.github/workflows/policy.yml index 77ec9ec..acede0c 100644 --- a/.github/workflows/policy.yml +++ b/.github/workflows/policy.yml @@ -6,10 +6,6 @@ on: description: Newline-separated allowed product names type: string default: '' - dependabot-exception: - description: Allow the exact signoff on an authenticated Dependabot commit - type: boolean - default: false permissions: {} jobs: policy: @@ -38,7 +34,6 @@ jobs: env: GH_TOKEN: ${{ github.token }} ALLOW_TERMS: ${{ inputs.allow-terms }} - DEPENDABOT_EXCEPTION: ${{ inputs.dependabot-exception }} run: python3 ../.shared/scripts/check-policy.py - if: hashFiles('member/.github/release.json') != '' run: python3 .shared/scripts/validate-version --root member diff --git a/.github/workflows/source-policy.yml b/.github/workflows/source-policy.yml index e5cf572..8c74b0d 100644 --- a/.github/workflows/source-policy.yml +++ b/.github/workflows/source-policy.yml @@ -18,4 +18,3 @@ jobs: Cursor Claude' - dependabot-exception: true diff --git a/CHANGELOG.md b/CHANGELOG.md index 3237667..ad13c52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## Unreleased -- `policy.yml` checks pull requests by their title, added lines and paths only. Commit verification, committer identity, Conventional Commit subjects and attribution trailers are checked on default-branch pushes and dispatch, so merge commits that update a pull request and external contributors' commits pass. +- `policy.yml` checks pull requests by their title, added lines and paths only. Commit verification, committer identity, Conventional Commit subjects and attribution trailers are checked on each new default-branch commit and on the commit selected for manual dispatch, so merge commits that update a pull request and external contributors' commits pass. +- **Breaking:** `policy.yml` no longer takes the `dependabot-exception` input. Pull requests are not checked commit by commit, and default-branch commits carry no trailers. Remove the input from callers. ## 0.1.1 diff --git a/README.md b/README.md index 3a9e9ad..a7c79b1 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ for Swift packages, and publication of a package template. Required member check | `demo` | Builds the release executable and checks a fresh terminal recording through the presentation action's recording-only mode. | | `commands` | Runs each repository command in its own job in the member checkout. | | `result` | Requires every declared job to succeed. Only undeclared optional jobs may be skipped; failures and cancellation fail the aggregate. | -| `policy.yml` / `policy` | On pull requests, checks the PR title, added lines, private paths and permitted product terms. On default-branch pushes and dispatch, also checks each commit's verification, committer identity, Conventional Commit subject and attribution. Validates version declarations when `.github/release.json` exists. | +| `policy.yml` / `policy` | On pull requests, checks the PR title, added lines, private paths and permitted product terms. On default-branch pushes, also checks each new commit's verification, committer identity, Conventional Commit subject and attribution; manual dispatch checks only those for the selected commit. Validates version declarations when `.github/release.json` exists. | | `swift-package-release.yml` / `verify`, `publish` | Verifies the accepted commit and signed annotated tag; validates version/changelog; publishes notes without assets. Only `publish` writes contents. | | `organization.yml` / `organization` | Compares catalog membership, About descriptions and, in online mode, declared repository settings. All API requests are reads. | @@ -108,18 +108,19 @@ jobs: uses: swift-library/github-workflows/.github/workflows/policy.yml@ # vX.Y.Z with: allow-terms: '' - dependabot-exception: false ``` `allow-terms` is a newline-separated, case-insensitive product-name allowlist. -It does not exempt attribution or private execution paths. The optional -Dependabot exception permits only its exact signoff after GitHub authenticates -the verified bot author and web-flow signer. Policy retains the family owner -identity `10173746+showxu@users.noreply.github.com` and GitHub's web committer. -Pull-request policy does not inspect individual commits: members squash-merge -with the PR title as the subject and an empty body, so merge commits that -update a pull request and external contributors' commits pass, and the -default-branch push checks the squash commit. +It does not exempt attribution or private execution paths. + +Pull-request policy checks the title, added content and added or modified file +paths, not individual commits. Callers must allow only squash merges with the +PR title as the subject and an empty body; default-branch push policy then +checks the resulting commit: GitHub verification, a committer that is GitHub's +web committer or the family owner identity +`10173746+showxu@users.noreply.github.com`, a Conventional Commit subject and +no attribution trailer. It does not restrict the original author. Manual +dispatch checks only the selected commit's verification, committer and message. ## Calling release diff --git a/scripts/check-policy.py b/scripts/check-policy.py index 89943ca..17b0ad8 100755 --- a/scripts/check-policy.py +++ b/scripts/check-policy.py @@ -8,7 +8,6 @@ import re import subprocess import sys -from urllib.request import Request, urlopen TYPES = "build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test" @@ -17,7 +16,6 @@ r"^\s*(?:Co-authored-by|Signed-off-by|Generated-by|Generated-with|" r"Made-with|Assisted-by|Powered-by):", re.IGNORECASE | re.MULTILINE ) -DEPENDABOT_SIGNOFF = "Signed-off-by: dependabot[bot] " CONFLICT = re.compile(r"^(?:<{7}(?: |$)|={7}$|>{7}(?: |$)|\|{7}(?: |$))") ZERO = "0" * 40 OWNER_EMAIL = "10173746+showxu@users.noreply.github.com" @@ -105,48 +103,16 @@ def push_base(event): return None -def message_findings(label, message, *, dependabot=False): +def message_findings(label, message): subject = message.split("\n", 1)[0] findings = [] if not SUBJECT.fullmatch(subject) or subject != subject.rstrip(): findings.append(f"{label}: expected a Conventional Commit subject ({TYPES})") - if any(ATTRIBUTION.search(line) and not (dependabot and line == DEPENDABOT_SIGNOFF) - for line in message.split("\n")): + if any(ATTRIBUTION.search(line) for line in message.split("\n")): findings.append(f"{label}: forbidden attribution trailer") return findings -def authenticated_dependabot_commit(repository, commit): - """Require GitHub's verified bot identity, not a spoofable Git author field.""" - if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", repository): - raise ValueError("invalid repository for commit authentication") - request = Request( - f"https://api.github.com/repos/{repository}/commits/{commit}", - headers={"Accept": "application/vnd.github+json", - "Authorization": "Bearer " + os.environ["GH_TOKEN"], - "X-GitHub-Api-Version": "2022-11-28"}, - method="GET", - ) - with urlopen(request, timeout=30) as response: - observed = json.load(response) - if not isinstance(observed, dict): - return False - author = observed.get("author") or {} - committer = observed.get("committer") or {} - details = observed.get("commit") or {} - if not all(isinstance(value, dict) for value in (author, committer, details)): - return False - verification = details.get("verification") or {} - if not isinstance(verification, dict): - return False - # A verified signature by a different contributor does not authenticate - # a claimed bot author. Dependabot commits use GitHub's web-flow signer. - return (observed.get("sha") == commit - and author.get("login") == "dependabot[bot]" and author.get("type") == "Bot" - and committer.get("login") == "web-flow" - and verification.get("verified") is True and verification.get("reason") == "valid") - - def added_findings(patch, *, trailing_blank_start=None, allow_terms=frozenset()): """Inspect hunk additions, including additions whose text starts with '++'.""" findings = [] @@ -195,7 +161,7 @@ def source_range(event, event_name): return base, head -def check(event, event_name, *, allow_terms=frozenset(), dependabot_exception=False, verify_commits=False): +def check(event, event_name, *, allow_terms=frozenset(), verify_commits=False): if event_name == "push" and event.get("deleted"): print("::notice::Policy skipped branch deletion push") return [] @@ -232,16 +198,12 @@ def check(event, event_name, *, allow_terms=frozenset(), dependabot_exception=Fa else: print(f"::notice::Policy push: validating {len(commits)} commits in {base}..{head}") for commit in commits: - # Preserve carriage returns: newline normalization would broaden the - # exact signoff exception to differently spelled trailer lines. message = subprocess.check_output( ["git", "show", "-s", "--format=%B", commit], stdin=subprocess.DEVNULL ).decode("utf-8", errors="replace") if verify_commits: findings.extend(commit_findings(commit, api_commit(event["repository"]["full_name"], commit))) - dependabot = (dependabot_exception and DEPENDABOT_SIGNOFF in message.split("\n") - and authenticated_dependabot_commit(event["repository"]["full_name"], commit)) - findings.extend(message_findings(commit, message, dependabot=dependabot)) + findings.extend(message_findings(commit, message)) if comparison is None: # Dispatch supplies a selected commit but no new-change range. return findings @@ -273,7 +235,7 @@ def main(): event = json.loads(Path(os.environ["GITHUB_EVENT_PATH"]).read_text()) findings = check(event, os.environ["GITHUB_EVENT_NAME"], allow_terms={term.strip().lower() for term in os.environ.get("ALLOW_TERMS", "").splitlines() if term.strip()}, - dependabot_exception=os.environ.get("DEPENDABOT_EXCEPTION") == "true", verify_commits=True) + verify_commits=True) # Avoid interpreting source-controlled text as workflow commands. for finding in findings: print("FAIL " + finding.replace("\n", " | ")) diff --git a/tests/test_policy.py b/tests/test_policy.py index d6dcf79..ceaa313 100644 --- a/tests/test_policy.py +++ b/tests/test_policy.py @@ -1,7 +1,7 @@ import os import json from contextlib import redirect_stdout -from io import BytesIO, StringIO +from io import StringIO from pathlib import Path import shutil import subprocess @@ -9,28 +9,11 @@ import textwrap import unittest from unittest import mock -from urllib.error import URLError from test_organization import ROOT, load_script policy = load_script('policy', 'check-policy.py') -_check = policy.check - -def check_with_dependabot(event, name): - return _check(event, name, dependabot_exception=True) - -policy.check = check_with_dependabot - - -def dependabot_metadata(sha): - return {'sha': sha, 'author': {'login': 'dependabot[bot]', 'type': 'Bot'}, - 'committer': {'login': 'web-flow', 'type': 'User'}, - 'commit': {'verification': {'verified': True, 'reason': 'valid'}}} - - -def github_response(metadata): - return BytesIO(json.dumps(metadata).encode()) class PolicyTests(unittest.TestCase): @@ -42,20 +25,11 @@ def test_subject_syntax_and_allowed_types(self): def test_attribution_in_body_and_legitimate_product_names(self): self.assertEqual(policy.message_findings('commit', 'feat: support Codex and Claude\n\nKeep notices.'), []) - for trailer in ('Co-authored-by', 'Generated-by', 'Signed-off-by', 'Assisted-by'): - self.assertTrue(policy.message_findings('commit', 'fix: change\n\n' + trailer + ': someone')) - - def test_dependabot_exception_is_exact_and_preserves_other_trailer_bans(self): - signoff = policy.DEPENDABOT_SIGNOFF - for trailer in (signoff.lower(), ' ' + signoff, signoff + ' ', signoff + '\r', - signoff.replace('support@github.com', 'someone@example.com'), - 'Co-authored-by: someone', 'Generated-by: tool', 'Generated-with: tool', - 'Made-with: tool', 'Assisted-by: tool', 'Powered-by: tool'): + for trailer in ('Co-authored-by', 'Signed-off-by', 'Generated-by', 'Generated-with', 'Made-with', + 'Assisted-by', 'Powered-by'): with self.subTest(trailer=trailer): - self.assertTrue(policy.message_findings( - 'commit', 'chore(deps): update\n\n' + signoff + '\n' + trailer, dependabot=True)) - self.assertTrue(policy.message_findings('PR title', 'chore: update\n' + signoff)) - self.assertTrue(policy.added_findings('@@ -0,0 +1 @@\n+' + signoff + '\n')) + self.assertTrue(policy.message_findings('commit', 'fix: change\n\n' + trailer + ': someone')) + self.assertTrue(policy.added_findings('@@ -0,0 +1 @@\n+' + trailer + ': someone\n')) def test_only_added_lines_are_scanned(self): patch = 'diff --git a/file b/file\n--- a/file\n+++ b/file\n@@ -1,2 +1 @@\n-<<<<<<< old\n-Generated-by: old\n+Useful documentation about Codex\n' @@ -123,12 +97,6 @@ def check_push(self, event): findings = policy.check(event, 'push') return findings, output.getvalue() - def dependency_update(self): - (self.root / 'file').write_text('updated dependency\n') - self.git('config', 'user.name', 'dependabot[bot]') - self.git('config', 'user.email', '49699333+dependabot[bot]@users.noreply.github.com') - return self.commit('chore(deps): bump dependency from 1.0 to 1.1\n\n' + policy.DEPENDABOT_SIGNOFF) - def dispatch(self, head): event_file = self.root / 'event.json' event_file.write_text(json.dumps({'repository': {'full_name': 'owner/member'}, @@ -176,101 +144,24 @@ def test_default_branch_push_checks_entire_range_without_pr_title(self): self.assertFalse(any('PR title' in line for line in findings)) self.assertIn('validating 2 commits in ' + self.base + '..' + head, output) - @mock.patch.dict(os.environ, {'GH_TOKEN': 'test-token'}) - def test_authenticated_dependabot_update_passes_on_push_and_dispatch(self): - head = self.dependency_update() - events = ((self.push_event(head, self.base), 'push'), - ({'repository': {'full_name': 'owner/member'}, 'ref': 'main'}, 'workflow_dispatch')) - for event, name in events: - with self.subTest(event=name), mock.patch.dict(os.environ, {'GITHUB_SHA': head}), mock.patch.object( - policy, 'urlopen', return_value=github_response(dependabot_metadata(head)) - ) as get, redirect_stdout(StringIO()): - self.assertEqual(policy.check(event, name), []) - request = get.call_args.args[0] - self.assertEqual(request.full_url, 'https://api.github.com/repos/owner/member/commits/' + head) - self.assertEqual(request.get_method(), 'GET') - self.assertEqual(request.get_header('Authorization'), 'Bearer test-token') - - @mock.patch.dict(os.environ, {'GH_TOKEN': 'test-token'}) - def test_same_signoff_fails_without_authenticated_dependabot_authorship(self): - head = self.dependency_update() - variants = [ - {'author': {'login': 'contributor', 'type': 'User'}}, - {'author': {'login': 'dependabot[bot]', 'type': 'User'}}, - {'author': None}, {'author': 'dependabot[bot]'}, - {'committer': {'login': 'contributor', 'type': 'User'}}, - {'commit': {'verification': {'verified': False, 'reason': 'unsigned'}}}, - {'commit': {'verification': {'verified': True, 'reason': 'invalid'}}}, - {'commit': {'verification': None}}, {'commit': None}, - {'sha': 'a' * 40}, - ] - for changed in variants: - with self.subTest(metadata=changed), mock.patch.object( - policy, 'urlopen', return_value=github_response({**dependabot_metadata(head), **changed}) - ): - findings, _ = self.check_push(self.push_event(head, self.base)) - self.assertEqual(findings, [head + ': forbidden attribution trailer']) - - def test_commit_message_carriage_return_does_not_match_exact_signoff(self): - (self.root / 'file').write_text('updated dependency\n') - self.git('add', '.') - self.git('commit', '-q', '--cleanup=verbatim', '-m', - 'chore(deps): update dependency\n\n' + policy.DEPENDABOT_SIGNOFF + '\r\n') - head = self.git('rev-parse', 'HEAD') - with mock.patch.object(policy, 'authenticated_dependabot_commit') as authenticate: - findings, _ = self.check_push(self.push_event(head, self.base)) - self.assertEqual(findings, [head + ': forbidden attribution trailer']) - authenticate.assert_not_called() - - @mock.patch.dict(os.environ, {'GH_TOKEN': 'test-token'}) - def test_dependabot_exception_does_not_extend_to_another_authors_commit(self): - first = self.dependency_update() - self.git('config', 'user.name', 'Contributor') - self.git('config', 'user.email', 'contributor@example.invalid') - (self.root / 'file').write_text('another dependency update\n') - second = self.commit('chore(deps): update another dependency\n\n' + policy.DEPENDABOT_SIGNOFF) - other = {**dependabot_metadata(second), 'author': {'login': 'contributor', 'type': 'User'}} - with mock.patch.object(policy, 'urlopen', side_effect=[ - github_response(dependabot_metadata(first)), github_response(other) - ]) as get: - findings, _ = self.check_push(self.push_event(second, self.base)) - self.assertEqual(findings, [second + ': forbidden attribution trailer']) - self.assertEqual(get.call_count, 2) - - @mock.patch.dict(os.environ, {'GH_TOKEN': 'test-token'}) - def test_dependabot_authentication_fails_closed_when_unavailable_or_malformed(self): - head = self.dependency_update() - event = self.push_event(head, self.base) - with mock.patch.object(policy, 'urlopen', side_effect=URLError('unavailable')): - with self.assertRaises(URLError): - self.check_push(event) - with mock.patch.object(policy, 'urlopen', return_value=github_response([])): - self.assertTrue(self.check_push(event)[0]) - with mock.patch.object(policy, 'urlopen', return_value=BytesIO(b'not JSON')): - with self.assertRaises(ValueError): - self.check_push(event) - with mock.patch.dict(os.environ, {}, clear=True): - with self.assertRaises(KeyError): - self.check_push(event) - def test_pull_request_checks_title_and_added_lines_but_not_commits(self): self.git('checkout', '-q', '-b', 'topic') - (self.root / 'topic').write_text('contributed content\n') + (self.root / 'topic').write_text('contributed content \n') self.git('add', '.') self.git('-c', 'user.name=Contributor', '-c', 'user.email=contributor@example.invalid', 'commit', '-q', '-m', 'wip\n\nCo-authored-by: Someone \n' 'Signed-off-by: Contributor ') self.git('checkout', '-q', 'main') - (self.root / 'base-only').write_text('base change\n') + (self.root / 'base-only').write_text('base change \n') self.base = self.commit('chore: update base') self.git('checkout', '-q', 'topic') self.git('merge', '-q', '--no-ff', '--no-edit', 'main') head = self.git('rev-parse', 'HEAD') self.assertTrue(self.git('log', '-1', '--format=%s', head).startswith('Merge branch')) with mock.patch.object(policy, 'api_commit', side_effect=AssertionError('commits must not be inspected')): - self.assertEqual(_check(self.event(head), 'pull_request', verify_commits=True), []) - findings = _check(self.event(head, 'Merge branch main'), 'pull_request', verify_commits=True) - self.assertEqual(findings, ['PR title: expected a Conventional Commit subject (' + policy.TYPES + ')']) + findings = policy.check(self.event(head, 'Merge branch main'), 'pull_request', verify_commits=True) + self.assertEqual(findings, ['PR title: expected a Conventional Commit subject (' + policy.TYPES + ')', + 'b/topic:1: whitespace error']) def test_default_branch_push_requires_verified_commits_by_github_or_owner(self): records = {} @@ -278,13 +169,17 @@ def test_default_branch_push_requires_verified_commits_by_github_or_owner(self): ('foreign', 'contributor@example.invalid', True), ('unsigned', policy.OWNER_EMAIL, False)): (self.root / 'file').write_text(label + ' change\n') - sha = self.commit('fix: record ' + label + ' change') - records[sha] = {'commit': {'committer': {'email': email}, 'verification': {'verified': verified}}} + self.git('add', '.') + self.git('commit', '-q', '--author', 'Contributor ', + '-m', 'fix: record ' + label + ' change') + sha = self.git('rev-parse', 'HEAD') + records[sha] = {'commit': {'author': {'email': 'contributor@example.invalid'}, + 'committer': {'email': email}, 'verification': {'verified': verified}}} foreign, unsigned = list(records)[2:] self.git('update-ref', 'refs/remotes/origin/main', unsigned) with mock.patch.object(policy, 'api_commit', side_effect=lambda _, sha: records[sha]), \ redirect_stdout(StringIO()): - findings = _check(self.push_event(unsigned, self.base, 'main'), 'push', verify_commits=True) + findings = policy.check(self.push_event(unsigned, self.base, 'main'), 'push', verify_commits=True) self.assertEqual(findings, [foreign + ': unexpected committer identity', unsigned + ': commit is not Verified by GitHub'])