blob: e7ee4d42590bb25d916d63d3950e0f1c17d6c6e6 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2018 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Functions that prepare and send email notifications of issue changes."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11
12import logging
13
14from features import features_constants
15from framework import cloud_tasks_helpers
16from framework import framework_constants
17from framework import urls
18from tracker import tracker_bizobj
19
20
21def PrepareAndSendIssueChangeNotification(
22 issue_id, hostport, commenter_id, send_email=True,
23 old_owner_id=framework_constants.NO_USER_SPECIFIED, comment_id=None):
24 """Create a task to notify users that an issue has changed.
25
26 Args:
27 issue_id: int ID of the issue that was changed.
28 hostport: string domain name and port number from the HTTP request.
29 commenter_id: int user ID of the user who made the comment.
30 send_email: True if email notifications should be sent.
31 old_owner_id: optional user ID of owner before the current change took
32 effect. They will also be notified.
33 comment_id: int Comment ID of the comment that was entered.
34
35 Returns nothing.
36 """
37 if old_owner_id is None:
38 old_owner_id = framework_constants.NO_USER_SPECIFIED
39 params = dict(
40 issue_id=issue_id, commenter_id=commenter_id, comment_id=comment_id,
41 hostport=hostport, old_owner_id=old_owner_id, send_email=int(send_email))
42 task = cloud_tasks_helpers.generate_simple_task(
43 urls.NOTIFY_ISSUE_CHANGE_TASK + '.do', params)
44 cloud_tasks_helpers.create_task(
45 task, queue=features_constants.QUEUE_NOTIFICATIONS)
46
47 task = cloud_tasks_helpers.generate_simple_task(
48 urls.PUBLISH_PUBSUB_ISSUE_CHANGE_TASK + '.do', params)
49 cloud_tasks_helpers.create_task(task, queue=features_constants.QUEUE_PUBSUB)
50
51
52def PrepareAndSendIssueBlockingNotification(
53 issue_id, hostport, delta_blocker_iids, commenter_id, send_email=True):
54 """Create a task to follow up on an issue blocked_on change."""
55 if not delta_blocker_iids:
56 return # No notification is needed
57
58 params = dict(
59 issue_id=issue_id, commenter_id=commenter_id, hostport=hostport,
60 send_email=int(send_email),
61 delta_blocker_iids=','.join(str(iid) for iid in delta_blocker_iids))
62
63 task = cloud_tasks_helpers.generate_simple_task(
64 urls.NOTIFY_BLOCKING_CHANGE_TASK + '.do', params)
65 cloud_tasks_helpers.create_task(
66 task, queue=features_constants.QUEUE_NOTIFICATIONS)
67
68
69def PrepareAndSendApprovalChangeNotification(
70 issue_id, approval_id, hostport, comment_id, send_email=True):
71 """Create a task to follow up on an approval change."""
72
73 params = dict(
74 issue_id=issue_id, approval_id=approval_id, hostport=hostport,
75 comment_id=comment_id, send_email=int(send_email))
76
77 task = cloud_tasks_helpers.generate_simple_task(
78 urls.NOTIFY_APPROVAL_CHANGE_TASK + '.do', params)
79 cloud_tasks_helpers.create_task(
80 task, queue=features_constants.QUEUE_NOTIFICATIONS)
81
82
83def SendIssueBulkChangeNotification(
84 issue_ids, hostport, old_owner_ids, comment_text, commenter_id,
85 amendments, send_email, users_by_id):
86 """Create a task to follow up on an issue blocked_on change."""
87 amendment_lines = []
88 for up in amendments:
89 line = ' %s: %s' % (
90 tracker_bizobj.GetAmendmentFieldName(up),
91 tracker_bizobj.AmendmentString(up, users_by_id))
92 if line not in amendment_lines:
93 amendment_lines.append(line)
94
95 params = dict(
96 issue_ids=','.join(str(iid) for iid in issue_ids),
97 commenter_id=commenter_id, hostport=hostport, send_email=int(send_email),
98 old_owner_ids=','.join(str(uid) for uid in old_owner_ids),
99 comment_text=comment_text, amendments='\n'.join(amendment_lines))
100
101 task = cloud_tasks_helpers.generate_simple_task(
102 urls.NOTIFY_BULK_CHANGE_TASK + '.do', params)
103 cloud_tasks_helpers.create_task(
104 task, queue=features_constants.QUEUE_NOTIFICATIONS)
105
106
107def PrepareAndSendDeletedFilterRulesNotification(
108 project_id, hostport, filter_rule_strs):
109 """Create a task to notify project owners of deleted filter rules."""
110
111 params = dict(
112 project_id=project_id, filter_rules=','.join(filter_rule_strs),
113 hostport=hostport)
114
115 task = cloud_tasks_helpers.generate_simple_task(
116 urls.NOTIFY_RULES_DELETED_TASK + '.do', params)
117 cloud_tasks_helpers.create_task(
118 task, queue=features_constants.QUEUE_NOTIFICATIONS)