Skip to content

Commit

Permalink
Support nested filters, including milestones
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphbean committed Apr 11, 2024
1 parent 87864d0 commit a957682
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
40 changes: 26 additions & 14 deletions sync2jira/upstream_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,9 @@ def handle_github_message(msg, config, pr_filter=True):
.get('github', {})\
.get(upstream, {})

for key, expected in _filter.items():
# special handling for label: we look for it in the list of msg labels
if key == 'labels':
actual = [label['name'] for label in msg['msg']['issue']['labels']]
if expected not in actual:
log.debug("Label %s not set on issue: %s", expected, upstream)
return None
else:
# direct comparison
actual = msg['msg']['issue'].get(key)
if actual != expected:
log.debug("Actual %r %r != expected %r on issue %s",
key, actual, expected, upstream)
return None
issue = msg['msg']['issue']
if not _github_filter_matches(issue, _filter, slug=upstream):
return None

if pr_filter and 'pull_request' in msg['msg']['issue']:
if not msg['msg']['issue'].get('closed_at', None):
Expand Down Expand Up @@ -145,6 +134,29 @@ def handle_github_message(msg, config, pr_filter=True):
return i.Issue.from_github(upstream, msg['msg']['issue'], config)


def _github_filter_matches(issue, _filter, slug):
for key, expected in _filter.items():

if isinstance(expected, dict):
if not _github_filter_matches(issue.get(key), expected, slug):
return False
elif key == 'labels':
# special handling for label: we look for it in the list of msg labels
actual = [label['name'] for label in issue['labels']]
if expected not in actual:
log.debug("Label %s not set on issue: %s", expected, slug)
return False
else:
# direct comparison
actual = issue.get(key)
if actual != expected:
log.debug("Actual %r %r != expected %r on issue %s",
key, actual, expected, slug)
return False

return True


def handle_pagure_message(msg, config):
"""
Handle Pagure message from FedMsg.
Expand Down
35 changes: 35 additions & 0 deletions tests/test_upstream_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,41 @@ def test_handle_github_message_bad_label(self,
mock_issue_from_github.assert_not_called()
self.assertEqual(None, response)

@mock.patch(PATH + 'Github')
@mock.patch('sync2jira.intermediary.Issue.from_github')
def test_handle_github_message_with_milestone_filter(self,
mock_issue_from_github,
mock_github):
"""
This function tests 'handle_github_message' where a milestone is used in the filter
"""
# Set up return values

del self.mock_config['sync2jira']['filters']['github']['org/repo']['filter1']
del self.mock_config['sync2jira']['filters']['github']['org/repo']['labels']
self.mock_config['sync2jira']['filters']['github']['org/repo']['milestone'] = {'title': 'right'}
self.mock_github_message['msg']['issue']['milestone'] = {'title': 'wrong', 'foo': 'bar'}

# Call function
response = u.handle_github_message(
msg=self.mock_github_message,
config=self.mock_config
)
# Assert that calls were not made
mock_issue_from_github.assert_not_called()
self.assertEqual(None, response)

# Now, mock out the message to match the filter
self.mock_github_message['msg']['issue']['milestone'] = {'title': 'right', 'foo': 'bar'}

# Call function
response = u.handle_github_message(
msg=self.mock_github_message,
config=self.mock_config
)
# Assert that calls were made this time
mock_issue_from_github.assert_called()

@mock.patch(PATH + 'Github')
@mock.patch('sync2jira.intermediary.Issue.from_github')
def test_handle_github_message_no_comments(self,
Expand Down

0 comments on commit a957682

Please sign in to comment.