Skip to content

Commit 32cb2fa

Browse files
committed
Generalize more
Sq
1 parent 6ebae33 commit 32cb2fa

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
lines changed

jbi/jira/service.py

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,21 @@ def assign_jira_user(self, context: ActionContext, email: str):
241241
# There doesn't appear to be an easy way to verify that
242242
# this user can be assigned to this issue, so just try
243243
# and do it.
244-
return self.client.update_issue_field(
245-
key=issue_key,
246-
fields={"assignee": {"accountId": jira_user_id}},
244+
return self.update_issue_field(
245+
context, "assignee", jira_user_id, wrap_value="accountId"
247246
)
248247
except (requests_exceptions.HTTPError, IOError) as exc:
249248
raise ValueError(
250249
f"Could not assign {jira_user_id} to issue {issue_key}"
251250
) from exc
252251

253-
def update_issue_named_field(self, context: ActionContext, field: str, value: str):
252+
def update_issue_field(
253+
self,
254+
context: ActionContext,
255+
field: str,
256+
value: Any,
257+
wrap_value: Optional[str] = None,
258+
):
254259
bug = context.bug
255260
issue_key = context.jira.issue
256261
logger.info(
@@ -260,9 +265,7 @@ def update_issue_named_field(self, context: ActionContext, field: str, value: st
260265
bug.id,
261266
extra=context.model_dump(),
262267
)
263-
fields: dict[str, Any] = {
264-
field: {"name": value},
265-
}
268+
fields: dict[str, Any] = {field: {wrap_value: value} if wrap_value else value}
266269
response = self.client.update_issue_field(key=issue_key, fields=fields)
267270
logger.info(
268271
f"Updated {field} of Jira issue %s to %s for Bug %s",
@@ -290,34 +293,25 @@ def update_issue_status(self, context: ActionContext, jira_status: str):
290293

291294
def update_issue_summary(self, context: ActionContext):
292295
"""Update's an issue's summary with the description of an incoming bug"""
293-
294-
bug = context.bug
295-
issue_key = context.jira.issue
296-
logger.info(
297-
"Update summary of Jira issue %s for Bug %s",
298-
issue_key,
299-
bug.id,
300-
extra=context.model_dump(),
301-
)
302296
truncated_summary = markdown_to_jira(
303-
bug.summary or "", max_length=JIRA_DESCRIPTION_CHAR_LIMIT
297+
context.bug.summary or "", max_length=JIRA_DESCRIPTION_CHAR_LIMIT
298+
)
299+
return self.update_issue_field(
300+
context, field="summary", value=truncated_summary
304301
)
305-
fields: dict[str, str] = {
306-
"summary": truncated_summary,
307-
}
308-
jira_response = self.client.update_issue_field(key=issue_key, fields=fields)
309-
return jira_response
310302

311303
def update_issue_resolution(self, context: ActionContext, jira_resolution: str):
312304
"""Update the resolution of the Jira issue."""
313-
return self.update_issue_named_field(
314-
context, field="resolution", value=jira_resolution
305+
return self.update_issue_field(
306+
context,
307+
field="resolution",
308+
value=jira_resolution,
309+
wrap_value="name",
315310
)
316311

317312
def update_issue_components(
318313
self,
319-
issue_key: str,
320-
project: str,
314+
context: ActionContext,
321315
components: Iterable[str],
322316
) -> tuple[Optional[dict], set]:
323317
"""Attempt to add components to the specified issue
@@ -334,7 +328,9 @@ def update_issue_components(
334328
missing_components = set(components)
335329
jira_components = []
336330

337-
all_project_components = self.client.get_project_components(project)
331+
all_project_components = self.client.get_project_components(
332+
context.jira.project
333+
)
338334
for comp in all_project_components:
339335
if comp["name"] in missing_components:
340336
jira_components.append({"id": comp["id"]})
@@ -343,13 +339,8 @@ def update_issue_components(
343339
if not jira_components:
344340
return None, missing_components
345341

346-
logger.info(
347-
"attempting to add components '%s' to issue '%s'",
348-
",".join(components),
349-
issue_key,
350-
)
351-
resp = self.client.update_issue_field(
352-
key=issue_key, fields={"components": jira_components}
342+
resp = self.update_issue_field(
343+
context, field="components", value=jira_components
353344
)
354345
return resp, missing_components
355346

@@ -576,9 +567,9 @@ def check_jira_all_project_issue_types_exist(actions, _get_service):
576567
action_issue_types = set(action.parameters.issue_type_map.values())
577568
project_issue_types = issue_types_by_project.get(action.jira_project_key, set())
578569
if missing_issue_types := action_issue_types - project_issue_types:
579-
missing_issue_types_by_project[
580-
action.jira_project_key
581-
] = missing_issue_types
570+
missing_issue_types_by_project[action.jira_project_key] = (
571+
missing_issue_types
572+
)
582573
if missing_issue_types_by_project:
583574
return [
584575
checks.Warning(

jbi/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class ActionParams(BaseModel, frozen=True):
7373
jira_project_key: str
7474
steps: ActionSteps = ActionSteps()
7575
jira_components: JiraComponents = JiraComponents()
76+
jira_severity_field: str = "severity"
77+
jira_priority_field: str = "priority"
78+
jira_resolution_field: str = "resolution"
7679
labels_brackets: Literal["yes", "no", "both"] = "no"
7780
status_map: dict[str, str] = {}
7881
priority_map: dict[str, str] = {

jbi/steps.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,14 @@ def maybe_assign_jira_user(
202202

203203
def _maybe_update_issue_mapped_field(
204204
source_field: str,
205-
target_field: str,
206205
context: ActionContext,
207206
parameters: ActionParams,
208207
jira_service: JiraService,
208+
wrap_value: Optional[str] = None,
209209
) -> StepResult:
210210
source_value = getattr(context.bug, source_field, None) or ""
211-
target_value = getattr(parameters, f"{source_field}_map").get(source_value)
211+
target_field = getattr(parameters, f"jira_{source_field}_field")
212+
target_value = getattr(parameters, f"{target_field}_map").get(source_value)
212213
if target_value is None:
213214
logger.info(
214215
f"Bug {source_field} %r was not in the {source_field} map.",
@@ -220,16 +221,22 @@ def _maybe_update_issue_mapped_field(
220221
return (StepStatus.INCOMPLETE, context)
221222

222223
if context.operation == Operation.CREATE:
223-
resp = jira_service.update_issue_named_field(
224-
context, target_field, target_value
224+
resp = jira_service.update_issue_field(
225+
context,
226+
target_field,
227+
target_value,
228+
wrap_value,
225229
)
226230
context.append_responses(resp)
227231
return (StepStatus.SUCCESS, context)
228232

229233
if context.operation == Operation.UPDATE:
230234
if source_field in context.event.changed_fields():
231-
resp = jira_service.update_issue_named_field(
232-
context, target_field, target_value
235+
resp = jira_service.update_issue_field(
236+
context,
237+
target_field,
238+
target_value,
239+
wrap_value,
233240
)
234241
context.append_responses(resp)
235242
return (StepStatus.SUCCESS, context)
@@ -244,7 +251,7 @@ def maybe_update_issue_priority(
244251
Update the Jira issue priority
245252
"""
246253
return _maybe_update_issue_mapped_field(
247-
"priority", "priority", context, parameters, jira_service
254+
"priority", context, parameters, jira_service, wrap_value="name"
248255
)
249256

250257

@@ -256,7 +263,7 @@ def maybe_update_issue_resolution(
256263
https://support.atlassian.com/jira-cloud-administration/docs/what-are-issue-statuses-priorities-and-resolutions/
257264
"""
258265
return _maybe_update_issue_mapped_field(
259-
"resolution", "resolution", context, parameters, jira_service
266+
"resolution", context, parameters, jira_service, wrap_value="name"
260267
)
261268

262269

@@ -267,7 +274,7 @@ def maybe_update_issue_severity(
267274
Update the Jira issue severity
268275
"""
269276
return _maybe_update_issue_mapped_field(
270-
"severity", "severity", context, parameters, jira_service
277+
"severity", context, parameters, jira_service, wrap_value="name"
271278
)
272279

273280

@@ -337,8 +344,7 @@ def maybe_update_components(
337344

338345
try:
339346
resp, missing_components = jira_service.update_issue_components(
340-
issue_key=context.jira.issue,
341-
project=parameters.jira_project_key,
347+
context=context,
342348
components=candidate_components,
343349
)
344350
except requests_exceptions.HTTPError as exc:

0 commit comments

Comments
 (0)