|
| 1 | +""" |
| 2 | +This script updates a specific version's release notes section in CHANGELOG.md with new content |
| 3 | +or reformats existing content. |
| 4 | +
|
| 5 | +The script: |
| 6 | +1. Takes a version number, changelog path, and optionally new content as input from environment variables |
| 7 | +2. Finds the section in the changelog for the specified version |
| 8 | +3. Either: |
| 9 | + a) Replaces the content with new content if provided, or |
| 10 | + b) Reformats existing content by: |
| 11 | + - Removing the first two lines of the changeset format |
| 12 | + - Ensuring version numbers are wrapped in square brackets |
| 13 | +4. Writes the updated changelog back to the file |
| 14 | +
|
| 15 | +Environment Variables: |
| 16 | + CHANGELOG_PATH: Path to the changelog file (defaults to 'CHANGELOG.md') |
| 17 | + VERSION: The version number to update/format |
| 18 | + PREV_VERSION: The previous version number (used to locate section boundaries) |
| 19 | + NEW_CONTENT: Optional new content to insert for this version |
| 20 | +""" |
| 21 | + |
| 22 | +#!/usr/bin/env python3 |
| 23 | + |
| 24 | +import os |
| 25 | + |
| 26 | +CHANGELOG_PATH = os.environ.get("CHANGELOG_PATH", "CHANGELOG.md") |
| 27 | +VERSION = os.environ["VERSION"] |
| 28 | +PREV_VERSION = os.environ.get("PREV_VERSION", "") |
| 29 | +NEW_CONTENT = os.environ.get("NEW_CONTENT", "") |
| 30 | + |
| 31 | + |
| 32 | +def overwrite_changelog_section(changelog_text: str, new_content: str): |
| 33 | + # Find the section for the specified version |
| 34 | + version_pattern = f"## {VERSION}\n" |
| 35 | + prev_version_pattern = f"## [{PREV_VERSION}]\n" |
| 36 | + print(f"latest version: {VERSION}") |
| 37 | + print(f"prev_version: {PREV_VERSION}") |
| 38 | + |
| 39 | + notes_start_index = changelog_text.find(version_pattern) + len(version_pattern) |
| 40 | + notes_end_index = ( |
| 41 | + changelog_text.find(prev_version_pattern, notes_start_index) |
| 42 | + if PREV_VERSION and prev_version_pattern in changelog_text |
| 43 | + else len(changelog_text) |
| 44 | + ) |
| 45 | + |
| 46 | + if new_content: |
| 47 | + return ( |
| 48 | + changelog_text[:notes_start_index] |
| 49 | + + f"{new_content}\n" |
| 50 | + + changelog_text[notes_end_index:] |
| 51 | + ) |
| 52 | + else: |
| 53 | + changeset_lines = changelog_text[notes_start_index:notes_end_index].split("\n") |
| 54 | + # Remove the first two lines from the regular changeset format, ex: \n### Patch Changes |
| 55 | + parsed_lines = "\n".join(changeset_lines[2:]) |
| 56 | + updated_changelog = ( |
| 57 | + changelog_text[:notes_start_index] |
| 58 | + + parsed_lines |
| 59 | + + changelog_text[notes_end_index:] |
| 60 | + ) |
| 61 | + updated_changelog = updated_changelog.replace( |
| 62 | + f"## {VERSION}", f"## [{VERSION}]" |
| 63 | + ) |
| 64 | + return updated_changelog |
| 65 | + |
| 66 | + |
| 67 | +with open(CHANGELOG_PATH, "r") as f: |
| 68 | + changelog_content = f.read() |
| 69 | + |
| 70 | +new_changelog = overwrite_changelog_section(changelog_content, NEW_CONTENT) |
| 71 | +print( |
| 72 | + "----------------------------------------------------------------------------------" |
| 73 | +) |
| 74 | +print(new_changelog) |
| 75 | +print( |
| 76 | + "----------------------------------------------------------------------------------" |
| 77 | +) |
| 78 | +# Write back to CHANGELOG.md |
| 79 | +with open(CHANGELOG_PATH, "w") as f: |
| 80 | + f.write(new_changelog) |
| 81 | + |
| 82 | +print(f"{CHANGELOG_PATH} updated successfully!") |
0 commit comments