Skip to content

Commit 60475a6

Browse files
committed
refactor(changelog): reduces complexity of generate_tree_from_commits
The linter was complaining. Likely best to refactor this further.
1 parent e84430c commit 60475a6

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

commitizen/changelog.py

+44-25
Original file line numberDiff line numberDiff line change
@@ -125,35 +125,54 @@ def generate_tree_from_commits(
125125
if not matches:
126126
continue
127127

128-
# Process subject from commit message
129-
message = map_pat.match(commit.message)
130-
if message:
131-
parsed_message: Dict = message.groupdict()
132-
# change_type becomes optional by providing None
133-
change_type = parsed_message.pop("change_type", None)
134-
135-
if change_type_map:
136-
change_type = change_type_map.get(change_type, change_type)
137-
if changelog_message_builder_hook:
138-
parsed_message = changelog_message_builder_hook(parsed_message, commit)
139-
changes[change_type].append(parsed_message)
140-
141-
# Process body from commit message
142-
body_parts = commit.body.split("\n\n")
143-
for body_part in body_parts:
144-
message_body = body_map_pat.match(body_part)
145-
if not message_body:
146-
continue
147-
parsed_message_body: Dict = message_body.groupdict()
148-
149-
change_type = parsed_message_body.pop("change_type", None)
150-
if change_type_map:
151-
change_type = change_type_map.get(change_type, change_type)
152-
changes[change_type].append(parsed_message_body)
128+
update_changes_for_commit(
129+
changes,
130+
commit,
131+
change_type_map,
132+
changelog_message_builder_hook,
133+
map_pat,
134+
body_map_pat,
135+
)
153136

154137
yield {"version": current_tag_name, "date": current_tag_date, "changes": changes}
155138

156139

140+
def update_changes_for_commit(
141+
changes: Dict,
142+
commit: GitCommit,
143+
change_type_map: Optional[Dict[str, str]],
144+
changelog_message_builder_hook: Optional[Callable],
145+
map_pat: Pattern,
146+
body_map_pat: Pattern,
147+
):
148+
"""Processes the commit message and will update changes if applicable."""
149+
# Process subject from commit message
150+
message = map_pat.match(commit.message)
151+
if message:
152+
parsed_message: Dict = message.groupdict()
153+
# change_type becomes optional by providing None
154+
change_type = parsed_message.pop("change_type", None)
155+
156+
if change_type_map:
157+
change_type = change_type_map.get(change_type, change_type)
158+
if changelog_message_builder_hook:
159+
parsed_message = changelog_message_builder_hook(parsed_message, commit)
160+
changes[change_type].append(parsed_message)
161+
162+
# Process body from commit message
163+
body_parts = commit.body.split("\n\n")
164+
for body_part in body_parts:
165+
message_body = body_map_pat.match(body_part)
166+
if not message_body:
167+
continue
168+
parsed_message_body: Dict = message_body.groupdict()
169+
170+
change_type = parsed_message_body.pop("change_type", None)
171+
if change_type_map:
172+
change_type = change_type_map.get(change_type, change_type)
173+
changes[change_type].append(parsed_message_body)
174+
175+
157176
def order_changelog_tree(tree: Iterable, change_type_order: List[str]) -> Iterable:
158177
if len(set(change_type_order)) != len(change_type_order):
159178
raise InvalidConfigurationError(

0 commit comments

Comments
 (0)