diff options
author | Alberto López Martín <alberto.lopez@sidisel.com> | 2019-05-24 23:19:24 +0200 |
---|---|---|
committer | Alberto López Martín <alberto.lopez@sidisel.com> | 2019-07-26 08:18:24 +0200 |
commit | 4a9ef9f0fa26e01fc6c97cf88b2a162e21f61cce (patch) | |
tree | 2e1b6893bc9d1927a48e4a95ceea72f690f33ce5 | |
parent | 2c1ea56a217525bbb0a5321eb392c7fe7c100d44 (diff) | |
download | gitlab-4a9ef9f0fa26e01fc6c97cf88b2a162e21f61cce.tar.gz |
feat: group labels with subscriptable mixin
-rw-r--r-- | gitlab/mixins.py | 8 | ||||
-rw-r--r-- | gitlab/v4/objects.py | 48 | ||||
-rw-r--r-- | tools/python_test_v4.py | 12 |
3 files changed, 66 insertions, 2 deletions
diff --git a/gitlab/mixins.py b/gitlab/mixins.py index 7cbb7b9..01a5b63 100644 --- a/gitlab/mixins.py +++ b/gitlab/mixins.py @@ -433,7 +433,9 @@ class AccessRequestMixin(object): class SubscribableMixin(object): - @cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest", "ProjectLabel")) + @cli.register_custom_action( + ("ProjectIssue", "ProjectMergeRequest", "ProjectLabel", "GroupLabel") + ) @exc.on_http_error(exc.GitlabSubscribeError) def subscribe(self, **kwargs): """Subscribe to the object notifications. @@ -449,7 +451,9 @@ class SubscribableMixin(object): server_data = self.manager.gitlab.http_post(path, **kwargs) self._update_attrs(server_data) - @cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest", "ProjectLabel")) + @cli.register_custom_action( + ("ProjectIssue", "ProjectMergeRequest", "ProjectLabel", "GroupLabel") + ) @exc.on_http_error(exc.GitlabUnsubscribeError) def unsubscribe(self, **kwargs): """Unsubscribe from the object notifications. diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py index 74157f2..eaf57c0 100644 --- a/gitlab/v4/objects.py +++ b/gitlab/v4/objects.py @@ -833,6 +833,53 @@ class GroupIssueManager(ListMixin, RESTManager): _types = {"labels": types.ListAttribute} +class GroupLabel(SubscribableMixin, SaveMixin, ObjectDeleteMixin, RESTObject): + _id_attr = "name" + + # Update without ID, but we need an ID to get from list. + @exc.on_http_error(exc.GitlabUpdateError) + def save(self, **kwargs): + """Saves the changes made to the object to the server. + + The object is updated to match what the server returns. + + Args: + **kwargs: Extra options to send to the server (e.g. sudo) + + Raises: + GitlabAuthenticationError: If authentication is not correct. + GitlabUpdateError: If the server cannot perform the request. + """ + updated_data = self._get_updated_data() + + # call the manager + server_data = self.manager.update(None, updated_data, **kwargs) + self._update_attrs(server_data) + + +class GroupLabelManager(ListMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTManager): + _path = "/groups/%(group_id)s/labels" + _obj_cls = GroupLabel + _from_parent_attrs = {"group_id": "id"} + _create_attrs = (("name", "color"), ("description", "priority")) + _update_attrs = (("name",), ("new_name", "color", "description", "priority")) + + # Delete without ID. + @exc.on_http_error(exc.GitlabDeleteError) + def delete(self, name, **kwargs): + """Delete a Label on the server. + + Args: + name: The name of the label + **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 + """ + self.gitlab.http_delete(self.path, query_data={"name": name}, **kwargs) + + class GroupMember(SaveMixin, ObjectDeleteMixin, RESTObject): _short_print_attr = "username" @@ -1042,6 +1089,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject): ("customattributes", "GroupCustomAttributeManager"), ("epics", "GroupEpicManager"), ("issues", "GroupIssueManager"), + ("labels", "GroupLabelManager"), ("members", "GroupMemberManager"), ("mergerequests", "GroupMergeRequestManager"), ("milestones", "GroupMilestoneManager"), diff --git a/tools/python_test_v4.py b/tools/python_test_v4.py index d65f39f..e6b6ab9 100644 --- a/tools/python_test_v4.py +++ b/tools/python_test_v4.py @@ -337,6 +337,18 @@ assert len(group1.variables.list()) == 1 g_v.delete() assert len(group1.variables.list()) == 0 +# group labels +group1.labels.create({"name": "foo", "description": "bar", "color": "#112233"}) +g_l = group1.labels.get("foo") +assert g_l.description == "bar" +g_l.description = "baz" +g_l.save() +g_l = group1.labels.get("foo") +assert g_l.description == "baz" +assert len(group1.labels.list()) == 1 +g_l.delete() +assert len(group1.labels.list()) == 0 + # hooks hook = gl.hooks.create({"url": "http://whatever.com"}) assert len(gl.hooks.list()) == 1 |