Skip to content

Commit edb3359

Browse files
authored
Merge pull request #847 from sidisel-albertolopez/feat/grouplabels
feat: Add grouplabel support with subscribable mixin
2 parents 2c1ea56 + f7f24bd commit edb3359

File tree

4 files changed

+144
-2
lines changed

4 files changed

+144
-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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,64 @@ 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+
# Update without ID.
868+
def update(self, name, new_data={}, **kwargs):
869+
"""Update a Label on the server.
870+
871+
Args:
872+
name: The name of the label
873+
**kwargs: Extra options to send to the server (e.g. sudo)
874+
"""
875+
new_data["name"] = name
876+
super().update(id=None, new_data=new_data, **kwargs)
877+
878+
# Delete without ID.
879+
@exc.on_http_error(exc.GitlabDeleteError)
880+
def delete(self, name, **kwargs):
881+
"""Delete a Label on the server.
882+
883+
Args:
884+
name: The name of the label
885+
**kwargs: Extra options to send to the server (e.g. sudo)
886+
887+
Raises:
888+
GitlabAuthenticationError: If authentication is not correct
889+
GitlabDeleteError: If the server cannot perform the request
890+
"""
891+
self.gitlab.http_delete(self.path, query_data={"name": name}, **kwargs)
892+
893+
836894
class GroupMember(SaveMixin, ObjectDeleteMixin, RESTObject):
837895
_short_print_attr = "username"
838896

@@ -1042,6 +1100,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
10421100
("customattributes", "GroupCustomAttributeManager"),
10431101
("epics", "GroupEpicManager"),
10441102
("issues", "GroupIssueManager"),
1103+
("labels", "GroupLabelManager"),
10451104
("members", "GroupMemberManager"),
10461105
("mergerequests", "GroupMergeRequestManager"),
10471106
("milestones", "GroupMilestoneManager"),
@@ -2934,6 +2993,17 @@ class ProjectLabelManager(
29342993
_create_attrs = (("name", "color"), ("description", "priority"))
29352994
_update_attrs = (("name",), ("new_name", "color", "description", "priority"))
29362995

2996+
# Update without ID.
2997+
def update(self, name, new_data={}, **kwargs):
2998+
"""Update a Label on the server.
2999+
3000+
Args:
3001+
name: The name of the label
3002+
**kwargs: Extra options to send to the server (e.g. sudo)
3003+
"""
3004+
new_data["name"] = name
3005+
super().update(id=None, new_data=new_data, **kwargs)
3006+
29373007
# Delete without ID.
29383008
@exc.on_http_error(exc.GitlabDeleteError)
29393009
def delete(self, name, **kwargs):

tools/cli_test_v4.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ testcase "project update" '
2525
GITLAB project update --id "$PROJECT_ID" --description "My New Description"
2626
'
2727

28+
testcase "group creation" '
29+
OUTPUT=$(try GITLAB group create --name test-group1 --path group1) || exit 1
30+
GROUP_ID=$(pecho "${OUTPUT}" | grep ^id: | cut -d" " -f2)
31+
OUTPUT=$(try GITLAB group list) || exit 1
32+
pecho "${OUTPUT}" | grep -q test-group1
33+
'
34+
35+
testcase "group update" '
36+
GITLAB group update --id "$GROUP_ID" --description "My New Description"
37+
'
38+
2839
testcase "user creation" '
2940
OUTPUT=$(GITLAB user create --email fake@email.com --username user1 \
3041
--name "User One" --password fakepassword)
@@ -89,6 +100,46 @@ testcase "merge request validation" '
89100
--iid "$MR_ID" >/dev/null 2>&1
90101
'
91102

103+
# Test project labels
104+
testcase "create project label" '
105+
OUTPUT=$(GITLAB -v project-label create --project-id $PROJECT_ID \
106+
--name prjlabel1 --description "prjlabel1 description" --color "#112233")
107+
'
108+
109+
testcase "list project label" '
110+
OUTPUT=$(GITLAB -v project-label list --project-id $PROJECT_ID)
111+
'
112+
113+
testcase "update project label" '
114+
OUTPUT=$(GITLAB -v project-label update --project-id $PROJECT_ID \
115+
--name prjlabel1 --new-name prjlabel2 --description "prjlabel2 description" --color "#332211")
116+
'
117+
118+
testcase "delete project label" '
119+
OUTPUT=$(GITLAB -v project-label delete --project-id $PROJECT_ID \
120+
--name prjlabel2)
121+
'
122+
123+
# Test group labels
124+
testcase "create group label" '
125+
OUTPUT=$(GITLAB -v group-label create --group-id $GROUP_ID \
126+
--name grplabel1 --description "grplabel1 description" --color "#112233")
127+
'
128+
129+
testcase "list group label" '
130+
OUTPUT=$(GITLAB -v group-label list --group-id $GROUP_ID)
131+
'
132+
133+
testcase "update group label" '
134+
OUTPUT=$(GITLAB -v group-label update --group-id $GROUP_ID \
135+
--name grplabel1 --new-name grplabel2 --description "grplabel2 description" --color "#332211")
136+
'
137+
138+
testcase "delete group label" '
139+
OUTPUT=$(GITLAB -v group-label delete --group-id $GROUP_ID \
140+
--name grplabel2)
141+
'
142+
92143
# Test project variables
93144
testcase "create project variable" '
94145
OUTPUT=$(GITLAB -v project-variable create --project-id $PROJECT_ID \
@@ -128,6 +179,10 @@ testcase "project deletion" '
128179
GITLAB project delete --id "$PROJECT_ID"
129180
'
130181

182+
testcase "group deletion" '
183+
OUTPUT=$(try GITLAB group delete --id $GROUP_ID)
184+
'
185+
131186
testcase "application settings get" '
132187
GITLAB application-settings get >/dev/null 2>&1
133188
'
@@ -146,3 +201,4 @@ testcase "values from files" '
146201
--description @/tmp/gitlab-project-description)
147202
echo $OUTPUT | grep -q "Multi line"
148203
'
204+

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)