Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame^] | 1 | # 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.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | |
| 12 | import logging |
| 13 | |
| 14 | from features import features_constants |
| 15 | from framework import cloud_tasks_helpers |
| 16 | from framework import framework_constants |
| 17 | from framework import urls |
| 18 | from tracker import tracker_bizobj |
| 19 | |
| 20 | |
| 21 | def 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 | |
| 52 | def 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 | |
| 69 | def 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 | |
| 83 | def 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 | |
| 107 | def 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) |