Skip to content

Commit 73ca92f

Browse files
fixes
1 parent eaf07a0 commit 73ca92f

File tree

4 files changed

+53
-114
lines changed

4 files changed

+53
-114
lines changed

sync2jira/upstream_issue.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ def passes_github_filters(item, config, upstream, item_type="issue"):
124124
filter_config = (
125125
config["sync2jira"].get("filters", {}).get("github", {}).get(upstream, {})
126126
)
127+
mapped_repos = config["sync2jira"]["map"]["github"]
128+
if upstream not in mapped_repos:
129+
log.debug("%r not in Github map: %r", upstream, mapped_repos.keys())
130+
return None
131+
key = "pullrequest" if item_type == "PR" else "issue"
132+
if key not in mapped_repos[upstream].get("sync", []):
133+
log.debug(
134+
"%r not in Github sync map: %r",
135+
key,
136+
mapped_repos[upstream].get("sync", []),
137+
)
138+
return None
127139

128140
for key, expected in filter_config.items():
129141
if key == "labels":
@@ -173,21 +185,8 @@ def handle_github_message(body, config, is_pr=False):
173185
repo = body["repository"]["name"]
174186
upstream = "{owner}/{repo}".format(owner=owner, repo=repo)
175187

176-
mapped_repos = config["sync2jira"]["map"]["github"]
177-
if upstream not in mapped_repos:
178-
log.debug("%r not in Github map: %r", upstream, mapped_repos.keys())
179-
return None
180-
key = "pullrequest" if is_pr else "issue"
181-
if key not in mapped_repos[upstream].get("sync", []):
182-
log.debug(
183-
"%r not in Github sync map: %r",
184-
key,
185-
mapped_repos[upstream].get("sync", []),
186-
)
187-
return None
188-
189188
issue = body["issue"]
190-
if not passes_github_filters(issue, config, upstream, "issue"):
189+
if not passes_github_filters(issue, config, upstream, item_type="issue"):
191190
return None
192191
if is_pr and not issue.get("closed_at"):
193192
log.debug(

sync2jira/upstream_pr.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,12 @@ def handle_github_message(body, config, suffix):
4141
repo = body["repository"]["name"]
4242
upstream = "{owner}/{repo}".format(owner=owner, repo=repo)
4343

44-
mapped_repos = config["sync2jira"]["map"]["github"]
45-
if upstream not in mapped_repos:
46-
log.debug("%r not in Github map: %r", upstream, mapped_repos.keys())
47-
return None
48-
elif "pullrequest" not in mapped_repos[upstream].get("sync", []):
49-
log.debug("%r not in Github PR map: %r", upstream, mapped_repos.keys())
50-
return None
5144

5245
pr = body["pull_request"]
5346
if not u_issue.passes_github_filters(pr, config, upstream, item_type="PR"):
5447
return None
55-
github_client = Github(config["sync2jira"]["github_token"])
48+
token = config["sync2jira"].get("github_token")
49+
github_client = Github(token, retry=5)
5650
reformat_github_pr(pr, upstream, github_client)
5751
return i.PR.from_github(upstream, pr, suffix, config)
5852

tests/test_upstream_issue.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,12 @@ def test_passes_github_filters(self):
773773
Test passes_github_filters for labels, milestone, and other fields.
774774
Tests all filtering conditions in one test case.
775775
"""
776-
self.mock_config["sync2jira"]["filters"]["github"]["org/repo"]["milestone"] = 1
777776
upstream = "org/repo"
777+
self.mock_config["sync2jira"]["filters"]["github"][upstream] = {
778+
"filter1": "filter1",
779+
"labels": ["custom_tag"],
780+
"milestone": 1
781+
}
778782

779783
# Test 1: Bad label - should return False
780784
item = {
@@ -815,3 +819,36 @@ def test_passes_github_filters(self):
815819
self.assertTrue(
816820
u.passes_github_filters(item, self.mock_config, upstream, item_type="issue")
817821
)
822+
823+
# Test 5: Config specifies only labels; item has matching label (wrong milestone/filter1 ignored) → True
824+
self.mock_config["sync2jira"]["filters"]["github"][upstream] = {"labels": ["custom_tag"]}
825+
item = {
826+
"labels": [{"name": "custom_tag"}],
827+
"milestone": {"number": 999},
828+
"filter1": "wrong",
829+
}
830+
self.assertTrue(
831+
u.passes_github_filters(item, self.mock_config, upstream, item_type="issue")
832+
)
833+
834+
# Test 6: Config specifies only milestone; item has matching milestone (wrong label/filter1 ignored) → True
835+
self.mock_config["sync2jira"]["filters"]["github"][upstream] = {"milestone": 1}
836+
item = {
837+
"labels": [{"name": "bad_label"}],
838+
"milestone": {"number": 1},
839+
"filter1": "wrong",
840+
}
841+
self.assertTrue(
842+
u.passes_github_filters(item, self.mock_config, upstream, item_type="issue")
843+
)
844+
845+
# Test 7: Config specifies only filter1; item has matching filter1 (wrong label/milestone ignored) → True
846+
self.mock_config["sync2jira"]["filters"]["github"][upstream] = {"filter1": "filter1"}
847+
item = {
848+
"labels": [{"name": "bad_label"}],
849+
"milestone": {"number": 999},
850+
"filter1": "filter1",
851+
}
852+
self.assertTrue(
853+
u.passes_github_filters(item, self.mock_config, upstream, item_type="issue")
854+
)

tests/test_upstream_pr.py

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -255,97 +255,6 @@ def test_filter_multiple_labels(
255255
["custom_tag", "another_tag", "and_another"],
256256
)
257257

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)
348-
349258
@mock.patch("sync2jira.upstream_pr.u_issue.passes_github_filters")
350259
@mock.patch("sync2jira.intermediary.PR.from_github")
351260
def test_handle_github_message_filter_returns_false(

0 commit comments

Comments
 (0)