1package gitlab
2
3import (
4 "fmt"
5)
6
7// CustomAttributesService handles communication with the group, project and
8// user custom attributes related methods of the GitLab API.
9//
10// GitLab API docs: https://docs.gitlab.com/ce/api/custom_attributes.html
11type CustomAttributesService struct {
12 client *Client
13}
14
15// CustomAttribute struct is used to unmarshal response to api calls.
16//
17// GitLab API docs: https://docs.gitlab.com/ce/api/custom_attributes.html
18type CustomAttribute struct {
19 Key string `json:"key"`
20 Value string `json:"value"`
21}
22
23// ListCustomUserAttributes lists the custom attributes of the specified user.
24//
25// GitLab API docs:
26// https://docs.gitlab.com/ce/api/custom_attributes.html#list-custom-attributes
27func (s *CustomAttributesService) ListCustomUserAttributes(user int, options ...OptionFunc) ([]*CustomAttribute, *Response, error) {
28 return s.listCustomAttributes("users", user, options...)
29}
30
31// ListCustomGroupAttributes lists the custom attributes of the specified group.
32//
33// GitLab API docs:
34// https://docs.gitlab.com/ce/api/custom_attributes.html#list-custom-attributes
35func (s *CustomAttributesService) ListCustomGroupAttributes(group int, options ...OptionFunc) ([]*CustomAttribute, *Response, error) {
36 return s.listCustomAttributes("groups", group, options...)
37}
38
39// ListCustomProjectAttributes lists the custom attributes of the specified project.
40//
41// GitLab API docs:
42// https://docs.gitlab.com/ce/api/custom_attributes.html#list-custom-attributes
43func (s *CustomAttributesService) ListCustomProjectAttributes(project int, options ...OptionFunc) ([]*CustomAttribute, *Response, error) {
44 return s.listCustomAttributes("projects", project, options...)
45}
46
47func (s *CustomAttributesService) listCustomAttributes(resource string, id int, options ...OptionFunc) ([]*CustomAttribute, *Response, error) {
48 u := fmt.Sprintf("%s/%d/custom_attributes", resource, id)
49 req, err := s.client.NewRequest("GET", u, nil, options)
50 if err != nil {
51 return nil, nil, err
52 }
53
54 var cas []*CustomAttribute
55 resp, err := s.client.Do(req, &cas)
56 if err != nil {
57 return nil, resp, err
58 }
59 return cas, resp, err
60}
61
62// GetCustomUserAttribute returns the user attribute with a speciifc key.
63//
64// GitLab API docs:
65// https://docs.gitlab.com/ce/api/custom_attributes.html#single-custom-attribute
66func (s *CustomAttributesService) GetCustomUserAttribute(user int, key string, options ...OptionFunc) (*CustomAttribute, *Response, error) {
67 return s.getCustomAttribute("users", user, key, options...)
68}
69
70// GetCustomGroupAttribute returns the group attribute with a speciifc key.
71//
72// GitLab API docs:
73// https://docs.gitlab.com/ce/api/custom_attributes.html#single-custom-attribute
74func (s *CustomAttributesService) GetCustomGroupAttribute(group int, key string, options ...OptionFunc) (*CustomAttribute, *Response, error) {
75 return s.getCustomAttribute("groups", group, key, options...)
76}
77
78// GetCustomProjectAttribute returns the project attribute with a speciifc key.
79//
80// GitLab API docs:
81// https://docs.gitlab.com/ce/api/custom_attributes.html#single-custom-attribute
82func (s *CustomAttributesService) GetCustomProjectAttribute(project int, key string, options ...OptionFunc) (*CustomAttribute, *Response, error) {
83 return s.getCustomAttribute("projects", project, key, options...)
84}
85
86func (s *CustomAttributesService) getCustomAttribute(resource string, id int, key string, options ...OptionFunc) (*CustomAttribute, *Response, error) {
87 u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, key)
88 req, err := s.client.NewRequest("GET", u, nil, options)
89 if err != nil {
90 return nil, nil, err
91 }
92
93 var ca *CustomAttribute
94 resp, err := s.client.Do(req, &ca)
95 if err != nil {
96 return nil, resp, err
97 }
98 return ca, resp, err
99}
100
101// SetCustomUserAttribute sets the custom attributes of the specified user.
102//
103// GitLab API docs:
104// https://docs.gitlab.com/ce/api/custom_attributes.html#set-custom-attribute
105func (s *CustomAttributesService) SetCustomUserAttribute(user int, c CustomAttribute, options ...OptionFunc) (*CustomAttribute, *Response, error) {
106 return s.setCustomAttribute("users", user, c, options...)
107}
108
109// SetCustomGroupAttribute sets the custom attributes of the specified group.
110//
111// GitLab API docs:
112// https://docs.gitlab.com/ce/api/custom_attributes.html#set-custom-attribute
113func (s *CustomAttributesService) SetCustomGroupAttribute(group int, c CustomAttribute, options ...OptionFunc) (*CustomAttribute, *Response, error) {
114 return s.setCustomAttribute("groups", group, c, options...)
115}
116
117// SetCustomProjectAttribute sets the custom attributes of the specified project.
118//
119// GitLab API docs:
120// https://docs.gitlab.com/ce/api/custom_attributes.html#set-custom-attribute
121func (s *CustomAttributesService) SetCustomProjectAttribute(project int, c CustomAttribute, options ...OptionFunc) (*CustomAttribute, *Response, error) {
122 return s.setCustomAttribute("projects", project, c, options...)
123}
124
125func (s *CustomAttributesService) setCustomAttribute(resource string, id int, c CustomAttribute, options ...OptionFunc) (*CustomAttribute, *Response, error) {
126 u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, c.Key)
127 req, err := s.client.NewRequest("PUT", u, c, options)
128 if err != nil {
129 return nil, nil, err
130 }
131
132 ca := new(CustomAttribute)
133 resp, err := s.client.Do(req, ca)
134 if err != nil {
135 return nil, resp, err
136 }
137 return ca, resp, err
138}
139
140// DeleteCustomUserAttribute removes the custom attribute of the specified user.
141//
142// GitLab API docs:
143// https://docs.gitlab.com/ce/api/custom_attributes.html#delete-custom-attribute
144func (s *CustomAttributesService) DeleteCustomUserAttribute(user int, key string, options ...OptionFunc) (*Response, error) {
145 return s.deleteCustomAttribute("users", user, key, options...)
146}
147
148// DeleteCustomGroupAttribute removes the custom attribute of the specified group.
149//
150// GitLab API docs:
151// https://docs.gitlab.com/ce/api/custom_attributes.html#delete-custom-attribute
152func (s *CustomAttributesService) DeleteCustomGroupAttribute(group int, key string, options ...OptionFunc) (*Response, error) {
153 return s.deleteCustomAttribute("groups", group, key, options...)
154}
155
156// DeleteCustomProjectAttribute removes the custom attribute of the specified project.
157//
158// GitLab API docs:
159// https://docs.gitlab.com/ce/api/custom_attributes.html#delete-custom-attribute
160func (s *CustomAttributesService) DeleteCustomProjectAttribute(project int, key string, options ...OptionFunc) (*Response, error) {
161 return s.deleteCustomAttribute("projects", project, key, options...)
162}
163
164func (s *CustomAttributesService) deleteCustomAttribute(resource string, id int, key string, options ...OptionFunc) (*Response, error) {
165 u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, key)
166 req, err := s.client.NewRequest("DELETE", u, nil, options)
167 if err != nil {
168 return nil, err
169 }
170 return s.client.Do(req, nil)
171}