#84 Adding overwrite field to assignee
Merged 4 years ago by sidpremkumar. Opened 4 years ago by sidpremkumar.
sidpremkumar/sync-to-jira better-assignee  into  develop

Adding overwrite field to assignee
sidpremkumar • 4 years ago  
file modified
+2 -1
@@ -35,7 +35,8 @@ 

      {'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/

file modified
+32 -12
@@ -612,9 +612,9 @@ 

          _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 @@ 

              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):

file modified
+33 -4
@@ -51,7 +51,7 @@ 

                  '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 @@ 

                  'comments',

                  {'tags': {'overwrite': False}},

                  {'fixVersion': {'overwrite': False}},

-                 'assignee', 'description', 'title',

+                 {'assignee': {'overwrite': True}}, 'description', 'title',

                  {'transition': 'CUSTOM TRANSITION'},

              ]

  
@@ -705,7 +705,8 @@ 

          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 @@ 

          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 @@ 

              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,

no initial comment

Pull-Request has been merged by sidpremkumar

4 years ago