Skip to content

Commit eaf07a0

Browse files
fixes
1 parent d0278bb commit eaf07a0

File tree

4 files changed

+96
-8
lines changed

4 files changed

+96
-8
lines changed

sync2jira/upstream_issue.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"""
111111

112112

113-
def _apply_github_filters(item, _filter, upstream, item_type="issue"):
113+
def passes_github_filters(item, config, upstream, item_type="issue"):
114114
"""
115115
Apply GitHub filters (labels, milestone, other fields) to an item.
116116
@@ -121,7 +121,11 @@ def _apply_github_filters(item, _filter, upstream, item_type="issue"):
121121
:returns: True if item passes all filters, False otherwise
122122
:rtype: bool
123123
"""
124-
for key, expected in _filter.items():
124+
filter_config = (
125+
config["sync2jira"].get("filters", {}).get("github", {}).get(upstream, {})
126+
)
127+
128+
for key, expected in filter_config.items():
125129
if key == "labels":
126130
# special handling for label: we look for it in the list of labels
127131
actual = {label["name"] for label in item.get("labels", [])}
@@ -182,10 +186,8 @@ def handle_github_message(body, config, is_pr=False):
182186
)
183187
return None
184188

185-
_filter = config["sync2jira"].get("filters", {}).get("github", {}).get(upstream, {})
186-
187189
issue = body["issue"]
188-
if not _apply_github_filters(issue, _filter, upstream, "issue"):
190+
if not passes_github_filters(issue, config, upstream, "issue"):
189191
return None
190192
if is_pr and not issue.get("closed_at"):
191193
log.debug(

sync2jira/upstream_pr.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ def handle_github_message(body, config, suffix):
4949
log.debug("%r not in Github PR map: %r", upstream, mapped_repos.keys())
5050
return None
5151

52-
_filter = config["sync2jira"].get("filters", {}).get("github", {}).get(upstream, {})
53-
5452
pr = body["pull_request"]
55-
if not u_issue._apply_github_filters(pr, _filter, upstream, item_type="PR"):
53+
if not u_issue.passes_github_filters(pr, config, upstream, item_type="PR"):
5654
return None
5755
github_client = Github(config["sync2jira"]["github_token"])
5856
reformat_github_pr(pr, upstream, github_client)

tests/test_upstream_issue.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,51 @@ def test_add_project_values_early_exit(self, mock_requests_post):
767767
self.assertIsNone(result)
768768
# Reset mock
769769
mock_requests_post.reset_mock()
770+
771+
def test_passes_github_filters(self):
772+
"""
773+
Test passes_github_filters for labels, milestone, and other fields.
774+
Tests all filtering conditions in one test case.
775+
"""
776+
self.mock_config["sync2jira"]["filters"]["github"]["org/repo"]["milestone"] = 1
777+
upstream = "org/repo"
778+
779+
# Test 1: Bad label - should return False
780+
item = {
781+
"labels": [{"name": "bad_label"}],
782+
"milestone": {"number": 1},
783+
"filter1": "filter1",
784+
}
785+
self.assertFalse(
786+
u.passes_github_filters(item, self.mock_config, upstream, item_type="issue")
787+
)
788+
789+
# Test 2: Bad milestone - should return False
790+
item = {
791+
"labels": [{"name": "custom_tag"}],
792+
"milestone": {"number": 456},
793+
"filter1": "filter1",
794+
}
795+
self.assertFalse(
796+
u.passes_github_filters(item, self.mock_config, upstream, item_type="issue")
797+
)
798+
799+
# Test 3: Bad other field (filter1) - should return False
800+
item = {
801+
"labels": [{"name": "custom_tag"}],
802+
"milestone": {"number": 1},
803+
"filter1": "filter2",
804+
}
805+
self.assertFalse(
806+
u.passes_github_filters(item, self.mock_config, upstream, item_type="issue")
807+
)
808+
809+
# Test 4: All filters pass - should return True
810+
item = {
811+
"labels": [{"name": "custom_tag"}],
812+
"milestone": {"number": 1},
813+
"filter1": "filter1",
814+
}
815+
self.assertTrue(
816+
u.passes_github_filters(item, self.mock_config, upstream, item_type="issue")
817+
)

tests/test_upstream_pr.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,43 @@ def test_handle_github_message_filtering_passes(
345345
mock_pr_from_github.assert_called_once()
346346
mock_github.assert_called_with("mock_token")
347347
self.assertEqual("Successful Call!", response)
348+
349+
@mock.patch("sync2jira.upstream_pr.u_issue.passes_github_filters")
350+
@mock.patch("sync2jira.intermediary.PR.from_github")
351+
def test_handle_github_message_filter_returns_false(
352+
self, mock_pr_from_github, mock_passes
353+
):
354+
"""When passes_github_filters returns False, handle_github_message returns None."""
355+
mock_passes.return_value = False
356+
357+
response = u.handle_github_message(
358+
body=self.mock_github_message_body,
359+
config=self.mock_config,
360+
suffix="mock_suffix",
361+
)
362+
363+
mock_passes.assert_called_once()
364+
mock_pr_from_github.assert_not_called()
365+
self.assertIsNone(response)
366+
367+
@mock.patch(PATH + "Github")
368+
@mock.patch("sync2jira.upstream_pr.u_issue.passes_github_filters")
369+
@mock.patch("sync2jira.intermediary.PR.from_github")
370+
def test_handle_github_message_filter_returns_true(
371+
self, mock_pr_from_github, mock_passes, mock_github
372+
):
373+
"""When passes_github_filters returns True, handle_github_message proceeds to PR.from_github."""
374+
mock_passes.return_value = True
375+
mock_pr_from_github.return_value = "Successful Call!"
376+
mock_github.return_value = self.mock_github_client
377+
378+
response = u.handle_github_message(
379+
body=self.mock_github_message_body,
380+
config=self.mock_config,
381+
suffix="mock_suffix",
382+
)
383+
384+
mock_passes.assert_called_once()
385+
mock_pr_from_github.assert_called_once()
386+
mock_github.assert_called_with("mock_token")
387+
self.assertEqual("Successful Call!", response)

0 commit comments

Comments
 (0)