2023-12-31 21:25:13 +08:00
|
|
|
# mypy: disallow-untyped-defs
|
2019-08-10 22:03:36 +08:00
|
|
|
"""
|
|
|
|
Script used to publish GitHub release notes extracted from CHANGELOG.rst.
|
|
|
|
|
2020-12-17 19:19:50 +08:00
|
|
|
This script is meant to be executed after a successful deployment in GitHub actions.
|
2019-08-10 22:03:36 +08:00
|
|
|
|
|
|
|
Uses the following environment variables:
|
|
|
|
|
|
|
|
* GIT_TAG: the name of the tag of the current commit.
|
2019-12-20 19:54:44 +08:00
|
|
|
* GH_RELEASE_NOTES_TOKEN: a personal access token with 'repo' permissions.
|
|
|
|
|
|
|
|
Create one at:
|
|
|
|
|
|
|
|
https://github.com/settings/tokens
|
|
|
|
|
2020-12-17 19:19:50 +08:00
|
|
|
This token should be set in a secret in the repository, which is exposed as an
|
|
|
|
environment variable in the main.yml workflow file.
|
2019-08-10 22:03:36 +08:00
|
|
|
|
|
|
|
The script also requires ``pandoc`` to be previously installed in the system.
|
|
|
|
|
|
|
|
Requires Python3.6+.
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
2023-12-31 21:25:13 +08:00
|
|
|
from typing import Sequence
|
2019-08-10 22:03:36 +08:00
|
|
|
|
|
|
|
import pypandoc
|
|
|
|
|
|
|
|
|
2023-12-31 21:25:13 +08:00
|
|
|
def parse_changelog(tag_name: str) -> str:
|
2019-12-20 19:54:44 +08:00
|
|
|
p = Path(__file__).parent.parent / "doc/en/changelog.rst"
|
2019-08-10 22:03:36 +08:00
|
|
|
changelog_lines = p.read_text(encoding="UTF-8").splitlines()
|
|
|
|
|
2023-12-31 21:09:33 +08:00
|
|
|
title_regex = re.compile(r"pytest (\d\.\d+\.\d+\w*) \(\d{4}-\d{2}-\d{2}\)")
|
2019-08-10 22:03:36 +08:00
|
|
|
consuming_version = False
|
|
|
|
version_lines = []
|
|
|
|
for line in changelog_lines:
|
|
|
|
m = title_regex.match(line)
|
|
|
|
if m:
|
|
|
|
# found the version we want: start to consume lines until we find the next version title
|
|
|
|
if m.group(1) == tag_name:
|
|
|
|
consuming_version = True
|
|
|
|
# found a new version title while parsing the version we want: break out
|
|
|
|
elif consuming_version:
|
|
|
|
break
|
|
|
|
if consuming_version:
|
|
|
|
version_lines.append(line)
|
|
|
|
|
|
|
|
return "\n".join(version_lines)
|
|
|
|
|
|
|
|
|
2023-12-31 21:25:13 +08:00
|
|
|
def convert_rst_to_md(text: str) -> str:
|
|
|
|
result = pypandoc.convert_text(
|
2020-01-30 07:28:04 +08:00
|
|
|
text, "md", format="rst", extra_args=["--wrap=preserve"]
|
|
|
|
)
|
2023-12-31 21:25:13 +08:00
|
|
|
assert isinstance(result, str), repr(result)
|
|
|
|
return result
|
2019-08-10 22:03:36 +08:00
|
|
|
|
|
|
|
|
2023-12-31 21:25:13 +08:00
|
|
|
def main(argv: Sequence[str]) -> int:
|
2023-12-31 21:09:33 +08:00
|
|
|
if len(argv) != 3:
|
|
|
|
print("Usage: generate-gh-release-notes VERSION FILE")
|
|
|
|
return 2
|
2019-08-10 22:03:36 +08:00
|
|
|
|
2023-12-31 21:09:33 +08:00
|
|
|
version, filename = argv[1:3]
|
|
|
|
print(f"Generating GitHub release notes for version {version}")
|
|
|
|
rst_body = parse_changelog(version)
|
|
|
|
md_body = convert_rst_to_md(rst_body)
|
|
|
|
Path(filename).write_text(md_body, encoding="UTF-8")
|
2019-08-15 05:51:00 +08:00
|
|
|
print()
|
2023-12-31 21:09:33 +08:00
|
|
|
print(f"Done: {filename}")
|
2019-08-15 05:51:00 +08:00
|
|
|
print()
|
2019-08-10 22:03:36 +08:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main(sys.argv))
|