Skip to content

Commit 1a61265

Browse files
committed
Publish GitHub release notes after deployment
Fix #2933
1 parent 300f785 commit 1a61265

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

.travis.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,17 @@ jobs:
7272

7373
- stage: deploy
7474
python: '3.6'
75-
install: pip install -U setuptools setuptools_scm
75+
install: pip install -U setuptools setuptools_scm tox
7676
script: skip
77+
# token to upload github release notes: GH_RELEASE_NOTES_TOKEN
78+
env:
79+
- secure: "OjOeL7/0JUDkV00SsTs732e8vQjHynpbG9FKTNtZZJ+1Zn4Cib+hAlwmlBnvVukML0X60YpcfjnC4quDOIGLPsh5zeXnvJmYtAIIUNQXjWz8NhcGYrhyzuP1rqV22U68RTCdmOq3lMYU/W2acwHP7T49PwJtOiUM5kF120UAQ0Zi5EmkqkIvH8oM5mO9Dlver+/U7Htpz9rhKrHBXQNCMZI6yj2aUyukqB2PN2fjAlDbCF//+FmvYw9NjT4GeFOSkTCf4ER9yfqs7yglRfwiLtOCZ2qKQhWZNsSJDB89rxIRXWavJUjJKeY2EW2/NkomYJDpqJLIF4JeFRw/HhA47CYPeo6BJqyyNV+0CovL1frpWfi9UQw2cMbgFUkUIUk3F6DD59PHNIOX2R/HX56dQsw7WKl3QuHlCOkICXYg8F7Ta684IoKjeTX03/6QNOkURfDBwfGszY0FpbxrjCSWKom6RyZdyidnESaxv9RzjcIRZVh1rp8KMrwS1OrwRSdG0zjlsPr49hWMenN/8fKgcHTV4/r1Tj6mip0dorSRCrgUNIeRBKgmui6FS8642ab5JNKOxMteVPVR2sFuhjOQ0Jy+PmvceYY9ZMWc3+/B/KVh0dZ3hwvLGZep/vxDS2PwCA5/xw31714vT5LxidKo8yECjBynMU/wUTTS695D3NY="
80+
addons:
81+
apt:
82+
packages:
83+
# required by publish_gh_release_notes
84+
- pandoc
85+
after_deploy: tox -e publish_gh_release_notes
7786
deploy:
7887
provider: pypi
7988
user: nicoddemus

scripts/publish_gh_release_notes.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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))

tox.ini

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ deps =
114114
wheel
115115
commands = python scripts/release.py {posargs}
116116

117+
[testenv:publish_gh_release_notes]
118+
description = create GitHub release after deployment
119+
basepython = python3.6
120+
usedevelop = True
121+
passenv = GH_RELEASE_NOTES_TOKEN TRAVIS_TAG
122+
deps =
123+
github3.py
124+
pypandoc
125+
commands = python scripts/publish_gh_release_notes.py
126+
127+
117128
[pytest]
118129
minversion = 2.0
119130
addopts = -ra -p pytester --strict-markers

0 commit comments

Comments
 (0)