Skip to content

Commit

Permalink
Change: Extend get_last_release_version for ignoring pre releases
Browse files Browse the repository at this point in the history
Add a new ignore_pre_releases argument to get_last_release_version
function to allow ignoring pre releases to be considered as a release
version.

Using this feature will allow for generating a release changelog
including the changes from the pre releases.
  • Loading branch information
bjoernricks committed Mar 1, 2023
1 parent 95db4fa commit 9c1341c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
23 changes: 17 additions & 6 deletions pontos/version/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,31 @@ def versions_equal(new_version: str, old_version: str) -> bool:

def get_last_release_version(
git_tag_prefix: Optional[str] = "",
*,
ignore_pre_releases: Optional[bool] = False,
) -> Optional[Version]:
"""Get the last released Version from git.
Args:
git_tag_prefix: Git tag prefix to consider
ignore_pre_release: Ignore pre releases and only consider non pre
releases. Default is False.
Returns:
Last released git-tag if tags were found
Last released git-tag as Version if tags were found
or None
"""

tag_list = Git().list_tags(sort=TagSort.VERSION)

if not tag_list:
return None
while tag_list:
last_release_version = tag_list[-1]
last_release_version = last_release_version.strip(git_tag_prefix)

version = parse_version(last_release_version)
if not version.is_prerelease or not ignore_pre_releases:
return version

last_release_version = tag_list[-1]
last_release_version = last_release_version.strip(git_tag_prefix)
tag_list = tag_list[:-1]

return parse_version(last_release_version)
return None
17 changes: 17 additions & 0 deletions tests/version/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,20 @@ def test_get_last_release_version_with_git_prefix(
git_interface = _git_interface_mock.return_value
git_interface.list_tags.return_value = ["v1", "v2", "v3.55"]
self.assertEqual(get_last_release_version("v"), Version("3.55"))

@patch("pontos.version.helper.Git", spec=Git)
def test_get_last_release_version_ignore_pre_releases(
self, _git_interface_mock
):
git_interface = _git_interface_mock.return_value
git_interface.list_tags.return_value = [
"1",
"2",
"3.55a1",
"3.56.dev1",
"4.0.0rc1",
"4.0.1b1",
]
self.assertEqual(
get_last_release_version(ignore_pre_releases=True), Version("2")
)

0 comments on commit 9c1341c

Please sign in to comment.