-
Notifications
You must be signed in to change notification settings - Fork 0
/
trashman.py
338 lines (270 loc) · 11 KB
/
trashman.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import enum
from github import Github
import datetime
import os
import traceback
import re
import yaml
from io import BytesIO
import time
from pprint import pprint
import requests
import zipfile
from playlist_manager import manage_playlist
from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOAuth, SpotifyPKCE
from spotipy.exceptions import SpotifyException
import spotipy
class Trashman:
latest_comment_user = ""
datafile = "_data/trash.yml"
def comment_error(self, error, latest_comment_user=None):
if not latest_comment_user:
latest_comment_user = self.latest_comment_user
errortext_template = (
" **Error**\n"
"Encountered an error while handling your trash:\n"
" \n"
"```\n"
"{}\n"
" \n"
" \n"
"{}\n"
"```\n"
" \n"
"I hope this error is helpful.\n"
"You can retry by appending a new comment to this issue with the same formatting or by editing the issue content.\n"
" \n"
"And as always, remember to **REUSE**, **REDUCE** and **RAVE**\n"
)
errortext = errortext_template.format(error, traceback.format_exc())
self.issue.create_comment(self.g.render_markdown(errortext, context=self.repo))
self.issue.add_to_assignees(latest_comment_user)
print("::error file=,line=,col=::" + str(error))
raise (error)
def take_trash_from_github(self, issue):
print("::group::Loading yaml from issue/comment")
print("Setting default body and user from issue")
body = issue.body
latest_comment_user = issue.user
print("Loading body and user from last comment")
comments = issue.get_comments()
if comments.totalCount > 0:
try:
for comment in comments.reversed:
if comment.user.name:
latest_comment_user = comment.user
body = comment.body
break
except IndexError:
pass
print("Comment: \n{}".format(body))
print("User: {}".format(latest_comment_user.name))
try:
# regex returns matches in a list of tuples.
# Each tuple has this structure: ('```', 'content', '```')
trashdefinitions = re.findall(r"(```)([\s\S]*?)(```)", body)
# we are interested in the content of the first tuple in a posting
trashyaml = trashdefinitions[0][1]
print("YAML-String: \n{}".format(trashyaml))
new_trash = yaml.load(trashyaml, Loader=yaml.FullLoader)
except Exception as e:
self.comment_error(e, latest_comment_user)
print("Parsed YAML: \n{}".format(new_trash))
print("::endgroup::")
return new_trash, latest_comment_user
def upload_audio_to_s3(self, issue, new_trash):
print("::group::Uploading audio to S3")
file_url = new_trash.pop("audio_comment")
print("Identified audio url: {}".format(file_url))
try:
print("Downloading file")
r = requests.get(file_url, allow_redirects=True)
except Exception as e:
self.comment_error(e)
audiofile = " "
try:
zip = zipfile.ZipFile(BytesIO(r.content))
files = zip.namelist()
for file in files:
if file.endswith(".mp3"):
print("Unzipping file: {}".format(file))
# TODO: handle unzipping
# audiofile = zip.open(file)
# TODO: handle upload
audiofile = file
# TODO:audiofile contains public s3 locator
except Exception as e:
self.comment_error(e)
print("::endgroup::")
return audiofile
def insert_new_trash(self, new_trash):
print("::group::Inserting trash to existing file")
with open(self.datafile, "r") as file:
trash_list = yaml.load(file, Loader=yaml.FullLoader)
print("Loaded existing data file")
pprint(trash_list)
written = False
print(
"Check if datafile has an entry with the matching index: {}".format(
new_trash["issue_id"]
)
)
if trash_list:
for index, object in enumerate(trash_list):
try:
if object["issue_id"] == new_trash["issue_id"]:
print("Found entry with matching id")
object = new_trash
written = True
break
except KeyError:
pass
else:
trash_list=[]
if not written:
print("No matching entry found. Appending new trash.")
trash_list.append(new_trash)
print("Sorting all entries by date and if possible by id")
trash_list.sort(
key=lambda k: (
datetime.datetime.strptime(k["date"], "%d.%m.%Y"),
k.get("issue_id", 0),
),
reverse=True,
)
print("::endgroup::")
return trash_list
def commit_changes(self, branch_name, trashyaml, new_trash):
print("::group::Commiting changes")
commit_message = "Adds track {} as specified in issue #{}".format(
new_trash["songname"], new_trash["issue_id"]
)
ref = "refs/heads/" + branch_name
try:
self.repo.create_git_ref(ref, sha=self.repo.get_branch("main").commit.sha)
print("Created new branch: {}".format(ref))
old_file = self.repo.get_contents(self.datafile)
except:
print("Reused old branch: {}".format(ref))
old_file = self.repo.get_contents(self.datafile, ref)
self.repo.update_file(
path=old_file.path,
message=commit_message,
content=trashyaml,
sha=old_file.sha,
branch=branch_name,
)
print("Overwriting local file")
f = open(self.datafile, "w")
f.write(trashyaml)
f.close()
print("Commited file")
print("::endgroup::")
return
def create_pr(self, branch_name, new_trash):
print("::group::Creating PR")
pr_title = "Adds {} to Trash Tun.es".format(new_trash["songname"])
pr_body = "Closes #{}".format(new_trash["issue_id"])
try:
pr = self.repo.create_pull(
pr_title, pr_body, head=branch_name, base="main"
)
print("Created new PR")
except Exception as e:
pr = self.repo.get_pulls(
state="open", sort="created", head=branch_name, base="main"
)[0]
print("Reused existing PR")
print("::endgroup::")
return pr
def merge_pr(self, pr, user):
print("::group::Trying to merge PR")
if user in self.repo.get_collaborators():
print("User {} has the right to commit".format(user))
time.sleep(4)
try:
status = pr.merge()
if not status.merged:
print(
"Couldn't merge automatically. Merge status: {}".format(
self.pr.mergeable_state
)
)
pr.create_issue_comment("Couldn't merge automatically.")
pr.add_to_assignees(user)
except Exception as e:
print(
"Couldn't merge automatically. Merge status: {}".format(
pr.mergeable_state
)
)
print(e)
pr.create_issue_comment("Couldn't merge automatically.")
pr.add_to_assignees(user)
try:
if status:
print("Publishing playlist")
manage_playlist()
except Exception as e:
print(e)
else:
print("User {} has not the right to commit".format(user))
pr.create_issue_comment(
"Thank you for your suggestions. Our responsible trash men will take over from here"
)
print("Created issue comment")
pr.add_to_assignees(repo.get_collaborators())
print("Assigned collaborators")
print("::endgroup::")
return
def publish_changes(self, trashyaml, new_trash):
branch_name = "_".join(new_trash["songname"].lower().split()) + "_#{}".format(
new_trash["issue_id"]
)
print("::group::Interacting with repository content")
self.commit_changes(branch_name, trashyaml, new_trash)
pr = self.create_pr(branch_name, new_trash)
self.merge_pr(pr, self.latest_comment_user)
print("::endgroup::")
def enrich_trash(self, new_trash):
refresh_token = os.environ["SPOTIFY_REFRESH_TOKEN"]
scope = "playlist-modify-public"
manager = spotipy.SpotifyOAuth(scope=scope)
manager.refresh_access_token(refresh_token)
sp = spotipy.Spotify(auth_manager=manager)
track = sp.track(new_trash["spotify_uri"])
self.new_trash['trackname']=track['name']
self.new_trash['album']=track['album']['name']
self.new_trash['artist']= [x['name'] for x in track['artists']]
self.new_trash['mp3_url']=track['preview_url']
self.new_trash['cover_art']=track['album']['images'][0]['url']
self.new_trash['duration']=int(round(track['duration_ms']/60000, 0))
def main(self):
access_token = os.environ["GITHUB_TOKEN"]
repository = os.environ["REPOSITORY"]
issue_nr = int(os.environ["ISSUE"])
print("Repository: {}".format(repository))
print("Issue Number: {}".format(issue_nr))
self.g = Github(access_token)
self.repo = self.g.get_repo(repository)
self.issue = self.repo.get_issue(number=issue_nr)
if not self.repo.get_label("new trash") in self.issue.labels:
exit()
self.new_trash, self.latest_comment_user = self.take_trash_from_github(
self.issue
)
self.new_trash["issue_id"] = self.issue.number
self.new_trash["date"] = self.issue.created_at.strftime("%d.%m.%Y")
self.enrich_trash(self.new_trash)
print(self.new_trash)
if self.new_trash.get("audio_comment"):
self.new_trash["comment_url"] = self.upload_audio_to_s3(
self.issue, self.new_trash
)
else:
self.new_trash["comment_url"] = None
trash = self.insert_new_trash(self.new_trash)
trashyaml = yaml.dump(trash, allow_unicode=True)
self.publish_changes(trashyaml=trashyaml, new_trash=self.new_trash)
if __name__ == "__main__":
Trashman().main()