From fdc4ba1c7832f4ef096872ff562edb8f73408b3f Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: Mar 26 2019 12:30:23 +0000 Subject: Apply filters for comment messages. Fixes a bug introduced in the new comment sync feature. --- diff --git a/sync2jira/upstream.py b/sync2jira/upstream.py index 522d55e..df60cdb 100644 --- a/sync2jira/upstream.py +++ b/sync2jira/upstream.py @@ -170,6 +170,25 @@ def handle_github_comment(msg, config): log.info("%r not in github map: %r", upstream, mapped_repos.keys()) return None + _filter = config['sync2jira']\ + .get('filters', {})\ + .get('github', {})\ + .get(upstream, {'state': 'open'}) + + 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.info("Label %s not set on issue", expected) + return None + else: + # direct comparison + actual = msg['msg']['issue'].get(key) + if actual != expected: + log.info("Actual %r %r != expected %r", key, actual, expected) + return None + issue = msg['msg']['issue'] if 'pull_request' in issue: @@ -198,6 +217,32 @@ def handle_pagure_comment(msg, config): log.info("%r not in pagure map: %r", upstream, mapped_repos.keys()) return None + _filter = config['sync2jira']\ + .get('filters', {})\ + .get('pagure', {})\ + .get(upstream, {'status': 'Open'}) + + for key, expected in _filter.items(): + # special handling for tag: we look for it in the list of msg tags + if key == 'tags': + actual = msg['msg']['issue'].get('tags', []) + msg['msg'].get('tags', []) + + # Some messages send tags as strings, others as dicts. Handle both. + actual = \ + [tag['name'] for tag in actual if isinstance(tag, dict)] + \ + [tag for tag in actual if isinstance(tag, string_type)] + + intersection = set(actual) & set(expected) + if not intersection: + log.info("None of %r in %r on issue.", expected, actual) + return None + else: + # direct comparison + actual = msg['msg']['issue'].get(key) + if actual != expected: + log.info("Actual %r %r != expected %r", key, actual, expected) + return None + issue = msg['msg']['issue'] return i.Issue.from_pagure(upstream, issue, config) diff --git a/tests/test_upstream.py b/tests/test_upstream.py index e2a7a7b..8e8967d 100644 --- a/tests/test_upstream.py +++ b/tests/test_upstream.py @@ -68,7 +68,7 @@ class TestUpstream(unittest.TestCase): } comments = [comment] issue_dict = { - 'state': 'Open', + 'state': 'open', 'title': 'title', 'url': 'some_issue_url', } @@ -108,7 +108,7 @@ class TestUpstream(unittest.TestCase): } ] issue_dict = { - 'state': 'Open', + 'state': 'open', 'title': 'title', 'url': 'some_issue_url', }