overwrite_changeset_changelog.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. unformmatted_prev_version_pattern = f"## {PREV_VERSION}\n"
  29. prev_version_pattern = f"## [{PREV_VERSION}]\n"
  30. print(f"latest version: {VERSION}")
  31. print(f"prev_version: {PREV_VERSION}")
  32. notes_start_index = changelog_text.find(version_pattern) + len(version_pattern)
  33. notes_end_index = changelog_text.find(prev_version_pattern, notes_start_index) if PREV_VERSION and (prev_version_pattern in changelog_text or unformmatted_prev_version_pattern in changelog_text) else len(changelog_text)
  34. if new_content:
  35. return changelog_text[:notes_start_index] + f"{new_content}\n" + changelog_text[notes_end_index:]
  36. else:
  37. changeset_lines = changelog_text[notes_start_index:notes_end_index].split("\n")
  38. filtered_lines = []
  39. for line in changeset_lines:
  40. # If the previous line is a changeset format
  41. if len(filtered_lines) > 1 and filtered_lines[-1].startswith("### "):
  42. # Remove the last two lines from the filted_lines
  43. filtered_lines.pop()
  44. filtered_lines.pop()
  45. else:
  46. filtered_lines.append(line.strip())
  47. # Prepend a new line to the first line of filtered_lines
  48. if filtered_lines:
  49. filtered_lines[0] = "\n" + filtered_lines[0]
  50. # Print filted_lines wiht a "\n" at the end of each line
  51. for line in filtered_lines:
  52. print(line.strip())
  53. parsed_lines = "\n".join(line for line in filtered_lines)
  54. updated_changelog = changelog_text[:notes_start_index] + parsed_lines + changelog_text[notes_end_index:]
  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. # print(new_changelog)
  61. # print("----------------------------------------------------------------------------------")
  62. # Write back to CHANGELOG.md
  63. with open(CHANGELOG_PATH, 'w') as f:
  64. f.write(new_changelog)
  65. print(f"{CHANGELOG_PATH} updated successfully!")