Skip to content

Commit 1ba1ac1

Browse files
pr issue creation filtering in the same way as issue
1 parent 0a2303f commit 1ba1ac1

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

sync2jira/upstream_pr.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,35 @@ 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+
5254
pr = body["pull_request"]
55+
for key, expected in _filter.items():
56+
if key == "labels":
57+
# special handling for label: we look for it in the list of PR labels
58+
actual = {label["name"] for label in pr.get("labels", [])}
59+
if actual.isdisjoint(expected):
60+
log.debug("Labels %s not found on PR: %s", expected, upstream)
61+
return None
62+
elif key == "milestone":
63+
# special handling for milestone: use the number
64+
milestone = pr.get(key) or {} # Key might exist with value `None`
65+
actual = milestone.get("number")
66+
if expected != actual:
67+
log.debug("Milestone %s not set on PR: %s", expected, upstream)
68+
return None
69+
else:
70+
# direct comparison
71+
actual = pr.get(key)
72+
if actual != expected:
73+
log.debug(
74+
"Actual %r %r != expected %r on PR %s",
75+
key,
76+
actual,
77+
expected,
78+
upstream,
79+
)
80+
return None
5381
github_client = Github(config["sync2jira"]["github_token"])
5482
reformat_github_pr(pr, upstream, github_client)
5583
return i.PR.from_github(upstream, pr, suffix, config)

tests/test_upstream_pr.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,94 @@ def test_filter_multiple_labels(
254254
self.mock_config["sync2jira"]["filters"]["github"]["org/repo"]["labels"],
255255
["custom_tag", "another_tag", "and_another"],
256256
)
257+
258+
@mock.patch("sync2jira.intermediary.PR.from_github")
259+
def test_handle_github_message_filtering(self, mock_pr_from_github):
260+
"""
261+
Test 'handle_github_message' filtering for labels, milestone, and other fields.
262+
Tests all three filtering conditions in one test case.
263+
"""
264+
# Test 1: Bad label - PR should be filtered out
265+
self.mock_github_message_body["pull_request"]["labels"] = [
266+
{"name": "bad_label"}
267+
]
268+
269+
response = u.handle_github_message(
270+
body=self.mock_github_message_body,
271+
config=self.mock_config,
272+
suffix="mock_suffix",
273+
)
274+
275+
mock_pr_from_github.assert_not_called()
276+
self.assertEqual(None, response)
277+
278+
# Reset for next test
279+
mock_pr_from_github.reset_mock()
280+
self.mock_github_message_body["pull_request"]["labels"] = [
281+
{"name": "custom_tag"}
282+
]
283+
284+
# Test 2: Bad milestone - PR should be filtered out
285+
self.mock_config["sync2jira"]["filters"]["github"]["org/repo"][
286+
"milestone"
287+
] = 123
288+
self.mock_github_message_body["pull_request"]["milestone"] = {"number": 456}
289+
290+
response = u.handle_github_message(
291+
body=self.mock_github_message_body,
292+
config=self.mock_config,
293+
suffix="mock_suffix",
294+
)
295+
296+
mock_pr_from_github.assert_not_called()
297+
self.assertEqual(None, response)
298+
299+
# Reset for next test
300+
mock_pr_from_github.reset_mock()
301+
del self.mock_config["sync2jira"]["filters"]["github"]["org/repo"]["milestone"]
302+
self.mock_github_message_body["pull_request"]["milestone"] = {
303+
"title": "mock_milestone"
304+
}
305+
306+
# Test 3: Bad other field (filter1) - PR should be filtered out
307+
self.mock_github_message_body["pull_request"]["filter1"] = "filter2"
308+
309+
response = u.handle_github_message(
310+
body=self.mock_github_message_body,
311+
config=self.mock_config,
312+
suffix="mock_suffix",
313+
)
314+
315+
mock_pr_from_github.assert_not_called()
316+
self.assertEqual(None, response)
317+
318+
@mock.patch(PATH + "Github")
319+
@mock.patch("sync2jira.intermediary.PR.from_github")
320+
def test_handle_github_message_filtering_passes(
321+
self, mock_pr_from_github, mock_github
322+
):
323+
"""
324+
Test 'handle_github_message' when all filters pass (labels, milestone, other fields).
325+
"""
326+
# Set up filters with all three types
327+
self.mock_config["sync2jira"]["filters"]["github"]["org/repo"]["milestone"] = 1
328+
self.mock_github_message_body["pull_request"]["milestone"] = {
329+
"number": 1,
330+
"title": "mock_milestone",
331+
}
332+
333+
# Set up return values
334+
mock_pr_from_github.return_value = "Successful Call!"
335+
mock_github.return_value = self.mock_github_client
336+
337+
# Call function
338+
response = u.handle_github_message(
339+
body=self.mock_github_message_body,
340+
config=self.mock_config,
341+
suffix="mock_suffix",
342+
)
343+
344+
# Assert that PR was processed (all filters passed)
345+
mock_pr_from_github.assert_called_once()
346+
mock_github.assert_called_with("mock_token")
347+
self.assertEqual("Successful Call!", response)

0 commit comments

Comments
 (0)