|
| 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 | + |
| 12 | +from src.jbi.bugzilla import BugzillaBug, BugzillaWebhookRequest |
| 13 | +from src.jbi.whiteboard_actions.default import DefaultExecutor |
| 14 | + |
| 15 | + |
| 16 | +def init(whiteboard_tag, jira_project_key, **kwargs): |
| 17 | + """Function that takes required and optional params and returns a callable object""" |
| 18 | + return ExtendedExecutor( |
| 19 | + whiteboard_tag=whiteboard_tag, jira_project_key=jira_project_key, **kwargs |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +class ExtendedExecutor(DefaultExecutor): |
| 24 | + """Callable class that encapsulates the extended action.""" |
| 25 | + |
| 26 | + def __init__(self, **kwargs): |
| 27 | + """Initialize ExtendedExecutor Object""" |
| 28 | + super().__init__(**kwargs) |
| 29 | + self.status_map = kwargs.get("status_map", {}) |
| 30 | + |
| 31 | + def jira_comments_for_update( |
| 32 | + self, |
| 33 | + payload: BugzillaWebhookRequest, |
| 34 | + ): |
| 35 | + """Returns the comments to post to Jira for a changed bug""" |
| 36 | + return payload.map_as_comments(False, False) |
| 37 | + |
| 38 | + def update_issue( |
| 39 | + self, |
| 40 | + payload: BugzillaWebhookRequest, |
| 41 | + bug_obj: BugzillaBug, |
| 42 | + linked_issue_key: str, |
| 43 | + is_new: bool, |
| 44 | + ): |
| 45 | + def clear_assignee(): |
| 46 | + # New tickets already have no assignee. |
| 47 | + if not is_new: |
| 48 | + self.jira_client.update_issue_field( |
| 49 | + key=linked_issue_key, fields={"assignee": None} |
| 50 | + ) |
| 51 | + |
| 52 | + changed_fields = payload.event.changed_fields() or [] |
| 53 | + |
| 54 | + # If this is a new issue or if the bug's assignee has changed then |
| 55 | + # update the assignee. |
| 56 | + if is_new or "assigned_to" in changed_fields: |
| 57 | + if bug_obj. assigned_to == "[email protected]": |
| 58 | + clear_assignee() |
| 59 | + else: |
| 60 | + # Look up this user in Jira |
| 61 | + users = self.jira_client.user_find_by_user_string( |
| 62 | + query=bug_obj.assigned_to |
| 63 | + ) |
| 64 | + if len(users) == 1: |
| 65 | + try: |
| 66 | + # There doesn't appear to be an easy way to verify that |
| 67 | + # this user can be assigned to this issue, so just try |
| 68 | + # and do it. |
| 69 | + self.jira_client.update_issue_field( |
| 70 | + key=linked_issue_key, |
| 71 | + fields={"assignee": {"accountId": users[0]["accountId"]}}, |
| 72 | + ) |
| 73 | + except IOError: |
| 74 | + # If that failed then just fall back to clearing the |
| 75 | + # assignee. |
| 76 | + clear_assignee() |
| 77 | + |
| 78 | + # If this is a new issue or if the bug's status or resolution has |
| 79 | + # changed then update the assignee. |
| 80 | + if is_new or "status" in changed_fields or "resolution" in changed_fields: |
| 81 | + # We use resolution if one exists or status otherwise. |
| 82 | + status = bug_obj.resolution |
| 83 | + if status == "": |
| 84 | + status = bug_obj.status |
| 85 | + |
| 86 | + if status in self.status_map: |
| 87 | + self.jira_client.set_issue_status( |
| 88 | + linked_issue_key, self.status_map[status] |
| 89 | + ) |
0 commit comments