-
Notifications
You must be signed in to change notification settings - Fork 413
/
Copy pathnotifier.py
1274 lines (1061 loc) · 44 KB
/
notifier.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
# -*- coding: utf-8 -*-
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__author__ = '[email protected] (Eric Bidelman)'
from datetime import datetime, timedelta
import collections
import logging
import difflib
import re
from typing import Any, Optional
import urllib
from api import converters
from framework import permissions
from google.cloud import ndb # type: ignore
from flask import escape
from flask import render_template
from framework import basehandlers
from framework import cloud_tasks_helpers
from framework import users
import settings
from internals import approval_defs
from internals import core_enums
from internals import stage_helpers
from internals.core_models import FeatureEntry, MilestoneSet, Stage
from internals.data_types import StageDict
from internals.review_models import Gate
from internals.user_models import (
AppUser, BlinkComponent, FeatureOwner, UserPref)
OT_SUPPORT_EMAIL = '[email protected]'
BLINK_DEV_EMAIL = '[email protected]'
def _determine_milestone_string(ship_stages: list[Stage]) -> str:
"""Determine the shipping milestone string to display in the template."""
# Get the earliest desktop and android milestones.
first_desktop = min(
(stage.milestones.desktop_first for stage in ship_stages
if stage.milestones and stage.milestones.desktop_first),
default=None)
first_android = min(
(stage.milestones.android_first for stage in ship_stages
if stage.milestones and stage.milestones.android_first),
default=None)
# Use the desktop milestone by default if it's available.
milestone_str = str(first_desktop)
# Use the android milestone with the android suffix if there are no
# desktop milestones.
if not first_desktop and first_android:
milestone_str = f'{first_android} (android)'
return milestone_str
def highlight_diff(old_text, new_text, highlight_type):
differ = difflib.ndiff(
re.split(r'(\W)', old_text), re.split(r'(\W)', new_text))
highlighted_text = []
for item in differ:
text = escape(item[2:])
if not text: continue
if item.startswith('-') and highlight_type == 'deletion':
highlighted_text.append(
f'<span style="background:#FDD">{text}</span>')
elif item.startswith('+') and highlight_type == 'addition':
highlighted_text.append(
f'<span style="background:#DFD">{text}</span>')
elif item.startswith(' '):
highlighted_text.append(text)
return ''.join(highlighted_text)
def format_email_body(
template_path, fe: FeatureEntry, changes: list[dict[str, Any]],
updater_email: Optional[str] = None,
additional_template_data: dict[str, Any] | None = None) -> str:
"""Return an HTML string for a notification email body."""
stage_info = stage_helpers.get_stage_info_for_templates(fe)
milestone_str = _determine_milestone_string(stage_info['ship_stages'])
formatted_changes = ''
for prop in changes:
prop_name = escape(prop['prop_name']) # Ensure to escape
new_val = prop['new_val']
old_val = prop['old_val']
note = prop.get('note')
# Escaping values before passing to highlight_diff
highlighted_old_val = highlight_diff(old_val, new_val, 'deletion')
highlighted_new_val = highlight_diff(old_val, new_val, 'addition')
note_line = f'<b>Note:</b> {note}<br/>' if note else ''
# Using f-strings for clear formatting
formatted_changes += (
f'<li><b>{prop_name}:</b><br/>'
f'<b>Old:</b> {highlighted_old_val}<br/>'
f'<b>New:</b> {highlighted_new_val}<br/>'
f'{note_line}'
f'</li><br/>')
if not formatted_changes:
formatted_changes = '<li>None</li>'
body_data = {
'feature': fe,
'category': core_enums.FEATURE_CATEGORIES[fe.category],
'feature_type': core_enums.FEATURE_TYPES[fe.feature_type],
'stage_info': stage_info,
'should_render_mstone_table': stage_info['should_render_mstone_table'],
'creator_email': fe.creator_email,
'updater_email': updater_email or fe.updater_email,
'id': fe.key.integer_id(),
'milestone': milestone_str,
'status': core_enums.IMPLEMENTATION_STATUS[fe.impl_status_chrome],
'formatted_changes': formatted_changes,
'APP_TITLE': settings.APP_TITLE,
'SITE_URL': settings.SITE_URL,
}
body_data.update(additional_template_data or {})
body = render_template(template_path, **body_data)
return body
def accumulate_reasons(
addr_reasons: dict[str, list], addr_list: list[str], reason: str) -> None:
"""Add a reason string for each user."""
for email in addr_list:
addr_reasons[email].append(reason)
def convert_reasons_to_task(
addr, reasons, email_html, subject, triggering_user_email):
"""Add a task dict to task_list for each user who has not already got one."""
assert reasons, 'We are emailing someone without any reason'
footer_lines = ['<p>You are receiving this email because:</p>', '<ul>']
for reason in sorted(set(reasons)):
footer_lines.append('<li>%s</li>' % reason)
footer_lines.append('</ul>')
footer_lines.append('<p><a href="%ssettings">Unsubscribe</a></p>' %
settings.SITE_URL)
email_html_with_footer = email_html + '\n\n' + '\n'.join(footer_lines)
reply_to = None
recipient_user = users.User(email=addr)
if permissions.can_create_feature(recipient_user) and triggering_user_email:
reply_to = triggering_user_email
one_email_task = {
'to': addr,
'subject': subject,
'reply_to': reply_to,
'html': email_html_with_footer
}
return one_email_task
WEBVIEW_RULE_REASON = (
'This feature has an android milestone, but not a webview milestone')
WEBVIEW_RULE_ADDRS = ['[email protected]']
IWA_RULE_REASON = (
'You are subscribed to all IWA features')
IWA_RULE_ADDRS = ['[email protected]']
def apply_subscription_rules(
fe: FeatureEntry, changes: list) -> dict[str, list[str]]:
"""Return {"reason": [addrs]} for users who set up rules."""
# Note: for now this is hard-coded, but it will eventually be
# configurable through some kind of user preference.
changed_field_names = {c['prop_name'] for c in changes}
results: dict[str, list[str]] = {}
# Rule 1: Check for IWA features
if fe.category == core_enums.IWA:
results[IWA_RULE_REASON] = IWA_RULE_ADDRS
# Find an existing shipping stage with milestone info.
fe_stages = stage_helpers.get_feature_stages(fe.key.integer_id())
stage_type = core_enums.STAGE_TYPES_SHIPPING[fe.feature_type] or 0
ship_stages: list[Stage] = fe_stages.get(stage_type, [])
ship_milestones: MilestoneSet | None = (
ship_stages[0].milestones if len(ship_stages) > 0 else None)
# Rule 2: Check if feature has some other milestone set, but not webview.
if (ship_milestones is not None and
ship_milestones.android_first and
not ship_milestones.webview_first):
milestone_fields = ['shipped_android_milestone']
if not changed_field_names.isdisjoint(milestone_fields):
results[WEBVIEW_RULE_REASON] = WEBVIEW_RULE_ADDRS
return results
def add_core_receivers(fe: FeatureEntry, addr_reasons: dict[str, list[str]]):
accumulate_reasons(
addr_reasons, fe.owner_emails,
'You are listed as an owner of this feature'
)
accumulate_reasons(
addr_reasons, fe.editor_emails,
'You are listed as an editor of this feature'
)
accumulate_reasons(
addr_reasons, fe.cc_emails,
'You are CC\'d on this feature'
)
accumulate_reasons(
addr_reasons, fe.devrel_emails,
'You are a devrel contact for this feature.'
)
def make_feature_changes_email(
fe: FeatureEntry, is_update: bool=False, changes: Optional[list]=None,
triggering_user_email: Optional[str]=None):
"""Return a list of task dicts to notify users of feature changes."""
if changes is None:
changes = []
watchers: list[FeatureOwner] = FeatureOwner.query(
FeatureOwner.watching_all_features == True).fetch(None)
watcher_emails: list[str] = [watcher.email for watcher in watchers]
if is_update:
subject = 'updated feature: %s' % fe.name
triggering_user_email = triggering_user_email or fe.updater_email
template_path = 'update-feature-email.html'
else:
subject = 'new feature: %s' % fe.name
triggering_user_email = fe.creator_email
template_path = 'new-feature-email.html'
email_html = format_email_body(
template_path, fe, changes, updater_email=triggering_user_email)
addr_reasons: dict[str, list[str]] = collections.defaultdict(list)
add_core_receivers(fe, addr_reasons)
accumulate_reasons(
addr_reasons, watcher_emails,
'You are watching all feature changes'
)
# There will always be at least one component.
for component_name in fe.blink_components:
component = BlinkComponent.get_by_name(component_name)
if not component:
logging.warning('Blink component "%s" not found.'
'Not sending email to subscribers' % component_name)
continue
owner_emails: list[str] = [owner.email for owner in component.owners]
subscriber_emails: list[str] = [sub.email for sub in component.subscribers]
accumulate_reasons(
addr_reasons, owner_emails,
'You are an owner of this feature\'s component')
accumulate_reasons(
addr_reasons, subscriber_emails,
'You subscribe to this feature\'s component')
starrers = FeatureStar.get_feature_starrers(fe.key.integer_id())
starrer_emails: list[str] = [user.email for user in starrers]
accumulate_reasons(addr_reasons, starrer_emails, 'You starred this feature')
rule_results = apply_subscription_rules(fe, changes)
for reason, sub_addrs in rule_results.items():
accumulate_reasons(addr_reasons, sub_addrs, reason)
all_tasks = [convert_reasons_to_task(
addr, reasons, email_html, subject, triggering_user_email)
for addr, reasons in sorted(addr_reasons.items())]
return all_tasks
def add_reviewers(
fe: FeatureEntry, gate_type: int, addr_reasons: dict[str, list[str]]):
"""Add addresses of people who will do the review."""
gate = approval_defs.get_gate_by_type(fe.key.integer_id(), gate_type)
if gate and gate.assignee_emails:
recipients = gate.assignee_emails
reasons = 'This review is assigned to you'
else:
recipients = approval_defs.get_approvers(gate_type)
reasons = 'You are a reviewer for this type of gate'
accumulate_reasons(addr_reasons, recipients, reasons)
class FeatureStar(ndb.Model):
"""A FeatureStar represent one user's interest in one feature."""
email = ndb.StringProperty(required=True)
feature_id = ndb.IntegerProperty(required=True)
# This is so that we do not sync a bell to a star that the user has removed.
starred = ndb.BooleanProperty(default=True)
@classmethod
def get_star(self, email, feature_id):
"""If that user starred that feature, return the model or None."""
q = FeatureStar.query()
q = q.filter(FeatureStar.email == email)
q = q.filter(FeatureStar.feature_id == feature_id)
return q.get()
@classmethod
def set_star(self, email, feature_id, starred=True):
"""Set/clear a star for the specified user and feature."""
feature_star = self.get_star(email, feature_id)
if not feature_star and starred:
feature_star = FeatureStar(email=email, feature_id=feature_id)
feature_star.put()
elif feature_star and feature_star.starred != starred:
feature_star.starred = starred
feature_star.put()
else:
return # No need to update anything in datastore
feature_entry = FeatureEntry.get_by_id(feature_id)
feature_entry.star_count += 1 if starred else -1
if feature_entry.star_count < 0:
logging.error('count would be < 0: %r', (email, feature_id, starred))
return
feature_entry.put()
@classmethod
def get_user_stars(self, email):
"""Return a list of feature_ids of all features that the user starred."""
q = FeatureStar.query()
q = q.filter(FeatureStar.email == email)
q = q.filter(FeatureStar.starred == True)
feature_stars = q.fetch(None)
logging.info('found %d stars for %r', len(feature_stars), email)
feature_ids = [fs.feature_id for fs in feature_stars]
logging.info('returning %r', feature_ids)
return sorted(feature_ids, reverse=True)
@classmethod
def get_feature_starrers(self, feature_id: int) -> list[UserPref]:
"""Return list of UserPref objects for starrers that want notifications."""
q = FeatureStar.query()
q = q.filter(FeatureStar.feature_id == feature_id)
q = q.filter(FeatureStar.starred == True)
feature_stars: list[FeatureStar] = q.fetch(None)
logging.info('found %d stars for %r', len(feature_stars), feature_id)
emails: list[str] = [fs.email for fs in feature_stars]
logging.info('looking up %r', repr(emails)[:settings.MAX_LOG_LINE])
user_prefs = UserPref.get_prefs_for_emails(emails)
user_prefs = [up for up in user_prefs
if up.notify_as_starrer and not up.bounced]
return user_prefs
class NotifyInactiveUsersHandler(basehandlers.FlaskHandler):
JSONIFY = True
DEFAULT_LAST_VISIT = datetime(2022, 8, 1) # 2022-08-01
INACTIVE_WARN_DAYS = 180
EMAIL_TEMPLATE_PATH = 'inactive_user_email.html'
def get_template_data(self, **kwargs):
"""Notify any users that have been inactive for 6 months."""
self.require_cron_header()
now = kwargs.get('now', datetime.now())
users_to_notify = self._determine_users_to_notify(now)
email_tasks = self._build_email_tasks(users_to_notify)
send_emails(email_tasks)
message_parts = [f'{len(email_tasks)} users notified of inactivity.',
'Notified users:']
for task in email_tasks:
message_parts.append(task['to'])
message = '\n'.join(message_parts)
logging.info(message)
return {'message': message}
def _determine_users_to_notify(self, now=None):
# date var can be passed in for testing purposes.
if now is None:
now = datetime.now()
q = AppUser.query()
users: list[AppUser] = q.fetch()
inactive_users = []
inactive_cutoff = now - timedelta(days=self.INACTIVE_WARN_DAYS)
for user in users:
# Site admins and editors aren't warned due to inactivity.
# Also, users that have been previously notified are not notified again.
if user.is_admin or user.is_site_editor or user.notified_inactive:
continue
# If the user does not have a last visit, it is assumed the last visit
# is either the account's creation date or the date the last_visit
# field was created on the model - whatever is latest.
last_visit = user.last_visit or self.DEFAULT_LAST_VISIT
if user.created > last_visit:
last_visit = user.created
# Notify the user of inactivity if they haven't already been notified.
if (last_visit < inactive_cutoff):
inactive_users.append(user.email)
user.notified_inactive = True
user.put()
return inactive_users
def _build_email_tasks(self, users_to_notify):
email_tasks = []
for email in users_to_notify:
body_data = {'SITE_URL': settings.SITE_URL}
html = render_template(self.EMAIL_TEMPLATE_PATH, **body_data)
subject = f'Notice of WebStatus user inactivity for {email}'
email_tasks.append({
'to': email,
'subject': subject,
'reply_to': None,
'html': html
})
return email_tasks
class FeatureChangeHandler(basehandlers.FlaskHandler):
"""This task handles a feature creation or update by making email tasks."""
IS_INTERNAL_HANDLER = True
def process_post_data(self, **kwargs):
self.require_task_header()
feature = self.get_param('feature')
is_update = self.get_bool_param('is_update')
changes = self.get_param('changes', required=False) or []
triggering_user_email = self.get_param(
'triggering_user_email', required=False)
logging.info('Starting to notify subscribers for feature %s',
repr(feature)[:settings.MAX_LOG_LINE])
# Email feature subscribers if the feature exists and there were
# actually changes to it.
# Load feature directly from NDB so as to never get a stale cached copy.
fe = FeatureEntry.get_by_id(feature['id'])
if fe and (is_update and len(changes) or not is_update):
email_tasks = make_feature_changes_email(
fe, is_update=is_update, changes=changes,
triggering_user_email=triggering_user_email)
send_emails(email_tasks)
return {'message': 'Done'}
class FeatureReviewHandler(basehandlers.FlaskHandler):
"""This task handles feature review requests by making email tasks."""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'review-request-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
feature = self.get_param('feature')
gate_type = self.get_param('gate_type')
gate_url = self.get_param('gate_url', required=False)
new_val = self.get_param('new_val', required=False)
updater_email = self.get_param('updater_email', required=False)
team_name = None
appr_def = approval_defs.APPROVAL_FIELDS_BY_ID.get(gate_type)
if appr_def:
team_name = appr_def.team_name
# TODO(jrobbins): Remove this backward compatibility code
# after next deployment.
changes = self.get_param('changes', required=False) or []
if changes and not gate_url:
prop_name = changes[0]['prop_name']
gate_url = prop_name.split()[-1]
if changes and not new_val:
new_val = changes[0]['new_val']
logging.info('Starting to notify reviewers for feature %s',
repr(feature)[:settings.MAX_LOG_LINE])
logging.info('gate type is %r', gate_type)
logging.info('team_name is %r', team_name)
fe = FeatureEntry.get_by_id(feature['id'])
if fe:
additional_template_data = {
'gate_url': gate_url,
'new_val': new_val,
'updater_email': updater_email,
'team_name': team_name,
}
email_tasks = self.make_review_requests_email(
fe, gate_type, additional_template_data)
send_emails(email_tasks)
return {'message': 'Done'}
def make_review_requests_email(
self, fe: FeatureEntry, gate_type: int,
additional_template_data: dict[str, str]):
"""Return a list of task dicts to notify approvers of review requests."""
email_html = format_email_body(
self.EMAIL_TEMPLATE_PATH, fe, [],
additional_template_data=additional_template_data)
subject = 'Review Request for feature: %s' % fe.name
addr_reasons: dict[str, list[str]] = collections.defaultdict(list)
add_reviewers(fe, gate_type, addr_reasons)
all_tasks = [
convert_reasons_to_task(
addr, reasons, email_html, subject, None)
for addr, reasons in sorted(addr_reasons.items())]
return all_tasks
class ReviewAssignmentHandler(basehandlers.FlaskHandler):
"""This task handles feature review assignments by making email tasks."""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'review-assigned-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
feature = self.get_param('feature')
gate_type = self.get_param('gate_type')
gate_url = self.get_param('gate_url')
triggering_user_email = self.get_param('triggering_user_email')
old_assignees = self.get_param('old_assignees')
new_assignees = self.get_param('new_assignees')
team_name = None
appr_def = approval_defs.APPROVAL_FIELDS_BY_ID.get(gate_type)
if appr_def:
team_name = appr_def.team_name
logging.info('Starting to notify assignees for feature %s',
repr(feature)[:settings.MAX_LOG_LINE])
fe = FeatureEntry.get_by_id(feature['id'])
if fe:
additional_template_data = {
'gate_url': gate_url,
'updater_email': triggering_user_email,
'team_name': team_name,
}
email_tasks = self.make_review_assignment_email(
fe, triggering_user_email, old_assignees, new_assignees,
additional_template_data)
send_emails(email_tasks)
return {'message': 'Done'}
def make_review_assignment_email(
self, fe: FeatureEntry, triggering_user_email: str,
old_assignees: list[str], new_assignees: list[str],
additional_template_data: dict[str, str]):
"""Return a list of task dicts to notify assigned reviewers."""
changed_prop = {
'prop_name': 'Assigned reviewer',
'old_val': ', '.join(old_assignees or ['None']),
'new_val': ', '.join(new_assignees or ['None']),
}
email_html = format_email_body(
self.EMAIL_TEMPLATE_PATH, fe, [changed_prop],
additional_template_data=additional_template_data)
subject = 'Review assigned for feature: %s' % fe.name
addr_reasons: dict[str, list[str]] = collections.defaultdict(list)
accumulate_reasons(
addr_reasons, old_assignees,
'The review was previously assigned to you')
accumulate_reasons(
addr_reasons, new_assignees, 'The review is now assigned to you')
all_tasks = [
convert_reasons_to_task(
addr, reasons, email_html, subject, triggering_user_email)
for addr, reasons in sorted(addr_reasons.items())]
return all_tasks
class FeatureCommentHandler(basehandlers.FlaskHandler):
"""This task handles feature comment notifications by making email tasks."""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'review-comment-notification-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
feature = self.get_param('feature')
gate_type = self.get_param('gate_type')
gate_url = self.get_param('gate_url')
triggering_user_email = self.get_param('triggering_user_email')
content = self.get_param('content')
logging.info('Starting to notify of comments for feature %s',
repr(feature)[:settings.MAX_LOG_LINE])
fe = FeatureEntry.get_by_id(feature['id'])
if fe:
additional_template_data = {
'gate_url': gate_url,
'triggering_user_email': triggering_user_email,
'content': content,
}
email_tasks = self.make_new_comments_email(
fe, gate_type, triggering_user_email, additional_template_data)
send_emails(email_tasks)
return {'message': 'Done'}
def make_new_comments_email(
self, fe: FeatureEntry, gate_type: int, triggering_user_email: str,
additional_template_data: dict[str, str]):
"""Return a list of task dicts to notify of new comments."""
email_html = format_email_body(
self.EMAIL_TEMPLATE_PATH, fe, [],
additional_template_data=additional_template_data)
subject = 'New comments for feature: %s' % fe.name
addr_reasons: dict[str, list[str]] = collections.defaultdict(list)
add_core_receivers(fe, addr_reasons)
add_reviewers(fe, gate_type, addr_reasons)
all_tasks = [convert_reasons_to_task(
addr, reasons, email_html, subject, triggering_user_email)
for addr, reasons in sorted(addr_reasons.items())]
return all_tasks
class OTActivatedHandler(basehandlers.FlaskHandler):
"""Notify about an origin trial being activated."""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'origintrials/ot-activated-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
stage = self.get_param('stage', required=True)
contacts = stage['ot_emails'] or []
contacts.append(stage['ot_owner_email'])
send_emails([self.build_email(stage, contacts)])
return {'message': 'OK'}
def build_email(self, stage: StageDict, contacts: list[str]) -> dict:
body_data = {
'stage': stage,
'ot_url': f'{settings.OT_URL}#/view_trial/{stage["origin_trial_id"]}',
'chromestatus_url': ('https://chromestatus.com/feature/'
f'{stage["feature_id"]}')
}
body = render_template(self.EMAIL_TEMPLATE_PATH, **body_data)
return {
'to': contacts,
'subject': f'{stage["ot_display_name"]} origin trial is now available',
'reply_to': None,
'html': body,
}
class OTCreationApprovedHandler(basehandlers.FlaskHandler):
"""Notify about an origin trial having received reviews and approvals and being
ready for the user to request creation."""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'origintrials/ot-creation-approved-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
feature = self.get_param('feature', required=True)
contacts = feature['owner_emails'] or []
if len(contacts) == 0:
return {'message': 'No contacts available for this feature'}
send_emails([self.build_email(feature, contacts)])
return {'message': 'OK'}
def build_email(self, feature: dict[str, Any], contacts: list[str]) -> dict:
body_data = {
'chromestatus_url': f'https://chromestatus.com/feature/{feature["id"]}'
}
body = render_template(self.EMAIL_TEMPLATE_PATH, **body_data)
return {
'to': contacts,
'subject': 'You can now submit your origin trial creation request',
'reply_to': None,
'html': body,
}
class OTCreationProcessedHandler(basehandlers.FlaskHandler):
"""Notify about an origin trial creation request being processed,
but activation is at a later date.
"""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'origintrials/ot-creation-processed-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
stage = self.get_param('stage', required=True)
contacts = stage['ot_emails'] or []
contacts.append(stage['ot_owner_email'])
send_emails([self.build_email(stage, contacts)])
return {'message': 'OK'}
def build_email(self, stage: dict[str, Any], contacts: list[str]) -> dict:
body_data = {
'stage': stage,
'ot_url': settings.OT_URL,
'chromestatus_url': ('https://chromestatus.com/feature/'
f'{stage["feature_id"]}')
}
body = render_template(self.EMAIL_TEMPLATE_PATH, **body_data)
return {
'to': contacts,
'subject': (f'{stage["ot_display_name"]} origin trial has been created '
f'and will begin {stage["ot_activation_date"]}'),
'reply_to': None,
'html': body,
}
class OTCreationRequestFailedHandler(basehandlers.FlaskHandler):
"""Notify about an origin trial creation request failing automated request."""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'origintrials/ot-creation-request-failed-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
stage = self.get_param('stage', required=True)
error_text = self.get_param('error_text')
send_emails([self.build_email(stage, error_text)])
return {'message': 'OK'}
def build_email(self, stage: StageDict, error_text: str|None) -> dict:
body_data = {
'stage': stage,
'error_text': error_text,
'chromestatus_url': ('https://chromestatus.com/feature/'
f'{stage["feature_id"]}')
}
body = render_template(self.EMAIL_TEMPLATE_PATH, **body_data)
return {
'to': OT_SUPPORT_EMAIL,
'subject': ('Automated trial creation request failed for '
f'{stage["ot_display_name"]}'),
'reply_to': None,
'html': body,
}
class OTActivationFailedHandler(basehandlers.FlaskHandler):
"""Notify about an origin trial activation automated request failing."""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'origintrials/ot-activation-failed-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
stage = self.get_param('stage', required=True)
send_emails([self.build_email(stage)])
return {'message': 'OK'}
def build_email(self, stage: StageDict) -> dict:
body_data = {
'stage': stage,
'chromestatus_url': ('https://chromestatus.com/feature/'
f'{stage["feature_id"]}')
}
body = render_template(self.EMAIL_TEMPLATE_PATH, **body_data)
return {
'to': OT_SUPPORT_EMAIL,
'subject': ('Automated trial activation request failed for '
f'{stage["ot_display_name"]}'),
'reply_to': None,
'html': body,
}
class OTCreationRequestHandler(basehandlers.FlaskHandler):
"""Notify about an origin trial creation request."""
IS_INTERNAL_HANDLER = True
def process_post_data(self, **kwargs):
self.require_task_header()
stage = self.get_param('stage')
logging.info('Starting to notify about origin trial creation request.')
send_emails([self.make_creation_request_email(stage)])
return {'message': 'OK'}
def _yes_or_no(self, value: bool):
return 'Yes' if value else 'No'
def make_creation_request_email(self, stage):
chromestatus_url = ('https://chromestatus.com/feature/'
f'{stage["feature_id"]}')
email_body = f"""
<p>
Requested by: {stage["ot_owner_email"]}
<br>
Additional contacts for your team?: {",".join(stage["ot_emails"])}
<br>
Feature name: {stage["ot_display_name"]}
<br>
Feature description: {stage["ot_description"]}
<br>
Start Chrome milestone: {stage["desktop_first"]}
<br>
End Chrome milestone: {stage["desktop_last"]}
<br>
Chromium trial name: {stage["ot_chromium_trial_name"]}
<br>
Is this a deprecation trial?: {self._yes_or_no(stage["ot_is_deprecation_trial"])}
<br>
Third party origin support: {self._yes_or_no(stage["ot_has_third_party_support"])}
<br>
WebFeature UseCounter value: {stage["ot_webfeature_use_counter"]}
<br>
Documentation link: {stage["ot_documentation_url"]}
<br>
Chromestatus link: {chromestatus_url}
<br>
Feature feedback link: {stage["ot_feedback_submission_url"]}
<br>
Intent to Experiment link: {stage["intent_thread_url"]}
<br>
Is this a critical trial?: {self._yes_or_no(stage["ot_is_critical_trial"])}
<br>
Anything else?: {stage["ot_request_note"]}
<br>
<br>
Instructions for handling this request can be found at: https://g3doc.corp.google.com/chrome/origin_trials/g3doc/trial_admin.md?cl=head#setup-a-new-trial
</p>
"""
return {
'to': OT_SUPPORT_EMAIL,
'subject': f'New Trial Creation Request for {stage["ot_display_name"]}',
'reply_to': None,
'html': email_body,
}
class OTExtensionApprovedHandler(basehandlers.FlaskHandler):
"""Notify about an origin trial extension that is approved and needs
finalized.
"""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'origintrials/ot-extension-approved-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
feature = self.get_param('feature')
gate_id = self.get_param('gate_id')
requester_email = self.get_param('requester_email')
ot_display_name = self.get_param('ot_display_name')
logging.info('Starting to notify about successful origin trial extension.')
send_emails([self.build_email(
feature, requester_email, gate_id, ot_display_name)])
return {'message': 'OK'}
def build_email(
self,
feature: FeatureEntry,
requester_email: str,
gate_id: int,
ot_display_name: str
):
body_data = {
'feature': feature,
'id': feature['id'],
'gate_id': gate_id,
'SITE_URL': settings.SITE_URL,
}
body = render_template(self.EMAIL_TEMPLATE_PATH, **body_data)
return {
'to': requester_email,
'cc': [OT_SUPPORT_EMAIL],
'subject': ('Origin trial extension approved and ready to be '
f'initiated: {ot_display_name}'),
'reply_to': None,
'html': body,
}
class IntentToBlinkDevHandler(basehandlers.FlaskHandler):
"""Submit an intent email directly to blink-dev."""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'blink/intent_to_implement.html'
def process_post_data(self, **kwargs):
self.require_task_header()
feature_id = self.get_param('feature_id', required=True)
feature = FeatureEntry.get_by_id(feature_id)
if not feature:
self.abort(400, 'Feature not found.')
json_data = self.get_json_param_dict()
email_data = self.build_email(feature, json_data)
logging.info('Submitting email task:\n'
f'To: {email_data["to"]}\n'
f'CC: {email_data["cc"]}\n'
f'Subject: {email_data["subject"]}\n')
send_emails([email_data])
return {'message': 'OK'}
def build_email(self, feature: FeatureEntry, json_data: dict):
stage_info = stage_helpers.get_stage_info_for_templates(feature)
template_data = {
'feature': converters.feature_entry_to_json_verbose(feature),
'stage_info': stage_helpers.get_stage_info_for_templates(feature),
'sections_to_show': json_data['sections_to_show'],
'should_render_mstone_table': stage_info['should_render_mstone_table'],
'should_render_intents': stage_info['should_render_intents'],
'intent_stage': json_data['intent_stage'],
'default_url': json_data['default_url'],
'APP_TITLE': settings.APP_TITLE,
}
body = render_template(self.EMAIL_TEMPLATE_PATH, **template_data)
return {
'to': BLINK_DEV_EMAIL,
'cc': json_data['intent_cc_emails'],
'subject': json_data['subject'],
'reply_to': None,
'html': body,
}
GLOBAL_OT_PROCESS_REMINDER_CC_LIST = [
OT_SUPPORT_EMAIL,
]
class OTEndingNextReleaseReminderHandler(basehandlers.FlaskHandler):
"""Send origin trial ending next release reminder email to OT contacts."""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'origintrials/ot-ending-next-release-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
contacts = self.get_param('contacts')
body_data = {
'name': self.get_param('name'),
'release_milestone': self.get_param('release_milestone'),
'after_end_release': self.get_param('after_end_release'),
'after_end_date': self.get_param('after_end_date'),
}
send_emails([self.build_email(body_data, contacts)])
return {'message': 'OK'}
def build_email(self, body_data: dict[str, Any], contacts: list[str]):
body = render_template(self.EMAIL_TEMPLATE_PATH, **body_data)
return {
'to': contacts,
'cc': GLOBAL_OT_PROCESS_REMINDER_CC_LIST,
'subject': f'{body_data["name"]} origin trial ship decision approaching',
'reply_to': None,
'html': body,
}
class OTEndingThisReleaseReminderHandler(basehandlers.FlaskHandler):
"""Send origin trial ending this release reminder email to OT contacts."""
IS_INTERNAL_HANDLER = True
EMAIL_TEMPLATE_PATH = 'origintrials/ot-ending-this-release-email.html'
def process_post_data(self, **kwargs):
self.require_task_header()
name = self.get_param('name')
release_milestone = self.get_param('release_milestone')
next_release = self.get_param('next_release')