From 45b4f208e85fa417721fe07ffc1e317000b46caf Mon Sep 17 00:00:00 2001 From: sidpremkumar Date: Nov 18 2019 14:12:36 +0000 Subject: Add url field to updates array --- diff --git a/README.rst b/README.rst index 0503fff..91809ef 100644 --- a/README.rst +++ b/README.rst @@ -50,6 +50,7 @@ JIRA issues:: 'github_markdown' :: If description syncing is turned on, this flag will convert Github markdown to plaintext. 'upstream_id' :: This flag will add a comment indicating the upstream issue id when an issue is created, allowing the user to search for the issue downstream via the upstream ID. + 'url' :: This flag will add the upstream url to the bottom of the JIRA ticket Note: Overwrite set to True will ensure that upstream issue fields will clear downstream issue fields, overwrite set to False will never delete downstream issue fields only append. diff --git a/docs/source/config-file.rst b/docs/source/config-file.rst index d012b53..14e4e3f 100644 --- a/docs/source/config-file.rst +++ b/docs/source/config-file.rst @@ -106,8 +106,10 @@ getting a JIRA client and failure email will be sent anytime the service fails. * If description syncing is turned on, this flag will convert Github markdown to plaintext. This uses the pypandoc module. * :code:`upstream_id` * If selected this will add a comment to all newly created JIRA issue in the format 'UPSTREAM_PROJECT-#1' where the number indicates the issue ID. This allows users to search for the issue on JIRA via the issue number. + * :code:`url` + * This flag will add the upstream url to the bottom of the JIRA ticket - .. note:: + .. note:: :Overwrite: Setting this to :code:`True` will ensure that Upstream (GitHub or Pagure) values will overwrite downstream ones (i.e. if its empty upstream it'll be empty downstream) :CUSTOM_TRANSITION: Setting this value will get Sync2Jira to automatially transition downstream tickets once their upstream counterparts get closed. Set this to whatever 'closed' means downstream. diff --git a/sync2jira/downstream.py b/sync2jira/downstream.py index 595bf87..e47f305 100644 --- a/sync2jira/downstream.py +++ b/sync2jira/downstream.py @@ -576,6 +576,10 @@ def _create_jira_issue(client, issue, config): description ) + # Add the url if requested + if 'url' in issue.downstream.get('updates', {}): + description = description + f"\nUpstream URL: {issue.url}" + kwargs = dict( summary=issue.title, description=description, @@ -708,6 +712,11 @@ def _update_jira_issue(existing, issue, client): log.info(" Looking for new title") _update_title(issue, existing) + # Only synchronize url for listings that op-in + if 'url' in updates: + log.info(" Looking for new url") + _update_url(existing, issue) + # Only synchronize transition (status) for listings that op-in if any('transition' in item for item in updates): log.info(" Looking for new transition(s)") @@ -716,6 +725,29 @@ def _update_jira_issue(existing, issue, client): log.info(' Done updating %s!' % issue.title) +def _update_url(existing, issue): + """ + Helper function to update the transition of a downstream JIRA issue. + + :param jira.resource.Issue existing: Existing JIRA issue + :param sync2jira.intermediary.Issue issue: Upstream issue + :returns: Nothing + """ + # First check if the url needs to be updated + if issue.url in existing.fields.description: + # There is nothing to update + return + + # Else add the url to the bottom of the description + new_description = f"Upstream URL: {issue.url}\n" + new_description = existing.fields.description + "\n" + new_description + + # Update our issue + data = {'description': new_description} + existing.update(data) + log.info(' Updated description') + + def _update_transition(client, existing, issue): """ Helper function to update the transition of a downstream JIRA issue. @@ -1045,11 +1077,12 @@ def _update_description(existing, issue): (upstream_description, new_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 - ) + if 'url' not in issue.downstream.get('updates', {}): + 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: diff --git a/tests/test_downstream.py b/tests/test_downstream.py index 550d05e..6fbcfab 100644 --- a/tests/test_downstream.py +++ b/tests/test_downstream.py @@ -1466,3 +1466,31 @@ class TestDownstream(unittest.TestCase): # Assert everything was called correctly self.assertEqual(response, True) mock_jira_client.search_issues.assert_called_with("issueFunction in linkedIssuesOfRemote('*')") + + def test_update_url_no_update(self): + """ + This function tests '_update_url' where we already have the URL + """ + # Set up return values + self.mock_downstream.fields.description = self.mock_issue.url + + # Call the function + d._update_url(self.mock_downstream, self.mock_issue) + + # Assert everything was called correctly + self.mock_downstream.update.assert_not_called() + + def test_update_url_update(self): + """ + This function tests '_update_url' where we already have the URL + """ + # Set up return values + self.mock_downstream.fields.description = "" + + # Call the function + d._update_url(self.mock_downstream, self.mock_issue) + + # Assert everything was called correctly + self.mock_downstream.update.assert_called_with( + {'description': + f"\nUpstream URL: {self.mock_issue.url}\n"})