Skip to content

Commit

Permalink
Use new bump-my-version version to skip altering changelog for a rc
Browse files Browse the repository at this point in the history
  • Loading branch information
wkoot committed Jun 18, 2024
1 parent 9c6c779 commit a1d864c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
3 changes: 3 additions & 0 deletions release/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ replace = "sonar.projectVersion={new_version}"
filename = "../docs/src/changelog.md"
search = "[Unreleased]"
replace = "v{new_version} - {$RELEASE_DATE}"
include_bumps = [
"pre_release_label",
] # this is the only bump that produces a non-rc release

[[tool.bumpversion.files]]
filename = "../.env"
Expand Down
35 changes: 11 additions & 24 deletions release/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,9 @@ def parse_arguments() -> tuple[str, str, bool]:
epilog=epilog,
formatter_class=RawDescriptionHelpFormatter,
)
allowed_bumps_in_rc_mode = [
"rc",
"rc-major",
"rc-minor",
"rc-patch",
"drop-rc",
] # rc = release candidate
allowed_bumps = ["rc-patch", "rc-minor", "rc-major", "patch", "minor", "major"]
bumps = allowed_bumps_in_rc_mode if "rc" in current_version else allowed_bumps
bumps = ["patch", "minor", "major"]
if "rc" in current_version:
bumps += ["rc", "release"]
parser.add_argument("bump", choices=bumps)
parser.add_argument(
"-c",
Expand All @@ -71,7 +65,7 @@ def parse_arguments() -> tuple[str, str, bool]:
return arguments.bump, current_version, arguments.check_preconditions_only


def check_preconditions(bump: str, current_version: str, rc: bool = False) -> None:
def check_preconditions(bump: str, current_version: str) -> None:
"""Check preconditions for version bump."""
messages = []
release_folder = get_release_folder()
Expand All @@ -80,7 +74,7 @@ def check_preconditions(bump: str, current_version: str, rc: bool = False) -> No
root = release_folder.parent
messages.extend(failed_preconditions_repo(root))
messages.extend(failed_preconditions_changelog(bump, root))
if not rc:
if bump == "release": # don't update the version overview for release candidates
messages.extend(failed_preconditions_version_overview(current_version, root))
if messages:
formatted_messages = "\n".join([f"- {message}" for message in messages])
Expand Down Expand Up @@ -115,7 +109,7 @@ def failed_preconditions_changelog(bump: str, root: pathlib.Path) -> list[str]:
changelog_text = changelog_file.read()
if "[Unreleased]" not in changelog_text:
messages.append(f"The changelog ({changelog}) has no '[Unreleased]' header.")
if bump == "drop-rc" and "-rc." in changelog_text:
if bump == "release" and "-rc." in changelog_text:
messages.append(
f"The changelog ({changelog}) still contains release candidates; remove "
"the release candidates and move their changes under the '[Unreleased]' header."
Expand All @@ -130,8 +124,9 @@ def failed_preconditions_version_overview(current_version: str, root: pathlib.Pa
version_overview_lines = version_overview_file.readlines()
missing = f"The version overview ({version_overview}) does not contain"
previous_line = ""
target_version = current_version.split("-rc.")[0]
for line in version_overview_lines:
if line.startswith(f"| v{current_version} "):
if line.startswith(f"| v{target_version} "):
if previous_line.startswith("| v"):
today = utc_today().isoformat()
release_date = previous_line.split(" | ")[1].strip()
Expand All @@ -140,7 +135,7 @@ def failed_preconditions_version_overview(current_version: str, root: pathlib.Pa
return [] # All good: current version, next version, and release date found
return [f"{missing}) the new version."]
previous_line = line
return [f"{missing} the current version ({current_version})."]
return [f"{missing} the target version ({target_version})."]


def utc_today() -> datetime.date:
Expand All @@ -154,25 +149,17 @@ def main() -> None:
bump, current_version, check_preconditions_only = parse_arguments()
# See https://github.com/callowayproject/bump-my-version?tab=readme-ov-file#add-support-for-pre-release-versions
# for how bump-my-version deals with pre-release versions
create_rc = bump.startswith("rc") # needs to be True for case "rc", and "rc-*"
if bump.startswith("rc-"):
bump = bump.split("-", maxsplit=1)[1] # Create a patch, minor, or major release candidate
check_preconditions(bump, current_version, create_rc)
check_preconditions(bump, current_version)
if check_preconditions_only:
return
cmd = ["bump-my-version", "bump"]
if bump == "drop-rc":
if bump == "release":
cmd.append("pre_release_label") # Bump the pre-release label from "rc" to "final" (being optional and omitted)
elif bump == "rc":
cmd.append("pre_release_number") # Bump the release candidate number, when already on a -rc version
else:
cmd.append(bump)
subprocess.run(cmd, check=True) # noqa: S603
if create_rc:
changelog_path = get_release_folder().parent / "docs" / "src" / "changelog.md"
subprocess.run(("git", "checkout", "HEAD~1", "--", changelog_path), check=True)
subprocess.run(("git", "add", changelog_path), check=True)
subprocess.run(("git", "commit", "-m", "Reset changelog after producing release candidate"), check=True)
subprocess.run(("git", "push", "--follow-tags"), check=True) # noqa: S603


Expand Down

0 comments on commit a1d864c

Please sign in to comment.