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