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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
######
Groups
######
Groups
======
Reference
---------
* v4 API:
+ :class:`gitlab.v4.objects.Group`
+ :class:`gitlab.v4.objects.GroupManager`
+ :attr:`gitlab.Gitlab.groups`
* GitLab API: https://docs.gitlab.com/ce/api/groups.html
Examples
--------
List the groups::
groups = gl.groups.list()
Get a group's detail::
group = gl.groups.get(group_id)
List a group's projects::
projects = group.projects.list()
List a group's shared projects::
projects = group.shared_projects.list()
.. note::
``GroupProject`` and ``SharedProject`` objects returned by these two API calls
are very limited, and do not provide all the features of ``Project`` objects.
If you need to manipulate projects, create a new ``Project`` object::
first_group_project = group.projects.list()[0]
manageable_project = gl.projects.get(first_group_project.id, lazy=True)
You can filter and sort the result using the following parameters:
* ``archived``: limit by archived status
* ``visibility``: limit by visibility. Allowed values are ``public``,
``internal`` and ``private``
* ``search``: limit to groups matching the given value
* ``order_by``: sort by criteria. Allowed values are ``id``, ``name``, ``path``,
``created_at``, ``updated_at`` and ``last_activity_at``
* ``sort``: sort order: ``asc`` or ``desc``
* ``ci_enabled_first``: return CI enabled groups first
* ``include_subgroups``: include projects in subgroups
Create a group::
group = gl.groups.create({'name': 'group1', 'path': 'group1'})
.. warning::
On GitLab.com, creating top-level groups is currently
`not permitted using the API <https://docs.gitlab.com/ee/api/groups.html#new-group>`_.
You can only use the API to create subgroups.
Create a subgroup under an existing group::
subgroup = gl.groups.create({'name': 'subgroup1', 'path': 'subgroup1', 'parent_id': parent_group_id})
Update a group::
group.description = 'My awesome group'
group.save()
Set the avatar image for a group::
# the avatar image can be passed as data (content of the file) or as a file
# object opened in binary mode
group.avatar = open('path/to/file.png', 'rb')
group.save()
Remove a group::
gl.groups.delete(group_id)
# or
group.delete()
Restore a Group marked for deletion (Premium only):::
group.restore()
Share/unshare the group with a group::
group.share(group2.id, gitlab.const.AccessLevel.DEVELOPER)
group.unshare(group2.id)
Import / Export
===============
You can export groups from gitlab, and re-import them to create new groups.
Reference
---------
* v4 API:
+ :class:`gitlab.v4.objects.GroupExport`
+ :class:`gitlab.v4.objects.GroupExportManager`
+ :attr:`gitlab.v4.objects.Group.exports`
+ :class:`gitlab.v4.objects.GroupImport`
+ :class:`gitlab.v4.objects.GroupImportManager`
+ :attr:`gitlab.v4.objects.Group.imports`
+ :attr:`gitlab.v4.objects.GroupManager.import_group`
* GitLab API: https://docs.gitlab.com/ce/api/group_import_export.html
Examples
--------
A group export is an asynchronous operation. To retrieve the archive
generated by GitLab you need to:
#. Create an export using the API
#. Wait for the export to be done
#. Download the result
.. warning::
Unlike the Project Export API, GitLab does not provide an export_status
for Group Exports. It is up to the user to ensure the export is finished.
However, Group Exports only contain metadata, so they are much faster
than Project Exports.
::
# Create the export
group = gl.groups.get(my_group)
export = group.exports.create()
# Wait for the export to finish
time.sleep(3)
# Download the result
with open('/tmp/export.tgz', 'wb') as f:
export.download(streamed=True, action=f.write)
Import the group::
with open('/tmp/export.tgz', 'rb') as f:
gl.groups.import_group(f, path='imported-group', name="Imported Group")
Subgroups
=========
Reference
---------
* v4 API:
+ :class:`gitlab.v4.objects.GroupSubgroup`
+ :class:`gitlab.v4.objects.GroupSubgroupManager`
+ :attr:`gitlab.v4.objects.Group.subgroups`
Examples
--------
List the subgroups for a group::
subgroups = group.subgroups.list()
.. note::
The ``GroupSubgroup`` objects don't expose the same API as the ``Group``
objects. If you need to manipulate a subgroup as a group, create a new
``Group`` object::
real_group = gl.groups.get(subgroup_id, lazy=True)
real_group.issues.list()
Descendant Groups
=================
Reference
---------
* v4 API:
+ :class:`gitlab.v4.objects.GroupDescendantGroup`
+ :class:`gitlab.v4.objects.GroupDescendantGroupManager`
+ :attr:`gitlab.v4.objects.Group.descendant_groups`
Examples
--------
List the descendant groups of a group::
descendant_groups = group.descendant_groups.list()
.. note::
Like the ``GroupSubgroup`` objects described above, ``GroupDescendantGroup``
objects do not expose the same API as the ``Group`` objects. Create a new
``Group`` object instead if needed, as shown in the subgroup example.
Group custom attributes
=======================
Reference
---------
* v4 API:
+ :class:`gitlab.v4.objects.GroupCustomAttribute`
+ :class:`gitlab.v4.objects.GroupCustomAttributeManager`
+ :attr:`gitlab.v4.objects.Group.customattributes`
* GitLab API: https://docs.gitlab.com/ce/api/custom_attributes.html
Examples
--------
List custom attributes for a group::
attrs = group.customattributes.list()
Get a custom attribute for a group::
attr = group.customattributes.get(attr_key)
Set (create or update) a custom attribute for a group::
attr = group.customattributes.set(attr_key, attr_value)
Delete a custom attribute for a group::
attr.delete()
# or
group.customattributes.delete(attr_key)
Search groups by custom attribute::
group.customattributes.set('role': 'admin')
gl.groups.list(custom_attributes={'role': 'admin'})
Group members
=============
The following constants define the supported access levels:
* ``gitlab.const.AccessLevel.GUEST = 10``
* ``gitlab.const.AccessLevel.REPORTER = 20``
* ``gitlab.const.AccessLevel.DEVELOPER = 30``
* ``gitlab.const.AccessLevel.MAINTAINER = 40``
* ``gitlab.const.AccessLevel.OWNER = 50``
Reference
---------
* v4 API:
+ :class:`gitlab.v4.objects.GroupMember`
+ :class:`gitlab.v4.objects.GroupMemberManager`
+ :class:`gitlab.v4.objects.GroupMemberAllManager`
+ :class:`gitlab.v4.objects.GroupBillableMember`
+ :class:`gitlab.v4.objects.GroupBillableMemberManager`
+ :attr:`gitlab.v4.objects.Group.members`
+ :attr:`gitlab.v4.objects.Group.members_all`
+ :attr:`gitlab.v4.objects.Group.billable_members`
* GitLab API: https://docs.gitlab.com/ce/api/members.html
Billable group members are only available in GitLab EE.
Examples
--------
List only direct group members::
members = group.members.list()
List the group members recursively (including inherited members through
ancestor groups)::
members = group.members_all.list(get_all=True)
Get only direct group member::
members = group.members.get(member_id)
Get a member of a group, including members inherited through ancestor groups::
members = group.members_all.get(member_id)
Add a member to the group::
member = group.members.create({'user_id': user_id,
'access_level': gitlab.const.AccessLevel.GUEST})
Update a member (change the access level)::
member.access_level = gitlab.const.AccessLevel.DEVELOPER
member.save()
Remove a member from the group::
group.members.delete(member_id)
# or
member.delete()
List billable members of a group (top-level groups only)::
billable_members = group.billable_members.list()
Remove a billable member from the group::
group.billable_members.delete(member_id)
# or
billable_member.delete()
List memberships of a billable member::
billable_member.memberships.list()
LDAP group links
================
Add an LDAP group link to an existing GitLab group::
ldap_link = group.ldap_group_links.create({
'provider': 'ldapmain',
'group_access': gitlab.const.AccessLevel.DEVELOPER,
'cn: 'ldap_group_cn'
})
List a group's LDAP group links:
group.ldap_group_links.list()
Remove a link::
ldap_link.delete()
# or by explicitly providing the CN or filter
group.ldap_group_links.delete(provider='ldapmain', cn='ldap_group_cn')
group.ldap_group_links.delete(provider='ldapmain', filter='(cn=Common Name)')
Sync the LDAP groups::
group.ldap_sync()
You can use the ``ldapgroups`` manager to list available LDAP groups::
# listing (supports pagination)
ldap_groups = gl.ldapgroups.list()
# filter using a group name
ldap_groups = gl.ldapgroups.list(search='foo')
# list the groups for a specific LDAP provider
ldap_groups = gl.ldapgroups.list(search='foo', provider='ldapmain')
SAML group links
================
Add a SAML group link to an existing GitLab group::
saml_link = group.saml_group_links.create({
"saml_group_name": "<your_saml_group_name>",
"access_level": <chosen_access_level>
})
List a group's SAML group links::
group.saml_group_links.list()
Get a SAML group link::
group.saml_group_links.get("<your_saml_group_name>")
Remove a link::
saml_link.delete()
Groups hooks
============
Reference
---------
* v4 API:
+ :class:`gitlab.v4.objects.GroupHook`
+ :class:`gitlab.v4.objects.GroupHookManager`
+ :attr:`gitlab.v4.objects.Group.hooks`
* GitLab API: https://docs.gitlab.com/ce/api/groups.html#hooks
Examples
--------
List the group hooks::
hooks = group.hooks.list()
Get a group hook::
hook = group.hooks.get(hook_id)
Create a group hook::
hook = group.hooks.create({'url': 'http://my/action/url', 'push_events': 1})
Update a group hook::
hook.push_events = 0
hook.save()
Delete a group hook::
group.hooks.delete(hook_id)
# or
hook.delete()
Group push rules
==================
Reference
---------
* v4 API:
+ :class:`gitlab.v4.objects.GroupPushRules`
+ :class:`gitlab.v4.objects.GroupPushRulesManager`
+ :attr:`gitlab.v4.objects.Group.pushrules`
* GitLab API: https://docs.gitlab.com/ee/api/groups.html#push-rules
Examples
---------
Create group push rules (at least one rule is necessary)::
group.pushrules.create({'deny_delete_tag': True})
Get group push rules::
pr = group.pushrules.get()
Edit group push rules::
pr.branch_name_regex = '^(master|develop|support-\d+|release-\d+\..+|hotfix-.+|feature-.+)$'
pr.save()
Delete group push rules::
pr.delete()
|