1
+ from typing import Any , BinaryIO , cast , Dict , List , Optional , Type , TYPE_CHECKING , Union
2
+
3
+ import requests
4
+
5
+ import gitlab
1
6
from gitlab import cli
2
7
from gitlab import exceptions as exc
3
8
from gitlab import types
@@ -74,7 +79,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
74
79
75
80
@cli .register_custom_action ("Group" , ("project_id" ,))
76
81
@exc .on_http_error (exc .GitlabTransferProjectError )
77
- def transfer_project (self , project_id , ** kwargs ) :
82
+ def transfer_project (self , project_id : int , ** kwargs : Any ) -> None :
78
83
"""Transfer a project to this group.
79
84
80
85
Args:
@@ -90,7 +95,9 @@ def transfer_project(self, project_id, **kwargs):
90
95
91
96
@cli .register_custom_action ("Group" , ("scope" , "search" ))
92
97
@exc .on_http_error (exc .GitlabSearchError )
93
- def search (self , scope , search , ** kwargs ):
98
+ def search (
99
+ self , scope : str , search : str , ** kwargs : Any
100
+ ) -> Union [gitlab .GitlabList , List [Dict [str , Any ]]]:
94
101
"""Search the group resources matching the provided string.'
95
102
96
103
Args:
@@ -111,7 +118,9 @@ def search(self, scope, search, **kwargs):
111
118
112
119
@cli .register_custom_action ("Group" , ("cn" , "group_access" , "provider" ))
113
120
@exc .on_http_error (exc .GitlabCreateError )
114
- def add_ldap_group_link (self , cn , group_access , provider , ** kwargs ):
121
+ def add_ldap_group_link (
122
+ self , cn : str , group_access : int , provider : str , ** kwargs : Any
123
+ ) -> None :
115
124
"""Add an LDAP group link.
116
125
117
126
Args:
@@ -131,7 +140,9 @@ def add_ldap_group_link(self, cn, group_access, provider, **kwargs):
131
140
132
141
@cli .register_custom_action ("Group" , ("cn" ,), ("provider" ,))
133
142
@exc .on_http_error (exc .GitlabDeleteError )
134
- def delete_ldap_group_link (self , cn , provider = None , ** kwargs ):
143
+ def delete_ldap_group_link (
144
+ self , cn : str , provider : Optional [str ] = None , ** kwargs : Any
145
+ ) -> None :
135
146
"""Delete an LDAP group link.
136
147
137
148
Args:
@@ -151,7 +162,7 @@ def delete_ldap_group_link(self, cn, provider=None, **kwargs):
151
162
152
163
@cli .register_custom_action ("Group" )
153
164
@exc .on_http_error (exc .GitlabCreateError )
154
- def ldap_sync (self , ** kwargs ) :
165
+ def ldap_sync (self , ** kwargs : Any ) -> None :
155
166
"""Sync LDAP groups.
156
167
157
168
Args:
@@ -166,7 +177,13 @@ def ldap_sync(self, **kwargs):
166
177
167
178
@cli .register_custom_action ("Group" , ("group_id" , "group_access" ), ("expires_at" ,))
168
179
@exc .on_http_error (exc .GitlabCreateError )
169
- def share (self , group_id , group_access , expires_at = None , ** kwargs ):
180
+ def share (
181
+ self ,
182
+ group_id : int ,
183
+ group_access : int ,
184
+ expires_at : Optional [str ] = None ,
185
+ ** kwargs : Any ,
186
+ ) -> None :
170
187
"""Share the group with a group.
171
188
172
189
Args:
@@ -177,18 +194,24 @@ def share(self, group_id, group_access, expires_at=None, **kwargs):
177
194
Raises:
178
195
GitlabAuthenticationError: If authentication is not correct
179
196
GitlabCreateError: If the server failed to perform the request
197
+
198
+ Returns:
199
+ Group
180
200
"""
181
201
path = f"/groups/{ self .get_id ()} /share"
182
202
data = {
183
203
"group_id" : group_id ,
184
204
"group_access" : group_access ,
185
205
"expires_at" : expires_at ,
186
206
}
187
- self .manager .gitlab .http_post (path , post_data = data , ** kwargs )
207
+ server_data = self .manager .gitlab .http_post (path , post_data = data , ** kwargs )
208
+ if TYPE_CHECKING :
209
+ assert isinstance (server_data , dict )
210
+ self ._update_attrs (server_data )
188
211
189
212
@cli .register_custom_action ("Group" , ("group_id" ,))
190
213
@exc .on_http_error (exc .GitlabDeleteError )
191
- def unshare (self , group_id , ** kwargs ) :
214
+ def unshare (self , group_id : int , ** kwargs : Any ) -> None :
192
215
"""Delete a shared group link within a group.
193
216
194
217
Args:
@@ -269,8 +292,18 @@ class GroupManager(CRUDMixin, RESTManager):
269
292
)
270
293
_types = {"avatar" : types .ImageAttribute , "skip_groups" : types .ListAttribute }
271
294
295
+ def get (self , id : Union [str , int ], lazy : bool = False , ** kwargs : Any ) -> Group :
296
+ return cast (Group , super ().get (id = id , lazy = lazy , ** kwargs ))
297
+
272
298
@exc .on_http_error (exc .GitlabImportError )
273
- def import_group (self , file , path , name , parent_id = None , ** kwargs ):
299
+ def import_group (
300
+ self ,
301
+ file : BinaryIO ,
302
+ path : str ,
303
+ name : str ,
304
+ parent_id : Optional [str ] = None ,
305
+ ** kwargs : Any ,
306
+ ) -> Union [Dict [str , Any ], requests .Response ]:
274
307
"""Import a group from an archive file.
275
308
276
309
Args:
@@ -304,7 +337,7 @@ class GroupSubgroup(RESTObject):
304
337
305
338
class GroupSubgroupManager (ListMixin , RESTManager ):
306
339
_path = "/groups/%(group_id)s/subgroups"
307
- _obj_cls = GroupSubgroup
340
+ _obj_cls : Union [ Type [ "GroupDescendantGroup" ], Type [ GroupSubgroup ]] = GroupSubgroup
308
341
_from_parent_attrs = {"group_id" : "id" }
309
342
_list_filters = (
310
343
"skip_groups" ,
@@ -331,4 +364,4 @@ class GroupDescendantGroupManager(GroupSubgroupManager):
331
364
"""
332
365
333
366
_path = "/groups/%(group_id)s/descendant_groups"
334
- _obj_cls = GroupDescendantGroup
367
+ _obj_cls : Type [ GroupDescendantGroup ] = GroupDescendantGroup
0 commit comments