overwrite_changeset_changelog.py 2.7 KB

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