Skip to content

Commit 4a9ef9f

Browse files
feat: group labels with subscriptable mixin
1 parent 2c1ea56 commit 4a9ef9f

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

gitlab/mixins.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,9 @@ def approve(self, access_level=gitlab.DEVELOPER_ACCESS, **kwargs):
433433

434434

435435
class SubscribableMixin(object):
436-
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest", "ProjectLabel"))
436+
@cli.register_custom_action(
437+
("ProjectIssue", "ProjectMergeRequest", "ProjectLabel", "GroupLabel")
438+
)
437439
@exc.on_http_error(exc.GitlabSubscribeError)
438440
def subscribe(self, **kwargs):
439441
"""Subscribe to the object notifications.
@@ -449,7 +451,9 @@ def subscribe(self, **kwargs):
449451
server_data = self.manager.gitlab.http_post(path, **kwargs)
450452
self._update_attrs(server_data)
451453

452-
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest", "ProjectLabel"))
454+
@cli.register_custom_action(
455+
("ProjectIssue", "ProjectMergeRequest", "ProjectLabel", "GroupLabel")
456+
)
453457
@exc.on_http_error(exc.GitlabUnsubscribeError)
454458
def unsubscribe(self, **kwargs):
455459
"""Unsubscribe from the object notifications.

gitlab/v4/objects.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,53 @@ class GroupIssueManager(ListMixin, RESTManager):
833833
_types = {"labels": types.ListAttribute}
834834

835835

836+
class GroupLabel(SubscribableMixin, SaveMixin, ObjectDeleteMixin, RESTObject):
837+
_id_attr = "name"
838+
839+
# Update without ID, but we need an ID to get from list.
840+
@exc.on_http_error(exc.GitlabUpdateError)
841+
def save(self, **kwargs):
842+
"""Saves the changes made to the object to the server.
843+
844+
The object is updated to match what the server returns.
845+
846+
Args:
847+
**kwargs: Extra options to send to the server (e.g. sudo)
848+
849+
Raises:
850+
GitlabAuthenticationError: If authentication is not correct.
851+
GitlabUpdateError: If the server cannot perform the request.
852+
"""
853+
updated_data = self._get_updated_data()
854+
855+
# call the manager
856+
server_data = self.manager.update(None, updated_data, **kwargs)
857+
self._update_attrs(server_data)
858+
859+
860+
class GroupLabelManager(ListMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTManager):
861+
_path = "/groups/%(group_id)s/labels"
862+
_obj_cls = GroupLabel
863+
_from_parent_attrs = {"group_id": "id"}
864+
_create_attrs = (("name", "color"), ("description", "priority"))
865+
_update_attrs = (("name",), ("new_name", "color", "description", "priority"))
866+
867+
# Delete without ID.
868+
@exc.on_http_error(exc.GitlabDeleteError)
869+
def delete(self, name, **kwargs):
870+
"""Delete a Label on the server.
871+
872+
Args:
873+
name: The name of the label
874+
**kwargs: Extra options to send to the server (e.g. sudo)
875+
876+
Raises:
877+
GitlabAuthenticationError: If authentication is not correct
878+
GitlabDeleteError: If the server cannot perform the request
879+
"""
880+
self.gitlab.http_delete(self.path, query_data={"name": name}, **kwargs)
881+
882+
836883
class GroupMember(SaveMixin, ObjectDeleteMixin, RESTObject):
837884
_short_print_attr = "username"
838885

@@ -1042,6 +1089,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
10421089
("customattributes", "GroupCustomAttributeManager"),
10431090
("epics", "GroupEpicManager"),
10441091
("issues", "GroupIssueManager"),
1092+
("labels", "GroupLabelManager"),
10451093
("members", "GroupMemberManager"),
10461094
("mergerequests", "GroupMergeRequestManager"),
10471095
("milestones", "GroupMilestoneManager"),

tools/python_test_v4.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,18 @@
337337
g_v.delete()
338338
assert len(group1.variables.list()) == 0
339339

340+
# group labels
341+
group1.labels.create({"name": "foo", "description": "bar", "color": "#112233"})
342+
g_l = group1.labels.get("foo")
343+
assert g_l.description == "bar"
344+
g_l.description = "baz"
345+
g_l.save()
346+
g_l = group1.labels.get("foo")
347+
assert g_l.description == "baz"
348+
assert len(group1.labels.list()) == 1
349+
g_l.delete()
350+
assert len(group1.labels.list()) == 0
351+
340352
# hooks
341353
hook = gl.hooks.create({"url": "http://whatever.com"})
342354
assert len(gl.hooks.list()) == 1

0 commit comments

Comments
 (0)