From 6060b99456678c59b1ecaa47e4b3969456d534c1 Mon Sep 17 00:00:00 2001 From: sidpremkumar Date: Jul 18 2019 19:24:49 +0000 Subject: Adding overwrite field to assignee --- diff --git a/README.rst b/README.rst index 14eb2dc..04c0662 100644 --- a/README.rst +++ b/README.rst @@ -35,7 +35,8 @@ JIRA issues:: {'tags': {'overwrite': True/False}} :: Sync tags, do/don't overwrite downstream tags {'fixVersion'; {'overwrite': True/False}} :: Sync fixVersion (downstream milestone), do/don't overwrite downstream fixVersion - 'assignee' :: Sync assignee (for Github only the first assignee will sync) + {'assignee': {'overwrite': True/False}} :: Sync assignee (for Github only the first assignee will sync) + do/don't overwrite downstream assignee 'description' :: Sync description 'title' :: Sync title {'transition': True/'CUSTOM_TRANSITION'} :: Sync status (open/closed), Sync only status/ diff --git a/sync2jira/downstream.py b/sync2jira/downstream.py index 8939529..cf86dd7 100644 --- a/sync2jira/downstream.py +++ b/sync2jira/downstream.py @@ -612,9 +612,9 @@ def _update_jira_issue(existing, issue, client): _update_fixVersion(updates, existing, issue) # Only synchronize assignee for listings that op-in - if 'assignee' in updates: + if any('assignee' in item for item in updates): log.info(" Looking for new assignee(s)") - _update_assignee(client, existing, issue) + _update_assignee(client, existing, issue, updates) # Only synchronize descriptions for listings that op-in if 'description' in updates: @@ -825,33 +825,53 @@ def _update_fixVersion(updates, existing, issue): log.warning(' Error updating the fixVersion. %s is an invalid fixVersion.' % issue.fixVersion) -def _update_assignee(client, existing, issue): +def _update_assignee(client, existing, issue, updates): """ Helper function update existing JIRA assignee from downstream issue Args: client (jira.client.JIRA): JIRA client existing (jira.resource.Issue): Existing JIRA issue issue (sync2jira.intermediary.Issue): Upstream issue + updates (list): Downstream updates requested by the user Returns: Nothing """ - # Update the assignee + # First check if overwrite is set to True + try: + # For python 3 > + overwrite = bool(list(filter(lambda d: "assignee" in d, updates))[0]['assignee']['overwrite']) + except ValueError: + # for python 2.7 + overwrite = bool((filter(lambda d: "assignee" in d, updates))[0]['assignee']['overwrite']) + + # First check if the issue is already assigned to the same person + update = False if issue.assignee and issue.assignee[0]: - # Check if the issue is already assigned to reduce API calls try: update = issue.assignee[0]['fullname'] != existing.fields.assignee.displayName except AttributeError: update = True + + if not overwrite: + # Only assign if the existing JIRA issue doesn't have an assignee + # And the issue has an assignee + if not existing.fields.assignee and issue.assignee: + if issue.assignee[0]: + # Update the assignee + assign_user(client, issue, existing) + log.info(' Updated assignee') + return + else: + # Update the assignee if we have someone to assignee it too if update: - # If we have someone to assign it too assign_user(client, issue, existing) log.info(' Updated assignee') - else: - if existing.fields.assignee: - # Else we should remove all assignees - # Set removeAll flag to true - assign_user(client, issue, existing, remove_all=True) - log.info(' Updated assignee') + else: + if existing.fields.assignee: + # Else we should remove all assignees + # Set removeAll flag to true + assign_user(client, issue, existing, remove_all=True) + log.info(' Updated assignee') def _update_tags(updates, existing, issue): diff --git a/tests/test_downstream.py b/tests/test_downstream.py index 1659880..7e9a928 100644 --- a/tests/test_downstream.py +++ b/tests/test_downstream.py @@ -51,7 +51,7 @@ class TestDownstream(unittest.TestCase): 'comments', {'tags': {'overwrite': False}}, {'fixVersion': {'overwrite': False}}, - 'assignee', 'description', 'title', + {'assignee': {'overwrite': True}}, 'description', 'title', {'transition': 'CUSTOM TRANSITION'} ], 'owner': 'mock_owner' @@ -73,7 +73,7 @@ class TestDownstream(unittest.TestCase): 'comments', {'tags': {'overwrite': False}}, {'fixVersion': {'overwrite': False}}, - 'assignee', 'description', 'title', + {'assignee': {'overwrite': True}}, 'description', 'title', {'transition': 'CUSTOM TRANSITION'}, ] @@ -705,7 +705,8 @@ class TestDownstream(unittest.TestCase): d._update_assignee( client=mock_client, existing=self.mock_downstream, - issue=self.mock_issue + issue=self.mock_issue, + updates=[{'assignee': {'overwrite': True}}] ) # Assert all calls were made correctly @@ -730,7 +731,8 @@ class TestDownstream(unittest.TestCase): d._update_assignee( client=mock_client, existing=self.mock_downstream, - issue=self.mock_issue + issue=self.mock_issue, + updates=[{'assignee': {'overwrite': True}}] ) # Assert all calls were made correctly @@ -741,6 +743,33 @@ class TestDownstream(unittest.TestCase): remove_all=True ) + @mock.patch(PATH + 'assign_user') + @mock.patch('jira.client.JIRA') + def test_update_assignee_no_overwrite(self, + mock_client, + mock_assign_user): + """ + This function tests the '_update_assignee' function where overwrite is false + """ + # Set up return values + self.mock_downstream.fields.assignee = None + + # Call the function + d._update_assignee( + client=mock_client, + existing=self.mock_downstream, + issue=self.mock_issue, + updates=[{'assignee': {'overwrite': False}}] + ) + + # Assert all calls were made correctly + mock_assign_user.assert_called_with( + mock_client, + self.mock_issue, + self.mock_downstream + ) + + @mock.patch(PATH + 'verify_tags') @mock.patch(PATH + '_label_matching') def test_update_tags(self,