@@ -90,15 +90,14 @@ def test_jira_does_not_retry_4XX(mocked_responses, context_create_example):
90
90
mocked_responses .add (
91
91
responses .POST ,
92
92
url ,
93
- json = {},
93
+ json = {"errorMessages" : [ "You done goofed" ] },
94
94
status = 400 ,
95
95
)
96
96
97
- with pytest .raises (requests . HTTPError ):
97
+ with pytest .raises (jira . JiraCreateError ):
98
98
jira .get_service ().create_jira_issue (
99
99
context = context_create_example , description = "" , issue_type = "Task"
100
100
)
101
-
102
101
assert len (mocked_responses .calls ) == 1
103
102
104
103
@@ -155,3 +154,232 @@ def test_all_projects_components_exist_no_components_param(
155
154
)
156
155
result = jira .get_service ()._all_projects_components_exist (actions )
157
156
assert result is True
157
+
158
+
159
+ def test_get_issue (mocked_responses , action_context_factory , capturelogs ):
160
+ context = action_context_factory ()
161
+ url = f"{ get_settings ().jira_base_url } rest/api/2/issue/JBI-234"
162
+ mock_response_data = {"key" : "JBI-234" , "fields" : {"project" : {"key" : "JBI" }}}
163
+ mocked_responses .add (responses .GET , url , json = mock_response_data )
164
+
165
+ with capturelogs .for_logger ("jbi.services.jira" ).at_level (logging .DEBUG ):
166
+ response = jira .get_service ().get_issue (context = context , issue_key = "JBI-234" )
167
+
168
+ assert response == mock_response_data
169
+ for record in capturelogs .records :
170
+ assert record .rid == context .rid
171
+ assert record .action ["whiteboard_tag" ] == context .action .whiteboard_tag
172
+
173
+ before , after = capturelogs .messages
174
+ assert before == "Getting issue JBI-234"
175
+ assert after == "Received issue JBI-234"
176
+
177
+
178
+ def test_get_issue_handles_404 (mocked_responses , action_context_factory , capturelogs ):
179
+ context = action_context_factory ()
180
+ url = f"{ get_settings ().jira_base_url } rest/api/2/issue/JBI-234"
181
+ mocked_responses .add (responses .GET , url , status = 404 )
182
+
183
+ with capturelogs .for_logger ("jbi.services.jira" ).at_level (logging .DEBUG ):
184
+ return_val = jira .get_service ().get_issue (context = context , issue_key = "JBI-234" )
185
+
186
+ assert return_val is None
187
+
188
+ for record in capturelogs .records :
189
+ assert record .rid == context .rid
190
+
191
+ before , after = capturelogs .records
192
+ assert before .levelno == logging .DEBUG
193
+ assert before .message == "Getting issue JBI-234"
194
+
195
+ assert after .levelno == logging .ERROR
196
+ assert after .message .startswith ("Could not read issue JBI-234" )
197
+
198
+
199
+ def test_get_issue_reraises_other_erroring_status_codes (
200
+ mocked_responses , action_context_factory , capturelogs
201
+ ):
202
+ context = action_context_factory ()
203
+ url = f"{ get_settings ().jira_base_url } rest/api/2/issue/JBI-234"
204
+ mocked_responses .add (responses .GET , url , status = 401 )
205
+
206
+ with capturelogs .for_logger ("jbi.services.jira" ).at_level (logging .DEBUG ):
207
+ with pytest .raises (requests .HTTPError ):
208
+ jira .get_service ().get_issue (context = context , issue_key = "JBI-234" )
209
+
210
+ [record ] = capturelogs .records
211
+ assert record .rid == context .rid
212
+ assert record .levelno == logging .DEBUG
213
+ assert record .message == "Getting issue JBI-234"
214
+
215
+
216
+ def test_update_issue_resolution (mocked_responses , action_context_factory , capturelogs ):
217
+ context = action_context_factory (jira__issue = "JBI-234" )
218
+ url = f"{ get_settings ().jira_base_url } rest/api/2/issue/JBI-234"
219
+ mocked_responses .add (
220
+ responses .PUT ,
221
+ url ,
222
+ match = [
223
+ responses .matchers .json_params_matcher ({"fields" : {"resolution" : "DONEZO" }})
224
+ ],
225
+ )
226
+
227
+ with capturelogs .for_logger ("jbi.services.jira" ).at_level (logging .DEBUG ):
228
+ jira .get_service ().update_issue_resolution (
229
+ context = context , jira_resolution = "DONEZO"
230
+ )
231
+
232
+ for record in capturelogs .records :
233
+ assert record .rid == context .rid
234
+ assert record .levelno == logging .DEBUG
235
+
236
+ before , after = capturelogs .messages
237
+ assert before == "Updating resolution of Jira issue JBI-234 to DONEZO"
238
+ assert after == "Updated resolution of Jira issue JBI-234 to DONEZO"
239
+
240
+
241
+ def test_update_issue_resolution_raises (
242
+ mocked_responses , action_context_factory , capturelogs
243
+ ):
244
+ context = action_context_factory (jira__issue = "JBI-234" )
245
+ url = f"{ get_settings ().jira_base_url } rest/api/2/issue/JBI-234"
246
+ mocked_responses .add (
247
+ responses .PUT ,
248
+ url ,
249
+ status = 401 ,
250
+ match = [
251
+ responses .matchers .json_params_matcher ({"fields" : {"resolution" : "DONEZO" }})
252
+ ],
253
+ )
254
+
255
+ with capturelogs .for_logger ("jbi.services.jira" ).at_level (logging .DEBUG ):
256
+ with pytest .raises (requests .HTTPError ):
257
+ jira .get_service ().update_issue_resolution (
258
+ context = context , jira_resolution = "DONEZO"
259
+ )
260
+
261
+ [record ] = capturelogs .records
262
+ assert record .rid == context .rid
263
+ assert record .levelno == logging .DEBUG
264
+ assert record .message == "Updating resolution of Jira issue JBI-234 to DONEZO"
265
+
266
+
267
+ def test_create_jira_issue (mocked_responses , action_context_factory , capturelogs ):
268
+ context = action_context_factory (jira__project_key = "JBI" )
269
+ url = f"{ get_settings ().jira_base_url } rest/api/2/issue"
270
+ mocked_response_data = {"key" : "JBI-234" }
271
+ issue_fields = {
272
+ "summary" : context .bug .summary ,
273
+ "issuetype" : {"name" : "Task" },
274
+ "description" : "description" ,
275
+ "project" : {"key" : "JBI" },
276
+ }
277
+ mocked_responses .add (
278
+ responses .POST ,
279
+ url ,
280
+ status = 201 ,
281
+ match = [
282
+ responses .matchers .json_params_matcher (
283
+ {"fields" : issue_fields },
284
+ )
285
+ ],
286
+ json = mocked_response_data ,
287
+ )
288
+
289
+ with capturelogs .for_logger ("jbi.services.jira" ).at_level (logging .DEBUG ):
290
+ response = jira .get_service ().create_jira_issue (
291
+ context = context , description = "description" , issue_type = "Task"
292
+ )
293
+
294
+ assert response == mocked_response_data
295
+
296
+ for record in capturelogs .records :
297
+ assert record .rid == context .rid
298
+ assert record .levelno == logging .DEBUG
299
+
300
+ before , after = capturelogs .records
301
+ assert before .message == f"Creating new Jira issue for Bug { context .bug .id } "
302
+ assert before .fields == issue_fields
303
+
304
+ assert after .message == f"Jira issue JBI-234 created for Bug { context .bug .id } "
305
+ assert after .response == mocked_response_data
306
+
307
+
308
+ def test_create_jira_issue_when_list_is_returned (
309
+ mocked_responses , action_context_factory , capturelogs
310
+ ):
311
+ context = action_context_factory (jira__project_key = "JBI" )
312
+ url = f"{ get_settings ().jira_base_url } rest/api/2/issue"
313
+ mocked_issue_data = {"key" : "JBI-234" }
314
+ issue_fields = {
315
+ "summary" : context .bug .summary ,
316
+ "issuetype" : {"name" : "Task" },
317
+ "description" : "description" ,
318
+ "project" : {"key" : "JBI" },
319
+ }
320
+ mocked_responses .add (
321
+ responses .POST ,
322
+ url ,
323
+ status = 201 ,
324
+ match = [
325
+ responses .matchers .json_params_matcher (
326
+ {"fields" : issue_fields },
327
+ )
328
+ ],
329
+ json = [mocked_issue_data ],
330
+ )
331
+
332
+ with capturelogs .for_logger ("jbi.services.jira" ).at_level (logging .DEBUG ):
333
+ issue_data = jira .get_service ().create_jira_issue (
334
+ context = context , description = "description" , issue_type = "Task"
335
+ )
336
+
337
+ assert issue_data == mocked_issue_data
338
+
339
+ before , after = capturelogs .records
340
+ assert before .message == f"Creating new Jira issue for Bug { context .bug .id } "
341
+ assert before .levelno == logging .DEBUG
342
+ assert before .rid == context .rid
343
+ assert before .fields == issue_fields
344
+
345
+ assert after .message == f"Jira issue JBI-234 created for Bug { context .bug .id } "
346
+ assert after .levelno == logging .DEBUG
347
+ assert after .rid == context .rid
348
+ assert after .response == [mocked_issue_data ]
349
+
350
+
351
+ def test_create_jira_issue_returns_errors (
352
+ mocked_responses , action_context_factory , capturelogs
353
+ ):
354
+ context = action_context_factory (jira__project_key = "JBI" )
355
+ url = f"{ get_settings ().jira_base_url } rest/api/2/issue"
356
+ fake_error_data = {
357
+ "errorMessages" : ["You done goofed" ],
358
+ "errors" : ["You messed up this time" ],
359
+ }
360
+ issue_fields = {
361
+ "summary" : context .bug .summary ,
362
+ "issuetype" : {"name" : "Task" },
363
+ "description" : "description" ,
364
+ "project" : {"key" : "JBI" },
365
+ }
366
+ mocked_responses .add (responses .POST , url , status = 400 , json = fake_error_data )
367
+
368
+ with capturelogs .for_logger ("jbi.services.jira" ).at_level (logging .DEBUG ):
369
+ with pytest .raises (jira .JiraCreateError ) as exc :
370
+ jira .get_service ().create_jira_issue (
371
+ context = context , description = "description" , issue_type = "Task"
372
+ )
373
+
374
+ assert str (exc .value ) == "Failed to create issue for Bug 654321"
375
+
376
+ before , after = capturelogs .records
377
+ assert before .message == f"Creating new Jira issue for Bug { context .bug .id } "
378
+ assert before .rid == context .rid
379
+ assert before .levelno == logging .DEBUG
380
+ assert before .fields == issue_fields
381
+
382
+ assert after .message == f"Failed to create issue for Bug { context .bug .id } "
383
+ assert after .rid == context .rid
384
+ assert after .levelno == logging .ERROR
385
+ assert after .response == fake_error_data
0 commit comments