#78 Regex description matching
Merged 4 years ago by sidpremkumar. Opened 4 years ago by sidpremkumar.
sidpremkumar/sync-to-jira fix-for-description  into  develop

file modified
+76 -28
@@ -627,9 +627,7 @@ 

      if 'title' in updates:

          # Update the title if needed

          if issue.title != existing.fields.summary:

-             data = {'summary': issue.title}

-             existing.update(data)

-             log.info('   Updated title')

+             _update_title(issue, existing)

  

      # Only synchronize transition (status) for listings that op-in

      if any('transition' in item for item in updates):
@@ -703,6 +701,53 @@ 

              log.warning('   Could not update JIRA closed_status for %s' % issue.title)

  

  

+ def _update_title(issue, existing):

+     """

+     Helper function to sync upstream/downstream title

+     Args:

+         issue (sync2jira.intermediary.Issue): Upstream issue

+         existing (jira.resource.Issue): Existing JIRA issue

+     Returns:

+         Nothing

+     """

+     new_description = existing.fields.description

+     if not new_description:

+         new_description = ''

+     if '] Upstream Reporter:' not in new_description:

+         # We have to add the issue ID to the description so we can find it again

+         if '] Upstream issue status:' in new_description:

+             # We have to use regex to update the upstream reporter

+             today = datetime.today()

+             new_description = re.sub(

+                 r'\[[\w\W]*\] Upstream issue status: %s' % issue.status,

+                 '[%s] Upstream issue status: %s\n[%s] Upstream Reporter: %s'

+                 % (today.strftime("%a %b %y - %H:%M"),

+                    issue.status, issue.id, issue.reporter['fullname']),

+                 new_description)

+         else:

+             # We can just add it to the top

+             new_description = '[%s] Upstream Repoter: %s' % \

+                               (issue.id, issue.reporter['fullname']) + new_description

+         # Update the description

+         # Now that we've updated the description (i.e. added

+         # issue.id) we can delete the link in the description if its still there.

+         new_description = re.sub(

+             r'%s' % issue.url,

+             r'',

+             new_description

+         )

+ 

+         # Now we can update the JIRA issue if we need to

+         if new_description != existing.fields.description:

+             data = {'description': new_description}

+             existing.update(data)

+             log.info('   Updated description')

+     # Then we can update the title

+     data = {'summary': issue.title}

+     existing.update(data)

+     log.info('   Updated title')

+ 

