From 4568c269eebb81fd2a5314cb27451171a64ab483 Mon Sep 17 00:00:00 2001 From: Georgiy Ignatov Date: Thu, 9 Apr 2020 10:43:54 +0300 Subject: [PATCH 1/3] feat(find_increment): allow to use regex keys for bump_map Default bump_map doesnt consider breaking change identificator "!". Regex keys should take care of it. --- commitizen/bump.py | 37 ++++++++++++++++++------------- scripts/test | 4 ++-- tests/test_bump_find_increment.py | 36 +++++++++++++++++++++++------- 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/commitizen/bump.py b/commitizen/bump.py index d9fcbc1564..2bb1511d7b 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -1,5 +1,5 @@ import re -from collections import defaultdict +from collections import OrderedDict from itertools import zip_longest from string import Template from typing import List, Optional, Union @@ -18,28 +18,35 @@ def find_increment( - commits: List[GitCommit], regex: str = bump_pattern, increments_map: dict = bump_map + commits: List[GitCommit], + regex: str = bump_pattern, + increments_map: Union[dict, OrderedDict] = bump_map, ) -> Optional[str]: + if isinstance(increments_map, dict): + increments_map = OrderedDict(increments_map) + # Most important cases are major and minor. # Everything else will be considered patch. - increments_map_default = defaultdict(lambda: None, increments_map) - pattern = re.compile(regex) + select_pattern = re.compile(regex) increment = None for commit in commits: for message in commit.message.split("\n"): - result = pattern.search(message) - if not result: - continue - found_keyword = result.group(0) - new_increment = increments_map_default[found_keyword] - if increment == "MAJOR": - continue - elif increment == "MINOR" and new_increment == "MAJOR": - increment = new_increment - elif increment == "PATCH" or increment is None: - increment = new_increment + result = select_pattern.search(message) + if result: + found_keyword = result.group(0) + new_increment = None + for match_pattern in increments_map.keys(): + if re.match(match_pattern, found_keyword): + new_increment = increments_map[match_pattern] + + if increment == "MAJOR": + continue + elif increment == "MINOR" and new_increment == "MAJOR": + increment = new_increment + elif increment == "PATCH" or increment is None: + increment = new_increment return increment diff --git a/scripts/test b/scripts/test index 6a8082162e..699d3549fa 100755 --- a/scripts/test +++ b/scripts/test @@ -6,6 +6,6 @@ if [ -d 'venv' ] ; then fi ${PREFIX}pytest --cov-report term-missing --cov-report=xml:coverage.xml --cov=commitizen tests/ -${PREFIX}black commitizen tests --check -${PREFIX}isort --recursive --check-only commitizen tests +${PREFIX}black commitizen tests +${PREFIX}isort --recursive commitizen tests ${PREFIX}flake8 --max-line-length=88 commitizen/ tests/ diff --git a/tests/test_bump_find_increment.py b/tests/test_bump_find_increment.py index 5930be078f..b063b5cee6 100644 --- a/tests/test_bump_find_increment.py +++ b/tests/test_bump_find_increment.py @@ -2,9 +2,11 @@ CC: Conventional commits SVE: Semantic version at the end """ +from collections import OrderedDict + import pytest -from commitizen import bump +from commitizen import bump, defaults from commitizen.git import GitCommit NONE_INCREMENT_CC = ["docs(README): motivation", "ci: added travis"] @@ -27,6 +29,23 @@ "fix(setup.py): future is now required for every python version", ] +BUMP_PATTERN_REGEX_CC = r"^(BREAKING[\-\ ]CHANGE|feat|fix|refactor|perf)(\(.+\))?(!)?" +BUMP_MAP_REGEX_CC = OrderedDict( + ( + (r"^.+!$", defaults.MAJOR), + (r"^BREAKING[\-\ ]CHANGE", defaults.MAJOR), + (r"^feat", defaults.MINOR), + (r"^fix", defaults.PATCH), + (r"^refactor", defaults.PATCH), + (r"^perf", defaults.PATCH), + ) +) +MAJOR_INCREMENTS_REGEX_CC = [ + "feat(cli)!: added version", + "BREAKING CHANGE: break everything", + "BREAKING-CHANGE: alternative breaking change key", +] + PATCH_INCREMENTS_SVE = ["readme motivation PATCH", "fix setup.py PATCH"] MINOR_INCREMENTS_SVE = [ @@ -47,17 +66,18 @@ @pytest.mark.parametrize( - "messages, expected_type", + "messages, expected_type, regex, increment_map", ( - (PATCH_INCREMENTS_CC, "PATCH"), - (MINOR_INCREMENTS_CC, "MINOR"), - (MAJOR_INCREMENTS_CC, "MAJOR"), - (NONE_INCREMENT_CC, None), + (PATCH_INCREMENTS_CC, "PATCH", defaults.bump_pattern, defaults.bump_map), + (MINOR_INCREMENTS_CC, "MINOR", defaults.bump_pattern, defaults.bump_map), + (MAJOR_INCREMENTS_CC, "MAJOR", defaults.bump_pattern, defaults.bump_map), + (MAJOR_INCREMENTS_REGEX_CC, "MAJOR", BUMP_PATTERN_REGEX_CC, BUMP_MAP_REGEX_CC), + (NONE_INCREMENT_CC, None, defaults.bump_pattern, defaults.bump_map), ), ) -def test_find_increment(messages, expected_type): +def test_find_increment(messages, expected_type, regex, increment_map): commits = [GitCommit(rev="test", title=message) for message in messages] - increment_type = bump.find_increment(commits) + increment_type = bump.find_increment(commits, regex, increment_map) assert increment_type == expected_type From e1bc74f6d627a6360e27ebe69a2ad0afa226cf0a Mon Sep 17 00:00:00 2001 From: Georgiy Ignatov Date: Thu, 9 Apr 2020 11:07:44 +0300 Subject: [PATCH 2/3] test: return check for black and isort --- scripts/test | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/test b/scripts/test index 699d3549fa..6a8082162e 100755 --- a/scripts/test +++ b/scripts/test @@ -6,6 +6,6 @@ if [ -d 'venv' ] ; then fi ${PREFIX}pytest --cov-report term-missing --cov-report=xml:coverage.xml --cov=commitizen tests/ -${PREFIX}black commitizen tests -${PREFIX}isort --recursive commitizen tests +${PREFIX}black commitizen tests --check +${PREFIX}isort --recursive --check-only commitizen tests ${PREFIX}flake8 --max-line-length=88 commitizen/ tests/ From f47d1b772a4825d5397675e3a573f71c6c7b4e76 Mon Sep 17 00:00:00 2001 From: Georgiy Ignatov Date: Thu, 9 Apr 2020 12:15:04 +0300 Subject: [PATCH 3/3] feat(defaults): change default `bump_pattern` and `bump_map` --- commitizen/bump.py | 1 + commitizen/defaults.py | 23 +++++++++------ tests/test_bump_find_increment.py | 47 ++++++++++++++----------------- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/commitizen/bump.py b/commitizen/bump.py index 2bb1511d7b..bb80e2ba6a 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -40,6 +40,7 @@ def find_increment( for match_pattern in increments_map.keys(): if re.match(match_pattern, found_keyword): new_increment = increments_map[match_pattern] + break if increment == "MAJOR": continue diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 0dfee12246..e38ef01bf0 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -1,10 +1,12 @@ +from collections import OrderedDict + name: str = "cz_conventional_commits" # TODO: .cz, setup.cfg, .cz.cfg should be removed in 2.0 long_term_support_config_files: list = ["pyproject.toml", ".cz.toml"] deprcated_config_files: list = [".cz", "setup.cfg", ".cz.cfg"] config_files: list = long_term_support_config_files + deprcated_config_files -DEFAULT_SETTINGS = { +DEFAULT_SETTINGS: dict = { "name": "cz_conventional_commits", "version": None, "version_files": [], @@ -16,12 +18,15 @@ MINOR = "MINOR" PATCH = "PATCH" -bump_pattern = r"^(BREAKING CHANGE|feat|fix|refactor|perf)" -bump_map = { - "BREAKING CHANGE": MAJOR, - "feat": MINOR, - "fix": PATCH, - "refactor": PATCH, - "perf": PATCH, -} +bump_pattern = r"^(BREAKING[\-\ ]CHANGE|feat|fix|refactor|perf)(\(.+\))?(!)?" +bump_map = OrderedDict( + ( + (r"^.+!$", MAJOR), + (r"^BREAKING[\-\ ]CHANGE", MAJOR), + (r"^feat", MINOR), + (r"^fix", PATCH), + (r"^refactor", PATCH), + (r"^perf", PATCH), + ) +) bump_message = "bump: version $current_version → $new_version" diff --git a/tests/test_bump_find_increment.py b/tests/test_bump_find_increment.py index b063b5cee6..1f98694a2b 100644 --- a/tests/test_bump_find_increment.py +++ b/tests/test_bump_find_increment.py @@ -2,11 +2,9 @@ CC: Conventional commits SVE: Semantic version at the end """ -from collections import OrderedDict - import pytest -from commitizen import bump, defaults +from commitizen import bump from commitizen.git import GitCommit NONE_INCREMENT_CC = ["docs(README): motivation", "ci: added travis"] @@ -22,28 +20,24 @@ "fix(setup.py): future is now required for every python version", ] -MAJOR_INCREMENTS_CC = [ +MAJOR_INCREMENTS_BREAKING_CHANGE_CC = [ "feat(cli): added version", "docs(README): motivation", "BREAKING CHANGE: `extends` key in config file is now used for extending other config files", # noqa "fix(setup.py): future is now required for every python version", ] -BUMP_PATTERN_REGEX_CC = r"^(BREAKING[\-\ ]CHANGE|feat|fix|refactor|perf)(\(.+\))?(!)?" -BUMP_MAP_REGEX_CC = OrderedDict( - ( - (r"^.+!$", defaults.MAJOR), - (r"^BREAKING[\-\ ]CHANGE", defaults.MAJOR), - (r"^feat", defaults.MINOR), - (r"^fix", defaults.PATCH), - (r"^refactor", defaults.PATCH), - (r"^perf", defaults.PATCH), - ) -) -MAJOR_INCREMENTS_REGEX_CC = [ +MAJOR_INCREMENTS_BREAKING_CHANGE_ALT_CC = [ + "feat(cli): added version", + "docs(README): motivation", + "BREAKING-CHANGE: `extends` key in config file is now used for extending other config files", # noqa + "fix(setup.py): future is now required for every python version", +] + +MAJOR_INCREMENTS_EXCLAMATION_CC = [ "feat(cli)!: added version", - "BREAKING CHANGE: break everything", - "BREAKING-CHANGE: alternative breaking change key", + "docs(README): motivation", + "fix(setup.py): future is now required for every python version", ] PATCH_INCREMENTS_SVE = ["readme motivation PATCH", "fix setup.py PATCH"] @@ -66,18 +60,19 @@ @pytest.mark.parametrize( - "messages, expected_type, regex, increment_map", + "messages, expected_type", ( - (PATCH_INCREMENTS_CC, "PATCH", defaults.bump_pattern, defaults.bump_map), - (MINOR_INCREMENTS_CC, "MINOR", defaults.bump_pattern, defaults.bump_map), - (MAJOR_INCREMENTS_CC, "MAJOR", defaults.bump_pattern, defaults.bump_map), - (MAJOR_INCREMENTS_REGEX_CC, "MAJOR", BUMP_PATTERN_REGEX_CC, BUMP_MAP_REGEX_CC), - (NONE_INCREMENT_CC, None, defaults.bump_pattern, defaults.bump_map), + (PATCH_INCREMENTS_CC, "PATCH"), + (MINOR_INCREMENTS_CC, "MINOR"), + (MAJOR_INCREMENTS_BREAKING_CHANGE_CC, "MAJOR"), + (MAJOR_INCREMENTS_BREAKING_CHANGE_ALT_CC, "MAJOR"), + (MAJOR_INCREMENTS_EXCLAMATION_CC, "MAJOR"), + (NONE_INCREMENT_CC, None), ), ) -def test_find_increment(messages, expected_type, regex, increment_map): +def test_find_increment(messages, expected_type): commits = [GitCommit(rev="test", title=message) for message in messages] - increment_type = bump.find_increment(commits, regex, increment_map) + increment_type = bump.find_increment(commits) assert increment_type == expected_type