forked from hacs/integration
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_releasenotes.py
More file actions
187 lines (155 loc) · 5.53 KB
/
generate_releasenotes.py
File metadata and controls
187 lines (155 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import json
import re
import sys
from github import Github
BODY = """
[](https://github.com/hacs/integration/releases/{version})
{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 sponsoring me on GitHub](https://github.com/sponsors/ludeeus)
- [Or buy me a ☕️ / 🍺](https://www.buymeacoffee.com/ludeeus)
"""
CHANGES = """
## Integration changes
{integration_changes}
## Frontend changes
{frontend_changes}
"""
COMMANDS = """
***
<details>
<summary>HACS Bot commands</summary>
_Commands can only be issued by a user with write privileges to the repository._
Command | Description
-- | --
`@hacs-bot no` | Will close the issue, and not publish a new release.
`@hacs-bot close` | Same as `@hacs-bot no`.
`@hacs-bot yes` | Will create a new release and close the issue.
`@hacs-bot LGTM` | Same as `@hacs-bot yes`.
`@hacs-bot release x.xx.x` | Same as `@hacs-bot yes` but will change the release number to the one specified.
</details>
"""
CHANGE = "- [{line}]({link}) @{author}\n"
NOCHANGE = "_No changes in this release._"
GITHUB = Github(sys.argv[2])
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, skip=True):
"""Return last release."""
repo = github.get_repo("hacs/integration")
tag_sha = None
data = {}
tags = list(repo.get_tags())
reg = "(v|^)?(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$"
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, skip=True):
changes = ""
repo = github.get_repo("hacs/frontend")
integration = github.get_repo("hacs/integration")
last_tag = last_integration_release(github, skip)["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:
msg = repo.get_git_commit(commit.sha).message
if "Merge branch " in msg:
continue
if "Merge pull request " in msg:
continue
if "\n" in msg:
msg = msg.split("\n")[0]
changes += CHANGE.format(
line=msg, link=commit.html_url, author=commit.author.login
)
return changes
def get_integration_commits(github, skip=True):
changes = ""
repo = github.get_repo("hacs/integration")
commits = new_commits(repo, last_integration_release(github, skip)["tag_sha"])
if not commits:
changes = NOCHANGE
else:
for commit in commits:
msg = repo.get_git_commit(commit.sha).message
if "Merge branch " in msg:
continue
if "Merge pull request " in msg:
continue
if "\n" in msg:
msg = msg.split("\n")[0]
changes += CHANGE.format(
line=msg, link=commit.html_url, author=commit.author.login
)
return changes
## Update release notes:
UPDATERELEASE = str(sys.argv[4])
REPO = GITHUB.get_repo("hacs/integration")
if UPDATERELEASE == "yes":
VERSION = str(sys.argv[6]).replace("refs/tags/", "")
RELEASE = REPO.get_release(VERSION)
RELEASE.update_release(
name=VERSION,
prerelease=RELEASE.prerelease,
draft=RELEASE.draft,
message=BODY.format(
version=VERSION,
changes=CHANGES.format(
integration_changes=get_integration_commits(GITHUB),
frontend_changes=get_frontend_commits(GITHUB),
),
),
)
else:
frontend_changes = get_frontend_commits(GITHUB, False)
integration_changes = get_integration_commits(GITHUB, False)
if integration_changes != NOCHANGE or frontend_changes != NOCHANGE:
VERSION = last_integration_release(GITHUB, False)["tag_name"]
VERSION = f"{VERSION[:-1]}{int(VERSION[-1])+1}"
REPO.create_issue(
title=f"Create release {VERSION}?",
labels=["New release"],
assignee="ludeeus",
body=CHANGES.format(
integration_changes=integration_changes,
frontend_changes=frontend_changes,
)
+ COMMANDS,
)
else:
print("Not enough changes for a release.")