Skip to content

Commit cd095ef

Browse files
bjoernricksy0urself
authored andcommitted
Change: Change ChangelogBuilder to allow passing Version instances
Make typing of ChangelogBuilder flexible by allowing to pass all kind of classes that implement `__str__` als version arguments. This allows for passing a Version instance to these methods too.
1 parent afd5a36 commit cd095ef

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

pontos/changelog/conventional_commits.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717

1818

1919
import re
20+
from abc import abstractmethod
2021
from datetime import date
2122
from pathlib import Path
22-
from typing import Dict, List, Optional, Union
23+
from typing import Dict, List, Optional, Protocol, Union, runtime_checkable
2324

2425
import tomlkit
2526

@@ -37,6 +38,13 @@
3738
"""
3839

3940

41+
@runtime_checkable
42+
class SupportsStr(Protocol):
43+
@abstractmethod
44+
def __str__(self) -> str:
45+
pass
46+
47+
4048
class ChangelogBuilder:
4149
"""
4250
Creates Changelog from conventional commits using the git log
@@ -92,8 +100,8 @@ def __init__(
92100
def create_changelog(
93101
self,
94102
*,
95-
last_version: Optional[str] = None,
96-
next_version: Optional[str] = None,
103+
last_version: Optional[SupportsStr] = None,
104+
next_version: Optional[SupportsStr] = None,
97105
) -> str:
98106
"""
99107
Create a changelog
@@ -116,8 +124,8 @@ def create_changelog_file(
116124
self,
117125
output: Union[str, Path],
118126
*,
119-
last_version: Optional[str] = None,
120-
next_version: Optional[str] = None,
127+
last_version: Optional[SupportsStr] = None,
128+
next_version: Optional[SupportsStr] = None,
121129
) -> None:
122130
"""
123131
Create a changelog and write the changelog to a file
@@ -142,7 +150,7 @@ def _get_first_commit(self) -> str:
142150
git = Git()
143151
return git.rev_list("HEAD", max_parents=0, abbrev_commit=True)[0]
144152

145-
def _get_git_log(self, last_version: Optional[str]) -> List[str]:
153+
def _get_git_log(self, last_version: Optional[SupportsStr]) -> List[str]:
146154
"""Getting the git log for the next version.
147155
148156
Requires the fitting branch to be checked out
@@ -209,8 +217,8 @@ def _sort_commits(self, commits: List[str]) -> Dict[str, List[str]]:
209217

210218
def _build_changelog(
211219
self,
212-
last_version: Optional[str],
213-
next_version: Optional[str],
220+
last_version: Optional[SupportsStr],
221+
next_version: Optional[SupportsStr],
214222
commit_dict: Dict[str, List[str]],
215223
) -> str:
216224
"""

0 commit comments

Comments
 (0)