11"""
2- This script updates a specific version's release notes section in CHANGELOG.md with new content.
2+ This script updates a specific version's release notes section in CHANGELOG.md with new content
3+ or reformats existing content.
34
45The script:
5- 1. Takes a version number, changelog path, and new content as input from environment variables
6+ 1. Takes a version number, changelog path, and optionally new content as input from environment variables
672. Finds the section in the changelog for the specified version
7- 3. Replaces the content between the current version header and the next version header
8- (or end of file if it's the latest version) with the new content
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
9134. Writes the updated changelog back to the file
1014
1115Environment Variables:
1216 CHANGELOG_PATH: Path to the changelog file (defaults to 'CHANGELOG.md')
13- VERSION: The version number to update notes for
14- PREV_VERSION: The previous version number (optional )
15- NEW_CONTENT: The new content to insert for this version
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
1620"""
1721
1822#!/usr/bin/env python3
2226CHANGELOG_PATH = os .environ .get ("CHANGELOG_PATH" , "CHANGELOG.md" )
2327VERSION = os .environ ['VERSION' ]
2428PREV_VERSION = os .environ .get ("PREV_VERSION" , "" )
25- NEW_CONTENT = os .environ ['NEW_CONTENT' ]
26-
27- def overwrite_changelog_section (content : str ):
28- """Replace a specific version section in the changelog content.
29-
30- Args:
31- content: The full changelog content as a string
32-
33- Returns:
34- The updated changelog content with the new section
35-
36- Example:
37- >>> content = "## 1.2.0\\ nOld changes\\ n## 1.1.0\\ nOld changes"
38- >>> NEW_CONTENT = "New changes"
39- >>> overwrite_changelog_section(content)
40- '## 1.2.0\\ nNew changes\\ n## 1.1.0\\ nOld changes'
41- """
29+ NEW_CONTENT = os .environ .get ("NEW_CONTENT" , "" )
30+
31+ def overwrite_changelog_section (changelog_text : str , new_content : str ):
4232 # Find the section for the specified version
4333 version_pattern = f"## { VERSION } \n "
34+ prev_version_pattern = f"## [{ PREV_VERSION } ]\n "
4435 print (f"latest version: { VERSION } " )
45- notes_start_index = content .find (version_pattern ) + len (version_pattern )
4636 print (f"prev_version: { PREV_VERSION } " )
47- prev_version_pattern = f"## { PREV_VERSION } \n "
48- notes_end_index = content .find (prev_version_pattern , notes_start_index ) if PREV_VERSION and prev_version_pattern in content else len (content )
49- return content [:notes_start_index ] + f"{ NEW_CONTENT } \n " + content [notes_end_index :]
5037
51- with open ( CHANGELOG_PATH , 'r' ) as f :
52- content = f . read ( )
38+ notes_start_index = changelog_text . find ( version_pattern ) + len ( version_pattern )
39+ notes_end_index = changelog_text . find ( prev_version_pattern , notes_start_index ) if PREV_VERSION and prev_version_pattern in changelog_text else len ( changelog_text )
5340
54- new_changelog = overwrite_changelog_section (content )
41+ if new_content :
42+ return changelog_text [:notes_start_index ] + f"{ new_content } \n " + changelog_text [notes_end_index :]
43+ else :
44+ changeset_lines = changelog_text [notes_start_index :notes_end_index ].split ("\n " )
45+ # Remove the first two lines from the regular changeset format, ex: \n### Patch Changes
46+ parsed_lines = "\n " .join (changeset_lines [2 :])
47+ updated_changelog = changelog_text [:notes_start_index ] + parsed_lines + changelog_text [notes_end_index :]
48+ updated_changelog = updated_changelog .replace (f"## { VERSION } " , f"## [{ VERSION } ]" )
49+ return updated_changelog
5550
56- print (new_changelog )
51+ with open (CHANGELOG_PATH , 'r' ) as f :
52+ changelog_content = f .read ()
5753
54+ new_changelog = overwrite_changelog_section (changelog_content , NEW_CONTENT )
55+ print ("----------------------------------------------------------------------------------" )
56+ print (new_changelog )
57+ print ("----------------------------------------------------------------------------------" )
5858# Write back to CHANGELOG.md
5959with open (CHANGELOG_PATH , 'w' ) as f :
6060 f .write (new_changelog )
61+
62+ print (f"{ CHANGELOG_PATH } updated successfully!" )
0 commit comments