Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SafetyQuincyF committed Oct 23, 2024
1 parent c2fafe8 commit 3d9f1d2
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions scripts/release_scripts/increment_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
from datetime import date
import sys


def get_current_version(file_path: str) -> str:
"""Read the current version from the VERSION file."""
with open(file_path, 'r') as file:
with open(file_path, "r") as file:
return file.read().strip()


def get_last_version_from_changelog(file_path: str) -> str:
"""Extract the last version noted in the CHANGELOG.MD file."""
with open(file_path, 'r') as file:
with open(file_path, "r") as file:
content = file.read()
match = re.search(r"\[(\d+\.\d+\.\d+)\] - \d{4}-\d{2}-\d{2}", content)
return match.group(1) if match else None


def increment_version(version: str, bump_type: str) -> str:
"""Increment the version number based on the bump type."""
major, minor, patch = map(int, version.split('.'))
major, minor, patch = map(int, version.split("."))

if bump_type == "major":
major += 1
Expand All @@ -33,11 +36,13 @@ def increment_version(version: str, bump_type: str) -> str:

return f"{major}.{minor}.{patch}"


def update_version_file(file_path: str, new_version: str):
"""Update the VERSION file with the new version."""
with open(file_path, 'w') as file:
with open(file_path, "w") as file:
file.write(new_version)


def get_git_commits_since_last_version(last_version: str) -> str:
"""Get all git commits since the last version."""
result = subprocess.run(
Expand All @@ -49,8 +54,6 @@ def get_git_commits_since_last_version(last_version: str) -> str:
raise Exception(f"Error running git log: {result.stderr}")
return result.stdout.strip()

import re


def format_commit_message(commit_message: str) -> str:
"""Format the commit message according to the changelog format."""
Expand All @@ -60,13 +63,14 @@ def format_commit_message(commit_message: str) -> str:
# Replace only the first occurrence of '/' with a ':'
slash_index = commit_message.find("/")
if slash_index != -1:
commit_message = commit_message[:slash_index] + ": " + commit_message[slash_index + 1:]
commit_message = (
commit_message[:slash_index] + ": " + commit_message[slash_index + 1 :]
)
return f"- {commit_message}"
else:
return f"- fix: {commit_message}"



def update_changelog(file_path: str, new_version: str, new_commits: str):
"""Add new version and commits to the changelog file in the correct section."""
today = date.today().strftime("%Y-%m-%d")
Expand All @@ -75,16 +79,16 @@ def update_changelog(file_path: str, new_version: str, new_commits: str):
for commit in new_commits.split("\n")
if (formatted_commit := format_commit_message(commit))
)

new_changelog_entry = f"## [{new_version}] - {today}\n{formatted_commits}\n"

with open(file_path, 'r+') as file:
with open(file_path, "r+") as file:
content = file.read()

if "[PEP 440](https://peps.python.org/pep-0440/)" in content:
content = content.replace(
"[PEP 440](https://peps.python.org/pep-0440/)",
"[PEP 440](https://peps.python.org/pep-0440/)\n"
"[PEP 440](https://peps.python.org/pep-0440/)\n",
)

changelog_header_index = content.find("# Changelog")
Expand All @@ -96,9 +100,7 @@ def update_changelog(file_path: str, new_version: str, new_commits: str):
insertion_point = len(content)

updated_content = (
content[:insertion_point] +
new_changelog_entry +
content[insertion_point:]
content[:insertion_point] + new_changelog_entry + content[insertion_point:]
)

file.seek(0)
Expand All @@ -113,8 +115,8 @@ def main():
return

bump_type = sys.argv[1]
version_file = 'safety/VERSION'
changelog_file = 'CHANGELOG.md'
version_file = "safety/VERSION"
changelog_file = "CHANGELOG.md"

# Get current version and last version in changelog
current_version = get_current_version(version_file)
Expand All @@ -140,5 +142,6 @@ def main():
update_changelog(changelog_file, new_version, commits)
print(f"CHANGELOG.MD updated with version {new_version}")


if __name__ == "__main__":
main()

0 comments on commit 3d9f1d2

Please sign in to comment.