44from enum import Enum
55from typing import Any
66
7- from asgiref .sync import async_to_sync
87from django .db import models
98from django .utils import timezone
109from gidgethub import abc
1110from gidgethub import sansio
1211from gidgethub .apps import get_installation_access_token
1312from gidgethub .apps import get_jwt
1413
14+ from ._sync import async_to_sync_method
1515from ._typing import override
1616from .conf import app_settings
1717from .github import AsyncGitHubAPI
@@ -27,9 +27,6 @@ async def acreate_from_event(self, event: sansio.Event):
2727 received_at = timezone .now (),
2828 )
2929
30- def create_from_event (self , event : sansio .Event ):
31- return async_to_sync (self .acreate_from_event )(event )
32-
3330 async def acleanup_events (
3431 self , days_to_keep : int = app_settings .DAYS_TO_KEEP_EVENTS
3532 ):
@@ -38,8 +35,8 @@ async def acleanup_events(
3835 ).adelete ()
3936 return deleted
4037
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 )
4340
4441
4542class EventLog (models .Model ):
@@ -77,24 +74,19 @@ async def acreate_from_event(self, event: sansio.Event):
7774
7875 return installation
7976
80- def create_from_event (self , event : sansio .Event ):
81- return async_to_sync (self .acreate_from_event )(event )
82-
8377 async def acreate_from_gh_data (self , data : dict [str , str ]):
8478 return await self .acreate (installation_id = data ["id" ], data = data )
8579
86- def create_from_gh_data (self , data : dict [str , str ]):
87- return async_to_sync (self .acreate_from_gh_data )(data )
88-
8980 async def aget_from_event (self , event : sansio .Event ):
9081 try :
9182 installation_id = event .data ["installation" ]["id" ]
9283 return await self .aget (installation_id = installation_id )
9384 except (Installation .DoesNotExist , KeyError ):
9485 return None
9586
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 )
9890
9991
10092class InstallationStatus (models .IntegerChoices ):
@@ -147,9 +139,6 @@ async def aget_access_token(self, gh: abc.GitHubAPI): # pragma: no cover
147139 )
148140 return data .get ("token" )
149141
150- def get_access_token (self , gh : abc .GitHubAPI ): # pragma: no cover
151- return async_to_sync (self .aget_access_token )(gh )
152-
153142 async def arefresh_from_gh (self , account_type : AccountType , account_name : str ):
154143 match account_type :
155144 case AccountType .ORG :
@@ -171,9 +160,6 @@ async def arefresh_from_gh(self, account_type: AccountType, account_name: str):
171160 self .data = data
172161 await self .asave ()
173162
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-
177163 async def aget_repos (self , params : dict [str , Any ] | None = None ):
178164 url = GitHubAPIUrl (
179165 GitHubAPIEndpoint .INSTALLATION_REPOS ,
@@ -186,13 +172,14 @@ async def aget_repos(self, params: dict[str, Any] | None = None):
186172 ]
187173 return repos
188174
189- def get_repos (self , params : dict [str , Any ] | None = None ):
190- return async_to_sync (self .aget_repos )(params )
191-
192175 @property
193176 def app_slug (self ):
194177 return self .data .get ("app_slug" , app_settings .SLUG )
195178
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+
196183
197184class RepositoryManager (models .Manager ["Repository" ]):
198185 async def acreate_from_gh_data (
@@ -217,20 +204,15 @@ async def acreate_from_gh_data(
217204 full_name = data ["full_name" ],
218205 )
219206
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-
225207 async def aget_from_event (self , event : sansio .Event ):
226208 try :
227209 repository_id = event .data ["repository" ]["id" ]
228210 return await self .aget (repository_id = repository_id )
229211 except Repository .DoesNotExist :
230212 return None
231213
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 )
234216
235217
236218class Repository (models .Model ):
@@ -266,13 +248,12 @@ async def aget_issues(self, params: dict[str, Any] | None = None):
266248 issues = [issue async for issue in gh .getiter (url .full_url )]
267249 return issues
268250
269- def get_issues (self , params : dict [str , Any ] | None = None ):
270- return async_to_sync (self .aget_issues )(params )
271-
272251 @property
273252 def owner (self ):
274253 return self .full_name .split ("/" )[0 ]
275254
276255 @property
277256 def repo (self ):
278257 return self .full_name .split ("/" )[1 ]
258+
259+ get_issues = async_to_sync_method (aget_issues )
0 commit comments