4
4
from enum import Enum
5
5
from typing import Any
6
6
7
- from asgiref .sync import async_to_sync
8
7
from django .db import models
9
8
from django .utils import timezone
10
9
from gidgethub import abc
11
10
from gidgethub import sansio
12
11
from gidgethub .apps import get_installation_access_token
13
12
from gidgethub .apps import get_jwt
14
13
14
+ from ._sync import async_to_sync_method
15
15
from ._typing import override
16
16
from .conf import app_settings
17
17
from .github import AsyncGitHubAPI
@@ -27,9 +27,6 @@ async def acreate_from_event(self, event: sansio.Event):
27
27
received_at = timezone .now (),
28
28
)
29
29
30
- def create_from_event (self , event : sansio .Event ):
31
- return async_to_sync (self .acreate_from_event )(event )
32
-
33
30
async def acleanup_events (
34
31
self , days_to_keep : int = app_settings .DAYS_TO_KEEP_EVENTS
35
32
):
@@ -38,8 +35,8 @@ async def acleanup_events(
38
35
).adelete ()
39
36
return deleted
40
37
41
- def cleanup_events ( self , days_to_keep : int = 7 ):
42
- return async_to_sync ( self . acleanup_events )( days_to_keep )
38
+ create_from_event = async_to_sync_method ( acreate_from_event )
39
+ cleanup_events = async_to_sync_method ( acleanup_events )
43
40
44
41
45
42
class EventLog (models .Model ):
@@ -77,24 +74,19 @@ async def acreate_from_event(self, event: sansio.Event):
77
74
78
75
return installation
79
76
80
- def create_from_event (self , event : sansio .Event ):
81
- return async_to_sync (self .acreate_from_event )(event )
82
-
83
77
async def acreate_from_gh_data (self , data : dict [str , str ]):
84
78
return await self .acreate (installation_id = data ["id" ], data = data )
85
79
86
- def create_from_gh_data (self , data : dict [str , str ]):
87
- return async_to_sync (self .acreate_from_gh_data )(data )
88
-
89
80
async def aget_from_event (self , event : sansio .Event ):
90
81
try :
91
82
installation_id = event .data ["installation" ]["id" ]
92
83
return await self .aget (installation_id = installation_id )
93
84
except (Installation .DoesNotExist , KeyError ):
94
85
return None
95
86
96
- def get_from_event (self , event : sansio .Event ):
97
- return async_to_sync (self .aget_from_event )(event )
87
+ create_from_event = async_to_sync_method (acreate_from_event )
88
+ create_from_gh_data = async_to_sync_method (acreate_from_gh_data )
89
+ get_from_event = async_to_sync_method (aget_from_event )
98
90
99
91
100
92
class InstallationStatus (models .IntegerChoices ):
@@ -147,9 +139,6 @@ async def aget_access_token(self, gh: abc.GitHubAPI): # pragma: no cover
147
139
)
148
140
return data .get ("token" )
149
141
150
- def get_access_token (self , gh : abc .GitHubAPI ): # pragma: no cover
151
- return async_to_sync (self .aget_access_token )(gh )
152
-
153
142
async def arefresh_from_gh (self , account_type : AccountType , account_name : str ):
154
143
match account_type :
155
144
case AccountType .ORG :
@@ -171,9 +160,6 @@ async def arefresh_from_gh(self, account_type: AccountType, account_name: str):
171
160
self .data = data
172
161
await self .asave ()
173
162
174
- def refresh_from_gh (self , account_type : AccountType , account_name : str ):
175
- return async_to_sync (self .arefresh_from_gh )(account_type , account_name )
176
-
177
163
async def aget_repos (self , params : dict [str , Any ] | None = None ):
178
164
url = GitHubAPIUrl (
179
165
GitHubAPIEndpoint .INSTALLATION_REPOS ,
@@ -186,13 +172,14 @@ async def aget_repos(self, params: dict[str, Any] | None = None):
186
172
]
187
173
return repos
188
174
189
- def get_repos (self , params : dict [str , Any ] | None = None ):
190
- return async_to_sync (self .aget_repos )(params )
191
-
192
175
@property
193
176
def app_slug (self ):
194
177
return self .data .get ("app_slug" , app_settings .SLUG )
195
178
179
+ get_access_token = async_to_sync_method (aget_access_token )
180
+ refresh_from_gh = async_to_sync_method (arefresh_from_gh )
181
+ get_repos = async_to_sync_method (aget_repos )
182
+
196
183
197
184
class RepositoryManager (models .Manager ["Repository" ]):
198
185
async def acreate_from_gh_data (
@@ -217,20 +204,15 @@ async def acreate_from_gh_data(
217
204
full_name = data ["full_name" ],
218
205
)
219
206
220
- def create_from_gh_data (
221
- self , data : dict [str , str ] | list [dict [str , str ]], installation : Installation
222
- ):
223
- return async_to_sync (self .acreate_from_gh_data )(data , installation )
224
-
225
207
async def aget_from_event (self , event : sansio .Event ):
226
208
try :
227
209
repository_id = event .data ["repository" ]["id" ]
228
210
return await self .aget (repository_id = repository_id )
229
211
except Repository .DoesNotExist :
230
212
return None
231
213
232
- def get_from_event ( self , event : sansio . Event ):
233
- return async_to_sync ( self . aget_from_event )( event )
214
+ create_from_gh_data = async_to_sync_method ( acreate_from_gh_data )
215
+ get_from_event = async_to_sync_method ( aget_from_event )
234
216
235
217
236
218
class Repository (models .Model ):
@@ -266,13 +248,12 @@ async def aget_issues(self, params: dict[str, Any] | None = None):
266
248
issues = [issue async for issue in gh .getiter (url .full_url )]
267
249
return issues
268
250
269
- def get_issues (self , params : dict [str , Any ] | None = None ):
270
- return async_to_sync (self .aget_issues )(params )
271
-
272
251
@property
273
252
def owner (self ):
274
253
return self .full_name .split ("/" )[0 ]
275
254
276
255
@property
277
256
def repo (self ):
278
257
return self .full_name .split ("/" )[1 ]
258
+
259
+ get_issues = async_to_sync_method (aget_issues )
0 commit comments