Skip to content

Commit 117f52d

Browse files
authored
Merge pull request #5767 from nicoddemus/backport-5723-5740-5750
[4.6] Publish GitHub release notes after deployment
2 parents 0b039b1 + 9191857 commit 117f52d

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

.travis.yml

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

100100
- stage: deploy
101101
python: '3.6'
102-
install: pip install -U setuptools setuptools_scm
102+
install: pip install -U setuptools setuptools_scm tox
103103
script: skip
104+
# token to upload github release notes: GH_RELEASE_NOTES_TOKEN
105+
env:
106+
- secure: "OjOeL7/0JUDkV00SsTs732e8vQjHynpbG9FKTNtZZJ+1Zn4Cib+hAlwmlBnvVukML0X60YpcfjnC4quDOIGLPsh5zeXnvJmYtAIIUNQXjWz8NhcGYrhyzuP1rqV22U68RTCdmOq3lMYU/W2acwHP7T49PwJtOiUM5kF120UAQ0Zi5EmkqkIvH8oM5mO9Dlver+/U7Htpz9rhKrHBXQNCMZI6yj2aUyukqB2PN2fjAlDbCF//+FmvYw9NjT4GeFOSkTCf4ER9yfqs7yglRfwiLtOCZ2qKQhWZNsSJDB89rxIRXWavJUjJKeY2EW2/NkomYJDpqJLIF4JeFRw/HhA47CYPeo6BJqyyNV+0CovL1frpWfi9UQw2cMbgFUkUIUk3F6DD59PHNIOX2R/HX56dQsw7WKl3QuHlCOkICXYg8F7Ta684IoKjeTX03/6QNOkURfDBwfGszY0FpbxrjCSWKom6RyZdyidnESaxv9RzjcIRZVh1rp8KMrwS1OrwRSdG0zjlsPr49hWMenN/8fKgcHTV4/r1Tj6mip0dorSRCrgUNIeRBKgmui6FS8642ab5JNKOxMteVPVR2sFuhjOQ0Jy+PmvceYY9ZMWc3+/B/KVh0dZ3hwvLGZep/vxDS2PwCA5/xw31714vT5LxidKo8yECjBynMU/wUTTS695D3NY="
107+
addons:
108+
apt:
109+
packages:
110+
# required by publish_gh_release_notes
111+
- pandoc
112+
after_deploy: tox -e publish_gh_release_notes
104113
deploy:
105114
provider: pypi
106115
user: nicoddemus

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ jobs:
9191
condition: eq(variables['python.needs_vc'], True)
9292
displayName: 'Install VC for py27'
9393

94-
- script: python -m pip install --upgrade pip && python -m pip install tox
94+
- script: python -m pip install tox
9595
displayName: 'Install tox'
9696

9797
- script: |

scripts/publish_gh_release_notes.py

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

tox.ini

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

139+
[testenv:publish_gh_release_notes]
140+
description = create GitHub release after deployment
141+
basepython = python3.6
142+
usedevelop = True
143+
passenv = GH_RELEASE_NOTES_TOKEN TRAVIS_TAG TRAVIS_REPO_SLUG
144+
deps =
145+
github3.py
146+
pypandoc
147+
commands = python scripts/publish_gh_release_notes.py
148+
149+
139150
[pytest]
140151
minversion = 2.0
141152
addopts = -ra -p pytester --strict-markers

0 commit comments

Comments
 (0)