|
| 1 | +""" |
| 2 | +Script used to publish GitHub release notes extracted from CHANGELOG.rst. |
| 3 | +
|
| 4 | +This script is meant to be executed after a successful deployment in Travis. |
| 5 | +
|
| 6 | +Uses the following environment variables: |
| 7 | +
|
| 8 | +* GIT_TAG: the name of the tag of the current commit. |
| 9 | +* GH_RELEASE_NOTES_TOKEN: a personal access token with 'repo' permissions. It should be encrypted using: |
| 10 | +
|
| 11 | + $travis encrypt GH_RELEASE_NOTES_TOKEN=<token> -r pytest-dev/pytest |
| 12 | +
|
| 13 | + And the contents pasted in the ``deploy.env.secure`` section in the ``travis.yml`` file. |
| 14 | +
|
| 15 | +The script also requires ``pandoc`` to be previously installed in the system. |
| 16 | +
|
| 17 | +Requires Python3.6+. |
| 18 | +""" |
| 19 | +import os |
| 20 | +import re |
| 21 | +import sys |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +import github3 |
| 25 | +import pypandoc |
| 26 | + |
| 27 | + |
| 28 | +def publish_github_release(token, tag_name, body): |
| 29 | + github = github3.login(token=token) |
| 30 | + repo = github.repository("pytest-dev", "pytest") |
| 31 | + return repo.create_release(tag_name=tag_name, body=body) |
| 32 | + |
| 33 | + |
| 34 | +def parse_changelog(tag_name): |
| 35 | + p = Path(__file__).parent.parent / "CHANGELOG.rst" |
| 36 | + changelog_lines = p.read_text(encoding="UTF-8").splitlines() |
| 37 | + |
| 38 | + title_regex = re.compile(r"pytest (\d\.\d+\.\d+) \(\d{4}-\d{2}-\d{2}\)") |
| 39 | + consuming_version = False |
| 40 | + version_lines = [] |
| 41 | + for line in changelog_lines: |
| 42 | + m = title_regex.match(line) |
| 43 | + if m: |
| 44 | + # found the version we want: start to consume lines until we find the next version title |
| 45 | + if m.group(1) == tag_name: |
| 46 | + consuming_version = True |
| 47 | + # found a new version title while parsing the version we want: break out |
| 48 | + elif consuming_version: |
| 49 | + break |
| 50 | + if consuming_version: |
| 51 | + version_lines.append(line) |
| 52 | + |
| 53 | + return "\n".join(version_lines) |
| 54 | + |
| 55 | + |
| 56 | +def convert_rst_to_md(text): |
| 57 | + return pypandoc.convert_text(text, "md", format="rst") |
| 58 | + |
| 59 | + |
| 60 | +def main(argv): |
| 61 | + if len(argv) > 1: |
| 62 | + tag_name = argv[1] |
| 63 | + else: |
| 64 | + tag_name = os.environ.get("TRAVIS_TAG") |
| 65 | + if not tag_name: |
| 66 | + print("tag_name not given and $TRAVIS_TAG not set", file=sys.stderr) |
| 67 | + return 1 |
| 68 | + |
| 69 | + token = os.environ.get("GH_RELEASE_NOTES_TOKEN") |
| 70 | + if not token: |
| 71 | + print("GH_RELEASE_NOTES_TOKEN not set", file=sys.stderr) |
| 72 | + return 1 |
| 73 | + |
| 74 | + rst_body = parse_changelog(tag_name) |
| 75 | + md_body = convert_rst_to_md(rst_body) |
| 76 | + if not publish_github_release(token, tag_name, md_body): |
| 77 | + print("Could not publish release notes:", file=sys.stderr) |
| 78 | + print(md_body, file=sys.stderr) |
| 79 | + return 5 |
| 80 | + |
| 81 | + print(f"Release notes for {tag_name} published successfully") |
| 82 | + return 0 |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + sys.exit(main(sys.argv)) |
0 commit comments