3131def overwrite_changelog_section (changelog_text : str , new_content : str ):
3232 # Find the section for the specified version
3333 version_pattern = f"## { VERSION } \n "
34+ bracketed_version_pattern = f"## [{ VERSION } ]\n "
3435 prev_version_pattern = f"## [{ PREV_VERSION } ]\n "
3536 print (f"latest version: { VERSION } " )
3637 print (f"prev_version: { PREV_VERSION } " )
3738
39+ # Try both unbracketed and bracketed version patterns
3840 version_index = changelog_text .find (version_pattern )
3941 if version_index == - 1 :
40- raise ValueError (f"Could not find version { VERSION } in changelog" )
42+ version_index = changelog_text .find (bracketed_version_pattern )
43+ if version_index == - 1 :
44+ # If version not found, add it at the top (after the first line)
45+ first_newline = changelog_text .find ('\n ' )
46+ if first_newline == - 1 :
47+ # If no newline found, just prepend
48+ return f"## [{ VERSION } ]\n \n { changelog_text } "
49+ return f"{ changelog_text [:first_newline + 1 ]} ## [{ VERSION } ]\n \n { changelog_text [first_newline + 1 :]} "
50+ else :
51+ # Using bracketed version
52+ version_pattern = bracketed_version_pattern
4153
4254 notes_start_index = version_index + len (version_pattern )
4355 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 )
@@ -54,18 +66,37 @@ def overwrite_changelog_section(changelog_text: str, new_content: str):
5466 # Remove the first two lines from the regular changeset format, ex: \n### Patch Changes
5567 parsed_lines = "\n " .join (changeset_lines [2 :])
5668 updated_changelog = changelog_text [:notes_start_index ] + parsed_lines + changelog_text [notes_end_index :]
69+ # Ensure version number is bracketed
5770 updated_changelog = updated_changelog .replace (f"## { VERSION } " , f"## [{ VERSION } ]" )
5871 return updated_changelog
5972
60- with open (CHANGELOG_PATH , 'r' ) as f :
61- changelog_content = f .read ()
73+ try :
74+ print (f"Reading changelog from: { CHANGELOG_PATH } " )
75+ with open (CHANGELOG_PATH , 'r' ) as f :
76+ changelog_content = f .read ()
77+
78+ print (f"Changelog content length: { len (changelog_content )} characters" )
79+ print ("First 200 characters of changelog:" )
80+ print (changelog_content [:200 ])
81+ print ("----------------------------------------------------------------------------------" )
82+
83+ new_changelog = overwrite_changelog_section (changelog_content , NEW_CONTENT )
84+
85+ print ("New changelog content:" )
86+ print ("----------------------------------------------------------------------------------" )
87+ print (new_changelog )
88+ print ("----------------------------------------------------------------------------------" )
89+
90+ print (f"Writing updated changelog back to: { CHANGELOG_PATH } " )
91+ with open (CHANGELOG_PATH , 'w' ) as f :
92+ f .write (new_changelog )
6293
63- new_changelog = overwrite_changelog_section (changelog_content , NEW_CONTENT )
64- print ("----------------------------------------------------------------------------------" )
65- print (new_changelog )
66- print ("----------------------------------------------------------------------------------" )
67- # Write back to CHANGELOG.md
68- with open (CHANGELOG_PATH , 'w' ) as f :
69- f .write (new_changelog )
94+ print (f"{ CHANGELOG_PATH } updated successfully!" )
7095
71- print (f"{ CHANGELOG_PATH } updated successfully!" )
96+ except FileNotFoundError :
97+ print (f"Error: Changelog file not found at { CHANGELOG_PATH } " )
98+ exit (1 )
99+ except Exception as e :
100+ print (f"Error updating changelog: { str (e )} " )
101+ print (f"Current working directory: { os .getcwd ()} " )
102+ exit (1 )
0 commit comments