Skip to content

Commit 30be1b7

Browse files
Refactor get_event_scope to MentionScope.from_event classmethod
Moves the get_event_scope function to be a classmethod on MentionScope called from_event, following the same pattern as Comment.from_event. This provides a more consistent API and better encapsulation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 078912a commit 30be1b7

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

src/django_github_app/mentions.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ def all_events(cls) -> list[EventAction]:
4646
)
4747
)
4848

49+
@classmethod
50+
def from_event(cls, event: sansio.Event) -> MentionScope | None:
51+
"""Determine the scope of a GitHub event based on its type and context."""
52+
if event.event == "issue_comment":
53+
issue = event.data.get("issue", {})
54+
is_pull_request = (
55+
"pull_request" in issue and issue["pull_request"] is not None
56+
)
57+
return cls.PR if is_pull_request else cls.ISSUE
58+
59+
for scope in cls:
60+
scope_events = scope.get_events()
61+
if any(event_action.event == event.event for event_action in scope_events):
62+
return scope
63+
64+
return None
65+
4966

5067
@dataclass
5168
class Mention:
@@ -127,20 +144,6 @@ class MentionContext:
127144
QUOTE_PATTERN = re.compile(r"^\s*>.*$", re.MULTILINE)
128145

129146

130-
def get_event_scope(event: sansio.Event) -> MentionScope | None:
131-
if event.event == "issue_comment":
132-
issue = event.data.get("issue", {})
133-
is_pull_request = "pull_request" in issue and issue["pull_request"] is not None
134-
return MentionScope.PR if is_pull_request else MentionScope.ISSUE
135-
136-
for scope in MentionScope:
137-
scope_events = scope.get_events()
138-
if any(event_action.event == event.event for event_action in scope_events):
139-
return scope
140-
141-
return None
142-
143-
144147
def check_pattern_match(
145148
text: str, pattern: str | re.Pattern[str] | None
146149
) -> re.Match[str] | None:

src/django_github_app/routing.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from .mentions import MentionContext
2222
from .mentions import MentionScope
2323
from .mentions import check_pattern_match
24-
from .mentions import get_event_scope
2524
from .mentions import parse_mentions_for_username
2625

2726
AsyncCallback = Callable[..., Awaitable[None]]
@@ -84,7 +83,7 @@ def decorator(func: CB) -> CB:
8483
async def async_wrapper(
8584
event: sansio.Event, gh: AsyncGitHubAPI, *args: Any, **kwargs: Any
8685
) -> None:
87-
event_scope = get_event_scope(event)
86+
event_scope = MentionScope.from_event(event)
8887
if scope is not None and event_scope != scope:
8988
return
9089

