/
issue_impl_adf.go
420 lines (339 loc) · 13.6 KB
/
issue_impl_adf.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package internal
import (
"context"
"fmt"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service"
"github.com/ctreminiom/go-atlassian/service/jira"
"github.com/imdario/mergo"
"net/http"
"net/url"
"strings"
)
type IssueADFService struct {
internalClient jira.IssueADFConnector
Attachment *IssueAttachmentService
Comment *CommentADFService
Field *IssueFieldService
Label *LabelService
Link *LinkADFService
Metadata *MetadataService
Priority *PriorityService
Resolution *ResolutionService
Search *SearchADFService
Type *TypeService
Vote *VoteService
Watcher *WatcherService
Worklog *WorklogADFService
Property *IssuePropertyService
}
// Delete deletes an issue.
//
// 1.An issue cannot be deleted if it has one or more subtasks.
//
// 2.To delete an issue with subtasks, set deleteSubtasks.
//
// 3.This causes the issue's subtasks to be deleted with the issue.
//
// DELETE /rest/api/{2-3}/issue/{issueIdOrKey}
//
// https://docs.go-atlassian.io/jira-software-cloud/issues#delete-issue
func (i *IssueADFService) Delete(ctx context.Context, issueKeyOrId string, deleteSubTasks bool) (*model.ResponseScheme, error) {
return i.internalClient.Delete(ctx, issueKeyOrId, deleteSubTasks)
}
// Assign assigns an issue to a user.
//
// # Use this operation when the calling user does not have the Edit Issues permission but has the
//
// Assign issue permission for the project that the issue is in.
//
// If accountId is set to:
//
// 1. "-1", the issue is assigned to the default assignee for the project.
// 2. null, the issue is set to unassigned.
//
// PUT /rest/api/{2-3}/issue/{issueIdOrKey}/assignee
//
// https://docs.go-atlassian.io/jira-software-cloud/issues#assign-issue
func (i *IssueADFService) Assign(ctx context.Context, issueKeyOrId, accountId string) (*model.ResponseScheme, error) {
return i.internalClient.Assign(ctx, issueKeyOrId, accountId)
}
// Notify creates an email notification for an issue and adds it to the mail queue.
//
// POST /rest/api/{2-3}/issue/{issueIdOrKey}/notify
//
// https://docs.go-atlassian.io/jira-software-cloud/issues#send-notification-for-issue
func (i *IssueADFService) Notify(ctx context.Context, issueKeyOrId string, options *model.IssueNotifyOptionsScheme) (*model.ResponseScheme, error) {
return i.internalClient.Notify(ctx, issueKeyOrId, options)
}
// Transitions returns either all transitions or a transition that can be performed by the user on an issue, based on the issue's status.
//
// Note, if a request is made for a transition that does not exist or cannot be performed on the issue,
//
// given its status, the response will return any empty transitions list.
//
// GET /rest/api/{2-3}/issue/{issueIdOrKey}/transitions
//
// https://docs.go-atlassian.io/jira-software-cloud/issues#get-transitions
func (i *IssueADFService) Transitions(ctx context.Context, issueKeyOrId string) (*model.IssueTransitionsScheme, *model.ResponseScheme, error) {
return i.internalClient.Transitions(ctx, issueKeyOrId)
}
// Create creates an issue or, where the option to create subtasks is enabled in Jira, a subtask.
//
// POST /rest/api/{2-3}/issue
//
// https://docs.go-atlassian.io/jira-software-cloud/issues#create-issue
func (i *IssueADFService) Create(ctx context.Context, payload *model.IssueScheme, customFields *model.CustomFields) (*model.IssueResponseScheme, *model.ResponseScheme, error) {
return i.internalClient.Create(ctx, payload, customFields)
}
// Creates issues and, where the option to create subtasks is enabled in Jira, subtasks.
//
// 1.Creates upto 50 issues and, where the option to create subtasks is enabled in Jira, subtasks.
//
// 2.Transitions may be applied, to move the issues or subtasks to a workflow step other than the default start step, and issue properties set.
//
// POST /rest/api/{2-3}/issue/bulk
//
// https://docs.go-atlassian.io/jira-software-cloud/issues#bulk-create-issue
func (i *IssueADFService) Creates(ctx context.Context, payload []*model.IssueBulkSchemeV3) (*model.IssueBulkResponseScheme, *model.ResponseScheme, error) {
return i.internalClient.Creates(ctx, payload)
}
// Get returns the details for an issue.
//
// The issue is identified by its ID or key, however, if the identifier doesn't match an issue, a case-insensitive search
//
// and check for moved issues is performed. If a matching issue is found its details are returned, a 302 or other redirect is not returned.
//
// The issue key returned to the response is the key of the issue found.
//
// GET /rest/api/{2-3}/issue/{issueIdOrKey}
//
// https://docs.go-atlassian.io/jira-software-cloud/issues#get-issue
func (i *IssueADFService) Get(ctx context.Context, issueKeyOrId string, fields, expand []string) (*model.IssueScheme, *model.ResponseScheme, error) {
return i.internalClient.Get(ctx, issueKeyOrId, fields, expand)
}
// Update edits an issue.
//
// Edits an issue. A transition may be applied and issue properties updated as part of the edit.
//
// # The edits to the issue's fields are defined using update and fields
//
// PUT /rest/api/{2-3}/issue/{issueIdOrKey}
//
// https://docs.go-atlassian.io/jira-software-cloud/issues#edit-issue
func (i *IssueADFService) Update(ctx context.Context, issueKeyOrId string, notify bool, payload *model.IssueScheme, customFields *model.CustomFields, operations *model.UpdateOperations) (*model.ResponseScheme, error) {
return i.internalClient.Update(ctx, issueKeyOrId, notify, payload, customFields, operations)
}
// Move performs an issue transition and, if the transition has a screen, updates the fields from the transition screen.
//
// sortByCategory To update the fields on the transition screen, specify the fields in the fields or update parameters in the request body. Get details about the fields using Get transitions with the transitions.fields expand.
//
// POST /rest/api/{2-3}/issue/{issueIdOrKey}/transitions
//
// https://docs.go-atlassian.io/jira-software-cloud/issues#transition-issue
func (i *IssueADFService) Move(ctx context.Context, issueKeyOrId, transitionId string, options *model.IssueMoveOptionsV3) (*model.ResponseScheme, error) {
return i.internalClient.Move(ctx, issueKeyOrId, transitionId, options)
}
type internalIssueADFServiceImpl struct {
c service.Connector
version string
}
func (i *internalIssueADFServiceImpl) Delete(ctx context.Context, issueKeyOrId string, deleteSubTasks bool) (*model.ResponseScheme, error) {
return deleteIssue(ctx, i.c, i.version, issueKeyOrId, deleteSubTasks)
}
func (i *internalIssueADFServiceImpl) Assign(ctx context.Context, issueKeyOrId, accountId string) (*model.ResponseScheme, error) {
return assignIssue(ctx, i.c, i.version, issueKeyOrId, accountId)
}
func (i *internalIssueADFServiceImpl) Notify(ctx context.Context, issueKeyOrId string, options *model.IssueNotifyOptionsScheme) (*model.ResponseScheme, error) {
return sendNotification(ctx, i.c, i.version, issueKeyOrId, options)
}
func (i *internalIssueADFServiceImpl) Transitions(ctx context.Context, issueKeyOrId string) (*model.IssueTransitionsScheme, *model.ResponseScheme, error) {
return getTransitions(ctx, i.c, i.version, issueKeyOrId)
}
func (i *internalIssueADFServiceImpl) Create(ctx context.Context, payload *model.IssueScheme, customFields *model.CustomFields) (*model.IssueResponseScheme, *model.ResponseScheme, error) {
var request *http.Request
var err error
endpoint := fmt.Sprintf("rest/api/%v/issue", i.version)
if customFields != nil {
payloadWithFields, err := payload.MergeCustomFields(customFields)
if err != nil {
return nil, nil, err
}
request, err = i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payloadWithFields)
if err != nil {
return nil, nil, err
}
} else {
request, err = i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload)
if err != nil {
return nil, nil, err
}
}
issue := new(model.IssueResponseScheme)
response, err := i.c.Call(request, issue)
if err != nil {
return nil, response, err
}
return issue, response, nil
}
func (i *internalIssueADFServiceImpl) Creates(ctx context.Context, payload []*model.IssueBulkSchemeV3) (*model.IssueBulkResponseScheme, *model.ResponseScheme, error) {
if len(payload) == 0 {
return nil, nil, model.ErrNoCreateIssuesError
}
var issuePayloads []map[string]interface{}
for _, newIssue := range payload {
if newIssue.Payload == nil {
continue
}
issuePayload, err := newIssue.Payload.MergeCustomFields(newIssue.CustomFields)
if err != nil {
return nil, nil, err
}
issuePayloads = append(issuePayloads, issuePayload)
}
endpoint := fmt.Sprintf("rest/api/%v/issue/bulk", i.version)
request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"issueUpdates": issuePayloads})
if err != nil {
return nil, nil, err
}
issues := new(model.IssueBulkResponseScheme)
response, err := i.c.Call(request, issues)
if err != nil {
return nil, response, err
}
return issues, response, nil
}
func (i *internalIssueADFServiceImpl) Get(ctx context.Context, issueKeyOrId string, fields, expand []string) (*model.IssueScheme, *model.ResponseScheme, error) {
if issueKeyOrId == "" {
return nil, nil, model.ErrNoIssueKeyOrIDError
}
params := url.Values{}
if len(expand) != 0 {
params.Add("expand", strings.Join(expand, ","))
}
if len(fields) != 0 {
params.Add("fields", strings.Join(fields, ","))
}
var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("rest/api/%v/issue/%v", i.version, issueKeyOrId))
if params.Encode() != "" {
endpoint.WriteString(fmt.Sprintf("?%v", params.Encode()))
}
request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil)
if err != nil {
return nil, nil, err
}
issue := new(model.IssueScheme)
response, err := i.c.Call(request, issue)
if err != nil {
return nil, response, err
}
return issue, response, nil
}
func (i *internalIssueADFServiceImpl) Update(ctx context.Context, issueKeyOrId string, notify bool, payload *model.IssueScheme, customFields *model.CustomFields, operations *model.UpdateOperations) (*model.ResponseScheme, error) {
if issueKeyOrId == "" {
return nil, model.ErrNoIssueKeyOrIDError
}
params := url.Values{}
params.Add("notifyUsers", fmt.Sprintf("%v", notify))
endpoint := fmt.Sprintf("rest/api/%v/issue/%v?%v", i.version, issueKeyOrId, params.Encode())
withCustomFields := customFields != nil
withOperations := operations != nil
var err error
payloadUpdated := make(map[string]interface{})
// Executed when the customfields and operations are provided
if withCustomFields && withOperations {
payloadUpdated, err = payload.MergeCustomFields(customFields)
if err != nil {
return nil, err
}
payloadWithOperations, err := payload.MergeOperations(operations)
if err != nil {
return nil, err
}
if err = mergo.Map(&payloadUpdated, &payloadWithOperations, mergo.WithOverride); err != nil {
return nil, err
}
}
// Executed when only the customfields are provided, but not the operations
if withCustomFields && !withOperations {
payloadUpdated, err = payload.MergeCustomFields(customFields)
if err != nil {
return nil, err
}
}
// Executed when only the operations are provided, but not the customfields
if withOperations && !withCustomFields {
payloadUpdated, err = payload.MergeOperations(operations)
if err != nil {
return nil, err
}
}
// After the payload transformation, validate if the shadowed payloadUpdated variable contains data
var request *http.Request
if len(payloadUpdated) != 0 {
request, err = i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payloadUpdated)
if err != nil {
return nil, err
}
} else {
request, err = i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload)
if err != nil {
return nil, err
}
}
return i.c.Call(request, nil)
}
func (i *internalIssueADFServiceImpl) Move(ctx context.Context, issueKeyOrId, transitionId string, options *model.IssueMoveOptionsV3) (*model.ResponseScheme, error) {
if issueKeyOrId == "" {
return nil, model.ErrNoIssueKeyOrIDError
}
if transitionId == "" {
return nil, model.ErrNoTransitionIDError
}
payloadUpdated := make(map[string]interface{})
payloadUpdated["transition"] = map[string]interface{}{"id": transitionId}
// Process logic only if the transition options are provided
if options != nil {
if options.Fields == nil {
return nil, model.ErrNoIssueSchemeError
}
withCustomFields := options.CustomFields != nil
withOperations := options.Operations != nil
var err error
// Executed when the customfields and operations are provided
if withCustomFields && withOperations {
payloadUpdated, err = options.Fields.MergeCustomFields(options.CustomFields)
if err != nil {
return nil, err
}
payloadWithOperations, err := options.Fields.MergeOperations(options.Operations)
if err != nil {
return nil, err
}
if err = mergo.Map(&payloadUpdated, &payloadWithOperations, mergo.WithOverride); err != nil {
return nil, err
}
}
// Executed when only the customfields are provided, but not the operations
if withCustomFields && !withOperations {
payloadUpdated, err = options.Fields.MergeCustomFields(options.CustomFields)
if err != nil {
return nil, err
}
}
// Executed when only the operations are provided, but not the customfields
if withOperations && !withCustomFields {
payloadUpdated, err = options.Fields.MergeOperations(options.Operations)
if err != nil {
return nil, err
}
}
}
endpoint := fmt.Sprintf("rest/api/%v/issue/%v/transitions", i.version, issueKeyOrId)
request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payloadUpdated)
if err != nil {
return nil, err
}
return i.c.Call(request, nil)
}