Skip to content

Commit

Permalink
fix(changelog): use version as semver tag by default (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Jul 26, 2023
1 parent efa2b30 commit 5984c77
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 2 deletions.
2 changes: 1 addition & 1 deletion semantic_release/data/templates/CHANGELOG.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{% endif %}{% endfor %}{% endfor %}{% endif %}
{% for version, release in context.history.released.items() %}
{# RELEASED #}
## {{ version.as_tag() }} ({{ release.tagged_date.strftime("%Y-%m-%d") }})
## {{ version.as_semver_tag() }} ({{ release.tagged_date.strftime("%Y-%m-%d") }})
{% for type_, commits in release["elements"] | dictsort %}
### {{ type_ | capitalize }}
{% for commit in commits %}{% if type_ != "unknown" %}
Expand Down
3 changes: 3 additions & 0 deletions semantic_release/version/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ def __repr__(self) -> str:
def as_tag(self) -> str:
return self.tag_format.format(version=str(self))

def as_semver_tag(self) -> str:
return "v{version}".format(version=str(self))

def bump(self, level: LevelBump) -> Version:
"""
Return a new Version instance according to the level specified to bump.
Expand Down
83 changes: 83 additions & 0 deletions tests/fixtures/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,89 @@ def repo_with_git_flow_and_release_channels_angular_commits(
git_repo.close()


@pytest.fixture
def repo_with_git_flow_and_release_channels_angular_commits_using_tag_format(
git_repo_factory, file_in_repo
):
git_repo = git_repo_factory()

add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m="Initial commit")

add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m=COMMIT_MESSAGE.format(version="0.1.0"))
git_repo.git.tag("vpy0.1.0", m="vpy0.1.0")

# Do a prerelease
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m="fix: add some more text")
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m=COMMIT_MESSAGE.format(version="0.1.1-rc.1"))
git_repo.git.tag("vpy0.1.1-rc.1", m="vpy0.1.1-rc.1")

# Do a prerelease
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m="feat!: add some more text")
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m=COMMIT_MESSAGE.format(version="1.0.0-rc.1"))
git_repo.git.tag("vpy1.0.0-rc.1", m="vpy1.0.0-rc.1")

# Do a full release
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m="feat: add some more text")
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m=COMMIT_MESSAGE.format(version="1.0.0"))
git_repo.git.tag("vpy1.0.0", m="vpy1.0.0")

assert git_repo.commit("vpy1.0.0").hexsha == git_repo.head.commit.hexsha

# Suppose branch "dev" has prerelease suffix of "rc"
git_repo.create_head("dev")
git_repo.heads.dev.checkout()

add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m="feat: (dev) add some more text")
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m=COMMIT_MESSAGE.format(version="1.1.0-rc.1"))
git_repo.git.tag("vpy1.1.0-rc.1", m="vpy1.1.0-rc.1")

add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m="fix: (dev) add some more text")
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m=COMMIT_MESSAGE.format(version="1.1.0-rc.2"))
git_repo.git.tag("vpy1.1.0-rc.2", m="vpy1.1.0-rc.2")

assert git_repo.commit("vpy1.1.0-rc.2").hexsha == git_repo.head.commit.hexsha

# Suppose branch "feature" has prerelease suffix of "alpha"
git_repo.create_head("feature")
git_repo.heads.feature.checkout()

# Do a prerelease on the branch
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m="feat: (feature) add some more text")
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m=COMMIT_MESSAGE.format(version="1.1.0-alpha.1"))
git_repo.git.tag("vpy1.1.0-alpha.1", m="vpy1.1.0-alpha.1")

add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m="feat: (feature) add some more text")
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m=COMMIT_MESSAGE.format(version="1.1.0-alpha.2"))
git_repo.git.tag("vpy1.1.0-alpha.2", m="vpy1.1.0-alpha.2")

add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m="fix: (feature) add some more text")
add_text_to_file(git_repo, file_in_repo)
git_repo.git.commit(m=COMMIT_MESSAGE.format(version="1.1.0-alpha.3"))
git_repo.git.tag("vpy1.1.0-alpha.3", m="vpy1.1.0-alpha.3")

assert git_repo.commit("vpy1.1.0-alpha.3").hexsha == git_repo.head.commit.hexsha
assert git_repo.active_branch.name == "feature"
yield git_repo
git_repo.close()


@pytest.fixture
def repo_with_git_flow_and_release_channels_emoji_commits(
git_repo_factory, file_in_repo
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/semantic_release/changelog/TEST_CHANGELOG.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{% endif %}{% endfor %}
{% endfor %}{% endif %}
{% for version, release in context.history.released.items() %}
## {{ version.as_tag() }} ({{ release.tagged_date.strftime("%Y-%m-%d") }})
## {{ version.as_semver_tag() }} ({{ release.tagged_date.strftime("%Y-%m-%d") }})
{% for type_, commits in release["elements"] | dictsort %}
### {{ type_ | capitalize }}
{% for commit in commits %}{% if type_ != "unknown" %}
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/semantic_release/changelog/test_default_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ def test_default_changelog_template(
context.bind_to_environment(env)
actual_content = env.from_string(default_changelog_template).render()
assert actual_content == EXPECTED_CONTENT

def test_default_changelog_template_using_tag_format(
repo_with_git_flow_and_release_channels_angular_commits_using_tag_format, default_angular_parser
):
repo = repo_with_git_flow_and_release_channels_angular_commits_using_tag_format
env = environment(trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True)
rh = ReleaseHistory.from_git_history(
repo=repo, translator=VersionTranslator(tag_format="vpy{version}"), commit_parser=default_angular_parser
)
context = make_changelog_context(
hvcs_client=Github(remote_url=repo.remote().url), release_history=rh
)
context.bind_to_environment(env)

actual_content = env.from_string(default_changelog_template).render()
assert actual_content == EXPECTED_CONTENT

0 comments on commit 5984c77

Please sign in to comment.