Skip to content

Commit d0278bb

Browse files
filter functionality as single subroutine
1 parent 1ba1ac1 commit d0278bb

File tree

2 files changed

+49
-53
lines changed

2 files changed

+49
-53
lines changed

sync2jira/upstream_issue.py

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,51 @@
110110
"""
111111

112112

113+
def _apply_github_filters(item, _filter, upstream, item_type="issue"):
114+
"""
115+
Apply GitHub filters (labels, milestone, other fields) to an item.
116+
117+
:param dict item: GitHub issue or PR data
118+
:param dict _filter: Filter configuration
119+
:param str upstream: Upstream repository name
120+
:param str item_type: Type of item for logging ("issue" or "PR")
121+
:returns: True if item passes all filters, False otherwise
122+
:rtype: bool
123+
"""
124+
for key, expected in _filter.items():
125+
if key == "labels":
126+
# special handling for label: we look for it in the list of labels
127+
actual = {label["name"] for label in item.get("labels", [])}
128+
if actual.isdisjoint(expected):
129+
log.debug(
130+
"Labels %s not found on %s: %s", expected, upstream, item_type
131+
)
132+
return False
133+
elif key == "milestone":
134+
# special handling for milestone: use the number
135+
milestone = item.get(key) or {} # Key might exist with value `None`
136+
actual = milestone.get("number")
137+
if expected != actual:
138+
log.debug(
139+
"Milestone %s not set on %s: %s", expected, upstream, item_type
140+
)
141+
return False
142+
else:
143+
# direct comparison
144+
actual = item.get(key)
145+
if actual != expected:
146+
log.debug(
147+
"Actual %r %r != expected %r on %s %s",
148+
key,
149+
actual,
150+
expected,
151+
upstream,
152+
item_type,
153+
)
154+
return False
155+
return True
156+
157+
113158
def handle_github_message(body, config, is_pr=False):
114159
"""
115160
Handle GitHub message from FedMsg.
@@ -140,33 +185,8 @@ def handle_github_message(body, config, is_pr=False):
140185
_filter = config["sync2jira"].get("filters", {}).get("github", {}).get(upstream, {})
141186

142187
issue = body["issue"]
143-
for key, expected in _filter.items():
144-
if key == "labels":
145-
# special handling for label: we look for it in the list of msg labels
146-
actual = {label["name"] for label in issue["labels"]}
147-
if actual.isdisjoint(expected):
148-
log.debug("Labels %s not found on issue: %s", expected, upstream)
149-
return None
150-
elif key == "milestone":
151-
# special handling for milestone: use the number
152-
milestone = issue.get(key) or {} # Key might exist with value `None`
153-
actual = milestone.get("number")
154-
if expected != actual:
155-
log.debug("Milestone %s not set on issue: %s", expected, upstream)
156-
return None
157-
else:
158-
# direct comparison
159-
actual = issue.get(key)
160-
if actual != expected:
161-
log.debug(
162-
"Actual %r %r != expected %r on issue %s",
163-
key,
164-
actual,
165-
expected,
166-
upstream,
167-
)
168-
return None
169-
188+
if not _apply_github_filters(issue, _filter, upstream, "issue"):
189+
return None
170190
if is_pr and not issue.get("closed_at"):
171191
log.debug(
172192
"%r is a pull request. Ignoring.", issue.get("html_url", "<missing URL>")

sync2jira/upstream_pr.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,8 @@ def handle_github_message(body, config, suffix):
5252
_filter = config["sync2jira"].get("filters", {}).get("github", {}).get(upstream, {})
5353

5454
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
55+
if not u_issue._apply_github_filters(pr, _filter, upstream, item_type="PR"):
56+
return None
8157
github_client = Github(config["sync2jira"]["github_token"])
8258
reformat_github_pr(pr, upstream, github_client)
8359
return i.PR.from_github(upstream, pr, suffix, config)

0 commit comments

Comments
 (0)