From 4910036b76d846b4b7b0f80bb1291b0d1e67ea60 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 12 Aug 2019 13:20:16 -0300 Subject: [PATCH 1/5] Publish GitHub release notes after deployment (#5723) Publish GitHub release notes after deployment --- .travis.yml | 11 +++- scripts/publish_gh_release_notes.py | 86 +++++++++++++++++++++++++++++ tox.ini | 11 ++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 scripts/publish_gh_release_notes.py diff --git a/.travis.yml b/.travis.yml index abcbfbac5..2d6883f48 100644 --- a/.travis.yml +++ b/.travis.yml @@ -99,8 +99,17 @@ jobs: - stage: deploy python: '3.6' - install: pip install -U setuptools setuptools_scm + install: pip install -U setuptools setuptools_scm tox script: skip + # token to upload github release notes: GH_RELEASE_NOTES_TOKEN + env: + - secure: "OjOeL7/0JUDkV00SsTs732e8vQjHynpbG9FKTNtZZJ+1Zn4Cib+hAlwmlBnvVukML0X60YpcfjnC4quDOIGLPsh5zeXnvJmYtAIIUNQXjWz8NhcGYrhyzuP1rqV22U68RTCdmOq3lMYU/W2acwHP7T49PwJtOiUM5kF120UAQ0Zi5EmkqkIvH8oM5mO9Dlver+/U7Htpz9rhKrHBXQNCMZI6yj2aUyukqB2PN2fjAlDbCF//+FmvYw9NjT4GeFOSkTCf4ER9yfqs7yglRfwiLtOCZ2qKQhWZNsSJDB89rxIRXWavJUjJKeY2EW2/NkomYJDpqJLIF4JeFRw/HhA47CYPeo6BJqyyNV+0CovL1frpWfi9UQw2cMbgFUkUIUk3F6DD59PHNIOX2R/HX56dQsw7WKl3QuHlCOkICXYg8F7Ta684IoKjeTX03/6QNOkURfDBwfGszY0FpbxrjCSWKom6RyZdyidnESaxv9RzjcIRZVh1rp8KMrwS1OrwRSdG0zjlsPr49hWMenN/8fKgcHTV4/r1Tj6mip0dorSRCrgUNIeRBKgmui6FS8642ab5JNKOxMteVPVR2sFuhjOQ0Jy+PmvceYY9ZMWc3+/B/KVh0dZ3hwvLGZep/vxDS2PwCA5/xw31714vT5LxidKo8yECjBynMU/wUTTS695D3NY=" + addons: + apt: + packages: + # required by publish_gh_release_notes + - pandoc + after_deploy: tox -e publish_gh_release_notes deploy: provider: pypi user: nicoddemus diff --git a/scripts/publish_gh_release_notes.py b/scripts/publish_gh_release_notes.py new file mode 100644 index 000000000..f6c956fad --- /dev/null +++ b/scripts/publish_gh_release_notes.py @@ -0,0 +1,86 @@ +""" +Script used to publish GitHub release notes extracted from CHANGELOG.rst. + +This script is meant to be executed after a successful deployment in Travis. + +Uses the following environment variables: + +* GIT_TAG: the name of the tag of the current commit. +* GH_RELEASE_NOTES_TOKEN: a personal access token with 'repo' permissions. It should be encrypted using: + + $travis encrypt GH_RELEASE_NOTES_TOKEN= -r pytest-dev/pytest + + And the contents pasted in the ``deploy.env.secure`` section in the ``travis.yml`` file. + +The script also requires ``pandoc`` to be previously installed in the system. + +Requires Python3.6+. +""" +import os +import re +import sys +from pathlib import Path + +import github3 +import pypandoc + + +def publish_github_release(token, tag_name, body): + github = github3.login(token=token) + repo = github.repository("pytest-dev", "pytest") + return repo.create_release(tag_name=tag_name, body=body) + + +def parse_changelog(tag_name): + p = Path(__file__).parent.parent / "CHANGELOG.rst" + changelog_lines = p.read_text(encoding="UTF-8").splitlines() + + title_regex = re.compile(r"pytest (\d\.\d+\.\d+) \(\d{4}-\d{2}-\d{2}\)") + 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) + + +def convert_rst_to_md(text): + return pypandoc.convert_text(text, "md", format="rst") + + +def main(argv): + if len(argv) > 1: + tag_name = argv[1] + else: + tag_name = os.environ.get("TRAVIS_TAG") + if not tag_name: + print("tag_name not given and $TRAVIS_TAG not set", file=sys.stderr) + return 1 + + token = os.environ.get("GH_RELEASE_NOTES_TOKEN") + if not token: + print("GH_RELEASE_NOTES_TOKEN not set", file=sys.stderr) + return 1 + + rst_body = parse_changelog(tag_name) + md_body = convert_rst_to_md(rst_body) + if not publish_github_release(token, tag_name, md_body): + print("Could not publish release notes:", file=sys.stderr) + print(md_body, file=sys.stderr) + return 5 + + print(f"Release notes for {tag_name} published successfully") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/tox.ini b/tox.ini index 0b1be0d33..f2cb74e68 100644 --- a/tox.ini +++ b/tox.ini @@ -136,6 +136,17 @@ deps = wheel commands = python scripts/release.py {posargs} +[testenv:publish_gh_release_notes] +description = create GitHub release after deployment +basepython = python3.6 +usedevelop = True +passenv = GH_RELEASE_NOTES_TOKEN TRAVIS_TAG +deps = + github3.py + pypandoc +commands = python scripts/publish_gh_release_notes.py + + [pytest] minversion = 2.0 addopts = -ra -p pytester --strict-markers From 2fbea0e5e4175472d4936fa81245cc8c52086792 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 14 Aug 2019 21:46:57 -0300 Subject: [PATCH 2/5] Merge pull request #5740 from nicoddemus/use-repo-env-var Use TRAVIS_REPO_SLUG instead of hard-coding pytest-dev/pytest --- scripts/publish_gh_release_notes.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/scripts/publish_gh_release_notes.py b/scripts/publish_gh_release_notes.py index f6c956fad..23f7b40ad 100644 --- a/scripts/publish_gh_release_notes.py +++ b/scripts/publish_gh_release_notes.py @@ -25,9 +25,10 @@ import github3 import pypandoc -def publish_github_release(token, tag_name, body): +def publish_github_release(slug, token, tag_name, body): github = github3.login(token=token) - repo = github.repository("pytest-dev", "pytest") + owner, repo = slug.split("/") + repo = github.repository(owner, repo) return repo.create_release(tag_name=tag_name, body=body) @@ -71,14 +72,22 @@ def main(argv): print("GH_RELEASE_NOTES_TOKEN not set", file=sys.stderr) return 1 + slug = os.environ.get("TRAVIS_REPO_SLUG") + if not slug: + print("TRAVIS_REPO_SLUG not set", file=sys.stderr) + return 1 + rst_body = parse_changelog(tag_name) md_body = convert_rst_to_md(rst_body) - if not publish_github_release(token, tag_name, md_body): + if not publish_github_release(slug, token, tag_name, md_body): print("Could not publish release notes:", file=sys.stderr) print(md_body, file=sys.stderr) return 5 - print(f"Release notes for {tag_name} published successfully") + print() + print(f"Release notes for {tag_name} published successfully:") + print(f"https://github.com/{slug}/releases/tag/{tag_name}") + print() return 0 From 7a96f3f9701b9a3009dc7ddff8f2029bfcfc72aa Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 16 Aug 2019 15:57:01 -0300 Subject: [PATCH 3/5] Merge pull request #5750 from nicoddemus/fix-gh-publish-notes Forward $TRAVIS_REPO_SLUG for GH publish notes --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index f2cb74e68..ab9f581f7 100644 --- a/tox.ini +++ b/tox.ini @@ -140,7 +140,7 @@ commands = python scripts/release.py {posargs} description = create GitHub release after deployment basepython = python3.6 usedevelop = True -passenv = GH_RELEASE_NOTES_TOKEN TRAVIS_TAG +passenv = GH_RELEASE_NOTES_TOKEN TRAVIS_TAG TRAVIS_REPO_SLUG deps = github3.py pypandoc From 7718d8c97267174ffcaca2e5205de4c8732b58b5 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 19 Aug 2019 16:58:47 -0300 Subject: [PATCH 4/5] Fix linting --- scripts/publish_gh_release_notes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/publish_gh_release_notes.py b/scripts/publish_gh_release_notes.py index 23f7b40ad..3ff946b58 100644 --- a/scripts/publish_gh_release_notes.py +++ b/scripts/publish_gh_release_notes.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ Script used to publish GitHub release notes extracted from CHANGELOG.rst. From 9191857b5ff6a945f0ffb7cdc3dfbe719fd3cfa2 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 20 Aug 2019 19:11:14 -0300 Subject: [PATCH 5/5] Do not update pip on Azure Avoid upgrading pip because it is giving this error on py34: Requirement already up-to-date: pip in c:\hostedtoolcache\windows\python\3.4.4\x64\lib\site-packages (19.2.1) ERROR: Package 'pip' requires a different Python: 3.4.4 not in '>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*' [error]Cmd.exe exited with code '1'. --- azure-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 8e50486de..d33a9e09b 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -91,7 +91,7 @@ jobs: condition: eq(variables['python.needs_vc'], True) displayName: 'Install VC for py27' - - script: python -m pip install --upgrade pip && python -m pip install tox + - script: python -m pip install tox displayName: 'Install tox' - script: |