-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbot.py
executable file
·1660 lines (1498 loc) · 56.2 KB
/
bot.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# pylint: disable=too-many-lines
"""Slack bot for managing releases"""
import asyncio
from asyncio import sleep as async_sleep # importing separately for simpler mocking
from collections import namedtuple
import os
import logging
import json
import re
import pytz
import sentry_sdk
from client_wrapper import ClientWrapper
from constants import (
ALL_CHECKBOXES_CHECKED,
CI,
DEPLOYED_TO_PROD,
DEPLOYING_TO_PROD,
DEPLOYING_TO_RC,
WAITING_FOR_CHECKBOXES,
FINISH_RELEASE_ID,
NEW_RELEASE_ID,
LIBRARY_TYPE,
MINOR,
NPM,
PROD,
RC,
SETUPTOOLS,
VALID_DEPLOYMENT_SERVER_TYPES,
VALID_RELEASE_ALL_TYPES,
WEB_APPLICATION_TYPE,
)
from exception import (
InputException,
ReleaseException,
StatusException,
)
from finish_release import finish_release
from github import (
get_org_and_repo,
needs_review,
set_release_label,
)
from release import (
any_new_commits,
create_release_notes,
init_working_dir,
release,
)
from lib import (
get_default_branch,
get_release_pr,
get_unchecked_authors,
format_user_id,
load_repos_info,
match_user,
next_versions,
now_in_utc,
parse_text_matching_options,
VERSION_RE,
COMMIT_HASH_RE,
remove_path_from_url,
)
from publish import publish
from slack import get_channels_info, get_doofs_id
from status import (
format_status_for_repo,
status_for_repo_last_pr,
status_for_repo_new_commits,
)
from version import (
get_commit_oneline_message,
get_project_version,
get_version_tag,
)
from wait_for_deploy import (
fetch_release_hash,
wait_for_deploy,
)
from web import make_app
log = logging.getLogger(__name__)
Task = namedtuple("Task", ["channel_id", "task"])
CommandArgs = namedtuple("CommandArgs", ["channel_id", "repo_info", "args", "manager"])
Command = namedtuple(
"Command",
["command", "parsers", "command_func", "description", "supported_project_types"],
)
Parser = namedtuple("Parser", ["func", "description"])
def get_envs():
"""Get required environment variables"""
required_keys = (
"SLACK_ACCESS_TOKEN",
"BOT_ACCESS_TOKEN",
"GITHUB_ACCESS_TOKEN",
"NPM_TOKEN",
"SLACK_SECRET",
"TIMEZONE",
"PORT",
"PYPI_USERNAME",
"PYPI_PASSWORD",
"PYPITEST_USERNAME",
"PYPITEST_PASSWORD",
)
env_dict = {key: os.environ.get(key, None) for key in required_keys}
missing_env_keys = [k for k, v in env_dict.items() if v is None]
if missing_env_keys:
raise Exception(
f"Missing required env variable(s): {', '.join(missing_env_keys)}"
)
return env_dict
# pylint: disable=too-many-instance-attributes,too-many-arguments,too-many-public-methods
class Bot:
"""Slack bot used to manage the release"""
def __init__(
self,
*,
doof_id,
slack_access_token,
github_access_token,
npm_token,
timezone,
repos_info,
loop,
):
"""
Create the slack bot
Args:
doof_id (str): Doof's id
slack_access_token (str): The OAuth access token used to interact with Slack
github_access_token (str): The Github access token used to interact with Github
npm_token (str): The NPM token to publish npm packages
timezone (tzinfo): The time zone of the team interacting with the bot
repos_info (list of RepoInfo): Information about the repositories connected to channels
loop (asyncio.events.AbstractEventLoop): The asyncio event loop
"""
self.doof_id = doof_id
self.slack_access_token = slack_access_token
self.github_access_token = github_access_token
self.npm_token = npm_token
self.timezone = timezone
self.repos_info = repos_info
self.loop = loop
# Keep track of long running or scheduled tasks
self.tasks = set()
self.doof_boot = now_in_utc()
async def lookup_users(self):
"""
Get users list from slack
"""
client = ClientWrapper()
resp = await client.post(
"https://slack.com/api/users.list", data={"token": self.slack_access_token}
)
resp.raise_for_status()
return resp.json()["members"]
async def translate_slack_usernames(self, names):
"""
Try to match each full name with a slack username.
Args:
names (iterable of str): An iterable of full names
Returns:
set of str:
A iterable of either the slack name or a full name if a slack name was not found
"""
try:
slack_users = await self.lookup_users()
return {match_user(slack_users, author) for author in names}
except: # pylint: disable=bare-except
log.exception(
"Exception during translate_slack_usernames, continuing with untranslated names..."
)
return set(names)
def get_repo_info(self, channel_id):
"""
Get the repo info for a channel, or return None if no channel matches
Args:
channel_id (str): The channel id
"""
for repo_info in self.repos_info:
if repo_info.channel_id == channel_id:
return repo_info
return None
async def _say(self, *, channel_id, text, attachments, message_type):
"""
Post a message in a Slack channel
Args:
channel_id (str): A channel id
text (str): A message
attachments (list of dict): Attachment information
message_type (str): The type of message
"""
attachments_dict = (
{"attachments": json.dumps(attachments)} if attachments else {}
)
text_dict = {"text": text} if text else {}
message_type_dict = {"type": message_type} if message_type else {}
client = ClientWrapper()
resp = await client.post(
"https://slack.com/api/chat.postMessage",
data={
"token": self.slack_access_token,
"channel": channel_id,
**text_dict,
**attachments_dict,
**message_type_dict,
},
)
resp.raise_for_status()
async def say(self, *, channel_id, text=None, attachments=None, message_type=None):
"""
Post a message in the Slack channel
Args:
channel_id (str): A channel id
text (str): A message
attachments (list of dict): Attachment information
message_type (str): The type of message
"""
await self._say(
channel_id=channel_id,
text=text,
attachments=attachments,
message_type=message_type,
)
async def update_message(
self, *, channel_id, timestamp, text=None, attachments=None
):
"""
Update an existing message in slack
Args:
channel_id (str): The channel id
timestamp (str): The timestamp of the message to update
text (str): New text for the message
attachments (list of dict): New attachments for the message
"""
attachments_dict = (
{"attachments": json.dumps(attachments)} if attachments else {}
)
text_dict = {"text": text} if text else {}
client = ClientWrapper()
resp = await client.post(
"https://slack.com/api/chat.update",
data={
"token": self.slack_access_token,
"channel": channel_id,
"ts": timestamp,
**text_dict,
**attachments_dict,
},
)
resp.raise_for_status()
async def delete_message(self, *, channel_id, timestamp):
"""
Deletes an existing message in slack
Args:
channel_id (str): The channel id
timestamp (str): The timestamp of the message to update
"""
client = ClientWrapper()
resp = await client.post(
"https://slack.com/api/chat.delete",
data={
"token": self.slack_access_token,
"channel": channel_id,
"ts": timestamp,
},
)
resp.raise_for_status()
async def say_with_attachment(self, *, channel_id, title, text, message_type=None):
"""
Post a message in the Slack channel, putting the text in an attachment with markdown enabled
Args:
channel_id (channel_id): A channel id
title (str): A line of text before the main message
text (str): A message
message_type (str): The type of message
"""
await self.say(
channel_id=channel_id,
text=title,
attachments=[{"fallback": title, "text": text, "mrkdwn_in": ["text"]}],
message_type=message_type,
)
async def run_release_lifecycle(self, *, repo_info, manager, release_pr):
"""
Look up the release step from the label, then continue release from that step
Args:
repo_info (RepoInfo): Info for a repository
manager (str): Release manager
release_pr (ReleasePR): Release pull request
"""
status = await status_for_repo_last_pr(
repo_info=repo_info,
github_access_token=self.github_access_token,
release_pr=release_pr,
)
# In general these functions should put things in this order:
# - polling and waiting at the beginning of the function
# - any actions like merging a branch
# - set label to next step
# - most speech by doof
# - call this function again to decide on the next step
# The intention is that if a function terminates unexpectedly it
# can start over with as little repeated speech/actions as possible.
if status == DEPLOYING_TO_RC:
await self._wait_for_deploy_rc(
repo_info=repo_info, manager=manager, release_pr=release_pr
)
elif status == WAITING_FOR_CHECKBOXES:
try:
await self.wait_for_checkboxes(
repo_info=repo_info, manager=manager, release_pr=release_pr
)
except ReleaseException:
# PR was probably closed
pass
elif status == ALL_CHECKBOXES_CHECKED:
# Done, waiting for the release to finish
pass
elif status == DEPLOYING_TO_PROD:
await self._wait_for_deploy_prod(
repo_info=repo_info, manager=manager, release_pr=release_pr
)
elif status == DEPLOYED_TO_PROD:
# all done
pass
# Give github some time to update label state
await async_sleep(10)
async def _library_release(self, *, repo_info, version):
"""Do a library release"""
channel_id = repo_info.channel_id
org, repo = get_org_and_repo(repo_info.repo_url)
await release(
github_access_token=self.github_access_token,
repo_info=repo_info,
new_version=version,
)
pr = await get_release_pr(
github_access_token=self.github_access_token,
org=org,
repo=repo,
)
await self.say(
channel_id=channel_id,
text=(
f"Behold, my new evil scheme - release {version} for {repo_info.name}! PR is up at {pr.url}. "
f"Once all tests pass, finish the release."
),
attachments=[
{
"fallback": "Finish the release",
"callback_id": FINISH_RELEASE_ID,
"actions": [
{
"name": "finish_release",
"text": "Finish the release",
"type": "button",
"confirm": {
"title": "Are you sure?",
"ok_text": "Finish the release",
"dismiss_text": "Cancel",
},
}
],
}
],
)
async def _web_application_release(
self, *, repo_info, version, hotfix_hash, manager
):
"""
Do a web application release
Args:
repo_info (RepoInfo): Repository info for a release
version (str): Version of a new release
hotfix_hash (str or None):
If present, treat this as a hotfix and use this for a commit hash.
If None, this is not a hotfix.
manager (str): Manager for the release
"""
channel_id = repo_info.channel_id
org, repo = get_org_and_repo(repo_info.repo_url)
if hotfix_hash:
await release(
github_access_token=self.github_access_token,
repo_info=repo_info,
new_version=version,
branch="release",
commit_hash=hotfix_hash,
)
await self.say(
channel_id=channel_id,
text=f"Behold, my new evil scheme - hotfix release {version} "
f"with commit {hotfix_hash}! Now deploying to RC...",
)
else:
await release(
github_access_token=self.github_access_token,
repo_info=repo_info,
new_version=version,
)
await self.say(
channel_id=channel_id,
text=f"Behold, my new evil scheme - release {version} for {repo_info.name}! Now deploying to RC...",
)
release_pr = await get_release_pr(
github_access_token=self.github_access_token, org=org, repo=repo
)
await set_release_label(
github_access_token=self.github_access_token,
repo_url=repo_info.repo_url,
pr_number=release_pr.number,
label=DEPLOYING_TO_RC,
)
await self.run_release_lifecycle(
repo_info=repo_info, manager=manager, release_pr=release_pr
)
async def _wait_for_deploy_with_alerts(
self, *, repo_info, release_pr, hash_url, watch_branch
):
"""
Wait for a deployment, but alert with a a timeout
"""
repo_url = repo_info.repo_url
channel_id = repo_info.channel_id
timeout_seconds = 60 * 60
while True:
if await wait_for_deploy(
github_access_token=self.github_access_token,
repo_url=repo_url,
hash_url=hash_url,
watch_branch=watch_branch,
timeout_seconds=timeout_seconds,
):
break
await self.say(
channel_id=channel_id,
text=(
f"Release {release_pr.version} for {repo_info.name} hasn't deployed yet."
),
)
# after the first failure, only alert every 24h
timeout_seconds = 60 * 60 * 24
async def _wait_for_deploy_rc(self, *, repo_info, manager, release_pr):
"""
Check hash values to wait for deployment for RC
"""
repo_url = repo_info.repo_url
channel_id = repo_info.channel_id
await self._wait_for_deploy_with_alerts(
repo_info=repo_info,
release_pr=release_pr,
hash_url=repo_info.rc_hash_url,
watch_branch="release-candidate",
)
rc_server = remove_path_from_url(repo_info.rc_hash_url)
await set_release_label(
github_access_token=self.github_access_token,
repo_url=repo_url,
pr_number=release_pr.number,
label=WAITING_FOR_CHECKBOXES,
)
await self.say(
channel_id=channel_id,
text=(
f"Release {release_pr.version} for {repo_info.name} was deployed at {rc_server}!"
),
)
await self._wait_for_checkboxes_initial_message(
repo_info=repo_info, release_pr=release_pr
)
await self.run_release_lifecycle(
repo_info=repo_info, manager=manager, release_pr=release_pr
)
async def _wait_for_deploy_prod(self, *, repo_info, manager, release_pr):
"""
Check hash values to wait for deployment for production
"""
repo_url = repo_info.repo_url
channel_id = repo_info.channel_id
version = await get_version_tag(
github_access_token=self.github_access_token,
repo_url=repo_url,
commit_hash="origin/release",
)
await self._wait_for_deploy_with_alerts(
repo_info=repo_info,
release_pr=release_pr,
hash_url=repo_info.prod_hash_url,
watch_branch="release",
)
await set_release_label(
github_access_token=self.github_access_token,
repo_url=repo_url,
pr_number=release_pr.number,
label=DEPLOYED_TO_PROD,
)
prod_server = remove_path_from_url(repo_info.prod_hash_url)
await self.say(
channel_id=channel_id,
text=(
f"My evil scheme {version} for {repo_info.name} has been released to production at {prod_server}. "
"And by 'released', I mean completely...um...leased."
),
)
await self.run_release_lifecycle(
repo_info=repo_info, manager=manager, release_pr=release_pr
)
async def _new_release(self, *, repo_info, version, manager):
"""
Start a new release
Args:
repo_info (RepoInfo): Repository information for the release
version (str): The version of the new release
manager (str): Person starting a release
"""
if repo_info.project_type == LIBRARY_TYPE:
await self._library_release(repo_info=repo_info, version=version)
elif repo_info.project_type == WEB_APPLICATION_TYPE:
await self._web_application_release(
repo_info=repo_info, version=version, hotfix_hash=None, manager=manager
)
else:
raise Exception(
f"Configuration error: unknown project type {repo_info.project_type}"
)
async def release_command(self, command_args):
"""
Start a new release and wait for deployment
Args:
command_args (CommandArgs): The arguments for this command
"""
repo_info = command_args.repo_info
version = command_args.args[0]
repo_url = repo_info.repo_url
org, repo = get_org_and_repo(repo_url)
pr = await get_release_pr(
github_access_token=self.github_access_token,
org=org,
repo=repo,
)
if pr:
raise ReleaseException(f"A release is already in progress: {pr.url}")
await self._new_release(
repo_info=repo_info, version=version, manager=command_args.manager
)
async def hotfix_command(self, command_args):
"""
Start a hotfix with the commit hash provided
Args:
command_args (CommandArgs): The arguments for this command
"""
repo_info = command_args.repo_info
hotfix_hash = command_args.args[0]
repo_url = repo_info.repo_url
org, repo = get_org_and_repo(repo_url)
release_pr = await get_release_pr(
github_access_token=self.github_access_token, org=org, repo=repo
)
if release_pr:
await self.say(
channel_id=repo_info.channel_id,
text=f"There is a release already in progress: {release_pr.url}. Close that first!",
)
raise ReleaseException(
f"There is a release already in progress: {release_pr.url}. Close that first!"
)
async with init_working_dir(
self.github_access_token, repo_info.repo_url
) as working_dir:
last_version = await get_project_version(
repo_info=repo_info, working_dir=working_dir
)
_, new_patch = next_versions(last_version)
await self._web_application_release(
repo_info=repo_info,
version=new_patch,
hotfix_hash=hotfix_hash,
manager=command_args.manager,
)
async def wait_for_checkboxes_command(self, command_args):
"""
Poll the Release PR and wait until all checkboxes are checked off
Args:
command_args (CommandArgs): The arguments for this command
"""
repo_info = command_args.repo_info
org, repo = get_org_and_repo(repo_info.repo_url)
release_pr = await get_release_pr(
github_access_token=self.github_access_token,
org=org,
repo=repo,
)
await set_release_label(
github_access_token=self.github_access_token,
repo_url=command_args.repo_info.repo_url,
pr_number=release_pr.number,
label=WAITING_FOR_CHECKBOXES,
)
await self._wait_for_checkboxes_initial_message(
repo_info=repo_info, release_pr=release_pr
)
await self.run_release_lifecycle(
repo_info=command_args.repo_info,
manager=command_args.manager,
release_pr=release_pr,
)
async def _wait_for_checkboxes_initial_message(self, *, repo_info, release_pr):
"""
Find out who hasn't checked their boxes and message the channel
Args:
repo_info (RepoInfo): Repo info
release_pr (ReleasePR): The release PR
"""
org, repo = get_org_and_repo(repo_info.repo_url)
channel_id = repo_info.channel_id
prev_unchecked_authors = await get_unchecked_authors(
github_access_token=self.github_access_token,
org=org,
repo=repo,
)
await self.say(
channel_id=channel_id,
text=(
f"PR is up at {release_pr.url}."
f" These people have commits in this release: "
f"{', '.join(await self.translate_slack_usernames(prev_unchecked_authors))}"
),
)
await self.say(
channel_id=channel_id,
text=(
f"Wait, wait. Time out. My evil plan for {repo_info.name} isn't evil enough "
"until all the checkboxes are checked..."
),
)
async def wait_for_checkboxes(self, *, repo_info, manager, release_pr):
"""
Poll the Release PR and wait until all checkboxes are checked off
Args:
repo_info (RepoInfo): Information for a repo
manager (str or None): User id for the release manager
release_pr (ReleasePR): Release PR
"""
repo_url = repo_info.repo_url
channel_id = repo_info.channel_id
org, repo = get_org_and_repo(repo_url)
prev_unchecked_authors = await get_unchecked_authors(
github_access_token=self.github_access_token,
org=org,
repo=repo,
)
while prev_unchecked_authors:
await async_sleep(60)
new_unchecked_authors = await get_unchecked_authors(
github_access_token=self.github_access_token,
org=org,
repo=repo,
)
newly_checked = prev_unchecked_authors - new_unchecked_authors
if newly_checked:
await self.say(
channel_id=channel_id,
text=f"Thanks for checking off your boxes "
f"{', '.join(sorted(await self.translate_slack_usernames(newly_checked)))}!",
)
prev_unchecked_authors = new_unchecked_authors
await set_release_label(
github_access_token=self.github_access_token,
repo_url=repo_url,
pr_number=release_pr.number,
label=ALL_CHECKBOXES_CHECKED,
)
pr = await get_release_pr(
github_access_token=self.github_access_token,
org=org,
repo=repo,
)
await self.say(
channel_id=channel_id,
text=f"All checkboxes checked off. Release {pr.version} is ready for the Merginator{' ' + format_user_id(manager) if manager else ''}!",
attachments=[
{
"fallback": "Finish the release",
"callback_id": FINISH_RELEASE_ID,
"actions": [
{
"name": "finish_release",
"text": "Finish the release",
"type": "button",
"confirm": {
"title": "Are you sure?",
"ok_text": "Finish the release",
"dismiss_text": "Cancel",
},
}
],
}
],
)
async def publish(self, command_args):
"""
Publish a package to PyPI or NPM
Args:
command_args (CommandArgs): The arguments for this command
"""
repo_info = command_args.repo_info
version = command_args.args[0]
if repo_info.packaging_tool == NPM:
server = "the npm registry"
elif repo_info.packaging_tool == SETUPTOOLS:
server = "PyPI"
else:
raise Exception(
f"Unexpected packaging tool {repo_info.packaging_tool} for {repo_info.name}"
)
await self.say(
channel_id=command_args.channel_id,
text=f"Publishing evil scheme {version} to {server}...",
)
await publish(
repo_info=repo_info,
version=version,
github_access_token=self.github_access_token,
npm_token=self.npm_token,
)
await self.say(
channel_id=command_args.channel_id,
text=f"Successfully uploaded {version} to {server}.",
)
async def finish_release(self, command_args):
"""
Merge the release candidate into the release branch, tag it, merge to master, and wait for deployment
Args:
command_args (CommandArgs): The arguments for this command
"""
repo_info = command_args.repo_info
channel_id = repo_info.channel_id
repo_url = repo_info.repo_url
org, repo = get_org_and_repo(repo_url)
pr = await get_release_pr(
github_access_token=self.github_access_token,
org=org,
repo=repo,
)
if not pr:
raise ReleaseException(
f"No release currently in progress for {repo_info.name}"
)
version = pr.version
await finish_release(
github_access_token=self.github_access_token,
repo_info=repo_info,
version=version,
timezone=self.timezone,
)
if repo_info.project_type == WEB_APPLICATION_TYPE:
await self.say(
channel_id=channel_id,
text=f"Merged evil scheme {version} for {repo_info.name}! Now deploying to production...",
)
await set_release_label(
github_access_token=self.github_access_token,
repo_url=repo_url,
pr_number=pr.number,
label=DEPLOYING_TO_PROD,
)
await self.run_release_lifecycle(
repo_info=repo_info, manager=command_args.manager, release_pr=pr
)
elif repo_info.project_type == LIBRARY_TYPE:
await self.say(
channel_id=channel_id,
text=f"Merged evil scheme {version} for {repo_info.name}!",
)
async def report_version(self, command_args):
"""
Report the version that is running in production
Args:
command_args (CommandArg): The arguments for this command
"""
repo_info = command_args.repo_info
channel_id = repo_info.channel_id
repo_url = repo_info.repo_url
commit_hash = await fetch_release_hash(repo_info.prod_hash_url)
version = await get_version_tag(
github_access_token=self.github_access_token,
repo_url=repo_url,
commit_hash=commit_hash,
)
await self.say(
channel_id=channel_id,
text=f"Wait a minute! My evil scheme is at version {version[1:]}!",
)
async def report_hash(self, command_args):
"""
Report the commit that is deployed on a server
Args:
command_args (CommandArg): The arguments for this command
"""
repo_info = command_args.repo_info
channel_id = repo_info.channel_id
repo_url = repo_info.repo_url
deployment_server_type = command_args.args[0]
if deployment_server_type == CI:
hash_url = repo_info.ci_hash_url
elif deployment_server_type == RC:
hash_url = repo_info.rc_hash_url
elif deployment_server_type == PROD:
hash_url = repo_info.prod_hash_url
else:
raise Exception("Unexpected server type")
commit_hash = await fetch_release_hash(hash_url)
message = await get_commit_oneline_message(
github_access_token=self.github_access_token,
repo_url=repo_url,
commit_hash=commit_hash,
)
await self.say(
channel_id=channel_id,
text=f"Oh, Perry the Platypus, look what you've done on {deployment_server_type}! {message}",
)
async def commits_since_last_release(self, command_args):
"""
Have doof show the release notes since the last release
Args:
command_args (CommandArgs): The arguments for this command
"""
repo_info = command_args.repo_info
async with init_working_dir(
self.github_access_token, repo_info.repo_url
) as working_dir:
default_branch = await get_default_branch(working_dir)
last_version = await get_project_version(
repo_info=repo_info, working_dir=working_dir
)
release_notes = await create_release_notes(
last_version,
with_checkboxes=False,
base_branch=default_branch,
root=working_dir,
)
has_new_commits = await any_new_commits(
last_version, base_branch=default_branch, root=working_dir
)
await self.say_with_attachment(
channel_id=repo_info.channel_id,
title=f"Release notes since {last_version}",
text=release_notes,
)
org, repo = get_org_and_repo(repo_info.repo_url)
release_pr = await get_release_pr(
github_access_token=self.github_access_token, org=org, repo=repo
)
if release_pr:
await self.say(
channel_id=repo_info.channel_id,
text=f"And also! There is a release already in progress: {release_pr.url}",
)
elif has_new_commits:
new_minor, new_patch = next_versions(last_version)
await self.say(
channel_id=repo_info.channel_id,
text="Start a new release?",
attachments=[
{
"fallback": "New release",
"callback_id": NEW_RELEASE_ID,
"actions": [
{
"name": "minor_release",
"text": new_minor,
"value": new_minor,
"type": "button",
},
{
"name": "patch_release",
"text": new_patch,
"value": new_patch,
"type": "button",
},
{
"name": "cancel",
"text": "Dismiss",
"value": "cancel",
"style": "danger",
"type": "button",
},
],
}
],
)
async def start_new_releases(self, command_args): # pylint: disable=too-many-locals
"""
Start new releases for all projects with new commits
Args:
command_args (CommandArgs): The arguments for this command
"""
new_release_type = command_args.args[0]
await self.say(
channel_id=command_args.channel_id, text="Starting new releases..."
)
new_release_repos = []