|
| 1 | +"""Parse the latest release notes from CHANGELOG.md. |
| 2 | +
|
| 3 | +If running in GitHub Actions, set the `release_title` output |
| 4 | +variable for use in subsequent step(s). |
| 5 | +
|
| 6 | +If running in CI, write the release notes to ReleaseNotes.md |
| 7 | +for upload as an artifact. |
| 8 | +
|
| 9 | +Otherwise, print the release title and notes to stdout. |
| 10 | +""" |
| 11 | + |
| 12 | +import re |
| 13 | +import subprocess |
| 14 | +from os import environ |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | + |
| 18 | +class ChangesEntry: |
| 19 | + def __init__(self, version: str, notes: str) -> None: |
| 20 | + self.version = version |
| 21 | + title = notes.splitlines()[0] |
| 22 | + self.title = f'{version} {title}' |
| 23 | + self.notes = notes[len(title) :].strip() |
| 24 | + |
| 25 | + |
| 26 | +H1 = re.compile(r'^# (\d+\.\d+\.\d+)', re.MULTILINE) |
| 27 | + |
| 28 | + |
| 29 | +def parse_changelog() -> list[ChangesEntry]: |
| 30 | + changelog = Path('CHANGELOG.md').read_text(encoding='utf-8') |
| 31 | + parsed = H1.split(changelog) # may result in a blank line at index 0 |
| 32 | + if not parsed[0]: # leading entry is a blank line due to re.split() implementation |
| 33 | + parsed = parsed[1:] |
| 34 | + assert len(parsed) % 2 == 0, ( |
| 35 | + 'Malformed CHANGELOG.md; Entries expected to start with "# x.y.x"' |
| 36 | + ) |
| 37 | + |
| 38 | + changes: list[ChangesEntry] = [] |
| 39 | + for i in range(0, len(parsed), 2): |
| 40 | + version = parsed[i] |
| 41 | + notes = parsed[i + 1].strip() |
| 42 | + changes.append(ChangesEntry(version, notes)) |
| 43 | + return changes |
| 44 | + |
| 45 | + |
| 46 | +def get_version_tag() -> str | None: |
| 47 | + if 'GITHUB_REF' in environ: # for use in GitHub Actions |
| 48 | + git_ref = environ['GITHUB_REF'] |
| 49 | + else: # for local use |
| 50 | + git_out = subprocess.run( |
| 51 | + ['git', 'rev-parse', '--symbolic-full-name', 'HEAD'], |
| 52 | + capture_output=True, |
| 53 | + text=True, |
| 54 | + check=True, |
| 55 | + ) |
| 56 | + git_ref = git_out.stdout.strip() |
| 57 | + version: str | None = None |
| 58 | + if git_ref and git_ref.startswith('refs/tags/'): |
| 59 | + version = git_ref[len('refs/tags/') :].lstrip('v') |
| 60 | + else: |
| 61 | + print( |
| 62 | + f"Using latest CHANGELOG.md entry because the git ref '{git_ref}' is not a tag." |
| 63 | + ) |
| 64 | + return version |
| 65 | + |
| 66 | + |
| 67 | +def get_entry(changes: list[ChangesEntry], version: str | None) -> ChangesEntry: |
| 68 | + latest = changes[0] |
| 69 | + if version is not None: |
| 70 | + for entry in changes: |
| 71 | + if entry.version == version: |
| 72 | + latest = entry |
| 73 | + break |
| 74 | + else: |
| 75 | + raise ValueError(f'No changelog entry found for version {version}') |
| 76 | + return latest |
| 77 | + |
| 78 | + |
| 79 | +def main() -> None: |
| 80 | + changes = parse_changelog() |
| 81 | + version = get_version_tag() |
| 82 | + latest = get_entry(changes=changes, version=version) |
| 83 | + if 'GITHUB_OUTPUT' in environ: |
| 84 | + with Path(environ['GITHUB_OUTPUT']).open('a') as gh_out: |
| 85 | + print(f'release_title={latest.title}', file=gh_out) |
| 86 | + if environ.get('CI', 'false') == 'true': |
| 87 | + Path('ReleaseNotes.md').write_text(latest.notes, encoding='utf-8') |
| 88 | + else: |
| 89 | + print('Release notes:') |
| 90 | + print(f'# {latest.title}\n{latest.notes}') |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + main() |
0 commit comments