From 9577a429ee074446f5eeda042957f0f94dd3f5d8 Mon Sep 17 00:00:00 2001 From: Ruslan Sayfutdinov Date: Mon, 6 Sep 2021 11:37:00 +0100 Subject: [PATCH] Workflow for publishing new releases (#325) --- .github/workflows/publish-release.yml | 34 ++++++++++++++++ poetry.lock | 40 +++++++++++++++++- pyproject.toml | 1 + script/publish_release.py | 58 +++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/publish-release.yml create mode 100644 script/publish_release.py diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml new file mode 100644 index 000000000..84112d57a --- /dev/null +++ b/.github/workflows/publish-release.yml @@ -0,0 +1,34 @@ +name: Publish new release + +on: + workflow_dispatch: + +jobs: + publish: + name: Update manifest and publish + strategy: + matrix: + python-version: [3.8] + runs-on: ubuntu-latest + steps: + - name: Check out the repository + uses: actions/checkout@v2.3.4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2.2.2 + with: + python-version: ${{ matrix.python-version }} + + - name: Install Poetry + uses: abatilo/actions-poetry@v2.1.2 + with: + poetry-version: 1.1.5 + + - name: Install dependencies + run: poetry install + + - name: Run script + run: | + poetry run python script/publish_release.py + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/poetry.lock b/poetry.lock index 4a417f445..d90f37891 100644 --- a/poetry.lock +++ b/poetry.lock @@ -253,6 +253,20 @@ pep8test = ["black", "flake8", "flake8-import-order", "pep8-naming"] ssh = ["bcrypt (>=3.1.5)"] test = ["pytest (>=3.6.0,!=3.9.0,!=3.9.1,!=3.9.2)", "pretend", "iso8601", "pytz", "hypothesis (>=1.11.4,!=3.79.2)"] +[[package]] +name = "deprecated" +version = "1.2.13" +description = "Python @deprecated decorator to deprecate old python classes, functions or methods." +category = "dev" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" + +[package.dependencies] +wrapt = ">=1.10,<2" + +[package.extras] +dev = ["tox", "bump2version (<1)", "sphinx (<2)", "importlib-metadata (<3)", "importlib-resources (<4)", "configparser (<5)", "sphinxcontrib-websupport (<2)", "zipp (<2)", "PyTest (<5)", "PyTest-Cov (<2.6)", "pytest", "pytest-cov"] + [[package]] name = "distlib" version = "0.3.2" @@ -672,6 +686,22 @@ category = "dev" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +[[package]] +name = "pygithub" +version = "1.54.1" +description = "Use the full Github API v3" +category = "dev" +optional = false +python-versions = ">=3.6" + +[package.dependencies] +deprecated = "*" +pyjwt = "<2.0" +requests = ">=2.14.0" + +[package.extras] +integrations = ["cryptography"] + [[package]] name = "pyjwt" version = "1.7.1" @@ -932,7 +962,7 @@ ifaddr = ">=0.1.7" [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "7d4586bca0e6617c3daa9224a540f814f17cc7c85bef01a748d2eb083e66b3d0" +content-hash = "4af816af3c930b4ffe8ec11822dbca4b69004a34bedc936e51c9a930fb066d8c" [metadata.files] aiohttp = [ @@ -1139,6 +1169,10 @@ cryptography = [ {file = "cryptography-3.3.2-cp36-abi3-win_amd64.whl", hash = "sha256:7951a966613c4211b6612b0352f5bf29989955ee592c4a885d8c7d0f830d0433"}, {file = "cryptography-3.3.2.tar.gz", hash = "sha256:5a60d3780149e13b7a6ff7ad6526b38846354d11a15e21068e57073e29e19bed"}, ] +deprecated = [ + {file = "Deprecated-1.2.13-py2.py3-none-any.whl", hash = "sha256:64756e3e14c8c5eea9795d93c524551432a0be75629f8f29e67ab8caf076c76d"}, + {file = "Deprecated-1.2.13.tar.gz", hash = "sha256:43ac5335da90c31c24ba028af536a91d41d53f9e6901ddb021bcc572ce44e38d"}, +] distlib = [ {file = "distlib-0.3.2-py2.py3-none-any.whl", hash = "sha256:23e223426b28491b1ced97dc3bbe183027419dfc7982b4fa2f05d5f3ff10711c"}, {file = "distlib-0.3.2.zip", hash = "sha256:106fef6dc37dd8c0e2c0a60d3fca3e77460a48907f335fa28420463a6f799736"}, @@ -1517,6 +1551,10 @@ pyflakes = [ {file = "pyflakes-2.3.1-py2.py3-none-any.whl", hash = "sha256:7893783d01b8a89811dd72d7dfd4d84ff098e5eed95cfa8905b22bbffe52efc3"}, {file = "pyflakes-2.3.1.tar.gz", hash = "sha256:f5bc8ecabc05bb9d291eb5203d6810b49040f6ff446a756326104746cc00c1db"}, ] +pygithub = [ + {file = "PyGithub-1.54.1-py3-none-any.whl", hash = "sha256:87afd6a67ea582aa7533afdbf41635725f13d12581faed7e3e04b1579c0c0627"}, + {file = "PyGithub-1.54.1.tar.gz", hash = "sha256:300bc16e62886ca6537b0830e8f516ea4bc3ef12d308e0c5aff8bdbd099173d4"}, +] pyjwt = [ {file = "PyJWT-1.7.1-py2.py3-none-any.whl", hash = "sha256:5c6eca3c2940464d106b99ba83b00c6add741c9becaec087fb7ccdefea71350e"}, {file = "PyJWT-1.7.1.tar.gz", hash = "sha256:8d59a976fb773f3e6a39c85636357c4f0e242707394cadadd9814f5cbaa20e96"}, diff --git a/pyproject.toml b/pyproject.toml index c34cbdac8..dafac5ac4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ pre-commit = "^2.15.0" pylint = "^2.10.2" codespell = "^2.1.0" types-requests = "^2.25.6" +PyGithub = "^1.54.1" [tool.pylint.messages_control] # Reasons disabled: diff --git a/script/publish_release.py b/script/publish_release.py new file mode 100644 index 000000000..611d1b5c5 --- /dev/null +++ b/script/publish_release.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""Update manifest.json and create new Github Release.""" +from __future__ import annotations + +import json +import os +import sys + +from github import Github +from github.ContentFile import ContentFile +from github.GitRelease import GitRelease +from github.Repository import Repository + + +def main() -> int: + """Main function""" + github_token = os.environ.get("GITHUB_TOKEN") + print("Fetching draft release...") + github = Github(github_token) + repo = github.get_repo("leikoilja/ha-google-home") + release = repo.get_releases()[0] + if not release.draft: + print("The latest release is not a draft!") + return 1 + version = release.title.lstrip("v") + update_version(repo, version) + publish_release(release) + return 0 + + +def update_version(repo: Repository, version: str) -> None: + """Update manifest.json with the new version""" + print("Updating manifest.json...") + manifest = repo.get_contents("custom_components/google_home/manifest.json") + assert isinstance(manifest, ContentFile) + manifest_json = json.loads(manifest.decoded_content) + manifest_json["version"] = version + updated_manifest = json.dumps(manifest_json, indent=2) + "\n" + repo.update_file( + path=manifest.path, + message=f"Release v{version}", + content=updated_manifest, + sha=manifest.sha, + ) + + +def publish_release(release: GitRelease) -> None: + """Publish draft release""" + print("Publishing new release...") + release.update_release( + name=release.title, + message=release.body, + draft=False, + ) + + +if __name__ == "__main__": + sys.exit(main())