-
Notifications
You must be signed in to change notification settings - Fork 29
/
issueFieldOptionContext.go
183 lines (142 loc) · 6.17 KB
/
issueFieldOptionContext.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
package jira
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
type FieldOptionContextService struct{ client *Client }
type FieldOptionContextParams struct {
OptionID int
OnlyOptions bool
}
type CustomFieldContextOptionPageScheme struct {
Self string `json:"self,omitempty"`
NextPage string `json:"nextPage,omitempty"`
MaxResults int `json:"maxResults,omitempty"`
StartAt int `json:"startAt,omitempty"`
Total int `json:"total,omitempty"`
IsLast bool `json:"isLast,omitempty"`
Values []*CustomFieldContextOptionScheme `json:"values,omitempty"`
}
type CustomFieldContextOptionScheme struct {
ID string `json:"id,omitempty"`
Value string `json:"value,omitempty"`
Disabled bool `json:"disabled,omitempty"`
OptionID string `json:"optionId,omitempty"`
}
// Returns a paginated list of all custom field option for a context.
// Options are returned first then cascading options, in the order they display in Jira.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/issues/fields/context/option#get-custom-field-options
func (f *FieldOptionContextService) Gets(ctx context.Context, fieldID string, contextID int, opts *FieldOptionContextParams, startAt, maxResults int) (result *CustomFieldContextOptionPageScheme, response *Response, err error) {
params := url.Values{}
params.Add("startAt", strconv.Itoa(startAt))
params.Add("maxResults", strconv.Itoa(maxResults))
if opts != nil {
if opts.OptionID != 0 {
params.Add("optionId", strconv.Itoa(opts.OptionID))
}
if opts.OnlyOptions {
params.Add("onlyOptions", "true")
}
}
var endpoint = fmt.Sprintf("rest/api/3/field/%v/context/%v/option?%v", fieldID, contextID, 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(CustomFieldContextOptionPageScheme)
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 FieldContextOptionListScheme struct {
Options []*CustomFieldContextOptionScheme `json:"options,omitempty"`
}
// Creates options and, where the custom select field is of the type Select List (cascading),
// cascading options for a custom select field. The options are added to a context of the field.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/issues/fields/context/option#create-custom-field-options
func (f *FieldOptionContextService) Create(ctx context.Context, fieldID string, contextID int, payload *FieldContextOptionListScheme) (result *FieldContextOptionListScheme, response *Response, err error) {
if payload == nil {
return nil, nil, fmt.Errorf("error, payload value is nil, please provide a valid CustomFieldOptionPayloadScheme pointer")
}
if fieldID == "" {
return nil, nil, fmt.Errorf("error, fieldID value is nil, please provide a valid fieldID value")
}
var endpoint = fmt.Sprintf("rest/api/3/field/%v/context/%v/option", fieldID, contextID)
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(FieldContextOptionListScheme)
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 the options of a custom field.
// If any of the options are not found, no options are updated.
// Options where the values in the request match the current values aren't updated and aren't reported in the response.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/issues/fields/context/option#update-custom-field-options
func (f *FieldOptionContextService) Update(ctx context.Context, fieldID string, contextID int, payload *FieldContextOptionListScheme) (result *FieldContextOptionListScheme, response *Response, err error) {
if payload == nil {
return nil, nil, fmt.Errorf("error, payload value is nil, please provide a valid CustomFieldOptionPayloadScheme pointer")
}
if fieldID == "" {
return nil, nil, fmt.Errorf("error, fieldID value is nil, please provide a valid fieldID value")
}
var endpoint = fmt.Sprintf("rest/api/3/field/%v/context/%v/option", fieldID, contextID)
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(FieldContextOptionListScheme)
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
}
// Deletes a custom field option.
// Options with cascading options cannot be deleted without deleting the cascading options first.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/issues/fields/context/option#delete-custom-field-options
func (f *FieldOptionContextService) Delete(ctx context.Context, fieldID string, contextID, optionID int) (response *Response, err error) {
if fieldID == "" {
return nil, fmt.Errorf("error, fieldID value is nil, please provide a valid fieldID value")
}
if contextID == 0 {
return nil, fmt.Errorf("error, fieldID value is nil, please provide a valid contextID value")
}
if optionID == 0 {
return nil, fmt.Errorf("error, fieldID value is nil, please provide a valid optionID value")
}
var endpoint = fmt.Sprintf("rest/api/3/field/%v/context/%v/option/%v", fieldID, contextID, optionID)
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
}