Skip to content

Commit fa57bee

Browse files
authored
Fix #210: do not always overwrite summary (#540)
* Change return type of changed_fields() * Fix #210: do not always overwrite summary * Add test for situation described in github issue
1 parent 63e2dde commit fa57bee

File tree

6 files changed

+40
-47
lines changed

6 files changed

+40
-47
lines changed

jbi/actions/steps.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def update_issue_summary(context: ActionContext, **parameters):
7676

7777
bug = context.bug
7878
issue_key = context.jira.issue
79+
80+
if "summary" not in context.event.changed_fields():
81+
return context, ()
82+
7983
logger.debug(
8084
"Update summary of Jira issue %s for Bug %s",
8185
issue_key,
@@ -122,9 +126,7 @@ def maybe_assign_jira_user(context: ActionContext, **parameters):
122126
logger.debug(str(exc), extra=context.dict())
123127

124128
if context.operation == Operation.UPDATE:
125-
changed_fields = event.changed_fields() or []
126-
127-
if "assigned_to" not in changed_fields:
129+
if "assigned_to" not in event.changed_fields():
128130
return context, ()
129131

130132
if not bug.is_assigned():
@@ -166,9 +168,7 @@ def maybe_update_issue_resolution(
166168
return context, (resp,)
167169

168170
if context.operation == Operation.UPDATE:
169-
changed_fields = context.event.changed_fields() or []
170-
171-
if "resolution" in changed_fields:
171+
if "resolution" in context.event.changed_fields():
172172
resp = jira.update_issue_resolution(context, jira_resolution)
173173
return context, (resp,)
174174

@@ -198,7 +198,7 @@ def maybe_update_issue_status(context: ActionContext, **parameters):
198198
return context, (resp,)
199199

200200
if context.operation == Operation.UPDATE:
201-
changed_fields = context.event.changed_fields() or []
201+
changed_fields = context.event.changed_fields()
202202

203203
if "status" in changed_fields or "resolution" in changed_fields:
204204
resp = jira.update_issue_status(context, jira_status)

jbi/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class BugzillaWebhookEvent(BaseModel):
182182
target: Optional[str]
183183
routing_key: Optional[str]
184184

185-
def changed_fields(self) -> Optional[list[str]]:
185+
def changed_fields(self) -> list[str]:
186186
"""Returns the names of changed fields in a bug"""
187187
if self.changes:
188188
return [c.field for c in self.changes]
@@ -192,7 +192,7 @@ def changed_fields(self) -> Optional[list[str]]:
192192
if self.routing_key is not None and self.routing_key[0:11] == "bug.modify:":
193193
return self.routing_key[11:].split(",")
194194

195-
return None
195+
return []
196196

197197

198198
class BugzillaWebhookAttachment(BaseModel):

jbi/runner.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,10 @@ def execute_action(
9696
)
9797

9898
if event.target == "bug":
99-
changed_fields = event.changed_fields() or []
10099
action_context = action_context.update(
101100
operation=Operation.UPDATE,
102101
extra={
103-
"changed_fields": ", ".join(changed_fields),
102+
"changed_fields": ", ".join(event.changed_fields()),
104103
**action_context.extra,
105104
},
106105
)

tests/unit/actions/test_steps.py

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from jbi.actions import default
1111
from jbi.environment import get_settings
12-
from jbi.models import ActionContext
12+
from jbi.models import ActionContext, BugzillaWebhookEventChange
1313
from jbi.services.jira import JiraCreateError
1414
from tests.fixtures.factories import comment_factory
1515

@@ -62,6 +62,12 @@ def test_created_public(
6262

6363

6464
def test_modified_public(context_update_example: ActionContext, mocked_jira):
65+
context_update_example.event.changes = [
66+
BugzillaWebhookEventChange.parse_obj(
67+
{"field": "summary", "removed": "", "added": "JBI Test"}
68+
)
69+
]
70+
6571
callable_object = default.init(jira_project_key=context_update_example.jira.project)
6672

6773
callable_object(context=context_update_example)
@@ -195,12 +201,6 @@ def test_clear_assignee(context_update_example: ActionContext, mocked_jira):
195201

196202
mocked_jira.create_issue.assert_not_called()
197203
mocked_jira.user_find_by_user_string.assert_not_called()
198-
mocked_jira.update_issue_field.assert_any_call(
199-
key="JBI-234",
200-
fields={
201-
"summary": "JBI Test",
202-
},
203-
)
204204
mocked_jira.update_issue_field.assert_any_call(
205205
key="JBI-234",
206206
fields={"assignee": None},
@@ -224,12 +224,6 @@ def test_set_assignee(context_update_example: ActionContext, mocked_jira):
224224
mocked_jira.user_find_by_user_string.assert_called_once_with(
225225
226226
)
227-
mocked_jira.update_issue_field.assert_any_call(
228-
key="JBI-234",
229-
fields={
230-
"summary": "JBI Test",
231-
},
232-
)
233227
mocked_jira.update_issue_field.assert_any_call(
234228
key="JBI-234",
235229
fields={"assignee": {"accountId": "6254"}},
@@ -324,12 +318,6 @@ def test_change_to_unknown_status(context_update_example: ActionContext, mocked_
324318

325319
mocked_jira.create_issue.assert_not_called()
326320
mocked_jira.user_find_by_user_string.assert_not_called()
327-
mocked_jira.update_issue_field.assert_called_once_with(
328-
key="JBI-234",
329-
fields={
330-
"summary": "JBI Test",
331-
},
332-
)
333321
mocked_jira.set_issue_status.assert_not_called()
334322

335323

@@ -351,12 +339,6 @@ def test_change_to_known_status(context_update_example: ActionContext, mocked_ji
351339

352340
mocked_jira.create_issue.assert_not_called()
353341
mocked_jira.user_find_by_user_string.assert_not_called()
354-
mocked_jira.update_issue_field.assert_called_once_with(
355-
key="JBI-234",
356-
fields={
357-
"summary": "JBI Test",
358-
},
359-
)
360342
mocked_jira.set_issue_status.assert_called_once_with("JBI-234", "In Progress")
361343

362344

@@ -378,12 +360,6 @@ def test_change_to_known_resolution(context_update_example: ActionContext, mocke
378360

379361
mocked_jira.create_issue.assert_not_called()
380362
mocked_jira.user_find_by_user_string.assert_not_called()
381-
mocked_jira.update_issue_field.assert_called_once_with(
382-
key="JBI-234",
383-
fields={
384-
"summary": "JBI Test",
385-
},
386-
)
387363
mocked_jira.set_issue_status.assert_called_once_with("JBI-234", "Closed")
388364

389365

@@ -423,10 +399,7 @@ def test_change_to_unknown_resolution_with_resolution_map(
423399
)
424400
callable_object(context=context_update_resolution_example)
425401

426-
mocked_jira.update_issue_field.assert_called_once_with(
427-
key="JBI-234",
428-
fields={"summary": "JBI Test"},
429-
)
402+
mocked_jira.update_issue_field.assert_not_called()
430403

431404

432405
@pytest.mark.parametrize(

tests/unit/test_bugzilla.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_lookup_action_not_found(whiteboard, actions_example):
8686

8787
def test_payload_empty_changes_list(webhook_change_status_assignee):
8888
webhook_change_status_assignee.event.changes = None
89-
assert webhook_change_status_assignee.event.changed_fields() is None
89+
assert webhook_change_status_assignee.event.changed_fields() == []
9090

9191

9292
def test_payload_changes_list(webhook_change_status_assignee):

tests/unit/test_runner.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,24 @@ def test_runner_ignores_if_jira_issue_is_not_readable(
329329
"Handling incoming request",
330330
"Ignore incoming request: ignore unreadable issue JBI-234",
331331
]
332+
333+
334+
def test_runner_ignores_request_if_jira_is_linked_but_without_whiteboard(
335+
webhook_comment_example: BugzillaWebhookRequest,
336+
actions_example: Actions,
337+
settings: Settings,
338+
mocked_bugzilla,
339+
):
340+
mocked_bugzilla.get_bug.return_value = webhook_comment_example.bug
341+
webhook_comment_example.bug.whiteboard = "[not-matching-local-config]"
342+
343+
assert webhook_comment_example.bug.extract_from_see_also() is not None
344+
345+
with pytest.raises(IgnoreInvalidRequestError) as exc_info:
346+
execute_action(
347+
request=webhook_comment_example,
348+
actions=actions_example,
349+
settings=settings,
350+
)
351+
352+
assert str(exc_info.value) == "no bug whiteboard matching action tags: devtest"

0 commit comments

Comments
 (0)