12
12
from jbi import ActionResult , Operation
13
13
from jbi .environment import get_settings
14
14
from jbi .errors import ActionError
15
- from jbi .models import (
16
- ActionLogContext ,
17
- BugzillaBug ,
18
- BugzillaWebhookRequest ,
19
- JiraContext ,
20
- )
15
+ from jbi .models import ActionLogContext , BugzillaBug , BugzillaWebhookEvent , JiraContext
21
16
from jbi .services import bugzilla , jira
22
17
23
18
settings = get_settings ()
@@ -54,32 +49,36 @@ def __init__(self, jira_project_key, **kwargs):
54
49
self .jira_client = jira .get_client ()
55
50
56
51
def __call__ ( # pylint: disable=inconsistent-return-statements
57
- self , payload : BugzillaWebhookRequest
52
+ self ,
53
+ bug : BugzillaBug ,
54
+ event : BugzillaWebhookEvent ,
58
55
) -> ActionResult :
59
56
"""Called from BZ webhook when default action is used. All default-action webhook-events are processed here."""
60
- target = payload . event .target # type: ignore
57
+ target = event .target # type: ignore
61
58
if target == "comment" :
62
- return self .comment_create_or_noop (payload = payload ) # type: ignore
59
+ return self .comment_create_or_noop (bug = bug , event = event ) # type: ignore
63
60
if target == "bug" :
64
- return self .bug_create_or_update (payload = payload )
61
+ return self .bug_create_or_update (bug = bug , event = event )
65
62
logger .debug (
66
63
"Ignore event target %r" ,
67
64
target ,
68
65
extra = ActionLogContext (
69
- request = payload ,
66
+ bug = bug ,
67
+ event = event ,
70
68
operation = Operation .IGNORE ,
71
69
).dict (),
72
70
)
73
71
return False , {}
74
72
75
- def comment_create_or_noop (self , payload : BugzillaWebhookRequest ) -> ActionResult :
73
+ def comment_create_or_noop (
74
+ self , bug : BugzillaBug , event : BugzillaWebhookEvent
75
+ ) -> ActionResult :
76
76
"""Confirm issue is already linked, then apply comments; otherwise noop"""
77
- bug_obj = payload .bug
78
- linked_issue_key = bug_obj .extract_from_see_also ()
77
+ linked_issue_key = bug .extract_from_see_also ()
79
78
80
79
log_context = ActionLogContext (
81
- request = payload ,
82
- bug = bug_obj ,
80
+ event = event ,
81
+ bug = bug ,
83
82
operation = Operation .COMMENT ,
84
83
jira = JiraContext (
85
84
issue = linked_issue_key ,
@@ -89,19 +88,19 @@ def comment_create_or_noop(self, payload: BugzillaWebhookRequest) -> ActionResul
89
88
if not linked_issue_key :
90
89
logger .debug (
91
90
"No Jira issue linked to Bug %s" ,
92
- bug_obj .id ,
91
+ bug .id ,
93
92
extra = log_context .dict (),
94
93
)
95
94
return False , {}
96
95
97
- if bug_obj .comment is None :
96
+ if bug .comment is None :
98
97
logger .debug (
99
98
"No matching comment found in payload" ,
100
99
extra = log_context .dict (),
101
100
)
102
101
return False , {}
103
102
104
- formatted_comment = payload . map_as_jira_comment ( )
103
+ formatted_comment = bug . map_event_as_comment ( event )
105
104
jira_response = self .jira_client .issue_add_comment (
106
105
issue_key = linked_issue_key ,
107
106
comment = formatted_comment ,
@@ -113,45 +112,45 @@ def comment_create_or_noop(self, payload: BugzillaWebhookRequest) -> ActionResul
113
112
)
114
113
return True , {"jira_response" : jira_response }
115
114
116
- def jira_fields (self , bug_obj : BugzillaBug ):
115
+ def jira_fields (self , bug : BugzillaBug ):
117
116
"""Extract bug info as jira issue dictionary"""
118
117
fields : dict [str , Any ] = {
119
- "summary" : bug_obj .summary ,
118
+ "summary" : bug .summary ,
120
119
}
121
120
122
121
if self .sync_whiteboard_labels :
123
- fields ["labels" ] = bug_obj .get_jira_labels ()
122
+ fields ["labels" ] = bug .get_jira_labels ()
124
123
125
124
return fields
126
125
127
126
def jira_comments_for_update (
128
127
self ,
129
- payload : BugzillaWebhookRequest ,
128
+ bug : BugzillaBug ,
129
+ event : BugzillaWebhookEvent ,
130
130
):
131
131
"""Returns the comments to post to Jira for a changed bug"""
132
- return payload . map_as_comments ( )
132
+ return bug . map_changes_as_comments ( event )
133
133
134
134
def update_issue (
135
135
self ,
136
- payload : BugzillaWebhookRequest ,
137
- bug_obj : BugzillaBug ,
136
+ bug : BugzillaBug ,
137
+ event : BugzillaWebhookEvent ,
138
138
linked_issue_key : str ,
139
139
is_new : bool ,
140
140
):
141
141
"""Allows sub-classes to modify the Jira issue in response to a bug event"""
142
142
143
143
def bug_create_or_update (
144
- self , payload : BugzillaWebhookRequest
144
+ self , bug : BugzillaBug , event : BugzillaWebhookEvent
145
145
) -> ActionResult : # pylint: disable=too-many-locals
146
146
"""Create and link jira issue with bug, or update; rollback if multiple events fire"""
147
- bug_obj = payload .bug
148
- linked_issue_key = bug_obj .extract_from_see_also () # type: ignore
147
+ linked_issue_key = bug .extract_from_see_also () # type: ignore
149
148
if not linked_issue_key :
150
- return self .create_and_link_issue (payload , bug_obj )
149
+ return self .create_and_link_issue (bug = bug , event = event )
151
150
152
151
log_context = ActionLogContext (
153
- request = payload ,
154
- bug = bug_obj ,
152
+ event = event ,
153
+ bug = bug ,
155
154
operation = Operation .LINK ,
156
155
jira = JiraContext (
157
156
issue = linked_issue_key ,
@@ -162,14 +161,14 @@ def bug_create_or_update(
162
161
logger .debug (
163
162
"Update fields of Jira issue %s for Bug %s" ,
164
163
linked_issue_key ,
165
- bug_obj .id ,
164
+ bug .id ,
166
165
extra = log_context .dict (),
167
166
)
168
167
jira_response_update = self .jira_client .update_issue_field (
169
- key = linked_issue_key , fields = self .jira_fields (bug_obj )
168
+ key = linked_issue_key , fields = self .jira_fields (bug )
170
169
)
171
170
172
- comments = self .jira_comments_for_update (payload )
171
+ comments = self .jira_comments_for_update (bug = bug , event = event )
173
172
jira_response_comments = []
174
173
for i , comment in enumerate (comments ):
175
174
logger .debug (
@@ -184,33 +183,35 @@ def bug_create_or_update(
184
183
)
185
184
)
186
185
187
- self .update_issue (payload , bug_obj , linked_issue_key , is_new = False )
186
+ self .update_issue (bug , event , linked_issue_key , is_new = False )
188
187
189
188
return True , {"jira_responses" : [jira_response_update , jira_response_comments ]}
190
189
191
190
def create_and_link_issue ( # pylint: disable=too-many-locals
192
- self , payload , bug_obj
191
+ self ,
192
+ bug ,
193
+ event ,
193
194
) -> ActionResult :
194
195
"""create jira issue and establish link between bug and issue; rollback/delete if required"""
195
196
log_context = ActionLogContext (
196
- request = payload ,
197
- bug = bug_obj ,
197
+ event = event ,
198
+ bug = bug ,
198
199
operation = Operation .CREATE ,
199
200
jira = JiraContext (
200
201
project = self .jira_project_key ,
201
202
),
202
203
)
203
204
logger .debug (
204
205
"Create new Jira issue for Bug %s" ,
205
- bug_obj .id ,
206
+ bug .id ,
206
207
extra = log_context .dict (),
207
208
)
208
- comment_list = self .bugzilla_client .get_comments (bug_obj .id )
209
+ comment_list = self .bugzilla_client .get_comments (bug .id )
209
210
description = comment_list [0 ].text [:JIRA_DESCRIPTION_CHAR_LIMIT ]
210
211
211
212
fields = {
212
- ** self .jira_fields (bug_obj ), # type: ignore
213
- "issuetype" : {"name" : bug_obj .issue_type ()},
213
+ ** self .jira_fields (bug ), # type: ignore
214
+ "issuetype" : {"name" : bug .issue_type ()},
214
215
"description" : description ,
215
216
"project" : {"key" : self .jira_project_key },
216
217
}
@@ -236,9 +237,9 @@ def create_and_link_issue( # pylint: disable=too-many-locals
236
237
237
238
# In the time taken to create the Jira issue the bug may have been updated so
238
239
# re-retrieve it to ensure we have the latest data.
239
- bug_obj = self .bugzilla_client .get_bug (bug_obj .id )
240
+ bug = self .bugzilla_client .get_bug (bug .id )
240
241
241
- jira_key_in_bugzilla = bug_obj .extract_from_see_also ()
242
+ jira_key_in_bugzilla = bug .extract_from_see_also ()
242
243
_duplicate_creation_event = (
243
244
jira_key_in_bugzilla is not None
244
245
and jira_key_in_response != jira_key_in_bugzilla
@@ -247,7 +248,7 @@ def create_and_link_issue( # pylint: disable=too-many-locals
247
248
logger .warning (
248
249
"Delete duplicated Jira issue %s from Bug %s" ,
249
250
jira_key_in_response ,
250
- bug_obj .id ,
251
+ bug .id ,
251
252
extra = log_context .update (operation = Operation .DELETE ).dict (),
252
253
)
253
254
jira_response_delete = self .jira_client .delete_issue (
@@ -259,14 +260,14 @@ def create_and_link_issue( # pylint: disable=too-many-locals
259
260
logger .debug (
260
261
"Link %r on Bug %s" ,
261
262
jira_url ,
262
- bug_obj .id ,
263
+ bug .id ,
263
264
extra = log_context .update (operation = Operation .LINK ).dict (),
264
265
)
265
266
bugzilla_response = self .bugzilla_client .update_bug (
266
- bug_obj .id , see_also_add = jira_url
267
+ bug .id , see_also_add = jira_url
267
268
)
268
269
269
- bugzilla_url = f"{ settings .bugzilla_base_url } /show_bug.cgi?id={ bug_obj .id } "
270
+ bugzilla_url = f"{ settings .bugzilla_base_url } /show_bug.cgi?id={ bug .id } "
270
271
logger .debug (
271
272
"Link %r on Jira issue %s" ,
272
273
bugzilla_url ,
@@ -282,7 +283,9 @@ def create_and_link_issue( # pylint: disable=too-many-locals
282
283
icon_title = icon_url ,
283
284
)
284
285
285
- self .update_issue (payload , bug_obj , jira_key_in_response , is_new = True )
286
+ self .update_issue (
287
+ bug = bug , event = event , linked_issue_key = jira_key_in_response , is_new = True
288
+ )
286
289
287
290
return True , {
288
291
"bugzilla_response" : bugzilla_response ,
0 commit comments