Skip to content

feat(#319): add optional change_type_order #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,21 @@ def generate_tree_from_commits(


def order_changelog_tree(tree: Iterable, change_type_order: List[str]) -> Iterable:
if len(set(change_type_order)) != len(change_type_order):
raise RuntimeError(
f"Change types contain duplicates types ({change_type_order})"
)

sorted_tree = []
for entry in tree:
entry_change_types = sorted(entry["changes"].keys())
ordered_change_types = []
for ct in change_type_order + entry_change_types:
if ct in entry_change_types and ct not in ordered_change_types:
ordered_change_types.append(ct)
changes = [(ct, entry["changes"][ct]) for ct in ordered_change_types]
ordered_change_types = change_type_order + sorted(
set(entry["changes"].keys()) - set(change_type_order)
)
changes = [
(ct, entry["changes"][ct])
for ct in ordered_change_types
if ct in entry["changes"]
]
sorted_tree.append({**entry, **{"changes": OrderedDict(changes)}})
return sorted_tree

Expand Down
4 changes: 2 additions & 2 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The equivalent example for a json config file:
```json
{
"commitizen": {
"name": "cz_customize",
"name": "cz_customize",
"customize": {
"message_template": "{{change_type}}:{% if show_message %} {{message}}{% endif %}",
"example": "feature: this feature enable customize through config file",
Expand All @@ -66,7 +66,7 @@ The equivalent example for a json config file:
"hotfix": "PATCH"
},
"change_type_order": ["BREAKING CHANGE", "feat", "fix", "refactor", "perf"],
"info_path": "cz_customize_info.txt",
"info_path": "cz_customize_info.txt",
"info": "This is customized info",
"questions": [
{
Expand Down
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ freezegun = "^0.3.15"
pydocstyle = "^5.0.2"
pre-commit = "^2.6.0"

# FIXME: Remove for submission (testing issues/319-only)
pytest-watch = "*"

[tool.poetry.scripts]
cz = "commitizen.cli:main"
git-cz = "commitizen.cli:main"
Expand Down
32 changes: 24 additions & 8 deletions tests/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,26 +803,42 @@ def test_generate_tree_from_commits(gitcommits, tags):
(
["BREAKING CHANGE", "refactor"],
{
2: (["refactor", "feat", "fix"], ["feat", "fix", "refactor"]),
3: (["BREAKING CHANGE", "refactor"], ["refactor", "BREAKING CHANGE"]),
"1.1.0": {
"original": ["feat", "fix", "refactor"],
"sorted": ["refactor", "feat", "fix"],
},
"1.0.0": {
"original": ["refactor", "BREAKING CHANGE"],
"sorted": ["BREAKING CHANGE", "refactor"],
},
},
),
),
)
def test_order_changelog_tree(change_type_order, expected_reordering):
tree = tuple(changelog.order_changelog_tree(COMMITS_TREE, change_type_order))
tree = changelog.order_changelog_tree(COMMITS_TREE, change_type_order)

index_of_reordered_entry = [*expected_reordering.keys()]
for index, entry in enumerate(tuple(tree)):
if index in index_of_reordered_entry:
sorted_order, original_order = expected_reordering[index]
version = tree[index]["version"]
if version in expected_reordering:
# Verify that all keys are present
assert [*tree[index].keys()] == [*COMMITS_TREE[index].keys()]
assert [*tree[index]["changes"].keys()] == sorted_order
assert [*COMMITS_TREE[index]["changes"].keys()] == original_order
# Verify that the reorder only impacted the returned dict and not the original
expected = expected_reordering[version]
assert [*tree[index]["changes"].keys()] == expected["sorted"]
assert [*COMMITS_TREE[index]["changes"].keys()] == expected["original"]
else:
assert [*entry["changes"].keys()] == [*tree[index]["changes"].keys()]


def test_order_changelog_tree_raises():
change_type_order = ["BREAKING CHANGE", "feat", "refactor", "feat"]
with pytest.raises(RuntimeError) as excinfo:
changelog.order_changelog_tree(COMMITS_TREE, change_type_order)

assert " duplicate" in str(excinfo)


def test_render_changelog(gitcommits, tags, changelog_content):
parser = defaults.commit_parser
changelog_pattern = defaults.bump_pattern
Expand Down