1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package gitlab
18
19 import (
20 "fmt"
21 "net/http"
22 )
23
24
25
26
27
28 type CustomAttributesService struct {
29 client *Client
30 }
31
32
33
34
35 type CustomAttribute struct {
36 Key string `json:"key"`
37 Value string `json:"value"`
38 }
39
40
41
42
43
44 func (s *CustomAttributesService) ListCustomUserAttributes(user int, options ...RequestOptionFunc) ([]*CustomAttribute, *Response, error) {
45 return s.listCustomAttributes("users", user, options...)
46 }
47
48
49
50
51
52 func (s *CustomAttributesService) ListCustomGroupAttributes(group int, options ...RequestOptionFunc) ([]*CustomAttribute, *Response, error) {
53 return s.listCustomAttributes("groups", group, options...)
54 }
55
56
57
58
59
60 func (s *CustomAttributesService) ListCustomProjectAttributes(project int, options ...RequestOptionFunc) ([]*CustomAttribute, *Response, error) {
61 return s.listCustomAttributes("projects", project, options...)
62 }
63
64 func (s *CustomAttributesService) listCustomAttributes(resource string, id int, options ...RequestOptionFunc) ([]*CustomAttribute, *Response, error) {
65 u := fmt.Sprintf("%s/%d/custom_attributes", resource, id)
66 req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
67 if err != nil {
68 return nil, nil, err
69 }
70
71 var cas []*CustomAttribute
72 resp, err := s.client.Do(req, &cas)
73 if err != nil {
74 return nil, resp, err
75 }
76 return cas, resp, nil
77 }
78
79
80
81
82
83 func (s *CustomAttributesService) GetCustomUserAttribute(user int, key string, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
84 return s.getCustomAttribute("users", user, key, options...)
85 }
86
87
88
89
90
91 func (s *CustomAttributesService) GetCustomGroupAttribute(group int, key string, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
92 return s.getCustomAttribute("groups", group, key, options...)
93 }
94
95
96
97
98
99 func (s *CustomAttributesService) GetCustomProjectAttribute(project int, key string, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
100 return s.getCustomAttribute("projects", project, key, options...)
101 }
102
103 func (s *CustomAttributesService) getCustomAttribute(resource string, id int, key string, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
104 u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, key)
105 req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
106 if err != nil {
107 return nil, nil, err
108 }
109
110 var ca *CustomAttribute
111 resp, err := s.client.Do(req, &ca)
112 if err != nil {
113 return nil, resp, err
114 }
115 return ca, resp, nil
116 }
117
118
119
120
121
122 func (s *CustomAttributesService) SetCustomUserAttribute(user int, c CustomAttribute, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
123 return s.setCustomAttribute("users", user, c, options...)
124 }
125
126
127
128
129
130 func (s *CustomAttributesService) SetCustomGroupAttribute(group int, c CustomAttribute, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
131 return s.setCustomAttribute("groups", group, c, options...)
132 }
133
134
135
136
137
138 func (s *CustomAttributesService) SetCustomProjectAttribute(project int, c CustomAttribute, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
139 return s.setCustomAttribute("projects", project, c, options...)
140 }
141
142 func (s *CustomAttributesService) setCustomAttribute(resource string, id int, c CustomAttribute, options ...RequestOptionFunc) (*CustomAttribute, *Response, error) {
143 u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, c.Key)
144 req, err := s.client.NewRequest(http.MethodPut, u, c, options)
145 if err != nil {
146 return nil, nil, err
147 }
148
149 ca := new(CustomAttribute)
150 resp, err := s.client.Do(req, ca)
151 if err != nil {
152 return nil, resp, err
153 }
154 return ca, resp, nil
155 }
156
157
158
159
160
161 func (s *CustomAttributesService) DeleteCustomUserAttribute(user int, key string, options ...RequestOptionFunc) (*Response, error) {
162 return s.deleteCustomAttribute("users", user, key, options...)
163 }
164
165
166
167
168
169 func (s *CustomAttributesService) DeleteCustomGroupAttribute(group int, key string, options ...RequestOptionFunc) (*Response, error) {
170 return s.deleteCustomAttribute("groups", group, key, options...)
171 }
172
173
174
175
176
177 func (s *CustomAttributesService) DeleteCustomProjectAttribute(project int, key string, options ...RequestOptionFunc) (*Response, error) {
178 return s.deleteCustomAttribute("projects", project, key, options...)
179 }
180
181 func (s *CustomAttributesService) deleteCustomAttribute(resource string, id int, key string, options ...RequestOptionFunc) (*Response, error) {
182 u := fmt.Sprintf("%s/%d/custom_attributes/%s", resource, id, key)
183 req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
184 if err != nil {
185 return nil, err
186 }
187 return s.client.Do(req, nil)
188 }
189
View as plain text