+ 

  def _update_comments(client, existing, issue):

      """

      Helper function to sync comments between existing JIRA issue and upstream issue
@@ -844,6 +889,8 @@ 

  

  def _update_description(existing, issue):

      new_description = existing.fields.description

+     if not new_description:

+         new_description = ''

      if 'Upstream description' in new_description:

          # If we just need to update the content of the description

          new_description = re.sub(
@@ -852,31 +899,32 @@ 

              new_description)

      elif '] Upstream issue status:' in new_description and '] Upstream Reporter:' in new_description:

          # We need to add a description field

-         # Need to find the 2nd new line as we already have

-         # Reporter and Status

-         index1 = new_description.find('\n')

-         index2 = new_description[index1+1:].find('\n')

-         new_description = "%s \nUpstream description: " \

-                           "{quote}%s{quote}\n%s" % \

-                           (new_description[:index1+index2+1],

-                            issue.content,

-                            new_description[index1+index2+1:])

- 

-     elif '] Upstream issue status:' in new_description:

+         today = datetime.today()

+         new_description = re.sub(r'\[[\w\W]*\] Upstream issue status: %s\n\[%s\] Upstream Reporter: %s' % (

+             issue.status, issue.id, issue.reporter['fullname']),

+                                  r'[%s] Upstream issue status: %s\n[%s] Upstream Reporter: %s\n'

+                                  r'Upstream description: {quote}%s{quote}' % (

+                                      today.strftime("%a %b %y - %H:%M"), issue.status, issue.id,

+                                      issue.reporter['fullname'],

+                                      issue.content),

+                                  new_description)

+ 

+     elif '] Upstream issue status:' in new_description and '] Upstream Reporter:' not in new_description:

          # We need to add a upstream reporter and description field

-         upstream_reporter = '[%s] Upstream Reporter: %s' % (

-             issue.id,

-             issue.reporter['fullname']

-         )

-         upstream_description = "%s \nUpstream description: " \

-                                "{quote}%s{quote}" % \

-                                (upstream_reporter, issue.content)

-         # Add it to the old description

-         index = new_description.find('\n')

-         new_description = (new_description[:index] +

-                            '\n' +

-                            upstream_description +

-                            new_description[index+1:])

+         today = datetime.today()

+         new_description = re.sub(r'\[[\w\W]*\] Upstream issue status: %s' % issue.status,

+                                  r'[%s] Upstream issue status: %s\n'

+                                  r'[%s] Upstream Reporter: %s\n'

+                                  r'Upstream description: {quote}%s{quote}' %

+                                  (today.strftime("%a %b %y - %H:%M"), issue.status, issue.id,

+                                   issue.reporter['fullname'], issue.content),

+                                  new_description)

+     elif '] Upstream issue status:' not in new_description and '] Upstream Reporter:' in new_description:

+         # We need to just add the description field

+         new_description = re.sub(

+             r'\[%s\] Upstream Reporter: %s [\w\W]*' % (issue.id, issue.reporter['fullname']),

+             r'[%s] Upstream Reporter: %s \nUpstream description: {quote} %s {quote}' %

+             (issue.id, issue.reporter['fullname'], issue.content), new_description)

      else:

          # Just add reporter and description to the top

          upstream_reporter = '[%s] Upstream Reporter: %s' % (
@@ -891,7 +939,7 @@ 

      # Now that we've updated the description (i.e. added

      # issue.id) we can delete the link in the description if its still there.

      new_description = re.sub(

-         r'\[%s\]' % issue.url,

+         r'%s' % issue.url,

          r'',

          new_description

      )

file modified
+24 -9
@@ -832,12 +832,18 @@ 

              {'description': '[123] Upstream Reporter: mock_user \n'

                              'Upstream description: {quote}mock_content{quote}'})

  

-     def test_update_description_add_reporter(self):

+     @mock.patch(PATH + 'datetime')

+     def test_update_description_add_reporter(self,

+                                              mock_datetime):

          """

          This function tests '_update_description' where we have to add a description and upstream reporter field

          """

          # Set up return values

          self.mock_downstream.fields.description = '[123] Upstream issue status: Open\n'

+         self.mock_issue.status = 'Open'

+         self.mock_issue.id = '123'

+         self.mock_issue.reporter = {'fullname': 'mock_user'}

+         mock_datetime.today.return_value = self.mock_today

  

          # Call the function

          d._update_description(
@@ -845,7 +851,10 @@ 

              issue=self.mock_issue

          )

          # Assert all calls were made correctly

-         self.mock_downstream.update.assert_called_with({'description': '[123] Upstream issue status: Open\n[1234] Upstream Reporter: mock_user \nUpstream description: {quote}mock_content{quote}'})

+         self.mock_downstream.update.assert_called_with(

+             {'description': '[mock_today] Upstream issue status: Open\n[123]'

+                             ' Upstream Reporter: mock_user\nUpstream description:'

+                             ' {quote}mock_content{quote}\n'})

  

      def test_update_description_add_reporter_no_status(self):

          """
@@ -865,13 +874,19 @@ 

              {'description': '[1234] Upstream Reporter: mock_user \n'

                              'Upstream description: {quote}mock_content{quote} \n '})

  

-     def test_update_description_add_description(self):

+     @mock.patch(PATH + 'datetime')

+     def test_update_description_add_description(self,

+                                                 mock_datetime):

          """

          This function tests '_update_description' where we have a reporter and status already

          """

          # Set up return values

          self.mock_downstream.fields.description = '[123] Upstream issue status: Open\n' \

                                                    '[123] Upstream Reporter: mock_user\n'

+         self.mock_issue.status = 'Open'

+         self.mock_issue.id = '123'

+         self.mock_issue.reporter = {'fullname': 'mock_user'}

+         mock_datetime.today.return_value = self.mock_today

  

          # Call the function

          d._update_description(
@@ -881,9 +896,9 @@ 

  

          # Assert all calls were made correctly

          self.mock_downstream.update.assert_called_with(

-             {'description': '[123] Upstream issue status: Open\n'

-                             '[123] Upstream Reporter: mock_user \n'

-                             'Upstream description: {quote}mock_content{quote}\n\n'})

+             {'description': '[mock_today] Upstream issue status: Open\n'

+                             '[123] Upstream Reporter: mock_user\n'

+                             'Upstream description: {quote}mock_content{quote}\n'})

  

      def test_verify_tags(self):

          """
@@ -1168,8 +1183,8 @@ 

  

  

      @mock.patch('jira.client.JIRA')

-     def check_comments_for_duplicates(self,

-                                       mock_client):

+     def test_check_comments_for_duplicates(self,

+                                            mock_client):

          """

          Tests 'check_comments_for_duplicates' function

          """
@@ -1182,7 +1197,7 @@ 

  

          # Call the function

          response = d.check_comments_for_duplicate(

-             client=self.mock_client,

+             client=mock_client,

              result=self.mock_downstream,

              username='mock_user'

          )

Updated description matching to be all regex

2 new commits added

  • adding regex matches to all description updates
  • adding regex matches to all description updates
4 years ago

2 new commits added

  • adding regex matches to all description updates
  • adding another catch for updating descrtiption
4 years ago

1 new commit added

  • Adding regex and upstream reporter to title update
4 years ago

Pull-Request has been merged by sidpremkumar

4 years ago