1package gitlab
2
3import (
4 "fmt"
5)
6
7// GroupLabelsService handles communication with the label related methods of the
8// GitLab API.
9//
10// GitLab API docs: https://docs.gitlab.com/ce/api/group_labels.html
11type GroupLabelsService struct {
12 client *Client
13}
14
15// GroupLabel represents a GitLab group label.
16//
17// GitLab API docs: https://docs.gitlab.com/ce/api/group_labels.html
18type GroupLabel Label
19
20func (l GroupLabel) String() string {
21 return Stringify(l)
22}
23
24// ListGroupLabelsOptions represents the available ListGroupLabels() options.
25//
26// GitLab API docs: https://docs.gitlab.com/ce/api/labels.html#list-labels
27type ListGroupLabelsOptions ListOptions
28
29// ListGroupLabels gets all labels for given group.
30//
31// GitLab API docs:
32// https://docs.gitlab.com/ce/api/group_labels.html#list-group-labels
33func (s *GroupLabelsService) ListGroupLabels(gid interface{}, opt *ListGroupLabelsOptions, options ...OptionFunc) ([]*GroupLabel, *Response, error) {
34 group, err := parseID(gid)
35 if err != nil {
36 return nil, nil, err
37 }
38 u := fmt.Sprintf("groups/%s/labels", pathEscape(group))
39
40 req, err := s.client.NewRequest("GET", u, opt, options)
41 if err != nil {
42 return nil, nil, err
43 }
44
45 var l []*GroupLabel
46 resp, err := s.client.Do(req, &l)
47 if err != nil {
48 return nil, resp, err
49 }
50
51 return l, resp, err
52}
53
54// CreateGroupLabelOptions represents the available CreateGroupLabel() options.
55//
56// GitLab API docs:
57// https://docs.gitlab.com/ce/api/group_labels.html#create-a-new-group-label
58type CreateGroupLabelOptions CreateLabelOptions
59
60// CreateGroupLabel creates a new label for given group with given name and
61// color.
62//
63// GitLab API docs:
64// https://docs.gitlab.com/ce/api/group_labels.html#create-a-new-group-label
65func (s *GroupLabelsService) CreateGroupLabel(gid interface{}, opt *CreateGroupLabelOptions, options ...OptionFunc) (*GroupLabel, *Response, error) {
66 group, err := parseID(gid)
67 if err != nil {
68 return nil, nil, err
69 }
70 u := fmt.Sprintf("groups/%s/labels", pathEscape(group))
71
72 req, err := s.client.NewRequest("POST", u, opt, options)
73 if err != nil {
74 return nil, nil, err
75 }
76
77 l := new(GroupLabel)
78 resp, err := s.client.Do(req, l)
79 if err != nil {
80 return nil, resp, err
81 }
82
83 return l, resp, err
84}
85
86// DeleteGroupLabelOptions represents the available DeleteGroupLabel() options.
87//
88// GitLab API docs:
89// https://docs.gitlab.com/ce/api/group_labels.html#delete-a-group-label
90type DeleteGroupLabelOptions DeleteLabelOptions
91
92// DeleteGroupLabel deletes a group label given by its name.
93//
94// GitLab API docs: https://docs.gitlab.com/ce/api/labels.html#delete-a-label
95func (s *GroupLabelsService) DeleteGroupLabel(gid interface{}, opt *DeleteGroupLabelOptions, options ...OptionFunc) (*Response, error) {
96 group, err := parseID(gid)
97 if err != nil {
98 return nil, err
99 }
100 u := fmt.Sprintf("groups/%s/labels", pathEscape(group))
101
102 req, err := s.client.NewRequest("DELETE", u, opt, options)
103 if err != nil {
104 return nil, err
105 }
106
107 return s.client.Do(req, nil)
108}
109
110// UpdateGroupLabelOptions represents the available UpdateGroupLabel() options.
111//
112// GitLab API docs:
113// https://docs.gitlab.com/ce/api/group_labels.html#update-a-group-label
114type UpdateGroupLabelOptions UpdateLabelOptions
115
116// UpdateGroupLabel updates an existing label with new name or now color. At least
117// one parameter is required, to update the label.
118//
119// GitLab API docs:
120// https://docs.gitlab.com/ce/api/group_labels.html#update-a-group-label
121func (s *GroupLabelsService) UpdateGroupLabel(gid interface{}, opt *UpdateGroupLabelOptions, options ...OptionFunc) (*GroupLabel, *Response, error) {
122 group, err := parseID(gid)
123 if err != nil {
124 return nil, nil, err
125 }
126 u := fmt.Sprintf("groups/%s/labels", pathEscape(group))
127
128 req, err := s.client.NewRequest("PUT", u, opt, options)
129 if err != nil {
130 return nil, nil, err
131 }
132
133 l := new(GroupLabel)
134 resp, err := s.client.Do(req, l)
135 if err != nil {
136 return nil, resp, err
137 }
138
139 return l, resp, err
140}
141
142// SubscribeToGroupLabel subscribes the authenticated user to a label to receive
143// notifications. If the user is already subscribed to the label, the status
144// code 304 is returned.
145//
146// GitLab API docs:
147// https://docs.gitlab.com/ce/api/group_labels.html#subscribe-to-a-group-label
148func (s *GroupLabelsService) SubscribeToGroupLabel(gid interface{}, labelID interface{}, options ...OptionFunc) (*GroupLabel, *Response, error) {
149 group, err := parseID(gid)
150 if err != nil {
151 return nil, nil, err
152 }
153 label, err := parseID(labelID)
154 if err != nil {
155 return nil, nil, err
156 }
157 u := fmt.Sprintf("groups/%s/labels/%s/subscribe", pathEscape(group), label)
158
159 req, err := s.client.NewRequest("POST", u, nil, options)
160 if err != nil {
161 return nil, nil, err
162 }
163
164 l := new(GroupLabel)
165 resp, err := s.client.Do(req, l)
166 if err != nil {
167 return nil, resp, err
168 }
169
170 return l, resp, err
171}
172
173// UnsubscribeFromGroupLabel unsubscribes the authenticated user from a label to not
174// receive notifications from it. If the user is not subscribed to the label, the
175// status code 304 is returned.
176//
177// GitLab API docs:
178// https://docs.gitlab.com/ce/api/group_labels.html#unsubscribe-from-a-group-label
179func (s *GroupLabelsService) UnsubscribeFromGroupLabel(gid interface{}, labelID interface{}, options ...OptionFunc) (*Response, error) {
180 group, err := parseID(gid)
181 if err != nil {
182 return nil, err
183 }
184 label, err := parseID(labelID)
185 if err != nil {
186 return nil, err
187 }
188 u := fmt.Sprintf("groups/%s/labels/%s/unsubscribe", pathEscape(group), label)
189
190 req, err := s.client.NewRequest("POST", u, nil, options)
191 if err != nil {
192 return nil, err
193 }
194
195 return s.client.Do(req, nil)
196}