Add update_changelog script and integrate it into release workflow

This commit is contained in:
SergeantPanda 2025-12-02 12:14:50 -06:00
parent 3cb695279a
commit 0bf3499917
2 changed files with 64 additions and 1 deletions

View file

@ -43,6 +43,10 @@ jobs:
NEW_VERSION=$(python -c "import version; print(f'{version.__version__}')")
echo "new_version=${NEW_VERSION}" >> $GITHUB_OUTPUT
- name: Update Changelog
run: |
python scripts/update_changelog.py ${{ steps.update_version.outputs.new_version }}
- name: Set repository metadata
id: meta
run: |
@ -54,7 +58,7 @@ jobs:
- name: Commit and Tag
run: |
git add version.py
git add version.py CHANGELOG.md
git commit -m "Release v${{ steps.update_version.outputs.new_version }}"
git tag -a "v${{ steps.update_version.outputs.new_version }}" -m "Release v${{ steps.update_version.outputs.new_version }}"
git push origin main --tags

View file

@ -0,0 +1,59 @@
#!/usr/bin/env python
"""
Updates the CHANGELOG.md file for a new release.
Renames [Unreleased] section to the new version with current date.
Usage: python update_changelog.py <version>
"""
import re
import sys
from datetime import datetime
from pathlib import Path
def update_changelog(version):
"""Update CHANGELOG.md with new version and date."""
changelog_file = Path(__file__).parent.parent / "CHANGELOG.md"
if not changelog_file.exists():
print("CHANGELOG.md not found")
sys.exit(1)
content = changelog_file.read_text(encoding='utf-8')
# Check if there's an Unreleased section
if '## [Unreleased]' not in content:
print("No [Unreleased] section found in CHANGELOG.md")
sys.exit(1)
# Get current date in YYYY-MM-DD format
today = datetime.now().strftime('%Y-%m-%d')
# Replace [Unreleased] with new version and date, and add new [Unreleased] section
# This pattern preserves everything after [Unreleased] until the next version or end
new_content = re.sub(
r'## \[Unreleased\]',
f'## [Unreleased]\n\n## [{version}] - {today}',
content,
count=1
)
if new_content == content:
print("Failed to update CHANGELOG.md")
sys.exit(1)
changelog_file.write_text(new_content, encoding='utf-8')
print(f"CHANGELOG.md updated for version {version} ({today})")
return True
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python update_changelog.py <version>")
print("Example: python update_changelog.py 0.13.0")
sys.exit(1)
version = sys.argv[1]
# Remove 'v' prefix if present
version = version.lstrip('v')
update_changelog(version)