parse_changeset_changelog.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """
  2. This script extracts the release notes section for a specific version from CHANGELOG.md.
  3. The script:
  4. 1. Takes a version number and changelog path as input from environment variables
  5. 2. Finds the section in the changelog for the specified version
  6. 3. Extracts the content between the current version header and the next version header
  7. (or end of file if it's the latest version)
  8. 4. Outputs the extracted release notes to GITHUB_OUTPUT for use in creating GitHub releases
  9. Environment Variables:
  10. GITHUB_OUTPUT: Path to GitHub Actions output file
  11. CHANGELOG_PATH: Path to the changelog file (defaults to 'CHANGELOG.md')
  12. VERSION: The version number to extract notes for
  13. """
  14. #!/usr/bin/env python3
  15. import sys
  16. import os
  17. import subprocess
  18. GITHUB_OUTPUT = os.getenv("GITHUB_OUTPUT")
  19. CHANGELOG_PATH = os.environ.get("CHANGELOG_PATH", "CHANGELOG.md")
  20. VERSION = os.environ['VERSION']
  21. def parse_changelog_section(content: str):
  22. """Parse a specific version section from the changelog content.
  23. Args:
  24. content: The full changelog content as a string
  25. Returns:
  26. The formatted content for this version, or None if version not found
  27. Example:
  28. >>> content = "## 1.2.0\\nChanges\\n## 1.1.0\\nOld changes"
  29. >>> parse_changelog_section(content)
  30. 'Changes\\n'
  31. """
  32. # Find the section for the specified version
  33. version_pattern = f"## {VERSION}\n"
  34. print(f"latest version: {VERSION}")
  35. notes_start_index = content.find(version_pattern) + len(version_pattern)
  36. prev_version = subprocess.getoutput("git show origin/main:package.json | grep '\"version\":' | cut -d'\"' -f4")
  37. print(f"prev_version: {prev_version}")
  38. prev_version_pattern = f"## {prev_version}\n"
  39. notes_end_index = content.find(prev_version_pattern, notes_start_index) if prev_version_pattern in content else len(content)
  40. return content[notes_start_index:notes_end_index]
  41. with open(CHANGELOG_PATH, 'r') as f:
  42. content = f.read()
  43. formatted_content = parse_changelog_section(content)
  44. if not formatted_content:
  45. print(f"Version {VERSION} not found in changelog", file=sys.stderr)
  46. sys.exit(1)
  47. print(formatted_content)
  48. # Write the extracted release notes to GITHUB_OUTPUT
  49. with open(GITHUB_OUTPUT, "a") as gha_output:
  50. gha_output.write(f"release-notes<<EOF\n{formatted_content}\nEOF")