14
14
from .client import GithubRestApiClient
15
15
from .interpretations .relationship .user import simplify_user
16
16
from .logging import get_plugin_logger
17
- from .types import GithubRepo , RepositoryRecord
17
+ from .types import (
18
+ GithubRepo ,
19
+ GithubUser ,
20
+ JSONType ,
21
+ RepositoryRecord ,
22
+ SimplifiedUser ,
23
+ Webhook ,
24
+ )
18
25
from .types .enums import CollaboratorAffiliation , OrgRepoType , UserRepoType
19
26
20
27
logger = get_plugin_logger (__name__ )
@@ -63,7 +70,11 @@ def from_dict(raw_dict: dict[str, Any]) -> "CollectWhichRepos":
63
70
class GithubReposExtractor (Extractor ):
64
71
def __init__ (
65
72
self ,
73
+ * ,
66
74
collecting : CollectWhichRepos | dict [str , Any ] | None = None ,
75
+ include_languages : bool | None = True ,
76
+ include_webhooks : bool | None = True ,
77
+ include_collaborators : bool | None = True ,
67
78
** kwargs : Any ,
68
79
):
69
80
if isinstance (collecting , CollectWhichRepos ):
@@ -72,7 +83,18 @@ def __init__(
72
83
self .collecting = CollectWhichRepos .from_dict (collecting )
73
84
else :
74
85
self .collecting = CollectWhichRepos ()
86
+
87
+ self .include_languages = include_languages is True
88
+ self .include_webhooks = include_webhooks is True
89
+ self .include_collaborators = include_collaborators is True
90
+
75
91
self .client = GithubRestApiClient (** kwargs )
92
+ logger .info (
93
+ "%s, %s, %s" ,
94
+ self .include_collaborators ,
95
+ self .include_webhooks ,
96
+ self .include_languages ,
97
+ )
76
98
77
99
async def extract_records (self ) -> AsyncGenerator [RepositoryRecord ]:
78
100
if self .collecting .all_public :
@@ -93,37 +115,52 @@ async def _extract_repo(self, repo: GithubRepo) -> RepositoryRecord:
93
115
repo ["user_owner" ] = owner
94
116
elif owner :
95
117
repo ["org_owner" ] = owner
96
- repo ["languages" ] = [
97
- {"name" : lang }
98
- async for lang in self .client .fetch_languages_for_repo (
99
- owner_login = owner ["login" ],
100
- repo_name = repo ["name" ],
101
- )
102
- ]
103
- repo ["webhooks" ] = [
104
- hook
105
- async for hook in self .client .fetch_webhooks_for_repo (
106
- owner_login = owner ["login" ],
107
- repo_name = repo ["name" ],
108
- )
109
- ]
110
- repo ["collaborators" ] = []
111
118
119
+ if self .include_languages :
120
+ repo ["languages" ] = await self ._add_languages (owner , repo )
121
+ if self .include_webhooks :
122
+ repo ["webhooks" ] = await self ._add_webhooks (owner , repo )
123
+ if self .include_collaborators :
124
+ repo ["collaborators" ] = await self ._add_collaborators (owner , repo )
125
+
126
+ logger .debug ("yielded GithubRepo{full_name=%s}" , repo ["full_name" ])
127
+ return repo
128
+
129
+ async def _add_collaborators (
130
+ self , owner : GithubUser , repo : GithubRepo
131
+ ) -> list [SimplifiedUser ]:
132
+ collaborators = []
112
133
async for user in self .client .fetch_collaborators_for_repo (
113
134
owner_login = owner ["login" ],
114
135
repo_name = repo ["name" ],
115
136
affiliation = CollaboratorAffiliation .DIRECT ,
116
137
):
117
- repo [ " collaborators" ] .append (simplify_user (user , affiliation = "direct" ))
138
+ collaborators .append (simplify_user (user , affiliation = "direct" ))
118
139
async for user in self .client .fetch_collaborators_for_repo (
119
140
owner_login = owner ["login" ],
120
141
repo_name = repo ["name" ],
121
142
affiliation = CollaboratorAffiliation .OUTSIDE ,
122
143
):
123
- repo ["collaborators" ].append (simplify_user (user , affiliation = "outside" ))
144
+ collaborators .append (simplify_user (user , affiliation = "outside" ))
145
+ return collaborators
124
146
125
- logger .debug ("yielded GithubRepo{full_name=%s}" , repo ["full_name" ])
126
- return repo
147
+ async def _add_webhooks (self , owner : GithubUser , repo : GithubRepo ) -> list [Webhook ]:
148
+ return [
149
+ hook
150
+ async for hook in self .client .fetch_webhooks_for_repo (
151
+ owner_login = owner ["login" ],
152
+ repo_name = repo ["name" ],
153
+ )
154
+ ]
155
+
156
+ async def _add_languages (self , owner : GithubUser , repo : GithubRepo ) -> JSONType :
157
+ return [
158
+ {"name" : lang }
159
+ async for lang in self .client .fetch_languages_for_repo (
160
+ owner_login = owner ["login" ],
161
+ repo_name = repo ["name" ],
162
+ )
163
+ ]
127
164
128
165
async def _fetch_repos_by_org (self ) -> AsyncGenerator [GithubRepo ]:
129
166
async for org in self .client .fetch_all_organizations ():
0 commit comments