penpot/scripts/test_check_commit.py
andrés gonzález b0c569d891
🐛 Submit the import audit events from the import job (#13150)
* 🐛 Submit the import audit events from the import job

Since 2.20 the dashboard imports .penpot files through the
:import-binfile job, which submitted no audit event. The outcome of
an import and the files it created were lost, so analytics could no
longer tell completed imports from failed ones.

The job now submits the import-binfile event with the name and props
of the legacy RPC event (status, file-ids, file-count, project,
version, generated-by, referer), plus the job id, the team id, the
package size and the elapsed time. A cancelled job reports the
cancelled status. A completed import also submits a create-file event
per imported file, as the legacy RPC does. As in the RPC, an import
refused by the permission check submits no event.

Closes #13148

AI-assisted-by: claude-opus-5-5

* 🐛 Audit import jobs at their terminal transition

Run optional terminal hooks in the winning job transaction and use one
for import audit events. Preserve the separate import transaction and
cover cancellation, rollback, RPC paths, and dispatcher aborts.

AI-assisted-by: gpt-6.1-terra

* ♻️ Simplify the terminal job hook contract

Pass a semantic terminal job to the hook with its final status and
decoded parameters, result or error. Adapt the import audit and tests.

AI-assisted-by: gpt-6.1-terra

* 🐛 Keep import jobs bound to their original team

Freeze the destination team when creating an import job and serialize
imports with project moves. Use the frozen team for audit and webhook
events, and keep compatibility with existing jobs.

Select import audit props explicitly to prevent new job parameters from
being included in events by default.

AI-assisted-by: gpt-6.1-terra

* 📚 Document the import team fallback removal

Mark the legacy job audit fallback for removal in version 2.21.0.

AI-assisted-by: gpt-6.1-terra

* 🔧 Validate canonical AI model IDs in commit trailers

Require bare lowercase model IDs in AI-assisted-by trailers and cover
the validation with tests. Align the commit and PR workflows with the
same attribution rules.

AI-assisted-by: gpt-6.1-terra

---------

Co-authored-by: Andrey Antukh <niwi@niwi.nz>
2026-11-24 22:39:35 +01:00

288 lines
10 KiB
Python

#!/usr/bin/env python3
"""Tests for scripts/check-commit.
Run with:
python3 scripts/test_check_commit.py
Covers the body line-wrapping validator added to enforce the commit body
wrap rule documented in .serena/memories/workflow/creating-commits.md, and
the Co-authored-by validator, which allows human co-authors and rejects AI
providers and bots.
"""
import importlib.machinery
import importlib.util
import pathlib
import subprocess
import sys
import tempfile
import unittest
# Loading scripts/check-commit would otherwise emit scripts/__pycache__/.
sys.dont_write_bytecode = True
SCRIPT_PATH = pathlib.Path(__file__).resolve().parent / "check-commit"
def load_check_commit():
"""Load the extensionless scripts/check-commit as a module."""
loader = importlib.machinery.SourceFileLoader("check_commit", str(SCRIPT_PATH))
spec = importlib.util.spec_from_loader("check_commit", loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
return module
check_commit = load_check_commit()
class BodyLineLengthTests(unittest.TestCase):
def assert_ok(self, message):
ok, error = check_commit.check_body_line_length(message)
self.assertTrue(ok, error)
self.assertIsNone(error)
def assert_fail(self, message):
ok, error = check_commit.check_body_line_length(message)
self.assertFalse(ok)
self.assertIsNotNone(error)
return error
def test_wrapped_body_passes(self):
message = (
":bug: Fix crash when opening the file menu\n"
"\n"
"The menu reused a stale reference after the file was\n"
"closed, which raised an exception on reopen.\n"
)
self.assert_ok(message)
def test_line_at_limit_passes(self):
line = "x " * 40 # 80 chars, breakable
self.assertEqual(len(line), 80)
self.assert_ok(":bug: Fix crash\n\n" + line + "\n")
def test_line_one_over_limit_fails(self):
line = "x " * 40 + "x" # 81 chars, breakable
self.assertEqual(len(line), 81)
error = self.assert_fail(":bug: Fix crash\n\n" + line + "\n")
self.assertIn("80", error)
def test_long_body_line_fails(self):
long_line = "word " * 20 # 100 chars, breakable
error = self.assert_fail(":bug: Fix crash\n\n" + long_line + "\n")
self.assertIn("80", error)
self.assertIn("line 3", error)
def test_subject_is_not_checked(self):
# The subject has its own length rule; the body validator ignores it.
subject = ":bug: " + "S" * 100
self.assert_ok(subject + "\n")
def test_url_line_passes(self):
line = (
"See https://github.com/penpot/penpot/issues/1234"
"/comments/very/long/fragment"
)
self.assert_ok(":books: Update docs\n\n" + line + "\n")
def test_trailer_passes(self):
line = "Signed-off-by: Someone With A Long Name <someone@example.com>"
self.assert_ok(":bug: Fix crash\n\nBody.\n\n" + line + "\n")
def test_human_co_author_trailer_passes(self):
line = (
"Co-authored-by: Someone With A Very Long Name "
"<someone.with.a.long.address@example.com>"
)
self.assertGreater(len(line), 80)
self.assert_ok(":bug: Fix crash\n\nBody.\n\n" + line + "\n")
def test_unbreakable_token_passes(self):
line = "a" * 100 # no whitespace to wrap at
self.assert_ok(":bug: Fix crash\n\n" + line + "\n")
def test_blank_lines_are_ignored(self):
self.assert_ok(":bug: Fix crash\n\n\n\n")
def test_multiple_offenders_reported(self):
error = self.assert_fail(
":bug: Fix crash\n\n"
+ ("word " * 20)
+ "\n"
+ ("other " * 20)
+ "\n"
)
self.assertIn("line 3", error)
self.assertIn("line 4", error)
class CoAuthorTrailerTests(unittest.TestCase):
"""Co-authored-by is allowed for humans, forbidden for AI providers/bots."""
def assert_ok(self, message):
ok, error = check_commit.check_co_authored_by(message)
self.assertTrue(ok, error)
self.assertIsNone(error)
def assert_fail(self, message):
ok, error = check_commit.check_co_authored_by(message)
self.assertFalse(ok)
self.assertIsNotNone(error)
return error
def test_body_without_trailer_passes(self):
self.assert_ok(":bug: Fix crash\n\nBody.\n\nSigned-off-by: Someone <someone@example.com>\n")
def test_human_co_author_passes(self):
self.assert_ok(":bug: Fix crash\n\nBody.\n\nCo-authored-by: Jane Doe <jane@example.com>\n")
def test_human_with_github_noreply_email_passes(self):
self.assert_ok(
":bug: Fix crash\n\nBody.\n\n"
"Co-authored-by: Jane Doe <1234+jane@users.noreply.github.com>\n"
)
def test_human_whose_email_ends_with_bot_passes(self):
# "abbot@" must not count as a "bot@" identity.
self.assert_ok(
":bug: Fix crash\n\nBody.\n\n"
"Co-authored-by: Jane Abbot <abbot@example.com>\n"
)
def test_claude_fails(self):
error = self.assert_fail(
":bug: Fix crash\n\nBody.\n\n"
"Co-authored-by: Claude <noreply@anthropic.com>\n"
)
self.assertIn("Co-authored-by", error)
self.assertIn("AI-assisted-by", error)
def test_cursor_fails(self):
self.assert_fail(
":bug: Fix crash\n\nBody.\n\n"
"Co-authored-by: Cursor Agent <cursoragent@cursor.com>\n"
)
def test_copilot_fails(self):
self.assert_fail(
":bug: Fix crash\n\nBody.\n\n"
"Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>\n"
)
def test_case_insensitive_fails(self):
self.assert_fail(
":bug: Fix crash\n\nBody.\n\nco-authored-by: CHATGPT <x@example.com>\n"
)
def test_bot_identity_fails(self):
self.assert_fail(
":bug: Fix crash\n\nBody.\n\n"
"Co-authored-by: dependabot[bot] <support@github.com>\n"
)
def test_co_authored_by_in_body_fails(self):
self.assert_fail(
":bug: Fix crash\n\nCo-authored-by: Gemini <gemini@example.com>\nBody.\n"
)
def test_similar_trailer_passes(self):
self.assert_ok(":bug: Fix crash\n\nBody.\n\nCo-developed-by: Someone <someone@example.com>\n")
class AIAssistedTrailerTests(unittest.TestCase):
def test_valid_and_absent_trailers(self):
for trailer in ("", "AI-assisted-by: gpt-6.1-terra",
"ai-assisted-by: deepseek-v4-flash",
"AI-assisted-by: gpt-6.1-terra\nAI-assisted-by: mimo-v2.5"):
with self.subTest(trailer=trailer):
ok, error = check_commit.check_ai_assisted_by(":bug: Fix crash\n\n" + trailer)
self.assertTrue(ok, error)
def test_invalid_values_are_reported(self):
for value in ("", "openai/gpt-6.1-terra", "GPT-6.1 Terra",
"gpt-6.1-terra extra", "-gpt", "gpt-", "gpt..6", "gpt_6"):
with self.subTest(value=value):
ok, error = check_commit.check_ai_assisted_by(
":bug: Fix crash\n\nAI-assisted-by: " + value
)
self.assertFalse(ok)
self.assertIn("line 3", error)
def test_each_trailer_is_checked_and_all_offenders_reported(self):
ok, error = check_commit.check_ai_assisted_by(
":bug: Fix crash\n\nAI-assisted-by: gpt-6.1-terra\n"
"AI-assisted-by: openai/gpt-6.1-terra\n"
"ai-assisted-by: GPT-6.1 Terra"
)
self.assertFalse(ok)
self.assertIn("line 4", error)
self.assertIn("line 5", error)
def test_cli_rejects_invalid_trailer(self):
with tempfile.TemporaryDirectory() as directory:
subprocess.run(["git", "init", "-q", directory], check=True)
subprocess.run(
["git", "-C", directory, "-c", "user.name=Test",
"-c", "user.email=test@example.com", "commit", "-q",
"--allow-empty", "-m", ":bug: Fix crash",
"-m", "AI-assisted-by: openai/gpt-6.1-terra"],
check=True,
)
result = subprocess.run(
[str(SCRIPT_PATH), "-c", "HEAD"], cwd=directory,
capture_output=True, text=True, check=False,
)
self.assertEqual(result.returncode, 1)
self.assertIn("AI-assisted-by", result.stderr)
class SubjectRulesRegressionTests(unittest.TestCase):
"""Guard the pre-existing validators against accidental breakage."""
def test_valid_subject_passes_regex(self):
ok, error = check_commit.check_regex(":bug: Fix crash on startup")
self.assertTrue(ok, error)
def test_missing_emoji_fails_regex(self):
ok, _ = check_commit.check_regex("Fix crash on startup")
self.assertFalse(ok)
def test_trailing_dot_fails(self):
ok, _ = check_commit.check_subject_no_trailing_dot(":bug: Fix crash.")
self.assertFalse(ok)
def test_subject_at_80_chars_passes(self):
# ":bug: " is 6 chars, so 74 chars of text reach exactly 80.
ok, error = check_commit.check_subject_length(":bug: " + "S" * 74)
self.assertTrue(ok, error)
def test_subject_over_80_chars_fails(self):
ok, error = check_commit.check_subject_length(":bug: " + "S" * 75)
self.assertFalse(ok)
self.assertIn("80", error)
def test_merge_subject_skips_length_check(self):
# Git generates Merge subjects from branch names; the limit is skipped.
subject = "Merge branch 'some-very-long-branch-name-that-keeps-going' into develop with extra text here"
self.assertGreater(len(subject), 80)
ok, error = check_commit.check_subject_length(subject)
self.assertTrue(ok, error)
def test_revert_subject_skips_length_check(self):
subject = 'Revert ":bug: Fix some earlier commit with a fairly long subject line already here" extra'
self.assertGreater(len(subject), 80)
ok, error = check_commit.check_subject_length(subject)
self.assertTrue(ok, error)
def test_reapply_subject_skips_length_check(self):
subject = 'Reapply ":bug: Fix some earlier commit with a fairly long subject line already here" extra'
self.assertGreater(len(subject), 80)
ok, error = check_commit.check_subject_length(subject)
self.assertTrue(ok, error)
if __name__ == "__main__":
unittest.main(verbosity=2)