2
0

parse_changeset_changelog.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python3
  2. import sys
  3. import os
  4. import subprocess
  5. GITHUB_OUTPUT = os.getenv("GITHUB_OUTPUT")
  6. CHANGELOG_PATH = os.environ.get("CHANGELOG_PATH", "CHANGELOG.md")
  7. VERSION = os.environ['VERSION']
  8. def parse_changelog_section(content: str):
  9. """Parse a specific version section from the changelog content.
  10. Returns: The formatted content for this version, or None if version not found
  11. """
  12. # Find the section for the specified version
  13. version_pattern = f"## {VERSION}\n"
  14. print(f"latest version: {VERSION}")
  15. notes_start_index = content.find(version_pattern) + len(version_pattern)
  16. prev_version = subprocess.getoutput("git show origin/main:package.json | grep '\"version\":' | cut -d'\"' -f4")
  17. print(f"prev_version: {prev_version}")
  18. prev_version_pattern = f"## {prev_version}\n"
  19. notes_end_index = content.find(prev_version_pattern, notes_start_index) if prev_version_pattern in content else len(content)
  20. return content[notes_start_index:notes_end_index]
  21. with open(CHANGELOG_PATH, 'r') as f:
  22. content = f.read()
  23. formatted_content = parse_changelog_section(content)
  24. if not formatted_content:
  25. print(f"Version {VERSION} not found in changelog", file=sys.stderr)
  26. sys.exit(1)
  27. print(formatted_content)
  28. with open(GITHUB_OUTPUT, "a") as gha_output:
  29. gha_output.write(f"release-notes<<EOF\n{formatted_content}\nEOF")