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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
from typing import Any, BinaryIO, cast, Dict, List, Optional, Type, TYPE_CHECKING, Union
import requests
import gitlab
from gitlab import cli
from gitlab import exceptions as exc
from gitlab import types
from gitlab.base import RequiredOptional, RESTManager, RESTObject
from gitlab.mixins import CRUDMixin, ListMixin, ObjectDeleteMixin, SaveMixin
from .access_requests import GroupAccessRequestManager # noqa: F401
from .audit_events import GroupAuditEventManager # noqa: F401
from .badges import GroupBadgeManager # noqa: F401
from .boards import GroupBoardManager # noqa: F401
from .clusters import GroupClusterManager # noqa: F401
from .custom_attributes import GroupCustomAttributeManager # noqa: F401
from .deploy_tokens import GroupDeployTokenManager # noqa: F401
from .epics import GroupEpicManager # noqa: F401
from .export_import import GroupExportManager, GroupImportManager # noqa: F401
from .hooks import GroupHookManager # noqa: F401
from .issues import GroupIssueManager # noqa: F401
from .labels import GroupLabelManager # noqa: F401
from .members import ( # noqa: F401
GroupBillableMemberManager,
GroupMemberAllManager,
GroupMemberManager,
)
from .merge_requests import GroupMergeRequestManager # noqa: F401
from .milestones import GroupMilestoneManager # noqa: F401
from .notification_settings import GroupNotificationSettingsManager # noqa: F401
from .packages import GroupPackageManager # noqa: F401
from .projects import GroupProjectManager # noqa: F401
from .runners import GroupRunnerManager # noqa: F401
from .statistics import GroupIssuesStatisticsManager # noqa: F401
from .variables import GroupVariableManager # noqa: F401
from .wikis import GroupWikiManager # noqa: F401
__all__ = [
"Group",
"GroupManager",
"GroupDescendantGroup",
"GroupDescendantGroupManager",
"GroupSubgroup",
"GroupSubgroupManager",
]
class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
_short_print_attr = "name"
accessrequests: GroupAccessRequestManager
audit_events: GroupAuditEventManager
badges: GroupBadgeManager
billable_members: GroupBillableMemberManager
boards: GroupBoardManager
clusters: GroupClusterManager
customattributes: GroupCustomAttributeManager
deploytokens: GroupDeployTokenManager
descendant_groups: "GroupDescendantGroupManager"
epics: GroupEpicManager
exports: GroupExportManager
hooks: GroupHookManager
imports: GroupImportManager
issues: GroupIssueManager
issues_statistics: GroupIssuesStatisticsManager
labels: GroupLabelManager
members: GroupMemberManager
members_all: GroupMemberAllManager
mergerequests: GroupMergeRequestManager
milestones: GroupMilestoneManager
notificationsettings: GroupNotificationSettingsManager
packages: GroupPackageManager
projects: GroupProjectManager
runners: GroupRunnerManager
subgroups: "GroupSubgroupManager"
variables: GroupVariableManager
wikis: GroupWikiManager
@cli.register_custom_action("Group", ("project_id",))
@exc.on_http_error(exc.GitlabTransferProjectError)
def transfer_project(self, project_id: int, **kwargs: Any) -> None:
"""Transfer a project to this group.
Args:
to_project_id: ID of the project to transfer
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabTransferProjectError: If the project could not be transferred
"""
path = f"/groups/{self.id}/projects/{project_id}"
self.manager.gitlab.http_post(path, **kwargs)
@cli.register_custom_action("Group", ("scope", "search"))
@exc.on_http_error(exc.GitlabSearchError)
def search(
self, scope: str, search: str, **kwargs: Any
) -> Union[gitlab.GitlabList, List[Dict[str, Any]]]:
"""Search the group resources matching the provided string.'
Args:
scope: Scope of the search
search: Search string
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabSearchError: If the server failed to perform the request
Returns:
A list of dicts describing the resources found.
"""
data = {"scope": scope, "search": search}
path = f"/groups/{self.get_id()}/search"
return self.manager.gitlab.http_list(path, query_data=data, **kwargs)
@cli.register_custom_action("Group", ("cn", "group_access", "provider"))
@exc.on_http_error(exc.GitlabCreateError)
def add_ldap_group_link(
self, cn: str, group_access: int, provider: str, **kwargs: Any
) -> None:
"""Add an LDAP group link.
Args:
cn: CN of the LDAP group
group_access: Minimum access level for members of the LDAP
group
provider: LDAP provider for the LDAP group
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabCreateError: If the server cannot perform the request
"""
path = f"/groups/{self.get_id()}/ldap_group_links"
data = {"cn": cn, "group_access": group_access, "provider": provider}
self.manager.gitlab.http_post(path, post_data=data, **kwargs)
@cli.register_custom_action("Group", ("cn",), ("provider",))
@exc.on_http_error(exc.GitlabDeleteError)
def delete_ldap_group_link(
self, cn: str, provider: Optional[str] = None, **kwargs: Any
) -> None:
"""Delete an LDAP group link.
Args:
cn: CN of the LDAP group
provider: LDAP provider for the LDAP group
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabDeleteError: If the server cannot perform the request
"""
path = f"/groups/{self.get_id()}/ldap_group_links"
if provider is not None:
path += f"/{provider}"
path += f"/{cn}"
self.manager.gitlab.http_delete(path)
@cli.register_custom_action("Group")
@exc.on_http_error(exc.GitlabCreateError)
def ldap_sync(self, **kwargs: Any) -> None:
"""Sync LDAP groups.
Args:
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabCreateError: If the server cannot perform the request
"""
path = f"/groups/{self.get_id()}/ldap_sync"
self.manager.gitlab.http_post(path, **kwargs)
@cli.register_custom_action("Group", ("group_id", "group_access"), ("expires_at",))
@exc.on_http_error(exc.GitlabCreateError)
def share(
self,
group_id: int,
group_access: int,
expires_at: Optional[str] = None,
**kwargs: Any,
) -> None:
"""Share the group with a group.
Args:
group_id: ID of the group.
group_access: Access level for the group.
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabCreateError: If the server failed to perform the request
Returns:
Group
"""
path = f"/groups/{self.get_id()}/share"
data = {
"group_id": group_id,
"group_access": group_access,
"expires_at": expires_at,
}
server_data = self.manager.gitlab.http_post(path, post_data=data, **kwargs)
if TYPE_CHECKING:
assert isinstance(server_data, dict)
self._update_attrs(server_data)
@cli.register_custom_action("Group", ("group_id",))
@exc.on_http_error(exc.GitlabDeleteError)
def unshare(self, group_id: int, **kwargs: Any) -> None:
"""Delete a shared group link within a group.
Args:
group_id: ID of the group.
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabDeleteError: If the server failed to perform the request
"""
path = f"/groups/{self.get_id()}/share/{group_id}"
self.manager.gitlab.http_delete(path, **kwargs)
class GroupManager(CRUDMixin, RESTManager):
_path = "/groups"
_obj_cls = Group
_list_filters = (
"skip_groups",
"all_available",
"search",
"order_by",
"sort",
"statistics",
"owned",
"with_custom_attributes",
"min_access_level",
"top_level_only",
)
_create_attrs = RequiredOptional(
required=("name", "path"),
optional=(
"description",
"membership_lock",
"visibility",
"share_with_group_lock",
"require_two_factor_authentication",
"two_factor_grace_period",
"project_creation_level",
"auto_devops_enabled",
"subgroup_creation_level",
"emails_disabled",
"avatar",
"mentions_disabled",
"lfs_enabled",
"request_access_enabled",
"parent_id",
"default_branch_protection",
"shared_runners_minutes_limit",
"extra_shared_runners_minutes_limit",
),
)
_update_attrs = RequiredOptional(
optional=(
"name",
"path",
"description",
"membership_lock",
"share_with_group_lock",
"visibility",
"require_two_factor_authentication",
"two_factor_grace_period",
"project_creation_level",
"auto_devops_enabled",
"subgroup_creation_level",
"emails_disabled",
"avatar",
"mentions_disabled",
"lfs_enabled",
"request_access_enabled",
"default_branch_protection",
"file_template_project_id",
"shared_runners_minutes_limit",
"extra_shared_runners_minutes_limit",
"prevent_forking_outside_group",
"shared_runners_setting",
),
)
_types = {"avatar": types.ImageAttribute, "skip_groups": types.ListAttribute}
def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> Group:
return cast(Group, super().get(id=id, lazy=lazy, **kwargs))
@exc.on_http_error(exc.GitlabImportError)
def import_group(
self,
file: BinaryIO,
path: str,
name: str,
parent_id: Optional[str] = None,
**kwargs: Any,
) -> Union[Dict[str, Any], requests.Response]:
"""Import a group from an archive file.
Args:
file: Data or file object containing the group
path: The path for the new group to be imported.
name: The name for the new group.
parent_id: ID of a parent group that the group will
be imported into.
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabImportError: If the server failed to perform the request
Returns:
A representation of the import status.
"""
files = {"file": ("file.tar.gz", file, "application/octet-stream")}
data = {"path": path, "name": name}
if parent_id is not None:
data["parent_id"] = parent_id
return self.gitlab.http_post(
"/groups/import", post_data=data, files=files, **kwargs
)
class GroupSubgroup(RESTObject):
pass
class GroupSubgroupManager(ListMixin, RESTManager):
_path = "/groups/{group_id}/subgroups"
_obj_cls: Union[Type["GroupDescendantGroup"], Type[GroupSubgroup]] = GroupSubgroup
_from_parent_attrs = {"group_id": "id"}
_list_filters = (
"skip_groups",
"all_available",
"search",
"order_by",
"sort",
"statistics",
"owned",
"with_custom_attributes",
"min_access_level",
)
_types = {"skip_groups": types.ListAttribute}
class GroupDescendantGroup(RESTObject):
pass
class GroupDescendantGroupManager(GroupSubgroupManager):
"""
This manager inherits from GroupSubgroupManager as descendant groups
share all attributes with subgroups, except the path and object class.
"""
_path = "/groups/{group_id}/descendant_groups"
_obj_cls: Type[GroupDescendantGroup] = GroupDescendantGroup
|