-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathedit.go
More file actions
430 lines (384 loc) · 11 KB
/
Copy pathedit.go
File metadata and controls
430 lines (384 loc) · 11 KB
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
421
422
423
424
425
426
427
428
429
430
package jira
import (
"context"
"encoding/json"
"maps"
"net/http"
"slices"
"strconv"
"strings"
)
const separatorMinus = "-"
// EditResponse struct holds response from POST /issue endpoint.
type EditResponse struct {
ID string `json:"id"`
Key string `json:"key"`
}
// EditRequest struct holds request data for edit request.
// Setting an Assignee requires an account ID.
type EditRequest struct {
IssueType string
ParentIssueKey string
Summary string
Body string
Priority string
Labels []string
Components []string
FixVersions []string
AffectsVersions []string
// CustomFields holds all custom fields passed
// while editing the issue.
CustomFields map[string]string
SkipNotify bool
configuredCustomFields []IssueTypeField
}
// WithCustomFields sets valid custom fields for the issue.
func (er *EditRequest) WithCustomFields(cf []IssueTypeField) {
er.configuredCustomFields = cf
}
// Edit updates an issue using POST /issue endpoint.
func (c *Client) Edit(key string, req *EditRequest) error {
data := getRequestDataForEdit(req)
body, err := json.Marshal(&data)
if err != nil {
return err
}
endpoint := "/issue/" + key
if req.SkipNotify {
endpoint += "?notifyUsers=false"
}
res, err := c.PutV2(context.Background(), endpoint, body, Header{
"Accept": "application/json",
"Content-Type": "application/json",
})
if err != nil {
return err
}
if res == nil {
return ErrEmptyResponse
}
defer func() { _ = res.Body.Close() }()
if res.StatusCode != http.StatusNoContent {
return formatUnexpectedResponse(res)
}
return nil
}
type editFields struct {
Summary []struct {
Set string `json:"set,omitempty"`
} `json:"summary,omitempty"`
Description []struct {
Set string `json:"set,omitempty"`
} `json:"description,omitempty"`
Priority []struct {
Set struct {
Name string `json:"name,omitempty"`
} `json:"set"`
} `json:"priority,omitempty"`
Labels []struct {
Add string `json:"add,omitempty"`
Remove string `json:"remove,omitempty"`
} `json:"labels,omitempty"`
Components []struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
} `json:"components,omitempty"`
FixVersions []struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
} `json:"fixVersions,omitempty"`
AffectsVersions []struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
} `json:"versions,omitempty"`
customFields customField
}
type editFieldsMarshaler struct {
M editFields
}
// MarshalJSON is a custom marshaler to handle empty fields.
func (cfm *editFieldsMarshaler) MarshalJSON() ([]byte, error) {
if len(cfm.M.Summary) == 0 || cfm.M.Summary[0].Set == "" {
cfm.M.Summary = nil
}
if len(cfm.M.Description) == 0 || cfm.M.Description[0].Set == "" {
cfm.M.Description = nil
}
if len(cfm.M.Priority) == 0 || cfm.M.Priority[0].Set.Name == "" {
cfm.M.Priority = nil
}
if len(cfm.M.Components) == 0 || (cfm.M.Components[0].Add != nil && cfm.M.Components[0].Remove != nil) {
cfm.M.Components = nil
}
if len(cfm.M.Labels) == 0 || (cfm.M.Labels[0].Add == "" && cfm.M.Labels[0].Remove == "") {
cfm.M.Labels = nil
}
m, err := json.Marshal(cfm.M)
if err != nil {
return m, err
}
var temp any
if err := json.Unmarshal(m, &temp); err != nil {
return nil, err
}
dm := temp.(map[string]any)
maps.Copy(dm, cfm.M.customFields)
return json.Marshal(dm)
}
type editRequest struct {
Update editFieldsMarshaler `json:"update"`
Fields struct {
Parent *struct {
Key string `json:"key,omitempty"`
Set string `json:"set,omitempty"`
} `json:"parent,omitempty"`
} `json:"fields"`
}
func getRequestDataForEdit(req *EditRequest) *editRequest {
if req.Labels == nil {
req.Labels = []string{}
}
update := editFieldsMarshaler{editFields{
Summary: []struct {
Set string `json:"set,omitempty"`
}{{Set: req.Summary}},
Description: []struct {
Set string `json:"set,omitempty"`
}{{Set: req.Body}},
Priority: []struct {
Set struct {
Name string `json:"name,omitempty"`
} `json:"set"`
}{{Set: struct {
Name string `json:"name,omitempty"`
}{Name: req.Priority}}},
}}
if len(req.Labels) > 0 {
add, sub := splitAddAndRemove(req.Labels)
labels := make([]struct {
Add string `json:"add,omitempty"`
Remove string `json:"remove,omitempty"`
}, 0, len(req.Labels))
for _, l := range sub {
labels = append(labels, struct {
Add string `json:"add,omitempty"`
Remove string `json:"remove,omitempty"`
}{Remove: l})
}
for _, l := range add {
labels = append(labels, struct {
Add string `json:"add,omitempty"`
Remove string `json:"remove,omitempty"`
}{Add: l})
}
update.M.Labels = labels
}
if len(req.Components) > 0 {
add, sub := splitAddAndRemove(req.Components)
cmp := make([]struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
}, 0, len(req.Components))
for _, c := range sub {
cmp = append(cmp, struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
}{Remove: &struct {
Name string `json:"name,omitempty"`
}{Name: c}})
}
for _, c := range add {
cmp = append(cmp, struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
}{Add: &struct {
Name string `json:"name,omitempty"`
}{Name: c}})
}
update.M.Components = cmp
}
if len(req.FixVersions) > 0 {
add, sub := splitAddAndRemove(req.FixVersions)
versions := make([]struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
}, 0, len(req.FixVersions))
for _, v := range sub {
versions = append(versions, struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
}{Remove: &struct {
Name string `json:"name,omitempty"`
}{Name: v}})
}
for _, v := range add {
versions = append(versions, struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
}{Add: &struct {
Name string `json:"name,omitempty"`
}{Name: v}})
}
update.M.FixVersions = versions
}
if len(req.AffectsVersions) > 0 {
add, sub := splitAddAndRemove(req.AffectsVersions)
versions := make([]struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
}, 0, len(req.AffectsVersions))
for _, v := range sub {
versions = append(versions, struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
}{Remove: &struct {
Name string `json:"name,omitempty"`
}{Name: v}})
}
for _, v := range add {
versions = append(versions, struct {
Add *struct {
Name string `json:"name,omitempty"`
} `json:"add,omitempty"`
Remove *struct {
Name string `json:"name,omitempty"`
} `json:"remove,omitempty"`
}{Add: &struct {
Name string `json:"name,omitempty"`
}{Name: v}})
}
update.M.AffectsVersions = versions
}
fields := struct {
Parent *struct {
Key string `json:"key,omitempty"`
Set string `json:"set,omitempty"`
} `json:"parent,omitempty"`
}{
Parent: &struct {
Key string `json:"key,omitempty"`
Set string `json:"set,omitempty"`
}{},
}
if req.ParentIssueKey != "" {
if req.ParentIssueKey == AssigneeNone {
fields.Parent.Set = AssigneeNone
} else {
fields.Parent.Key = req.ParentIssueKey
}
}
data := editRequest{
Update: update,
Fields: fields,
}
constructCustomFieldsForEdit(req.CustomFields, req.configuredCustomFields, &data)
return &data
}
func constructCustomFieldsForEdit(fields map[string]string, configuredFields []IssueTypeField, data *editRequest) {
if len(fields) == 0 || len(configuredFields) == 0 {
return
}
data.Update.M.customFields = make(customField)
for key, val := range fields {
for _, configured := range configuredFields {
identifier := strings.ReplaceAll(strings.ToLower(strings.TrimSpace(configured.Name)), " ", "-")
if identifier != strings.ToLower(key) {
continue
}
if parsed, ok := parseCustomFieldJSONContainer(val); ok {
data.Update.M.customFields[configured.Key] = []customFieldTypeAnySet{{Set: parsed}}
continue
}
switch configured.Schema.DataType {
case customFieldFormatOption:
data.Update.M.customFields[configured.Key] = []customFieldTypeOptionSet{{Set: customFieldTypeOption{Value: val}}}
case customFieldFormatProject:
data.Update.M.customFields[configured.Key] = []customFieldTypeProjectSet{{Set: customFieldTypeProject{Value: val}}}
case customFieldFormatArray:
pieces := strings.Split(strings.TrimSpace(val), ",")
if configured.Schema.Items == customFieldFormatOption {
items := make([]customFieldTypeOptionAddRemove, 0)
for _, p := range pieces {
if strings.HasPrefix(p, separatorMinus) {
items = append(items, customFieldTypeOptionAddRemove{Remove: &customFieldTypeOption{Value: strings.TrimPrefix(p, separatorMinus)}})
} else {
items = append(items, customFieldTypeOptionAddRemove{Add: &customFieldTypeOption{Value: p}})
}
}
data.Update.M.customFields[configured.Key] = items
} else {
data.Update.M.customFields[configured.Key] = pieces
}
case customFieldFormatNumber:
num, err := strconv.ParseFloat(val, 64) //nolint:gomnd
if err != nil {
// Let Jira API handle data type error for now.
data.Update.M.customFields[configured.Key] = []customFieldTypeStringSet{{Set: val}}
} else {
data.Update.M.customFields[configured.Key] = []customFieldTypeNumberSet{{Set: customFieldTypeNumber(num)}}
}
default:
data.Update.M.customFields[configured.Key] = []customFieldTypeStringSet{{Set: val}}
}
}
}
}
func splitAddAndRemove(input []string) ([]string, []string) {
add := make([]string, 0, len(input))
sub := make([]string, 0, len(input))
for _, inp := range input {
if strings.HasPrefix(inp, separatorMinus) {
sub = append(sub, strings.TrimPrefix(inp, separatorMinus))
}
}
for _, inp := range input {
if !strings.HasPrefix(inp, separatorMinus) && !slices.Contains(sub, inp) {
add = append(add, inp)
}
}
return add, sub
}