Skip to content

Commit 4181c4c

Browse files
Refactor check_event functions to accept sansio.Event
1 parent d463236 commit 4181c4c

File tree

3 files changed

+89
-68
lines changed

3 files changed

+89
-68
lines changed

src/django_github_app/commands.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import re
44
from enum import Enum
5-
from typing import Any
65
from typing import NamedTuple
76

7+
from gidgethub import sansio
8+
89

910
class EventAction(NamedTuple):
1011
event: str
@@ -77,9 +78,9 @@ def parse_mentions(text: str, username: str) -> list[MentionMatch]:
7778

7879

7980
def check_event_for_mention(
80-
event: dict[str, Any], command: str | None, username: str
81+
event: sansio.Event, command: str | None, username: str
8182
) -> bool:
82-
comment = event.get("comment", {}).get("body", "")
83+
comment = event.data.get("comment", {}).get("body", "")
8384
mentions = parse_mentions(comment, username)
8485

8586
if not mentions:
@@ -91,15 +92,13 @@ def check_event_for_mention(
9192
return any(mention.command == command.lower() for mention in mentions)
9293

9394

94-
def check_event_scope(
95-
event_type: str, event_data: dict[str, Any], scope: CommandScope | None
96-
) -> bool:
95+
def check_event_scope(event: sansio.Event, scope: CommandScope | None) -> bool:
9796
if scope is None:
9897
return True
9998

10099
# For issue_comment events, we need to distinguish between issues and PRs
101-
if event_type == "issue_comment":
102-
issue = event_data.get("issue", {})
100+
if event.event == "issue_comment":
101+
issue = event.data.get("issue", {})
103102
is_pull_request = "pull_request" in issue and issue["pull_request"] is not None
104103

105104
# If scope is ISSUE, we only want actual issues (not PRs)
@@ -110,4 +109,4 @@ def check_event_scope(
110109
return is_pull_request
111110

112111
scope_events = scope.get_events()
113-
return any(event_action.event == event_type for event_action in scope_events)
112+
return any(event_action.event == event.event for event_action in scope_events)

src/django_github_app/routing.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,13 @@ async def async_wrapper(
8181
# TODO: Get actual bot username from installation/app data
8282
username = "bot" # Placeholder
8383

84-
if not check_event_for_mention(event.data, command, username):
84+
if not check_event_for_mention(event, command, username):
8585
return
8686

87-
# Check if the event matches the specified scope
88-
if not check_event_scope(event.event, event.data, scope):
87+
if not check_event_scope(event, scope):
8988
return
9089

91-
# TODO: Check permissions
92-
# For now, just call through
90+
# TODO: Check permissions. For now, just call through.
9391
await func(event, *args, **wrapper_kwargs) # type: ignore[func-returns-value]
9492

9593
@wraps(func)
@@ -99,15 +97,13 @@ def sync_wrapper(
9997
# TODO: Get actual bot username from installation/app data
10098
username = "bot" # Placeholder
10199

102-
if not check_event_for_mention(event.data, command, username):
100+
if not check_event_for_mention(event, command, username):
103101
return
104102

105-
# Check if the event matches the specified scope
106-
if not check_event_scope(event.event, event.data, scope):
103+
if not check_event_scope(event, scope):
107104
return
108105

109-
# TODO: Check permissions
110-
# For now, just call through
106+
# TODO: Check permissions. For now, just call through.
111107
func(event, *args, **wrapper_kwargs)
112108

113109
wrapper: MentionHandler

tests/test_commands.py

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from gidgethub import sansio
4+
35
from django_github_app.commands import CommandScope
46
from django_github_app.commands import check_event_for_mention
57
from django_github_app.commands import check_event_scope
@@ -161,37 +163,51 @@ def test_special_character_command(self):
161163

162164
class TestCheckMentionMatches:
163165
def test_match_with_command(self):
164-
event = {"comment": {"body": "@bot help"}}
166+
event = sansio.Event(
167+
{"comment": {"body": "@bot help"}}, event="issue_comment", delivery_id="123"
168+
)
165169

166170
assert check_event_for_mention(event, "help", "bot") is True
167171
assert check_event_for_mention(event, "deploy", "bot") is False
168172

169173
def test_match_without_command(self):
170-
event = {"comment": {"body": "@bot help"}}
174+
event = sansio.Event(
175+
{"comment": {"body": "@bot help"}}, event="issue_comment", delivery_id="123"
176+
)
171177

172178
assert check_event_for_mention(event, None, "bot") is True
173179

174-
event = {"comment": {"body": "no mention here"}}
180+
event = sansio.Event(
181+
{"comment": {"body": "no mention here"}},
182+
event="issue_comment",
183+
delivery_id="124",
184+
)
175185

176186
assert check_event_for_mention(event, None, "bot") is False
177187

178188
def test_no_comment_body(self):
179-
event = {}
189+
event = sansio.Event({}, event="issue_comment", delivery_id="123")
180190

181191
assert check_event_for_mention(event, "help", "bot") is False
182192

183-
event = {"comment": {}}
193+
event = sansio.Event({"comment": {}}, event="issue_comment", delivery_id="124")
184194

185195
assert check_event_for_mention(event, "help", "bot") is False
186196

187197
def test_case_insensitive_command_match(self):
188-
event = {"comment": {"body": "@bot HELP"}}
198+
event = sansio.Event(
199+
{"comment": {"body": "@bot HELP"}}, event="issue_comment", delivery_id="123"
200+
)
189201

190202
assert check_event_for_mention(event, "help", "bot") is True
191203
assert check_event_for_mention(event, "HELP", "bot") is True
192204

193205
def test_multiple_mentions(self):
194-
event = {"comment": {"body": "@bot help @bot deploy"}}
206+
event = sansio.Event(
207+
{"comment": {"body": "@bot help @bot deploy"}},
208+
event="issue_comment",
209+
delivery_id="123",
210+
)
195211

196212
assert check_event_for_mention(event, "help", "bot") is True
197213
assert check_event_for_mention(event, "deploy", "bot") is True
@@ -201,82 +217,92 @@ def test_multiple_mentions(self):
201217
class TestCheckEventScope:
202218
def test_no_scope_allows_all_events(self):
203219
# When no scope is specified, all events should pass
204-
assert check_event_scope("issue_comment", {"issue": {}}, None) is True
205-
assert check_event_scope("pull_request_review_comment", {}, None) is True
206-
assert check_event_scope("commit_comment", {}, None) is True
220+
event1 = sansio.Event({"issue": {}}, event="issue_comment", delivery_id="1")
221+
assert check_event_scope(event1, None) is True
222+
223+
event2 = sansio.Event({}, event="pull_request_review_comment", delivery_id="2")
224+
assert check_event_scope(event2, None) is True
225+
226+
event3 = sansio.Event({}, event="commit_comment", delivery_id="3")
227+
assert check_event_scope(event3, None) is True
207228

208229
def test_issue_scope_on_issue_comment(self):
209230
# Issue comment on an actual issue (no pull_request field)
210-
issue_event = {"issue": {"title": "Bug report"}}
211-
assert (
212-
check_event_scope("issue_comment", issue_event, CommandScope.ISSUE) is True
231+
issue_event = sansio.Event(
232+
{"issue": {"title": "Bug report"}}, event="issue_comment", delivery_id="1"
213233
)
234+
assert check_event_scope(issue_event, CommandScope.ISSUE) is True
214235

215236
# Issue comment on a pull request (has pull_request field)
216-
pr_event = {"issue": {"title": "PR title", "pull_request": {"url": "..."}}}
217-
assert check_event_scope("issue_comment", pr_event, CommandScope.ISSUE) is False
237+
pr_event = sansio.Event(
238+
{"issue": {"title": "PR title", "pull_request": {"url": "..."}}},
239+
event="issue_comment",
240+
delivery_id="2",
241+
)
242+
assert check_event_scope(pr_event, CommandScope.ISSUE) is False
218243

219244
def test_pr_scope_on_issue_comment(self):
220245
# Issue comment on an actual issue (no pull_request field)
221-
issue_event = {"issue": {"title": "Bug report"}}
222-
assert check_event_scope("issue_comment", issue_event, CommandScope.PR) is False
246+
issue_event = sansio.Event(
247+
{"issue": {"title": "Bug report"}}, event="issue_comment", delivery_id="1"
248+
)
249+
assert check_event_scope(issue_event, CommandScope.PR) is False
223250

224251
# Issue comment on a pull request (has pull_request field)
225-
pr_event = {"issue": {"title": "PR title", "pull_request": {"url": "..."}}}
226-
assert check_event_scope("issue_comment", pr_event, CommandScope.PR) is True
252+
pr_event = sansio.Event(
253+
{"issue": {"title": "PR title", "pull_request": {"url": "..."}}},
254+
event="issue_comment",
255+
delivery_id="2",
256+
)
257+
assert check_event_scope(pr_event, CommandScope.PR) is True
227258

228259
def test_pr_scope_allows_pr_specific_events(self):
229260
# PR scope should allow pull_request_review_comment
230-
assert (
231-
check_event_scope("pull_request_review_comment", {}, CommandScope.PR)
232-
is True
233-
)
261+
event1 = sansio.Event({}, event="pull_request_review_comment", delivery_id="1")
262+
assert check_event_scope(event1, CommandScope.PR) is True
234263

235264
# PR scope should allow pull_request_review
236-
assert check_event_scope("pull_request_review", {}, CommandScope.PR) is True
265+
event2 = sansio.Event({}, event="pull_request_review", delivery_id="2")
266+
assert check_event_scope(event2, CommandScope.PR) is True
237267

238268
# PR scope should not allow commit_comment
239-
assert check_event_scope("commit_comment", {}, CommandScope.PR) is False
269+
event3 = sansio.Event({}, event="commit_comment", delivery_id="3")
270+
assert check_event_scope(event3, CommandScope.PR) is False
240271

241272
def test_commit_scope_allows_commit_comment_only(self):
242273
# Commit scope should allow commit_comment
243-
assert check_event_scope("commit_comment", {}, CommandScope.COMMIT) is True
274+
event1 = sansio.Event({}, event="commit_comment", delivery_id="1")
275+
assert check_event_scope(event1, CommandScope.COMMIT) is True
244276

245277
# Commit scope should not allow issue_comment
246-
assert (
247-
check_event_scope("issue_comment", {"issue": {}}, CommandScope.COMMIT)
248-
is False
249-
)
278+
event2 = sansio.Event({"issue": {}}, event="issue_comment", delivery_id="2")
279+
assert check_event_scope(event2, CommandScope.COMMIT) is False
250280

251281
# Commit scope should not allow PR events
252-
assert (
253-
check_event_scope("pull_request_review_comment", {}, CommandScope.COMMIT)
254-
is False
255-
)
282+
event3 = sansio.Event({}, event="pull_request_review_comment", delivery_id="3")
283+
assert check_event_scope(event3, CommandScope.COMMIT) is False
256284

257285
def test_issue_scope_disallows_non_issue_events(self):
258286
# Issue scope should not allow pull_request_review_comment
259-
assert (
260-
check_event_scope("pull_request_review_comment", {}, CommandScope.ISSUE)
261-
is False
262-
)
287+
event1 = sansio.Event({}, event="pull_request_review_comment", delivery_id="1")
288+
assert check_event_scope(event1, CommandScope.ISSUE) is False
263289

264290
# Issue scope should not allow commit_comment
265-
assert check_event_scope("commit_comment", {}, CommandScope.ISSUE) is False
291+
event2 = sansio.Event({}, event="commit_comment", delivery_id="2")
292+
assert check_event_scope(event2, CommandScope.ISSUE) is False
266293

267294
def test_pull_request_field_none_treated_as_issue(self):
268295
# If pull_request field exists but is None, treat as issue
269-
event_with_none_pr = {"issue": {"title": "Issue", "pull_request": None}}
270-
assert (
271-
check_event_scope("issue_comment", event_with_none_pr, CommandScope.ISSUE)
272-
is True
273-
)
274-
assert (
275-
check_event_scope("issue_comment", event_with_none_pr, CommandScope.PR)
276-
is False
296+
event = sansio.Event(
297+
{"issue": {"title": "Issue", "pull_request": None}},
298+
event="issue_comment",
299+
delivery_id="1",
277300
)
301+
assert check_event_scope(event, CommandScope.ISSUE) is True
302+
assert check_event_scope(event, CommandScope.PR) is False
278303

279304
def test_missing_issue_data(self):
280305
# If issue data is missing entirely, default behavior
281-
assert check_event_scope("issue_comment", {}, CommandScope.ISSUE) is True
282-
assert check_event_scope("issue_comment", {}, CommandScope.PR) is False
306+
event = sansio.Event({}, event="issue_comment", delivery_id="1")
307+
assert check_event_scope(event, CommandScope.ISSUE) is True
308+
assert check_event_scope(event, CommandScope.PR) is False

0 commit comments

Comments
 (0)