Skip to content

Commit c0aa693

Browse files
authored
Support removal of mapped field (#899)
* Add working default field mappings * Add tests for empty values code paths
1 parent 31486c4 commit c0aa693

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

jbi/models.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,23 @@ class ActionParams(BaseModel, frozen=True):
8181
labels_brackets: Literal["yes", "no", "both"] = "no"
8282
status_map: dict[str, str] = {}
8383
priority_map: dict[str, str] = {
84+
"": "(none)",
8485
"P1": "P1",
8586
"P2": "P2",
8687
"P3": "P3",
8788
"P4": "Low",
8889
"P5": "Lowest",
8990
}
9091
resolution_map: dict[str, str] = {}
91-
severity_map: dict[str, str] = {}
92+
severity_map: dict[str, str] = {
93+
"": "N/A",
94+
"S1": "S1",
95+
"S2": "S2",
96+
"S3": "S3",
97+
"S4": "S4",
98+
}
9299
cf_fx_points_map: dict[str, int] = {
100+
"": 0,
93101
"?": 0,
94102
"1": 1,
95103
"2": 2,

jbi/steps.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ def _maybe_update_issue_mapped_field(
210210
source_value = getattr(context.bug, source_field, None) or ""
211211
target_field = getattr(parameters, f"jira_{source_field}_field")
212212
target_value = getattr(parameters, f"{source_field}_map").get(source_value)
213+
214+
# If field is empty on create, or update is about another field, then nothing to do.
215+
if (context.operation == Operation.CREATE and source_value == "") or (
216+
context.operation == Operation.UPDATE
217+
and source_field not in context.event.changed_fields()
218+
):
219+
return (StepStatus.NOOP, context)
220+
213221
if target_value is None:
214222
logger.info(
215223
f"Bug {source_field} %r was not in the {source_field} map.",
@@ -220,12 +228,6 @@ def _maybe_update_issue_mapped_field(
220228
)
221229
return (StepStatus.INCOMPLETE, context)
222230

223-
if (
224-
context.operation == Operation.UPDATE
225-
and source_field not in context.event.changed_fields()
226-
):
227-
return (StepStatus.NOOP, context)
228-
229231
resp = jira_service.update_issue_field(
230232
context,
231233
target_field,

tests/unit/test_steps.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,54 @@ def test_change_to_unknown_resolution_with_resolution_map(
728728
]
729729

730730

731+
def test_create_issue_empty_priority(
732+
action_context_factory,
733+
mocked_jira,
734+
action_params_factory,
735+
):
736+
action_context = action_context_factory(
737+
operation=Operation.CREATE, bug__priority=None
738+
)
739+
params = action_params_factory(
740+
jira_project_key=action_context.jira.project,
741+
)
742+
743+
result, _ = steps.maybe_update_issue_priority(
744+
action_context, parameters=params, jira_service=JiraService(mocked_jira)
745+
)
746+
747+
assert result == steps.StepStatus.NOOP
748+
749+
750+
def test_update_issue_remove_priority(
751+
action_context_factory,
752+
mocked_jira,
753+
action_params_factory,
754+
webhook_event_change_factory,
755+
):
756+
action_context = action_context_factory(
757+
operation=Operation.UPDATE,
758+
jira__issue="JBI-234",
759+
bug__priority=None,
760+
current_step="maybe_update_issue_priority",
761+
event__changes=[
762+
webhook_event_change_factory(field="priority", removed="P1", added="--")
763+
],
764+
)
765+
params = action_params_factory(
766+
jira_project_key=action_context.jira.project,
767+
)
768+
769+
result, _ = steps.maybe_update_issue_priority(
770+
action_context, parameters=params, jira_service=JiraService(mocked_jira)
771+
)
772+
773+
assert result == steps.StepStatus.SUCCESS
774+
mocked_jira.update_issue_field.assert_called_with(
775+
key="JBI-234", fields={"priority": {"name": "(none)"}}
776+
)
777+
778+
731779
def test_update_issue_priority(
732780
action_context_factory,
733781
mocked_jira,

0 commit comments

Comments
 (0)