overwrite_changeset_changelog.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 = (
  33. changelog_text.find(prev_version_pattern, notes_start_index)
  34. if PREV_VERSION and prev_version_pattern in changelog_text
  35. else len(changelog_text)
  36. )
  37. if new_content:
  38. return (
  39. changelog_text[:notes_start_index]
  40. + f"{new_content}\n"
  41. + changelog_text[notes_end_index:]
  42. )
  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 = (
  48. changelog_text[:notes_start_index]
  49. + parsed_lines
  50. + changelog_text[notes_end_index:]
  51. )
  52. updated_changelog = updated_changelog.replace(
  53. f"## {VERSION}", f"## [{VERSION}]"
  54. )
  55. return updated_changelog
  56. with open(CHANGELOG_PATH, "r") as f:
  57. changelog_content = f.read()
  58. new_changelog = overwrite_changelog_section(changelog_content, NEW_CONTENT)
  59. print(
  60. "----------------------------------------------------------------------------------"
  61. )
  62. print(new_changelog)
  63. print(
  64. "----------------------------------------------------------------------------------"
  65. )
  66. # Write back to CHANGELOG.md
  67. with open(CHANGELOG_PATH, "w") as f:
  68. f.write(new_changelog)
  69. print(f"{CHANGELOG_PATH} updated successfully!")