Skip to content

Commit 70efc52

Browse files
webbnhralphbean
authored andcommitted
Reduce closed-event logging; code clean-up
1 parent ff86694 commit 70efc52

File tree

2 files changed

+71
-21
lines changed

2 files changed

+71
-21
lines changed

sync2jira/downstream_issue.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,7 @@ def _upgrade_jira_issue(client, downstream, issue, config):
369369
attach_link(client, downstream, remote_link)
370370

371371

372-
def match_user(
373-
emails: list[str], client: jira.client, downstream: jira.Issue
374-
) -> Optional[str]:
372+
def match_user(emails: list[str], client: jira.client.JIRA) -> Optional[str]:
375373
"""Match an upstream user to an assignable downstream user and return the
376374
downstream username; return None on failure.
377375
"""
@@ -410,7 +408,7 @@ def match_user(
410408

411409

412410
def assign_user(
413-
client: jira.client, issue: Issue, downstream: jira.Issue, remove_all=False
411+
client: jira.client.JIRA, issue: Issue, downstream: jira.Issue, remove_all=False
414412
):
415413
"""
416414
Attempts to assign a JIRA issue to the correct
@@ -440,7 +438,7 @@ def assign_user(
440438
continue
441439

442440
# Try to match the upstream assignee's emails to a Jira user
443-
match_name = match_user(emails, client, downstream)
441+
match_name = match_user(emails, client)
444442
if match_name:
445443
# Assign the downstream issue to the matched user
446444
downstream.update({"assignee": {"name": match_name}})
@@ -762,8 +760,10 @@ def _update_jira_issue(existing, issue, client, config):
762760
_update_transition(client, existing, issue)
763761

764762
# Only execute 'on_close' events for listings that opt-in
765-
log.info("Attempting to update downstream issue on upstream closed event")
766-
_update_on_close(existing, issue, updates)
763+
# and when the issue is closed.
764+
if issue.status == "Closed":
765+
log.info("Attempting to update downstream issue on upstream closed event")
766+
_update_on_close(existing, issue, updates)
767767

768768
log.info("Done updating %s!", issue.url)
769769

@@ -1133,23 +1133,14 @@ def _update_on_close(existing, issue, updates: list[UPDATE_ENTRY]):
11331133
"""
11341134
for item in updates:
11351135
if isinstance(item, dict):
1136-
if on_close_updates := item.get("on_close"):
1136+
if new_labels := item.get("on_close", {}).get("apply_labels", []):
11371137
break
11381138
else:
1139-
on_close_updates = None
1140-
1141-
if not on_close_updates:
1142-
return
1143-
1144-
if issue.status != "Closed":
1145-
return
1146-
1147-
if "apply_labels" not in on_close_updates:
11481139
return
11491140

1150-
log.info("Applying 'on_close' labels to downstream Jira issue")
11511141
existing_labels = set(existing.fields.labels)
1152-
updated_labels = existing_labels.union(set(on_close_updates["apply_labels"]))
1142+
updated_labels = existing_labels.union(new_labels)
1143+
log.info("Applying 'on_close' labels %s to downstream Jira issue", updated_labels)
11531144
_update_jira_labels(existing, list(updated_labels))
11541145

11551146

tests/test_downstream_issue.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ def test_sync_with_jira_no_matching(
810810
@mock.patch(PATH + "_update_assignee")
811811
@mock.patch(PATH + "_update_on_close")
812812
@mock.patch("jira.client.JIRA")
813-
def test_update_jira_issue(
813+
def test_update_jira_issue_closed(
814814
self,
815815
mock_client,
816816
mock_update_on_close,
@@ -823,8 +823,11 @@ def test_update_jira_issue(
823823
mock_update_title,
824824
):
825825
"""
826-
This tests '_update_jira_issue' function
826+
This tests '_update_jira_issue' function when the issue is closed
827827
"""
828+
829+
self.mock_issue.status = "Closed"
830+
828831
# Call the function
829832
d._update_jira_issue(
830833
existing=self.mock_downstream,
@@ -846,6 +849,7 @@ def test_update_jira_issue(
846849
self.mock_issue,
847850
mock_client,
848851
)
852+
mock_update_assignee.assert_called_once()
849853
mock_update_description.assert_called_with(
850854
self.mock_downstream, self.mock_issue
851855
)
@@ -855,6 +859,61 @@ def test_update_jira_issue(
855859
)
856860
mock_update_on_close.assert_called_once()
857861

862+
@mock.patch(PATH + "_update_title")
863+
@mock.patch(PATH + "_update_description")
864+
@mock.patch(PATH + "_update_comments")
865+
@mock.patch(PATH + "_update_tags")
866+
@mock.patch(PATH + "_update_fixVersion")
867+
@mock.patch(PATH + "_update_transition")
868+
@mock.patch(PATH + "_update_assignee")
869+
@mock.patch(PATH + "_update_on_close")
870+
@mock.patch("jira.client.JIRA")
871+
def test_update_jira_issue_open(
872+
self,
873+
mock_client,
874+
mock_update_on_close,
875+
mock_update_assignee,
876+
mock_update_transition,
877+
mock_update_fixVersion,
878+
mock_update_tags,
879+
mock_update_comments,
880+
mock_update_description,
881+
mock_update_title,
882+
):
883+
"""
884+
This tests '_update_jira_issue' function when the issue is not closed
885+
"""
886+
# Call the function
887+
d._update_jira_issue(
888+
existing=self.mock_downstream,
889+
issue=self.mock_issue,
890+
client=mock_client,
891+
config=self.mock_config,
892+
)
893+
894+
# Assert all calls were made correctly
895+
mock_update_comments.assert_called_with(
896+
mock_client, self.mock_downstream, self.mock_issue
897+
)
898+
mock_update_tags.assert_called_with(
899+
self.mock_updates, self.mock_downstream, self.mock_issue
900+
)
901+
mock_update_fixVersion.assert_called_with(
902+
self.mock_updates,
903+
self.mock_downstream,
904+
self.mock_issue,
905+
mock_client,
906+
)
907+
mock_update_assignee.assert_called_once()
908+
mock_update_description.assert_called_with(
909+
self.mock_downstream, self.mock_issue
910+
)
911+
mock_update_title.assert_called_with(self.mock_issue, self.mock_downstream)
912+
mock_update_transition.assert_called_with(
913+
mock_client, self.mock_downstream, self.mock_issue
914+
)
915+
mock_update_on_close.assert_not_called()
916+
858917
@mock.patch("jira.client.JIRA")
859918
def test_update_transition_JIRAError(self, mock_client):
860919
"""

0 commit comments

Comments
 (0)