Skip to content

Commit

Permalink
Automatically create release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Dec 26, 2019
1 parent c04bb2e commit 1a0ed5a
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
10 changes: 9 additions & 1 deletion .github/workflows/IntegrationRelease.yml
Expand Up @@ -60,4 +60,12 @@ jobs:
file: /home/runner/work/integration/integration/custom_components/hacs/hacs.zip
asset_name: hacs.zip
tag: ${{ github.ref }}
overwrite: true
overwrite: true

- name: Enable version 3.7 of Python
uses: actions/setup-python@v1
with:
python-version: 3.7

- name: Update release notes
run: python3 /home/runner/work/integration/integration/manage/generate_releasenotes.py --tag ${{ github.ref }}
133 changes: 133 additions & 0 deletions manage/generate_releasenotes.py
@@ -0,0 +1,133 @@
import re
import sys
import json
from github import Github

BODY = """
[![Downloads for this release](https://img.shields.io/github/downloads/hacs/integration/{version}/total.svg)](https://github.com/hacs/integration/releases/{version})
# Integration changes
{integration_changes}
# Frontend changes
{frontend_changes}
# Links
- [Discord server for HACS](https://discord.gg/apgchf8)
- [HACS Documentation](https://hacs.xyz/)
- [How to submit bugs/feature requests](https://hacs.xyz/docs/issues)
- [If you like what I (@ludeeus) do please consider sposoring me on GitHub](https://github.com/sponsors/ludeeus)
- [Or by me a ☕️ / 🍺](https://www.buymeacoffee.com/ludeeus)
"""

CAHNGE = "- [{line}]({link}) @{author}\n"
NOCHANGE = "_No changes in this release._"

GITHUB = Github("e9556ecf7e532d2d7c9d57e12d805f4a14302a5b")
FRONTEND_CHANGES = ""
INTEGRATION_CHANGES = ""


def new_commits(repo, sha):
"""Get new commits in repo."""
from datetime import datetime

dateformat = "%a, %d %b %Y %H:%M:%S GMT"
release_commit = repo.get_commit(sha)
since = datetime.strptime(release_commit.last_modified, dateformat)
commits = repo.get_commits(since=since)
if len(list(commits)) == 1:
return False
return reversed(list(commits)[:-1])


def last_integration_release(github):
"""Return last release."""
repo = github.get_repo("hacs/integration")
tag_sha = None
data = {}
tags = list(repo.get_tags())
reg = "(v|^)?(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$"
skip = True
if tags:
for tag in tags:
tag_name = tag.name
if re.match(reg, tag_name):
tag_sha = tag.commit.sha
if skip:
skip = False
continue
break
data["tag_name"] = tag_name
data["tag_sha"] = tag_sha
return data


def last_frontend_release(repo, tag_name):
"""Return last release."""
tags = list(repo.get_tags())
if tags:
for tag in tags:
if tag_name == tag.name:
return tag.commit.sha


def get_frontend_commits(github):
changes = ""
repo = github.get_repo("hacs/frontend")
integration = github.get_repo("hacs/integration")
last_tag = last_integration_release(github)["tag_name"]
contents = integration.get_contents(
"custom_components/hacs/manifest.json", ref=f"refs/tags/{last_tag}"
)
for req in json.loads(contents.decoded_content)["requirements"]:
if "hacs_frontend" in req:
hacs_frontend = req.split("=")[1]
commits = new_commits(repo, last_frontend_release(repo, hacs_frontend))

if not commits:
changes = NOCHANGE
else:
for commit in commits:
changes += CAHNGE.format(
line=repo.get_git_commit(commit.sha).message,
link=commit.html_url,
author=commit.author.login,
)

return changes


def get_integration_commits(github):
changes = ""
repo = github.get_repo("hacs/integration")
commits = new_commits(repo, last_integration_release(github)["tag_sha"])

if not commits:
changes = NOCHANGE
else:
for commit in commits:
changes += CAHNGE.format(
line=repo.get_git_commit(commit.sha).message,
link=commit.html_url,
author=commit.author.login,
)

return changes


## Update release notes:
VERSION = str(sys.argv[2]).replace("refs/tags/", "")
REPO = GITHUB.get_repo("hacs/integration")
RELEASE = REPO.get_release(VERSION)
RELEASE.update_release(
name=VERSION,
message=BODY.format(
version=VERSION,
integration_changes=get_integration_commits(GITHUB),
frontend_changes=get_frontend_commits(GITHUB),
),
)
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -6,6 +6,7 @@ attrs==19.3.0
backoff==1.10.0
hacs_frontend>=20191226110059
integrationhelper==0.2.2
PyGithub==1.44.1
pytest-asyncio==0.10.0
pytest-cov==2.8.1
pytest==5.3.2
Expand Down

0 comments on commit 1a0ed5a

Please sign in to comment.