|
| 1 | +""" |
| 2 | +Extended action that provides some additional features over the default: |
| 3 | + * Updates the Jira assignee when the bug's assignee changes. |
| 4 | + * Optionally updates the Jira status when the bug's resolution or status changes. |
| 5 | +
|
| 6 | +`init` is required; and requires at minimum the |
| 7 | +`whiteboard_tag` and `jira_project_key`. `status_map` is optional. |
| 8 | +
|
| 9 | +`init` should return a __call__able |
| 10 | +""" |
| 11 | +import logging |
| 12 | + |
| 13 | +from src.jbi.bugzilla import BugzillaBug, BugzillaWebhookRequest |
| 14 | +from src.jbi.whiteboard_actions.default import DefaultExecutor |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +def init(status_map=None, **kwargs): |
| 20 | + """Function that takes required and optional params and returns a callable object""" |
| 21 | + return AssigneeAndStatusExecutor(status_map=status_map or {}, **kwargs) |
| 22 | + |
| 23 | + |
| 24 | +class AssigneeAndStatusExecutor(DefaultExecutor): |
| 25 | + """Callable class that encapsulates the default_with_assignee_and_status action.""" |
| 26 | + |
| 27 | + def __init__(self, status_map, **kwargs): |
| 28 | + """Initialize AssigneeAndStatusExecutor Object""" |
| 29 | + super().__init__(**kwargs) |
| 30 | + self.status_map = status_map |
| 31 | + |
| 32 | + def jira_comments_for_update( |
| 33 | + self, |
| 34 | + payload: BugzillaWebhookRequest, |
| 35 | + ): |
| 36 | + """Returns the comments to post to Jira for a changed bug""" |
| 37 | + return payload.map_as_comments( |
| 38 | + status_log_enabled=False, assignee_log_enabled=False |
| 39 | + ) |
| 40 | + |
| 41 | + def update_issue( |
| 42 | + self, |
| 43 | + payload: BugzillaWebhookRequest, |
| 44 | + bug_obj: BugzillaBug, |
| 45 | + linked_issue_key: str, |
| 46 | + is_new: bool, |
| 47 | + ): |
| 48 | + changed_fields = payload.event.changed_fields() or [] |
| 49 | + |
| 50 | + log_context = { |
| 51 | + "bug": { |
| 52 | + "id": bug_obj.id, |
| 53 | + "status": bug_obj.status, |
| 54 | + "resolution": bug_obj.resolution, |
| 55 | + "assigned_to": bug_obj.assigned_to, |
| 56 | + }, |
| 57 | + "jira": linked_issue_key, |
| 58 | + "changed_fields": changed_fields, |
| 59 | + } |
| 60 | + |
| 61 | + def clear_assignee(): |
| 62 | + # New tickets already have no assignee. |
| 63 | + if not is_new: |
| 64 | + logger.debug("Clearing assignee", extra=log_context) |
| 65 | + self.jira_client.update_issue_field( |
| 66 | + key=linked_issue_key, fields={"assignee": None} |
| 67 | + ) |
| 68 | + |
| 69 | + # If this is a new issue or if the bug's assignee has changed then |
| 70 | + # update the assignee. |
| 71 | + if is_new or "assigned_to" in changed_fields: |
| 72 | + if bug_obj. assigned_to == "[email protected]": |
| 73 | + clear_assignee() |
| 74 | + else: |
| 75 | + logger.debug( |
| 76 | + "Attempting to update assignee", |
| 77 | + extra=log_context, |
| 78 | + ) |
| 79 | + # Look up this user in Jira |
| 80 | + users = self.jira_client.user_find_by_user_string( |
| 81 | + query=bug_obj.assigned_to |
| 82 | + ) |
| 83 | + if len(users) == 1: |
| 84 | + try: |
| 85 | + # There doesn't appear to be an easy way to verify that |
| 86 | + # this user can be assigned to this issue, so just try |
| 87 | + # and do it. |
| 88 | + self.jira_client.update_issue_field( |
| 89 | + key=linked_issue_key, |
| 90 | + fields={"assignee": {"accountId": users[0]["accountId"]}}, |
| 91 | + ) |
| 92 | + except IOError as exception: |
| 93 | + logger.debug( |
| 94 | + "Setting assignee failed: %s", exception, extra=log_context |
| 95 | + ) |
| 96 | + # If that failed then just fall back to clearing the |
| 97 | + # assignee. |
| 98 | + clear_assignee() |
| 99 | + else: |
| 100 | + logger.debug("No assignee found", extra=log_context) |
| 101 | + clear_assignee() |
| 102 | + |
| 103 | + # If this is a new issue or if the bug's status or resolution has |
| 104 | + # changed then update the issue status. |
| 105 | + if is_new or "status" in changed_fields or "resolution" in changed_fields: |
| 106 | + # We use resolution if one exists or status otherwise. |
| 107 | + status = bug_obj.resolution or bug_obj.status |
| 108 | + |
| 109 | + if status in self.status_map: |
| 110 | + logger.debug( |
| 111 | + "Updating Jira status to %s", |
| 112 | + self.status_map[status], |
| 113 | + extra=log_context, |
| 114 | + ) |
| 115 | + self.jira_client.set_issue_status( |
| 116 | + linked_issue_key, self.status_map[status] |
| 117 | + ) |
| 118 | + else: |
| 119 | + logger.debug( |
| 120 | + "Bug status was not in the status map.", |
| 121 | + extra={**log_context, "status_map": self.status_map}, |
| 122 | + ) |
0 commit comments