-
Notifications
You must be signed in to change notification settings - Fork 29
/
filter.go
366 lines (290 loc) · 10.9 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package jira
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
type FilterService struct {
client *Client
Share *FilterShareService
}
type FilterBodyScheme struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
JQL string `json:"jql,omitempty"`
Favorite bool `json:"favourite,omitempty"`
SharePermissions []*SharePermissionScheme `json:"sharePermissions,omitempty"`
}
// Creates a filter. The filter is shared according to the default share scope. The filter is not selected as a favorite.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters#create-filter
func (f *FilterService) Create(ctx context.Context, payload *FilterBodyScheme) (result *FilterScheme, response *Response, err error) {
if payload == nil {
return nil, nil, fmt.Errorf("error, payload value is nil, please provide a valid FilterBodyScheme pointer")
}
var endpoint = "rest/api/3/filter"
request, err := f.client.newRequest(ctx, http.MethodPost, endpoint, &payload)
if err != nil {
return
}
request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")
response, err = f.client.Do(request)
if err != nil {
return
}
result = new(FilterScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return nil, response, fmt.Errorf("unable to marshall the response body, error: %v", err.Error())
}
return
}
// Returns the visible favorite filters of the user.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters#get-favorites
func (f *FilterService) Favorite(ctx context.Context) (result *[]FilterScheme, response *Response, err error) {
var endpoint = "rest/api/3/filter/favourite"
request, err := f.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}
request.Header.Set("Accept", "application/json")
response, err = f.client.Do(request)
if err != nil {
return
}
result = new([]FilterScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return nil, response, fmt.Errorf("unable to marshall the response body, error: %v", err.Error())
}
return
}
// Returns the filters owned by the user. If includeFavourites is true, the user's visible favorite filters are also returned.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters#get-my-filters
func (f *FilterService) My(ctx context.Context, favorites bool, expands []string) (result *[]FilterScheme, response *Response, err error) {
params := url.Values{}
var expand string
for index, value := range expands {
if index == 0 {
expand = value
continue
}
expand += "," + value
}
if len(expand) != 0 {
params.Add("expand", expand)
}
if favorites {
params.Add("includeFavourites", "true")
}
var endpoint string
if params.Encode() != "" {
endpoint = fmt.Sprintf("rest/api/3/filter/my?%v", params.Encode())
} else {
endpoint = "rest/api/3/filter/my"
}
request, err := f.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}
request.Header.Set("Accept", "application/json")
response, err = f.client.Do(request)
if err != nil {
return
}
result = new([]FilterScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return nil, response, fmt.Errorf("unable to marshall the response body, error: %v", err.Error())
}
return
}
type FilterSearchOptionScheme struct {
Name, AccountID string
Group, OrderBy string
ProjectID int
IDs []int
Expand []string
}
// Returns a paginated list of filters. Use this operation to get:
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters#search-filters
func (f *FilterService) Search(ctx context.Context, options *FilterSearchOptionScheme, startAt, maxResults int) (result *FilterPageScheme, response *Response, err error) {
if options == nil {
return nil, nil, fmt.Errorf("error, options value is nil, please provide a valid FilterSearchOptionScheme pointer")
}
params := url.Values{}
if options.Name != "" {
params.Add("filterName", options.Name)
}
if options.AccountID != "" {
params.Add("accountId", options.AccountID)
}
if options.Group != "" {
params.Add("groupname", options.Group)
}
if options.ProjectID != 0 {
params.Add("projectId", strconv.Itoa(options.ProjectID))
}
for _, filterID := range options.IDs {
params.Add("id", strconv.Itoa(filterID))
}
if options.OrderBy != "" {
params.Add("orderBy", options.OrderBy)
}
var expand string
for index, value := range options.Expand {
if index == 0 {
expand = value
continue
}
expand += "," + value
}
if len(expand) != 0 {
params.Add("expand", expand)
}
params.Add("startAt", strconv.Itoa(startAt))
params.Add("maxResults", strconv.Itoa(maxResults))
var endpoint = fmt.Sprintf("rest/api/3/filter/search?%v", params.Encode())
request, err := f.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}
request.Header.Set("Accept", "application/json")
response, err = f.client.Do(request)
if err != nil {
return
}
result = new(FilterPageScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return nil, response, fmt.Errorf("unable to marshall the response body, error: %v", err.Error())
}
return
}
// Returns a filter.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters#get-filter
func (f *FilterService) Get(ctx context.Context, filterID int, expands []string) (result *FilterScheme, response *Response, err error) {
params := url.Values{}
var expand string
for index, value := range expands {
if index == 0 {
expand = value
continue
}
expand += "," + value
}
if len(expand) != 0 {
params.Add("expand", expand)
}
var endpoint string
if params.Encode() != "" {
endpoint = fmt.Sprintf("rest/api/3/filter/%v?%v", filterID, params.Encode())
} else {
endpoint = fmt.Sprintf("rest/api/3/filter/%v", filterID)
}
request, err := f.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}
request.Header.Set("Content-Type", "application/json")
response, err = f.client.Do(request)
if err != nil {
return
}
result = new(FilterScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return nil, response, fmt.Errorf("unable to marshall the response body, error: %v", err.Error())
}
return
}
// Updates a filter. Use this operation to update a filter's name, description, JQL, or sharing.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters#update-filter
func (f *FilterService) Update(ctx context.Context, filterID int, payload *FilterBodyScheme) (result *FilterScheme, response *Response, err error) {
if payload == nil {
return nil, nil, fmt.Errorf("error, payload value is nil, please provide a valid FilterBodyScheme pointer")
}
var endpoint = fmt.Sprintf("rest/api/3/filter/%v", filterID)
request, err := f.client.newRequest(ctx, http.MethodPut, endpoint, &payload)
if err != nil {
return
}
request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")
response, err = f.client.Do(request)
if err != nil {
return
}
result = new(FilterScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return nil, response, fmt.Errorf("unable to marshall the response body, error: %v", err.Error())
}
return
}
// Delete a filter.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters#delete-filter
func (f *FilterService) Delete(ctx context.Context, filterID int) (response *Response, err error) {
var endpoint = fmt.Sprintf("rest/api/3/filter/%v", filterID)
request, err := f.client.newRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return
}
response, err = f.client.Do(request)
if err != nil {
return
}
return
}
type FilterPageScheme struct {
Self string `json:"self,omitempty"`
MaxResults int `json:"maxResults,omitempty"`
StartAt int `json:"startAt,omitempty"`
Total int `json:"total,omitempty"`
IsLast bool `json:"isLast,omitempty"`
Values []*FilterPageNodeScheme `json:"values,omitempty"`
}
type FilterScheme struct {
Self string `json:"self,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Owner *UserScheme `json:"owner,omitempty"`
Jql string `json:"jql,omitempty"`
ViewURL string `json:"viewUrl,omitempty"`
SearchURL string `json:"searchUrl,omitempty"`
Favourite bool `json:"favourite,omitempty"`
FavouritedCount int `json:"favouritedCount,omitempty"`
SharePermissions []*SharePermissionScheme `json:"sharePermissions,omitempty"`
ShareUsers *FilterUsersScheme `json:"sharedUsers,omitempty"`
Subscriptions *FilterSubscriptionPageScheme `json:"subscriptions,omitempty"`
}
type FilterPageNodeScheme struct {
Self string `json:"self,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Owner *UserScheme `json:"owner,omitempty"`
Jql string `json:"jql,omitempty"`
ViewURL string `json:"viewUrl,omitempty"`
SearchURL string `json:"searchUrl,omitempty"`
Favourite bool `json:"favourite,omitempty"`
FavouritedCount int `json:"favouritedCount,omitempty"`
SharePermissions []*SharePermissionScheme `json:"sharePermissions,omitempty"`
ShareUsers *FilterUsersScheme `json:"sharedUsers,omitempty"`
Subscriptions *[]FilterUsersScheme `json:"subscriptions,omitempty"`
}
type FilterSubscriptionPageScheme struct {
Size int `json:"size,omitempty"`
Items []*FilterSubscriptionScheme `json:"items,omitempty"`
MaxResults int `json:"max-results,omitempty"`
StartIndex int `json:"start-index,omitempty"`
EndIndex int `json:"end-index,omitempty"`
}
type FilterSubscriptionScheme struct {
ID int `json:"id,omitempty"`
User *UserScheme `json:"user,omitempty"`
Group *GroupScheme `json:"group,omitempty"`
}
type FilterUsersScheme struct {
Size int `json:"size,omitempty"`
Items []*UserScheme `json:"items,omitempty"`
MaxResults int `json:"max-results,omitempty"`
StartIndex int `json:"start-index,omitempty"`
EndIndex int `json:"end-index,omitempty"`
}