Skip to content

Commit

Permalink
fix: Ignore tags that are not a valid semver
Browse files Browse the repository at this point in the history
PR-74: #74
  • Loading branch information
chme authored Mar 21, 2024
1 parent b61199f commit 156afe1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/git_changelog/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Any, ClassVar, Pattern

from semver import VersionInfo

if TYPE_CHECKING:
from git_changelog.providers import ProviderRefParser, Ref

Expand All @@ -21,6 +23,12 @@ def _clean_body(lines: list[str]) -> list[str]:
return lines


def _is_semver(version: str) -> bool:
if version[0] == "v":
version = version[1:]
return VersionInfo.is_valid(version)


class Commit:
"""A class to represent a commit."""

Expand Down Expand Up @@ -82,8 +90,10 @@ def __init__(
for ref in refs.split(","):
ref = ref.strip() # noqa: PLW2901
if ref.startswith("tag: "):
tag = ref.replace("tag: ", "")
break
ref = ref.replace("tag: ", "") # noqa: PLW2901
if _is_semver(ref):
tag = ref
break
self.tag: str = tag
self.version: str = tag

Expand Down
33 changes: 33 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ def test_no_bump_on_first_tag(repo: GitRepo, bump: str) -> None:
assert changelog.versions_list[0].tag == "1.1.1"


def test_ignore_nonsemver_tag(repo: GitRepo) -> None:
r"""Test parsing and grouping commits to versions.
Commit graph:
1.0.0
|
main A-B-C
|
dummy
Expected:
- 1.0.0: C B A
Parameters:
repo: GitRepo to a temporary repository.
"""
commit_a = repo.first_hash
commit_b = repo.commit("fix: B")
repo.tag("dummy")
commit_c = repo.commit("feat: C")
repo.tag("1.0.0")

changelog = Changelog(repo.path, convention=AngularConvention)

assert len(changelog.versions_list) == 1
_assert_version(
changelog.versions_list[0],
expected_tag="1.0.0",
expected_prev_tag=None,
expected_commits=[commit_c, commit_b, commit_a],
)


def test_one_release_branch_with_feat_branch(repo: GitRepo) -> None:
r"""Test parsing and grouping commits to versions.
Expand Down

0 comments on commit 156afe1

Please sign in to comment.