Skip to content

Commit

Permalink
Workflow for publishing new releases (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI authored Sep 6, 2021
1 parent b28a5aa commit 9577a42
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
@@ -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 }}
40 changes: 39 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
58 changes: 58 additions & 0 deletions script/publish_release.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 9577a42

Please sign in to comment.