Skip to content

Commit 68418b2

Browse files
committed
Generalize code of resolution field
1 parent 783ac73 commit 68418b2

File tree

3 files changed

+53
-39
lines changed

3 files changed

+53
-39
lines changed

jbi/jira/service.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,29 @@ def assign_jira_user(self, context: ActionContext, email: str):
250250
f"Could not assign {jira_user_id} to issue {issue_key}"
251251
) from exc
252252

253+
def update_issue_named_field(self, context: ActionContext, field: str, value: str):
254+
bug = context.bug
255+
issue_key = context.jira.issue
256+
logger.info(
257+
f"Updating {field} of Jira issue %s to %s for Bug %s",
258+
issue_key,
259+
value,
260+
bug.id,
261+
extra=context.model_dump(),
262+
)
263+
fields: dict[str, Any] = {
264+
field: {"name": value},
265+
}
266+
response = self.client.update_issue_field(key=issue_key, fields=fields)
267+
logger.info(
268+
f"Updated {field} of Jira issue %s to %s for Bug %s",
269+
issue_key,
270+
value,
271+
bug.id,
272+
extra={"response": response, **context.model_dump()},
273+
)
274+
return response
275+
253276
def update_issue_status(self, context: ActionContext, jira_status: str):
254277
"""Update the status of the Jira issue"""
255278
issue_key = context.jira.issue
@@ -287,26 +310,7 @@ def update_issue_summary(self, context: ActionContext):
287310

288311
def update_issue_resolution(self, context: ActionContext, jira_resolution: str):
289312
"""Update the resolution of the Jira issue."""
290-
issue_key = context.jira.issue
291-
assert issue_key # Until we have more fine-grained typing of contexts
292-
293-
logger.info(
294-
"Updating resolution of Jira issue %s to %s",
295-
issue_key,
296-
jira_resolution,
297-
extra=context.model_dump(),
298-
)
299-
response = self.client.update_issue_field(
300-
key=issue_key,
301-
fields={"resolution": {"name": jira_resolution}},
302-
)
303-
logger.info(
304-
"Updated resolution of Jira issue %s to %s",
305-
issue_key,
306-
jira_resolution,
307-
extra={"response": response, **context.model_dump()},
308-
)
309-
return response
313+
return self.update_issue_named_field(context, field="resolution", value=jira_resolution)
310314

311315
def update_issue_components(
312316
self,

jbi/steps.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,41 +200,51 @@ def maybe_assign_jira_user(
200200
return (StepStatus.NOOP, context)
201201

202202

203-
def maybe_update_issue_resolution(
204-
context: ActionContext, *, parameters: ActionParams, jira_service: JiraService
203+
def _maybe_update_issue_mapped_field(
204+
source_field: str,
205+
target_field: str,
206+
context: ActionContext,
207+
parameters: ActionParams,
208+
jira_service: JiraService,
205209
) -> StepResult:
206-
"""
207-
Update the Jira issue status
208-
https://support.atlassian.com/jira-cloud-administration/docs/what-are-issue-statuses-priorities-and-resolutions/
209-
"""
210-
211-
bz_resolution = context.bug.resolution or ""
212-
jira_resolution = parameters.resolution_map.get(bz_resolution)
213-
214-
if jira_resolution is None:
210+
source_value = getattr(context.bug, source_field, None) or ""
211+
target_value = getattr(parameters, f"{source_field}_map").get(source_value)
212+
if target_value is None:
215213
logger.info(
216-
"Bug resolution %r was not in the resolution map.",
217-
bz_resolution,
214+
f"Bug {source_field} %r was not in the {source_field} map.",
215+
source_value,
218216
extra=context.update(
219217
operation=Operation.IGNORE,
220218
).model_dump(),
221219
)
222220
return (StepStatus.INCOMPLETE, context)
223221

224222
if context.operation == Operation.CREATE:
225-
resp = jira_service.update_issue_resolution(context, jira_resolution)
223+
resp = jira_service.update_issue_named_field(context, target_field, target_value)
226224
context.append_responses(resp)
227225
return (StepStatus.SUCCESS, context)
228226

229227
if context.operation == Operation.UPDATE:
230-
if "resolution" in context.event.changed_fields():
231-
resp = jira_service.update_issue_resolution(context, jira_resolution)
228+
if source_field in context.event.changed_fields():
229+
resp = jira_service.update_issue_named_field(context, target_field, target_value)
232230
context.append_responses(resp)
233231
return (StepStatus.SUCCESS, context)
234232

235233
return (StepStatus.NOOP, context)
236234

237235

236+
def maybe_update_issue_resolution(
237+
context: ActionContext, *, parameters: ActionParams, jira_service: JiraService
238+
) -> StepResult:
239+
"""
240+
Update the Jira issue status
241+
https://support.atlassian.com/jira-cloud-administration/docs/what-are-issue-statuses-priorities-and-resolutions/
242+
"""
243+
return _maybe_update_issue_mapped_field(
244+
"resolution", "resolution", context, parameters, jira_service
245+
)
246+
247+
238248
def maybe_update_issue_status(
239249
context: ActionContext, *, parameters: ActionParams, jira_service: JiraService
240250
) -> StepResult:

tests/unit/jira/test_service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ def test_update_issue_resolution(
203203
jira_service.update_issue_resolution(context=context, jira_resolution="DONEZO")
204204

205205
before, after = capturelogs.messages
206-
assert before == "Updating resolution of Jira issue JBI-234 to DONEZO"
207-
assert after == "Updated resolution of Jira issue JBI-234 to DONEZO"
206+
assert before == "Updating resolution of Jira issue JBI-234 to DONEZO for Bug 654321"
207+
assert after == "Updated resolution of Jira issue JBI-234 to DONEZO for Bug 654321"
208208

209209

210210
def test_update_issue_resolution_raises(
@@ -230,7 +230,7 @@ def test_update_issue_resolution_raises(
230230
)
231231

232232
[message] = capturelogs.messages
233-
assert message == "Updating resolution of Jira issue JBI-234 to DONEZO"
233+
assert message == "Updating resolution of Jira issue JBI-234 to DONEZO for Bug 654321"
234234

235235

236236
def test_create_jira_issue(

0 commit comments

Comments
 (0)