diff --git a/commitizen/bump.py b/commitizen/bump.py index 030c8f1e5..6100e2490 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -34,7 +34,20 @@ def find_increment( increment: str | None = None for commit in commits: - for message in commit.message.split("\n"): + # We only consider: + # * the commit title (the first line, where the commit type lives), and + # * lines in the body that are a ``BREAKING CHANGE:`` / + # ``BREAKING-CHANGE:`` footer (per the Conventional Commits spec). + # Scanning every body line for type-prefixed text matches generated + # commit bodies (e.g. Dependabot quoting an upstream changelog with a + # ``fix:`` line) and produces false-positive bumps -- see #1772. + candidate_lines = [commit.title] + for body_line in commit.body.split("\n"): + stripped = body_line.lstrip() + if stripped.startswith(("BREAKING CHANGE:", "BREAKING-CHANGE:")): + candidate_lines.append(stripped) + + for message in candidate_lines: result = select_pattern.search(message) if result: diff --git a/tests/test_bump_find_increment.py b/tests/test_bump_find_increment.py index 8209278ed..aacafe65a 100644 --- a/tests/test_bump_find_increment.py +++ b/tests/test_bump_find_increment.py @@ -122,3 +122,51 @@ def test_find_increment_sve(messages, expected_type): commits, regex=semantic_version_pattern, increments_map=semantic_version_map ) assert increment_type == expected_type + + +# Mimics the dependabot pull request body that triggered #1772: a ``ci:`` +# title with a commit body that quotes upstream changelog lines, including +# ``fix: ...`` text. None of the body lines should bump the version. +DEPENDABOT_BODY = ( + "Bumps actions/upload-artifact from 5 to 6.\n" + "
\n" + "Commits\n" + "\n" + "
\n" +) + + +def test_find_increment_ignores_type_tokens_in_commit_body(): + """Regression test for #1772: a ``ci:`` commit whose body lists upstream + commit messages -- including ones that look like ``fix:`` -- must not + trigger a PATCH bump. Only the title's commit type counts.""" + commit = GitCommit(rev="test", title="ci: bump dep", body=DEPENDABOT_BODY) + increment_type = bump.find_increment( + [commit], + regex=ConventionalCommitsCz.bump_pattern, + increments_map=ConventionalCommitsCz.bump_map, + ) + assert increment_type is None + + +def test_find_increment_still_honors_breaking_change_in_body(): + """The ``BREAKING CHANGE:`` / ``BREAKING-CHANGE:`` footer in a commit body + must still trigger a MAJOR bump even after the #1772 fix; only commit + *types* are now restricted to the title.""" + commit = GitCommit( + rev="test", + title="feat: new user interface", + body="some body content\n\nBREAKING CHANGE: age is no longer supported", + ) + increment_type = bump.find_increment( + [commit], + regex=ConventionalCommitsCz.bump_pattern, + increments_map=ConventionalCommitsCz.bump_map, + ) + assert increment_type == "MAJOR"