@@ -114,7 +113,7 @@ async def async_wrapper(
114113
def sync_wrapper(
115114
event: sansio.Event, gh: SyncGitHubAPI, *args: Any, **kwargs: Any
116115
) -> None:
117-
event_scope = get_event_scope(event)
116+
event_scope = MentionScope.from_event(event)
118117
if scope is not None and event_scope != scope:
119118
return
120119

tests/test_mentions.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from django_github_app.mentions import Comment
1010
from django_github_app.mentions import MentionScope
11-
from django_github_app.mentions import get_event_scope
1211
from django_github_app.mentions import parse_mentions_for_username
1312

1413

@@ -188,83 +187,83 @@ def test_special_character_command(self, create_comment_event):
188187

189188

190189
class TestGetEventScope:
191-
def test_get_event_scope_for_various_events(self):
190+
def test_from_event_for_various_events(self):
192191
# Issue comment on actual issue
193192
event1 = sansio.Event({"issue": {}}, event="issue_comment", delivery_id="1")
194-
assert get_event_scope(event1) == MentionScope.ISSUE
193+
assert MentionScope.from_event(event1) == MentionScope.ISSUE
195194

196195
# PR review comment
197196
event2 = sansio.Event({}, event="pull_request_review_comment", delivery_id="2")
198-
assert get_event_scope(event2) == MentionScope.PR
197+
assert MentionScope.from_event(event2) == MentionScope.PR
199198

200199
# Commit comment
201200
event3 = sansio.Event({}, event="commit_comment", delivery_id="3")
202-
assert get_event_scope(event3) == MentionScope.COMMIT
201+
assert MentionScope.from_event(event3) == MentionScope.COMMIT
203202

204203
def test_issue_scope_on_issue_comment(self):
205204
# Issue comment on an actual issue (no pull_request field)
206205
issue_event = sansio.Event(
207206
{"issue": {"title": "Bug report"}}, event="issue_comment", delivery_id="1"
208207
)
209-
assert get_event_scope(issue_event) == MentionScope.ISSUE
208+
assert MentionScope.from_event(issue_event) == MentionScope.ISSUE
210209

211210
# Issue comment on a pull request (has pull_request field)
212211
pr_event = sansio.Event(
213212
{"issue": {"title": "PR title", "pull_request": {"url": "..."}}},
214213
event="issue_comment",
215214
delivery_id="2",
216215
)
217-
assert get_event_scope(pr_event) == MentionScope.PR
216+
assert MentionScope.from_event(pr_event) == MentionScope.PR
218217

219218
def test_pr_scope_on_issue_comment(self):
220219
# Issue comment on an actual issue (no pull_request field)
221220
issue_event = sansio.Event(
222221
{"issue": {"title": "Bug report"}}, event="issue_comment", delivery_id="1"
223222
)
224-
assert get_event_scope(issue_event) == MentionScope.ISSUE
223+
assert MentionScope.from_event(issue_event) == MentionScope.ISSUE
225224

226225
# Issue comment on a pull request (has pull_request field)
227226
pr_event = sansio.Event(
228227
{"issue": {"title": "PR title", "pull_request": {"url": "..."}}},
229228
event="issue_comment",
230229
delivery_id="2",
231230
)
232-
assert get_event_scope(pr_event) == MentionScope.PR
231+
assert MentionScope.from_event(pr_event) == MentionScope.PR
233232

234233
def test_pr_scope_allows_pr_specific_events(self):
235234
# PR scope should allow pull_request_review_comment
236235
event1 = sansio.Event({}, event="pull_request_review_comment", delivery_id="1")
237-
assert get_event_scope(event1) == MentionScope.PR
236+
assert MentionScope.from_event(event1) == MentionScope.PR
238237

239238
# PR scope should allow pull_request_review
240239
event2 = sansio.Event({}, event="pull_request_review", delivery_id="2")
241-
assert get_event_scope(event2) == MentionScope.PR
240+
assert MentionScope.from_event(event2) == MentionScope.PR
242241

243242
# PR scope should not allow commit_comment
244243
event3 = sansio.Event({}, event="commit_comment", delivery_id="3")
245-
assert get_event_scope(event3) == MentionScope.COMMIT
244+
assert MentionScope.from_event(event3) == MentionScope.COMMIT
246245

247246
def test_commit_scope_allows_commit_comment_only(self):
248247
# Commit scope should allow commit_comment
249248
event1 = sansio.Event({}, event="commit_comment", delivery_id="1")
250-
assert get_event_scope(event1) == MentionScope.COMMIT
249+
assert MentionScope.from_event(event1) == MentionScope.COMMIT
251250

252251
# Commit scope should not allow issue_comment
253252
event2 = sansio.Event({"issue": {}}, event="issue_comment", delivery_id="2")
254-
assert get_event_scope(event2) == MentionScope.ISSUE
253+
assert MentionScope.from_event(event2) == MentionScope.ISSUE
255254

256255
# Commit scope should not allow PR events
257256
event3 = sansio.Event({}, event="pull_request_review_comment", delivery_id="3")
258-
assert get_event_scope(event3) == MentionScope.PR
257+
assert MentionScope.from_event(event3) == MentionScope.PR
259258

260259
def test_different_event_types_have_correct_scope(self):
261260
# pull_request_review_comment should be PR scope
262261
event1 = sansio.Event({}, event="pull_request_review_comment", delivery_id="1")
263-
assert get_event_scope(event1) == MentionScope.PR
262+
assert MentionScope.from_event(event1) == MentionScope.PR
264263

265264
# commit_comment should be COMMIT scope
266265
event2 = sansio.Event({}, event="commit_comment", delivery_id="2")
267-
assert get_event_scope(event2) == MentionScope.COMMIT
266+
assert MentionScope.from_event(event2) == MentionScope.COMMIT
268267

269268
def test_pull_request_field_none_treated_as_issue(self):
270269
# If pull_request field exists but is None, treat as issue
@@ -273,17 +272,17 @@ def test_pull_request_field_none_treated_as_issue(self):
273272
event="issue_comment",
274273
delivery_id="1",
275274
)
276-
assert get_event_scope(event) == MentionScope.ISSUE
275+
assert MentionScope.from_event(event) == MentionScope.ISSUE
277276

278277
def test_missing_issue_data(self):
279278
# If issue data is missing entirely, defaults to ISSUE scope for issue_comment
280279
event = sansio.Event({}, event="issue_comment", delivery_id="1")
281-
assert get_event_scope(event) == MentionScope.ISSUE
280+
assert MentionScope.from_event(event) == MentionScope.ISSUE
282281

283282
def test_unknown_event_returns_none(self):
284283
# Unknown event types should return None
285284
event = sansio.Event({}, event="unknown_event", delivery_id="1")
286-
assert get_event_scope(event) is None
285+
assert MentionScope.from_event(event) is None
287286

288287

289288
class TestComment:

0 commit comments

Comments
 (0)