-
Notifications
You must be signed in to change notification settings - Fork 34
/
screen.go
269 lines (208 loc) · 7.52 KB
/
screen.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
package jira
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
type ScreenService struct {
client *Client
Tab *ScreenTabService
Scheme *ScreenSchemeService
}
type ScreenScheme struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Scope *TeamManagedProjectScopeScheme `json:"scope,omitempty"`
}
type ScreenFieldPageScheme 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 []*ScreenWithTabScheme `json:"values,omitempty"`
}
type ScreenWithTabScheme struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Scope *TeamManagedProjectScopeScheme `json:"scope,omitempty"`
Tab *ScreenTabScheme `json:"tab,omitempty"`
}
// Returns a paginated list of the screens a field is used in.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/screens#get-screens-for-a-field
func (s *ScreenService) Fields(ctx context.Context, fieldID string, startAt, maxResults int) (result *ScreenFieldPageScheme, response *Response, err error) {
if len(fieldID) == 0 {
return nil, nil, fmt.Errorf("error, please provide a valid fieldID value ")
}
params := url.Values{}
params.Add("startAt", strconv.Itoa(startAt))
params.Add("maxResults", strconv.Itoa(maxResults))
var endpoint = fmt.Sprintf("rest/api/3/field/%v/screens?%v", fieldID, params.Encode())
request, err := s.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}
request.Header.Set("Accept", "application/json")
response, err = s.client.Do(request)
if err != nil {
return
}
result = new(ScreenFieldPageScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return
}
return
}
type ScreenSearchPageScheme 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 []*ScreenScheme `json:"values,omitempty"`
}
// Returns a paginated list of all screens or those specified by one or more screen IDs.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/screens#get-screens
func (s *ScreenService) Gets(ctx context.Context, screenIDs []int, startAt, maxResults int) (result *ScreenSearchPageScheme, response *Response, err error) {
params := url.Values{}
params.Add("startAt", strconv.Itoa(startAt))
params.Add("maxResults", strconv.Itoa(maxResults))
for _, screenID := range screenIDs {
params.Add("id", strconv.Itoa(screenID))
}
var endpoint = fmt.Sprintf("rest/api/3/screens?%v", params.Encode())
request, err := s.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}
request.Header.Set("Accept", "application/json")
response, err = s.client.Do(request)
if err != nil {
return
}
result = new(ScreenSearchPageScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return
}
return
}
// Creates a screen with a default field tab.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/screens#create-screen
func (s *ScreenService) Create(ctx context.Context, name, description string) (result *ScreenScheme, response *Response, err error) {
if len(name) == 0 {
return nil, nil, fmt.Errorf("error, please project a valid screen name value")
}
payload := struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}{
Name: name,
Description: description,
}
var endpoint = "rest/api/3/screens"
request, err := s.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 = s.client.Do(request)
if err != nil {
return
}
result = new(ScreenScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return
}
return
}
// Adds a field to the default tab of the default screen.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/screens#add-field-to-default-screen
func (s *ScreenService) AddToDefault(ctx context.Context, fieldID string) (response *Response, err error) {
if len(fieldID) == 0 {
return nil, fmt.Errorf("error, please provide a valid fieldID value")
}
var endpoint = fmt.Sprintf("rest/api/3/screens/addToDefault/%v", fieldID)
request, err := s.client.newRequest(ctx, http.MethodPost, endpoint, nil)
if err != nil {
return
}
request.Header.Set("Accept", "application/json")
response, err = s.client.Do(request)
if err != nil {
return
}
return
}
// Updates a screen. Only screens used in classic projects can be updated.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/screens#update-screen
func (s *ScreenService) Update(ctx context.Context, screenID int, name, description string) (result *ScreenScheme, response *Response, err error) {
payload := struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}{
Name: name,
Description: description,
}
var endpoint = fmt.Sprintf("rest/api/3/screens/%v", screenID)
request, err := s.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 = s.client.Do(request)
if err != nil {
return
}
result = new(ScreenScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return
}
return
}
// Deletes a screen.
// A screen cannot be deleted if it is used in a screen scheme,
// workflow, or workflow draft. Only screens used in classic projects can be deleted.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/screens#delete-screen
func (s *ScreenService) Delete(ctx context.Context, screenID int) (response *Response, err error) {
var endpoint = fmt.Sprintf("rest/api/3/screens/%v", screenID)
request, err := s.client.newRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return
}
response, err = s.client.Do(request)
if err != nil {
return
}
return
}
type AvailableScreenFieldScheme struct {
ID string `json:"id"`
Name string `json:"name"`
}
// Returns the fields that can be added to a tab on a screen.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/screens#get-available-screen-fields
func (s *ScreenService) Available(ctx context.Context, screenID int) (result *[]AvailableScreenFieldScheme, response *Response, err error) {
var endpoint = fmt.Sprintf("rest/api/3/screens/%v/availableFields", screenID)
request, err := s.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}
request.Header.Set("Accept", "application/json")
response, err = s.client.Do(request)
if err != nil {
return
}
result = new([]AvailableScreenFieldScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return
}
return
}