Skip to content

Commit 883bba6

Browse files
authored
Fix #600: catch 400 in maybe_update_components (#602)
1 parent 0e51d4d commit 883bba6

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

jbi/steps.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,27 @@ def maybe_update_components(context: ActionContext, parameters: ActionParams):
255255
if not jira_components:
256256
raise IncompleteStepError(context)
257257

258-
# Since we previously introspected the project components, we don't
259-
# have to catch any potential 400 error response here.
260-
resp = client.update_issue_field(
261-
key=context.jira.issue, fields={"components": jira_components}
262-
)
263-
context.append_responses(resp)
258+
# Although we previously introspected the project components, we
259+
# still have to catch any potential 400 error response here, because
260+
# the `components` field may not be on the create / update issue.
261+
try:
262+
resp = client.update_issue_field(
263+
key=context.jira.issue, fields={"components": jira_components}
264+
)
265+
context.append_responses(resp)
266+
except requests_exceptions.HTTPError as exc:
267+
if exc.response.status_code != 400:
268+
raise
269+
# If `components` is not a valid field on create/update screens,
270+
# then warn developers and ignore the error.
271+
logger.error(
272+
f"Could not set components on issue {context.jira.issue}: %s",
273+
str(exc),
274+
extra=context.dict(),
275+
)
276+
context.append_responses(exc.response)
277+
raise IncompleteStepError(context) from exc
278+
264279
return context
265280

266281

tests/unit/test_steps.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,34 @@ def test_maybe_update_components_raises_incompletesteperror_on_mismatch(
710710
steps.maybe_update_components(context_update_example, action_params)
711711

712712

713+
def test_maybe_update_components_failing(
714+
context_update_example: ActionContext, mocked_jira, caplog, action_params_factory
715+
):
716+
mocked_jira.get_project_components.return_value = [
717+
{"id": 1, "name": context_update_example.bug.component}
718+
]
719+
mocked_jira.update_issue_field.side_effect = requests.exceptions.HTTPError(
720+
"Field 'components' cannot be set", response=mock.MagicMock(status_code=400)
721+
)
722+
context_update_example.current_step = "maybe_update_components"
723+
724+
action_params = action_params_factory(
725+
jira_project_key=context_update_example.jira.project,
726+
)
727+
with pytest.raises(IncompleteStepError):
728+
with caplog.at_level(logging.DEBUG):
729+
steps.maybe_update_components(
730+
context=context_update_example, parameters=action_params
731+
)
732+
733+
captured_log_msgs = [
734+
r.msg % r.args for r in caplog.records if r.name == "jbi.steps"
735+
]
736+
assert captured_log_msgs == [
737+
"Could not set components on issue JBI-234: Field 'components' cannot be set"
738+
]
739+
740+
713741
def test_sync_whiteboard_labels(
714742
context_create_example: ActionContext, mocked_jira, action_params_factory
715743
):

0 commit comments

Comments
 (0)