-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.py
155 lines (120 loc) · 4.64 KB
/
release.py
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
from typing import Match, Tuple, Union
from urllib.request import urlopen, Request, HTTPError
from string import Formatter
import os
import json
import re
EVENT_PATH = os.environ["GITHUB_EVENT_PATH"]
TOKEN = os.environ["GITHUB_TOKEN"]
RELEASE_ENDPOINT = (
"https://api.github.com/repos/saulmaldonado/ds-and-algorithms/releases"
)
FILES_ENDPOINT = (
"https://api.github.com/repos/saulmaldonado/ds-and-algorithms/pulls/{}/files"
)
MARKDOWN_LINK = "[{}](https://github.com/saulmaldonado/ds-and-algorithms/tree/main/{})"
class PullRequest:
def __init__(self, event_path: str, files_endpoint: Formatter):
self.files_endpoint = files_endpoint
self.event_path = event_path
self.event = self.__get_event_payload()
self.type, self.title, self.number = self.__get_ref()
self.path = self.__get_path()
def __get_event_payload(self):
with open(self.event_path) as file_pointer:
return json.load(file_pointer)
def __get_path(self) -> Union[str, None]:
files_url = self.files_endpoint.format(self.number)
files = json.load(urlopen(files_url))
filenames = [file["filename"] for file in files]
for filename in filenames:
match = re.match(
r"(?P<topic>[\w!-)\-]+)\/(?P<name>[\w!-)\-]+)\/(?P<filename>README\.md|\2(?:\.js|\.ts|\.go|\.java))",
filename,
)
if match is not None:
return f"{match.group('topic')}/{match.group('name')}"
return None
def __get_ref(self) -> str:
ref = self.event["pull_request"]["head"]["ref"]
number = self.event["number"]
match = re.match(r"(?P<branch_type>feat|fix)\/(?P<name>[\w!-)\-]+)", ref)
if match is None:
raise Exception(
"Pull request ref does not match convention, (feat|fix)/(branch name)"
)
title = PullRequest.format_title(match)
return (match.group("branch_type"), title, number)
@staticmethod
def format_title(match: Match) -> str:
branch_type = match.group("branch_type")
name = match.group("name")
title = re.sub("-", " ", name)
if branch_type == "feat":
return re.sub(r"\b\w", lambda match: match.group(0).upper(), title)
else:
return title.capitalize()
class Version:
def __init__(self, release_endpoint: str):
self.major, self.minor = Version.get_recent_version(release_endpoint)
@staticmethod
def get_recent_version(release_endpoint) -> Tuple[int, int]:
recent_tag = ""
releases = json.load(urlopen(release_endpoint))
if len(releases) == 0:
recent_tag = "v0.0.0"
else:
recent_tag = releases[0]["tag_name"]
match = re.match(r"v(?P<major>[\d]+)\.(?P<minor>[\d]+)", recent_tag)
return int(match.group("major")), int(match.group("minor"))
def bump_major(self):
self.major += 1
self.minor = 0
def bump_minor(self):
self.minor += 1
def get_tag(self):
return "v" + ".".join((str(self.major), str(self.minor)))
def bump_to_next(self, pull_request):
if pull_request.type == "feat":
self.bump_major()
else:
self.bump_minor()
class Release:
def __init__(
self, release_endpoint: str, mardown_link: Formatter, pull_req: PullRequest
):
self.release_endpoint = release_endpoint
self.pull_req = pull_req
self.markdown_link = mardown_link
ver = Version(self.release_endpoint)
ver.bump_to_next(self.pull_req)
self.tag_name = ver.get_tag()
self.name = self.pull_req.title
self.body = self.get_body(self.pull_req.path)
def get_payload(self):
return json.dumps(
{"tag_name": self.tag_name, "name": self.name, "body": self.body}
)
def get_body(self, path):
if path is None:
return ""
return self.markdown_link.format(self.name, path)
def new_release(self, token):
req = Request(
self.release_endpoint,
self.get_payload().encode("utf-8"),
{
"Authorization": "Bearer " + token,
"Accept": "application/vnd.github.v3+json",
"Content-Type": "application/json",
},
)
try:
res = urlopen(req)
print(f"{res.reason} {res.code}")
except HTTPError as err:
print(f"{err.reason} {err.code}")
if __name__ == "__main__":
pr = PullRequest(EVENT_PATH, FILES_ENDPOINT)
release = Release(RELEASE_ENDPOINT, MARKDOWN_LINK, pr)
release.new_release(TOKEN)