Skip to content

Commit ae6238a

Browse files
Strip mention system down to core functionality
1 parent 9c55edf commit ae6238a

File tree

6 files changed

+77
-749
lines changed

6 files changed

+77
-749
lines changed

src/django_github_app/mentions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
from django.utils import timezone
1111
from gidgethub import sansio
1212

13-
from .permissions import Permission
14-
1513

1614
class EventAction(NamedTuple):
1715
event: str
@@ -121,7 +119,6 @@ def from_event(cls, event: sansio.Event) -> Comment:
121119
class MentionContext:
122120
comment: Comment
123121
triggered_by: Mention
124-
user_permission: Permission
125122
scope: MentionScope | None
126123

127124

@@ -169,7 +166,10 @@ def check_pattern_match(
169166
def parse_mentions_for_username(
170167
event: sansio.Event, username_pattern: str | re.Pattern[str] | None = None
171168
) -> list[Mention]:
172-
body = event.data.get("comment", {}).get("body", "")
169+
comment = event.data.get("comment", {})
170+
if comment is None:
171+
comment = {}
172+
body = comment.get("body", "")
173173

174174
if not body:
175175
return []

src/django_github_app/permissions.py

Lines changed: 0 additions & 139 deletions
This file was deleted.

src/django_github_app/routing.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
from .mentions import check_pattern_match
2424
from .mentions import get_event_scope
2525
from .mentions import parse_mentions_for_username
26-
from .permissions import Permission
27-
from .permissions import aget_user_permission_from_event
28-
from .permissions import get_user_permission_from_event
2926

3027
AsyncCallback = Callable[..., Awaitable[None]]
3128
SyncCallback = Callable[..., None]
@@ -80,11 +77,9 @@ def decorator(func: CB) -> CB:
8077

8178
def mention(self, **kwargs: Any) -> Callable[[CB], CB]:
8279
def decorator(func: CB) -> CB:
83-
# Support both old 'command' and new 'pattern' parameters
84-
pattern = kwargs.pop("pattern", kwargs.pop("command", None))
80+
pattern = kwargs.pop("pattern", None)
8581
username = kwargs.pop("username", None)
8682
scope = kwargs.pop("scope", None)
87-
permission = kwargs.pop("permission", None)
8883

8984
@wraps(func)
9085
async def async_wrapper(
@@ -98,14 +93,6 @@ async def async_wrapper(
9893
if not mentions:
9994
return
10095

101-
if permission is not None:
102-
user_permission = await aget_user_permission_from_event(event, gh)
103-
required_perm = Permission[permission.upper()]
104-
if user_permission < required_perm:
105-
return
106-
else:
107-
user_permission = Permission.NONE
108-
10996
comment = Comment.from_event(event)
11097
comment.mentions = mentions
11198

@@ -119,7 +106,6 @@ async def async_wrapper(
119106
kwargs["mention"] = MentionContext(
120107
comment=comment,
121108
triggered_by=mention,
122-
user_permission=user_permission,
123109
scope=event_scope,
124110
)
125111

@@ -137,14 +123,6 @@ def sync_wrapper(
137123
if not mentions:
138124
return
139125

140-
if permission is not None:
141-
user_permission = get_user_permission_from_event(event, gh)
142-
required_perm = Permission[permission.upper()]
143-
if user_permission < required_perm:
144-
return
145-
else:
146-
user_permission = Permission.NONE
147-
148126
comment = Comment.from_event(event)
149127
comment.mentions = mentions
150128

@@ -158,7 +136,6 @@ def sync_wrapper(
158136
kwargs["mention"] = MentionContext(
159137
comment=comment,
160138
triggered_by=mention,
161-
user_permission=user_permission,
162139
scope=event_scope,
163140
)
164141

@@ -171,7 +148,6 @@ def sync_wrapper(
171148
wrapper = cast(SyncMentionHandler, sync_wrapper)
172149

173150
wrapper._mention_pattern = pattern
174-
wrapper._mention_permission = permission
175151
wrapper._mention_scope = scope
176152
wrapper._mention_username = username
177153

tests/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def repository_id():
128128

129129
@pytest.fixture
130130
def get_mock_github_api():
131-
def _get_mock_github_api(return_data):
131+
def _get_mock_github_api(return_data, installation_id=12345):
132132
mock_api = AsyncMock(spec=AsyncGitHubAPI)
133133

134134
async def mock_getitem(*args, **kwargs):
@@ -142,6 +142,7 @@ async def mock_getiter(*args, **kwargs):
142142
mock_api.getiter = mock_getiter
143143
mock_api.__aenter__.return_value = mock_api
144144
mock_api.__aexit__.return_value = None
145+
mock_api.installation_id = installation_id
145146

146147
return mock_api
147148

@@ -150,7 +151,7 @@ async def mock_getiter(*args, **kwargs):
150151

151152
@pytest.fixture
152153
def get_mock_github_api_sync():
153-
def _get_mock_github_api_sync(return_data):
154+
def _get_mock_github_api_sync(return_data, installation_id=12345):
154155
from django_github_app.github import SyncGitHubAPI
155156

156157
mock_api = MagicMock(spec=SyncGitHubAPI)
@@ -167,6 +168,7 @@ def mock_post(*args, **kwargs):
167168
mock_api.getitem = mock_getitem
168169
mock_api.getiter = mock_getiter
169170
mock_api.post = mock_post
171+
mock_api.installation_id = installation_id
170172

171173
return mock_api
172174

0 commit comments

Comments
 (0)