Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bump changelog for prerelease without commits #681

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ def generate_tree_from_commits(
pat = re.compile(changelog_pattern)
map_pat = re.compile(commit_parser, re.MULTILINE)
body_map_pat = re.compile(commit_parser, re.MULTILINE | re.DOTALL)
current_tag: Optional[GitTag] = None

# Check if the latest commit is not tagged
latest_commit = commits[0]
current_tag: Optional[GitTag] = get_commit_tag(latest_commit, tags)
if commits:
latest_commit = commits[0]
current_tag = get_commit_tag(latest_commit, tags)

current_tag_name: str = unreleased_version or "Unreleased"
current_tag_date: str = ""
Expand Down
14 changes: 13 additions & 1 deletion commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from operator import itemgetter
from typing import Callable, Dict, List, Optional

from packaging.version import parse

from commitizen import bump, changelog, defaults, factory, git, out, version_types
from commitizen.config import BaseConfig
from commitizen.exceptions import (
Expand Down Expand Up @@ -36,6 +38,13 @@ def __init__(self, config: BaseConfig, args):
"changelog_incremental"
)
self.dry_run = args["dry_run"]

self.current_version = (
args.get("current_version") or self.config.settings.get("version") or ""
)
self.current_version_instance = (
parse(self.current_version) if self.current_version else None
)
self.unreleased_version = args["unreleased_version"]
self.change_type_map = (
self.config.settings.get("change_type_map") or self.cz.change_type_map
Expand Down Expand Up @@ -155,7 +164,10 @@ def __call__(self):
)

commits = git.get_commits(start=start_rev, end=end_rev, args="--topo-order")
if not commits:
if not commits and (
self.current_version_instance is None
or not self.current_version_instance.is_prerelease
):
raise NoCommitsFoundError("No commits found")

tree = changelog.generate_tree_from_commits(
Expand Down
31 changes: 31 additions & 0 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
import sys
from datetime import datetime

Expand Down Expand Up @@ -660,6 +661,36 @@ def test_changelog_incremental_with_release_candidate_version(
file_regression.check(out, extension=".md")


@pytest.mark.parametrize(
"from_pre,to_pre", itertools.product(["alpha", "beta", "rc"], repeat=2)
)
@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.freeze_time("2021-06-11")
def test_changelog_incremental_with_prerelease_version_to_prerelease_version(
mocker: MockFixture, changelog_path, file_regression, from_pre, to_pre
):
with open(changelog_path, "w") as f:
f.write(KEEP_A_CHANGELOG)
create_file_and_commit("irrelevant commit")
git.tag("1.0.0", annotated=True)

create_file_and_commit("feat: add new output")
create_file_and_commit("fix: output glitch")

testargs = ["cz", "bump", "--changelog", "--prerelease", from_pre, "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()

testargs = ["cz", "bump", "--changelog", "--prerelease", to_pre, "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()

with open(changelog_path, "r") as f:
out = f.read()

file_regression.check(out, extension=".md")


@pytest.mark.parametrize("test_input", ["rc", "alpha", "beta"])
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_changelog_release_candidate_version_with_merge_prerelease(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0a1 (2021-06-11)

## 0.2.0a0 (2021-06-11)

### Feat

- add new output

### Fix

- output glitch

## [1.0.0] - 2017-06-20
### Added
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
- Version navigation.

### Changed
- Start using "changelog" over "change log" since it's the common usage.

### Removed
- Section about "changelog" vs "CHANGELOG".

## [0.3.0] - 2015-12-03
### Added
- RU translation from [@aishek](https://github.com/aishek).
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0b0 (2021-06-11)

## 0.2.0a0 (2021-06-11)

### Feat

- add new output

### Fix

- output glitch

## [1.0.0] - 2017-06-20
### Added
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
- Version navigation.

### Changed
- Start using "changelog" over "change log" since it's the common usage.

### Removed
- Section about "changelog" vs "CHANGELOG".

## [0.3.0] - 2015-12-03
### Added
- RU translation from [@aishek](https://github.com/aishek).
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0rc0 (2021-06-11)

## 0.2.0a0 (2021-06-11)

### Feat

- add new output

### Fix

- output glitch

## [1.0.0] - 2017-06-20
### Added
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
- Version navigation.

### Changed
- Start using "changelog" over "change log" since it's the common usage.

### Removed
- Section about "changelog" vs "CHANGELOG".

## [0.3.0] - 2015-12-03
### Added
- RU translation from [@aishek](https://github.com/aishek).
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0a0 (2021-06-11)

## 0.2.0b0 (2021-06-11)

### Feat

- add new output

### Fix

- output glitch

## [1.0.0] - 2017-06-20
### Added
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
- Version navigation.

### Changed
- Start using "changelog" over "change log" since it's the common usage.

### Removed
- Section about "changelog" vs "CHANGELOG".

## [0.3.0] - 2015-12-03
### Added
- RU translation from [@aishek](https://github.com/aishek).
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0b1 (2021-06-11)

## 0.2.0b0 (2021-06-11)

### Feat

- add new output

### Fix

- output glitch

## [1.0.0] - 2017-06-20
### Added
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
- Version navigation.

### Changed
- Start using "changelog" over "change log" since it's the common usage.

### Removed
- Section about "changelog" vs "CHANGELOG".

## [0.3.0] - 2015-12-03
### Added
- RU translation from [@aishek](https://github.com/aishek).
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0rc0 (2021-06-11)

## 0.2.0b0 (2021-06-11)

### Feat

- add new output

### Fix

- output glitch

## [1.0.0] - 2017-06-20
### Added
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
- Version navigation.

### Changed
- Start using "changelog" over "change log" since it's the common usage.

### Removed
- Section about "changelog" vs "CHANGELOG".

## [0.3.0] - 2015-12-03
### Added
- RU translation from [@aishek](https://github.com/aishek).
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0a0 (2021-06-11)

## 0.2.0rc0 (2021-06-11)

### Feat

- add new output

### Fix

- output glitch

## [1.0.0] - 2017-06-20
### Added
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
- Version navigation.

### Changed
- Start using "changelog" over "change log" since it's the common usage.

### Removed
- Section about "changelog" vs "CHANGELOG".

## [0.3.0] - 2015-12-03
### Added
- RU translation from [@aishek](https://github.com/aishek).
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0b0 (2021-06-11)

## 0.2.0rc0 (2021-06-11)

### Feat

- add new output

### Fix

- output glitch

## [1.0.0] - 2017-06-20
### Added
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
- Version navigation.

### Changed
- Start using "changelog" over "change log" since it's the common usage.

### Removed
- Section about "changelog" vs "CHANGELOG".

## [0.3.0] - 2015-12-03
### Added
- RU translation from [@aishek](https://github.com/aishek).
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.0rc1 (2021-06-11)

## 0.2.0rc0 (2021-06-11)

### Feat

- add new output

### Fix

- output glitch

## [1.0.0] - 2017-06-20
### Added
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
- Version navigation.

### Changed
- Start using "changelog" over "change log" since it's the common usage.

### Removed
- Section about "changelog" vs "CHANGELOG".

## [0.3.0] - 2015-12-03
### Added
- RU translation from [@aishek](https://github.com/aishek).
Loading