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 BugzillaBug , BugzillaWebhookRequest
15
+ from jbi .models import (
16
+ ActionLogContext ,
17
+ BugzillaBug ,
18
+ BugzillaWebhookRequest ,
19
+ JiraContext ,
20
+ )
16
21
from jbi .services import get_bugzilla , get_jira
17
22
18
23
settings = get_settings ()
@@ -60,10 +65,10 @@ def __call__( # pylint: disable=inconsistent-return-statements
60
65
logger .debug (
61
66
"Ignore event target %r" ,
62
67
target ,
63
- extra = {
64
- " request" : payload . dict () ,
65
- " operation" : Operation .IGNORE ,
66
- } ,
68
+ extra = ActionLogContext (
69
+ request = payload ,
70
+ operation = Operation .IGNORE ,
71
+ ). dict () ,
67
72
)
68
73
return False , {}
69
74
@@ -72,28 +77,28 @@ def comment_create_or_noop(self, payload: BugzillaWebhookRequest) -> ActionResul
72
77
bug_obj = payload .bugzilla_object
73
78
linked_issue_key = bug_obj .extract_from_see_also ()
74
79
75
- log_context = {
76
- " request" : payload . dict () ,
77
- " bug" : bug_obj . dict () ,
78
- " operation" : Operation .COMMENT ,
79
- " jira" : {
80
- " issue" : linked_issue_key ,
81
- " project" : self .jira_project_key ,
82
- } ,
83
- }
80
+ log_context = ActionLogContext (
81
+ request = payload ,
82
+ bug = bug_obj ,
83
+ operation = Operation .COMMENT ,
84
+ jira = JiraContext (
85
+ issue = linked_issue_key ,
86
+ project = self .jira_project_key ,
87
+ ) ,
88
+ )
84
89
if not linked_issue_key :
85
90
logger .debug (
86
91
"No Jira issue linked to Bug %s" ,
87
92
bug_obj .id ,
88
- extra = log_context ,
93
+ extra = log_context . dict () ,
89
94
)
90
95
return False , {}
91
96
92
97
comment = payload .map_as_jira_comment ()
93
98
if comment is None :
94
99
logger .debug (
95
100
"No matching comment found in payload" ,
96
- extra = log_context ,
101
+ extra = log_context . dict () ,
97
102
)
98
103
return False , {}
99
104
@@ -104,7 +109,7 @@ def comment_create_or_noop(self, payload: BugzillaWebhookRequest) -> ActionResul
104
109
logger .debug (
105
110
"Comment added to Jira issue %s" ,
106
111
linked_issue_key ,
107
- extra = log_context ,
112
+ extra = log_context . dict () ,
108
113
)
109
114
return True , {"jira_response" : jira_response }
110
115
@@ -144,22 +149,21 @@ def bug_create_or_update(
144
149
if not linked_issue_key :
145
150
return self .create_and_link_issue (payload , bug_obj )
146
151
147
- log_context = {
148
- "request" : payload .dict (),
149
- "bug" : bug_obj .dict (),
150
- "jira" : {
151
- "issue" : linked_issue_key ,
152
- "project" : self .jira_project_key ,
153
- },
154
- }
152
+ log_context = ActionLogContext (
153
+ request = payload ,
154
+ bug = bug_obj ,
155
+ operation = Operation .LINK ,
156
+ jira = JiraContext (
157
+ issue = linked_issue_key ,
158
+ project = self .jira_project_key ,
159
+ ),
160
+ )
161
+
155
162
logger .debug (
156
163
"Update fields of Jira issue %s for Bug %s" ,
157
164
linked_issue_key ,
158
165
bug_obj .id ,
159
- extra = {
160
- ** log_context ,
161
- "operation" : Operation .LINK ,
162
- },
166
+ extra = log_context .dict (),
163
167
)
164
168
jira_response_update = self .jira_client .update_issue_field (
165
169
key = linked_issue_key , fields = self .jira_fields (bug_obj )
@@ -172,10 +176,7 @@ def bug_create_or_update(
172
176
"Create comment #%s on Jira issue %s" ,
173
177
i + 1 ,
174
178
linked_issue_key ,
175
- extra = {
176
- ** log_context ,
177
- "operation" : Operation .COMMENT ,
178
- },
179
+ extra = log_context .update (operation = Operation .COMMENT ).dict (),
179
180
)
180
181
jira_response_comments .append (
181
182
self .jira_client .issue_add_comment (
@@ -191,20 +192,18 @@ def create_and_link_issue( # pylint: disable=too-many-locals
191
192
self , payload , bug_obj
192
193
) -> ActionResult :
193
194
"""create jira issue and establish link between bug and issue; rollback/delete if required"""
194
- log_context = {
195
- "request" : payload .dict (),
196
- "bug" : bug_obj .dict (),
197
- "jira" : {
198
- "project" : self .jira_project_key ,
199
- },
200
- }
195
+ log_context = ActionLogContext (
196
+ request = payload ,
197
+ bug = bug_obj ,
198
+ operation = Operation .CREATE ,
199
+ jira = JiraContext (
200
+ project = self .jira_project_key ,
201
+ ),
202
+ )
201
203
logger .debug (
202
204
"Create new Jira issue for Bug %s" ,
203
205
bug_obj .id ,
204
- extra = {
205
- ** log_context ,
206
- "operation" : Operation .CREATE ,
207
- },
206
+ extra = log_context .dict (),
208
207
)
209
208
comment_list = self .bugzilla_client .get_comments (idlist = [bug_obj .id ])
210
209
description = comment_list ["bugs" ][str (bug_obj .id )]["comments" ][0 ]["text" ][
@@ -235,7 +234,7 @@ def create_and_link_issue( # pylint: disable=too-many-locals
235
234
236
235
jira_key_in_response = jira_response_create .get ("key" )
237
236
238
- log_context [ " jira" ][ " issue" ] = jira_key_in_response
237
+ log_context . jira . issue = jira_key_in_response
239
238
240
239
# In the time taken to create the Jira issue the bug may have been updated so
241
240
# re-retrieve it to ensure we have the latest data.
@@ -250,10 +249,7 @@ def create_and_link_issue( # pylint: disable=too-many-locals
250
249
"Delete duplicated Jira issue %s from Bug %s" ,
251
250
jira_key_in_response ,
252
251
bug_obj .id ,
253
- extra = {
254
- ** log_context ,
255
- "operation" : Operation .DELETE ,
256
- },
252
+ extra = log_context .update (operation = Operation .DELETE ).dict (),
257
253
)
258
254
jira_response_delete = self .jira_client .delete_issue (
259
255
issue_id_or_key = jira_key_in_response
@@ -265,10 +261,7 @@ def create_and_link_issue( # pylint: disable=too-many-locals
265
261
"Link %r on Bug %s" ,
266
262
jira_url ,
267
263
bug_obj .id ,
268
- extra = {
269
- ** log_context ,
270
- "operation" : Operation .LINK ,
271
- },
264
+ extra = log_context .update (operation = Operation .LINK ).dict (),
272
265
)
273
266
update = self .bugzilla_client .build_update (see_also_add = jira_url )
274
267
bugzilla_response = self .bugzilla_client .update_bugs ([bug_obj .id ], update )
@@ -278,10 +271,7 @@ def create_and_link_issue( # pylint: disable=too-many-locals
278
271
"Link %r on Jira issue %s" ,
279
272
bugzilla_url ,
280
273
jira_key_in_response ,
281
- extra = {
282
- ** log_context ,
283
- "operation" : Operation .LINK ,
284
- },
274
+ extra = log_context .update (operation = Operation .LINK ).dict (),
285
275
)
286
276
jira_response = self .jira_client .create_or_update_issue_remote_links (
287
277
issue_key = jira_key_in_response ,
0 commit comments