-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitguardian_calls.py
295 lines (208 loc) · 9.13 KB
/
gitguardian_calls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import logging
import re
from pygitguardian.models import (
CreateInvitation,
CreateInvitationParameters,
AccessLevel,
Invitation,
CreateTeamInvitation,
Member,
Team,
TeamInvitation,
TeamMember,
Detail,
CreateTeamMember,
CreateTeamMemberParameters,
CreateTeam,
UpdateTeam,
IncidentPermission,
UpdateTeamSource,
Source,
SourceParameters,
TeamSourceParameters,
TeamMemberParameters,
TeamInvitationParameters,
InvitationParameters,
TeamsParameters,
MembersParameters
)
from collections.abc import Generator
from typing import Optional
from config import CONFIG
logger = logging.getLogger(__name__)
def list_all_gg_invitations() -> Generator[Invitation]:
"""Fetch all installations existing on the workspace by iterating over the pagination."""
first_call = True
cursor = ''
while first_call or cursor:
first_call = False
parameters = InvitationParameters(cursor=cursor, per_page=100)
response = CONFIG.gg_client.list_invitations(parameters)
if isinstance(response, Detail):
raise RuntimeError(f"Error while listing members: {response.detail}")
cursor = _extract_cursor(response.next) if response.next else ''
yield from response.data
def list_all_gg_members() -> Generator[Member]:
"""Fetch all members existing on the workspace by iterating over the pagination."""
first_call = True
cursor = ''
while first_call or cursor:
first_call = False
parameters = MembersParameters(cursor=cursor, per_page=100)
response = CONFIG.gg_client.list_members(parameters)
if isinstance(response, Detail):
raise RuntimeError(f"Error while listing members: {response.detail}")
cursor = _extract_cursor(response.next) if response.next else ''
yield from response.data
def list_all_gg_teams() -> Generator[Team]:
"""Fetch all teams existing on the workspace by iterating over the pagination."""
first_call = True
cursor = ''
while first_call or cursor:
first_call = False
parameters = TeamsParameters(cursor=cursor, per_page=100)
response = CONFIG.gg_client.list_teams(parameters)
if isinstance(response, Detail):
raise RuntimeError(f"Error while listing teams: {response.detail}")
cursor = _extract_cursor(response.next) if response.next else ''
yield from response.data
def list_all_gg_sources() -> Generator[Source]:
"""Fetch all GitHub sources existing on the workspace by iterating over the pagination."""
first_call = True
cursor = ''
while first_call or cursor:
first_call = False
parameters = SourceParameters(type="github", cursor=cursor, per_page=100)
response = CONFIG.gg_client.list_sources(parameters)
if isinstance(response, Detail):
raise RuntimeError(f"Error while listing sources: {response.detail}")
cursor = _extract_cursor(response.next) if response.next else ''
yield from response.data
def list_team_sources(team: Team) -> Generator[Source]:
"""Fetch the GitHub sources a team has access to."""
first_call = True
cursor = ''
while first_call or cursor:
first_call = False
parameters = TeamSourceParameters(type="github", cursor=cursor, per_page=100)
response = CONFIG.gg_client.list_team_sources(team_id=team.id, parameters=parameters)
if isinstance(response, Detail):
raise RuntimeError(f"Error while listing team sources: {response.detail}")
cursor = _extract_cursor(response.next) if response.next else ''
yield from response.data
def invite_user(email: str) -> Invitation:
"""Create a new invitation on the workspace for the given email."""
parameters = CreateInvitationParameters(send_email=CONFIG.notify_users)
create_invitation = CreateInvitation(email=email, access_level=AccessLevel.MEMBER)
response = CONFIG.gg_client.create_invitation(create_invitation, parameters=parameters)
if isinstance(response, Detail):
raise RuntimeError(f"Error while inviting user {email}: {response.detail}")
logger.info(f"Invited user {email}")
return response
def create_team(name: str, description: Optional[str]) -> Team:
"""Create a new team on the workspace."""
payload = CreateTeam(name=name, description=description)
response = CONFIG.gg_client.create_team(payload)
if isinstance(response, Detail):
raise RuntimeError(f"Error while creating team {name}: {response.detail}")
logger.info(f"Created team {name}")
return response
def update_team(team_id: int, name: str) -> Team:
"""Update an existing team name on the workspace."""
payload = UpdateTeam(id=team_id, name=name)
response = CONFIG.gg_client.update_team(payload)
if isinstance(response, Detail):
raise RuntimeError(f"Error while updating team {name}: {response.detail}")
logger.info(f"Updated team name {name}")
return response
def delete_team(team: Team) -> None:
"""Remove a team from the workspace."""
response = CONFIG.gg_client.delete_team(team.id)
if isinstance(response, Detail):
raise RuntimeError(f"Error while deleting team {team.name}: {response.detail}")
logger.info(f"Deleted team {team.name}")
return response
def list_team_members(team: Team) -> Generator[TeamMember]:
first_call = True
cursor = ''
while first_call or cursor:
first_call = False
parameters = TeamMemberParameters(cursor=cursor, per_page=100)
response = CONFIG.gg_client.list_team_members(team_id=team.id, parameters=parameters)
if isinstance(response, Detail):
raise RuntimeError(f"Error while listing team members: {response.detail}")
cursor = _extract_cursor(response.next) if response.next else ''
yield from response.data
def add_team_member(team: Team, member_id: int) -> None:
payload = CreateTeamMember(
member_id=member_id,
is_team_leader=False,
incident_permission=IncidentPermission.VIEW,
)
parameters = CreateTeamMemberParameters(send_email=CONFIG.notify_users)
response = CONFIG.gg_client.create_team_member(
team_id=team.id,
member=payload,
parameters=parameters,
)
if response.status_code == 409:
logger.debug(f"User already belongs to team {team.name}")
return
if isinstance(response, Detail):
logger.error(f"Error while adding user to team {team.name}: {response.detail}")
logger.info(f"Added user to team {team.name}")
def delete_team_member(team: Team, team_member: TeamMember) -> None:
response = CONFIG.gg_client.delete_team_member(team_id=team.id, team_member_id=team_member.id)
if isinstance(response, Detail):
logger.error(f"Error while removing user from team {team.name}: {response.detail}")
logger.info(f"Removed user from team {team.name}")
def list_team_invitations(team: Team) -> Generator[TeamInvitation]:
first_call = True
cursor = ''
while first_call or cursor:
first_call = False
parameters = TeamInvitationParameters(cursor=cursor, per_page=100)
response = CONFIG.gg_client.list_team_invitations(team_id=team.id, parameters=parameters)
if isinstance(response, Detail):
raise RuntimeError(f"Error while listing team invitations: {response.detail}")
cursor = _extract_cursor(response.next) if response.next else ''
yield from response.data
def add_team_invitation(team: Team, invitation_id: int) -> None:
payload = CreateTeamInvitation(
invitation_id=invitation_id,
is_team_leader=False,
incident_permission=IncidentPermission.VIEW,
)
response = CONFIG.gg_client.create_team_invitation(
team_id=team.id,
invitation=payload,
)
if response.status_code == 409:
logger.debug(f"User already invited to team {team.name}")
return
if isinstance(response, Detail):
logger.error(f"Error while inviting user to team {team.name}: {response.detail}")
logger.info(f"Invited user to team {team.name}")
def delete_team_invitation(team: Team, team_invitation: TeamInvitation) -> None:
response = CONFIG.gg_client.delete_team_invitation(team_id=team.id, invitation_id=team_invitation.id)
if isinstance(response, Detail):
logger.error(f"Error while removing user from team {team.name}: {response.detail}")
logger.info(f"Removed user from team {team.name}")
def update_team_sources(
gg_team: Team,
sources_to_add: list[int],
sources_to_remove: list[int]
) -> None:
payload = UpdateTeamSource(
team_id=gg_team.id,
sources_to_add=sources_to_add,
sources_to_remove=sources_to_remove,
)
response = CONFIG.gg_client.update_team_source(payload)
if isinstance(response, Detail):
logger.error(f"Error while synchronizing team {gg_team.name} perimeter: {response.detail}")
logger.info(f"Synchronized team {gg_team.name} perimeter")
def _extract_cursor(url: str) -> str:
if result := re.search("(?<=cursor=)\w*(?=&)", url):
return result.group()
return ''