Skip to content

Commit

Permalink
Merge pull request #112 from release-engineering/add-issue-links-to-pr
Browse files Browse the repository at this point in the history
Add issue links to pr
  • Loading branch information
Sid Premkumar committed May 29, 2020
2 parents 76d2669 + 982e1ca commit bc14594
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 16 deletions.
8 changes: 4 additions & 4 deletions sync2jira/downstream_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,13 @@ def _get_existing_jira_issue_legacy(client, issue, config):
return None


def _attach_link(client, downstream, remote_link):
def attach_link(client, downstream, remote_link):
"""
Attaches the upstream link to the JIRA ticket.
:param jira.client.JIRA client: JIRA client
:param jira.resources.Issue downstream: Response from creating the JIRA ticket
:param str remote_link: Remote link
:param dict remote_link: Remote link dict with {'url': ... , 'title': ... }
:return: downstream: Response from creating the JIRA ticket
:rtype: jira.resources.Issue
"""
Expand Down Expand Up @@ -477,7 +477,7 @@ def _upgrade_jira_issue(client, downstream, issue, config):

# Do it!
remote_link = dict(url=issue.url, title=remote_link_title)
_attach_link(client, downstream, remote_link)
attach_link(client, downstream, remote_link)


def assign_user(client, issue, downstream, remove_all=False):
Expand Down Expand Up @@ -669,7 +669,7 @@ def _create_jira_issue(client, issue, config):
confluence_data['Misc. Fields'] = 1

remote_link = dict(url=issue.url, title=remote_link_title)
_attach_link(client, downstream, remote_link)
attach_link(client, downstream, remote_link)

default_status = issue.downstream.get('default_status', None)
if default_status is not None:
Expand Down
32 changes: 26 additions & 6 deletions sync2jira/downstream_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,26 @@ def format_comment(pr, pr_suffix, client):
return comment


def issue_link_exists(client, existing, pr):
"""
Checks if we've already linked this PR
:param jira.client.JIRA client: JIRA Client
:param jira.resources.Issue existing: Existing JIRA issue that was found
:param sync2jira.intermediary.PR pr: Upstream issue we're pulling data from
:returns: True/False if the issue exists/does not exists
"""
# Query for our issue
for issue_link in client.remote_links(existing):
if issue_link.object.url == pr.url:
# Issue has already been linked
return True
return False


