-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlabel.py
More file actions
executable file
·233 lines (197 loc) · 7.47 KB
/
Copy pathlabel.py
File metadata and controls
executable file
·233 lines (197 loc) · 7.47 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python3
import json
import os
import re
from contextlib import suppress
from dataclasses import dataclass
from json import JSONDecodeError
import requests
import yaml
from github import Auth, Github, GithubException
from parse import parse
@dataclass
class IssueBody:
def __init__(self, issue_body: json):
self.device: str = issue_body['device']
self.version: str = issue_body['version']
self.date: str = issue_body['date']
self.kernel: str = issue_body['kernel']
self.baseband: str = issue_body['baseband']
self.mods: str = issue_body['mods']
self.expected: str = issue_body['expected']
self.current: str = issue_body['current']
self.solution: str = issue_body['solution']
self.reproduce: str = issue_body['reproduce']
self.directions: str = issue_body['directions']
# Let's be friendly...
for pattern in [
'lineage-{:d}.{:d}-{}',
'lineage {:d}.{:d}-{}',
'lineage-{:d}.{:d}',
'lineage {:d}.{:d}',
'lineage-{:d}',
'lineage {:d}',
'lineageos-{:d}.{:d}-{}',
'lineageos {:d}.{:d}-{}',
'lineageos-{:d}.{:d}',
'lineageos {:d}.{:d}',
'lineageos-{:d}',
'lineageos {:d}',
'lineage os-{:d}.{:d}-{}',
'lineage os {:d}.{:d}-{}',
'lineage os-{:d}.{:d}',
'lineage os {:d}.{:d}',
'lineage os-{:d}',
'lineage os {:d}',
'{:d}.{:d}-{}',
'{:d}.{:d}',
'{:d}',
]:
if version := parse(pattern, self.version):
major = version.fixed[0]
minor = version.fixed[1] if len(version.fixed) > 1 else 0
self.version = f'lineage-{major}.{minor}'
break
def device_list() -> dict:
ret = {}
for line in requests.get(
'https://raw.githubusercontent.com/LineageOS/hudson/main/lineage-build-targets',
timeout=5,
).text.splitlines():
if ' lineage-' in line:
codename, _, version, _ = line.split()
ret[codename] = version
return ret
def device_maintainers(device: str) -> list:
ret = []
for url in [
f'https://raw.githubusercontent.com/LineageOS/lineage_wiki/main/_data/devices/{device}.yml',
f'https://raw.githubusercontent.com/LineageOS/lineage_wiki/main/_data/devices/{device}_variant1.yml',
]:
req = requests.get(url, timeout=5)
if req.status_code == 200:
ret = yaml.safe_load(req.text)['maintainers']
break
if ret:
req = requests.get(
'https://raw.githubusercontent.com/LineageOS/lineage_wiki/main/_data/github_usernames.yml',
timeout=5,
)
if req.status_code == 200:
mapping = yaml.safe_load(req.text)['usernames']
ret = [mapping.get(x, x) for x in ret]
return ret
def issue_errors(issue: IssueBody) -> list:
ret = []
# Load supported devices list
devices = device_list()
for device in devices:
if device.lower() == issue.device.lower():
issue.device = device
break
else:
ret.append(
f'Device "{issue.device}" is not a valid device codename. Supported values are: {", ".join([f"`{device}`" for device in devices.keys()])}'
)
if device_version := devices.get(issue.device, None):
if issue.version != device_version:
ret.append(
f'LineageOS version "{issue.version}" is not a valid LineageOS version. Supported value is: {device_version}'
)
if not re.findall(r'^\d{8}(-.*)?$', issue.date):
ret.append(
f'Build date "{issue.date}" is not a valid date. Valid date format is YYYYMMDD'
)
return ret
def main() -> None:
# Auth to GitHub
github = Github(auth=Auth.Token(os.environ.get('GITHUB_TOKEN')))
# Get repo and issue
repo = github.get_repo(os.environ.get('GITHUB_REPOSITORY'))
issue = repo.get_issue(number=int(os.environ.get('ISSUE_NUMBER')))
# Don't touch already labeled issue
if issue.get_labels().totalCount > 0:
print('Labels count > 0, exiting.')
return
# Parse issue body
try:
issue_body = IssueBody(json.loads(os.environ.get('ISSUE_BODY')))
except JSONDecodeError:
issue.create_comment(
'\n'.join(
[
"Hi! It appears that your issue doesn't use the correct template.",
'Please create a new one and make sure to select "Bug Report" template.',
'',
'(this action was performed by a bot)',
]
)
)
issue.edit(state='closed')
return
# Close issue if there are any errors
if errors := issue_errors(issue_body):
issue.create_comment(
'\n'.join(
[
"Hi! It appears you didn't read or follow the provided issue template.",
'Please edit your issue to include the requested fields and follow the provided template, then reopen it by commenting `/reopen`.',
'For more information please see https://wiki.lineageos.org/how-to/bugreport.',
'',
'Problems:',
'',
*[f'* {x}' for x in errors],
'',
'(this action was performed by a bot)',
]
)
)
issue.edit(state='closed')
return
# Reopen if closed
issue.edit(state='open')
# Label issue
for label, color in [
[f'device:{issue_body.device}', '0075ca'],
[issue_body.version, '008672'],
]:
with suppress(GithubException):
repo.create_label(label, color) # just in case
issue.add_to_labels(repo.get_label(label))
# Assign maintainers if possible
for maintainer in device_maintainers(issue_body.device):
try:
user = github.get_user(maintainer)
_, data = issue._requester.requestJsonAndCheck(
'POST',
f'{issue.url}/assignees',
input={
'assignees': [user.login],
},
)
if not any(x['id'] == user.id for x in data['assignees']):
raise GithubException(status=400, message='User not added')
except GithubException as e:
print(
f'::warning ::Failed to assign {maintainer}: {e.message}',
flush=True,
)
if url := os.environ.get('DISCORD_WEBHOOK'):
repository_name = os.environ.get('GITHUB_REPOSITORY_NAME')
workflow_run_url = os.environ.get('GITHUB_WORKFLOW_RUN_URL')
requests.post(
url,
json={
'username': 'GitHub',
'avatar_url': 'https://cdn.discordapp.com/avatars/1483379599995047987/e57fd67dc7ca0cc840a0e87a82281bc5',
'embeds': [
{
'title': f'[{repository_name}] Failed to assign {maintainer}: {e.message}',
'url': workflow_run_url,
'color': 15426592,
}
],
},
)
if __name__ == '__main__':
main()