def comment_exists(client, existing, new_comment):
"""
Checks if new_comment exists in existing
:param jira.client.JIRA client: JIRA Client
:param jira.resources.Issue existing: Existing JIRA issue that was found
:param String new_comment: Formatted comment we're looking for
Expand All @@ -78,7 +94,6 @@ def comment_exists(client, existing, new_comment):
if new_comment == comment.body:
# If the comment was
return True

return False


Expand All @@ -96,12 +111,17 @@ def update_jira_issue(existing, pr, client):

# Format and add comment to indicate PR has been linked
new_comment = format_comment(pr, pr.suffix, client)
# See if the comment exists
exists = comment_exists(client, existing, new_comment)
# See if the issue_link and comment exists
exists = issue_link_exists(client, existing, pr)
comment_exist = comment_exists(client, existing, new_comment)
# Check if the comment if already there
if not exists:
log.info(f"Added comment for PR {pr.title} on JIRA {pr.jira_key}")
client.add_comment(existing, new_comment)
if not comment_exist:
log.info(f"Added comment for PR {pr.title} on JIRA {pr.jira_key}")
client.add_comment(existing, new_comment)
# Attach remote link
remote_link = dict(url=pr.url, title=f"[PR] {pr.title}")
d_issue.attach_link(client, existing, remote_link)
if confluence_client.update_stat:
confluence_data = {'Comments': 1}
confluence_client.update_stat_page(confluence_data)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_downstream_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def test_assign_user_remove_all(self, mock_client):

@mock.patch(PATH + 'confluence_client')
@mock.patch(PATH + '_update_jira_issue')
@mock.patch(PATH + '_attach_link')
@mock.patch(PATH + 'attach_link')
@mock.patch('jira.client.JIRA')
def test_create_jira_issue(self,
mock_client,
Expand Down Expand Up @@ -389,7 +389,7 @@ def test_create_jira_issue(self,

@mock.patch(PATH + 'confluence_client')
@mock.patch(PATH + '_update_jira_issue')
@mock.patch(PATH + '_attach_link')
@mock.patch(PATH + 'attach_link')
@mock.patch('jira.client.JIRA')
def test_create_jira_issue_failed_epic_link(self,
mock_client,
Expand Down Expand Up @@ -449,7 +449,7 @@ def test_create_jira_issue_failed_epic_link(self,

@mock.patch(PATH + 'confluence_client')
@mock.patch(PATH + '_update_jira_issue')
@mock.patch(PATH + '_attach_link')
@mock.patch(PATH + 'attach_link')
@mock.patch('jira.client.JIRA')
def test_create_jira_issue_failed_exd_service(self,
mock_client,
Expand Down Expand Up @@ -512,7 +512,7 @@ def test_create_jira_issue_failed_exd_service(self,

@mock.patch(PATH + 'confluence_client')
@mock.patch(PATH + '_update_jira_issue')
@mock.patch(PATH + '_attach_link')
@mock.patch(PATH + 'attach_link')
@mock.patch('jira.client.JIRA')
def test_create_jira_issue_no_updates(self,
mock_client,
Expand Down
50 changes: 48 additions & 2 deletions tests/test_downstream_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ def test_sync_with_jira_testing(self,
@mock.patch(PATH + 'confluence_client')
@mock.patch(PATH + 'comment_exists')
@mock.patch(PATH + 'format_comment')
@mock.patch(PATH + 'd_issue.attach_link')
@mock.patch(PATH + 'issue_link_exists')
def test_update_jira_issue_link(self,
mock_issue_link_exists,
mock_attach_link,
mock_format_comment,
mock_comment_exists,
mock_confluence_client):
Expand All @@ -157,6 +161,7 @@ def test_update_jira_issue_link(self,
# Set up return values
mock_format_comment.return_value = 'mock_formatted_comment'
mock_comment_exists.return_value = False
mock_issue_link_exists.return_value = False
mock_confluence_client.update_stat = True

# Call the function
Expand All @@ -167,19 +172,57 @@ def test_update_jira_issue_link(self,
mock_format_comment.assert_called_with(self.mock_pr, self.mock_pr.suffix, self.mock_client)
mock_comment_exists.assert_called_with(self.mock_client, 'mock_existing', 'mock_formatted_comment')
mock_confluence_client.update_stat_page.assert_called_with({'Comments': 1})
mock_attach_link.assert_called_with(self.mock_client, 'mock_existing', {'url': 'mock_url', 'title': '[PR] mock_title'})

def test_issue_link_exists_false(self):
"""
This function tests 'issue_link_exists' where it does not exist
"""
# Set up return values
mock_issue_link = MagicMock()
mock_issue_link.object.url = 'bad_url'
self.mock_client.remote_links.return_value = [mock_issue_link]

# Call the function
ret = d.issue_link_exists(self.mock_client, self.mock_existing, self.mock_pr)

# Assert everything was called correctly
self.mock_client.remote_links.assert_called_with(self.mock_existing)
self.assertEqual(ret, False)

def test_issue_link_exists_true(self):
"""
This function tests 'issue_link_exists' where it does exist
"""
# Set up return values
mock_issue_link = MagicMock()
mock_issue_link.object.url = self.mock_pr.url
self.mock_client.remote_links.return_value = [mock_issue_link]

# Call the function
ret = d.issue_link_exists(self.mock_client, self.mock_existing, self.mock_pr)

# Assert everything was called correctly
self.mock_client.remote_links.assert_called_with(self.mock_existing)
self.assertEqual(ret, True)

@mock.patch(PATH + 'comment_exists')
@mock.patch(PATH + 'format_comment')
@mock.patch(PATH + 'comment_exists')
@mock.patch(PATH + 'd_issue.attach_link')
@mock.patch(PATH + 'issue_link_exists')
def test_update_jira_issue_exists(self,
mock_issue_link_exists,
mock_attach_link,
mock_comment_exists,
mock_format_comment,
mock_comment_exists):
):
"""
This function tests 'update_jira_issue' where the comment already exists
"""
# Set up return values
mock_format_comment.return_value = 'mock_formatted_comment'
mock_comment_exists.return_value = True
mock_issue_link_exists.return_value = True

# Call the function
d.update_jira_issue('mock_existing', self.mock_pr, self.mock_client)
Expand All @@ -188,6 +231,9 @@ def test_update_jira_issue_exists(self,
self.mock_client.add_comment.assert_not_called()
mock_format_comment.assert_called_with(self.mock_pr, self.mock_pr.suffix, self.mock_client)
mock_comment_exists.assert_called_with(self.mock_client, 'mock_existing', 'mock_formatted_comment')
mock_attach_link.assert_not_called()
mock_issue_link_exists.assert_called_with(self.mock_client, 'mock_existing', self.mock_pr)


def test_comment_exists_false(self):
"""
Expand Down

0 comments on commit bc14594

Please sign in to comment.