-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbusiness_logic.go
335 lines (278 loc) · 10.1 KB
/
business_logic.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
package main
import (
"context"
"fmt"
"github.com/alexeldeib/incli/client"
kitlog "github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/sanity-io/litter"
)
func FindCustomFieldByName(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, targetName string) (*client.CustomFieldV2, error) {
customFields, err := ListAllCustomFields(ctx, logger, cl)
if err != nil {
return nil, errors.Wrap(err, "listing custom fields types")
}
for _, v := range customFields {
if v.Name == targetName {
return &v, nil
}
}
return nil, errors.Errorf("catalog type %q not found", targetName)
}
func FindCatalogTypeByID(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, targetID string) (*client.CatalogTypeV2, error) {
catalogTypes, err := ListAllCatalogTypes(ctx, logger, cl)
if err != nil {
return nil, fmt.Errorf("finding catalog type by name: %s", err)
}
for _, v := range catalogTypes {
if v.Id == targetID {
return &v, nil
}
}
return nil, errors.Errorf("catalog type %q not found", targetID)
}
func FindCatalogTypeByName(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, targetName string) (*client.CatalogTypeV2, error) {
catalogTypes, err := ListAllCatalogTypes(ctx, logger, cl)
if err != nil {
return nil, fmt.Errorf("finding catalog type by name: %s", err)
}
for _, v := range catalogTypes {
if v.Name == targetName {
return &v, nil
}
}
return nil, errors.Errorf("catalog type %q not found", targetName)
}
func FindCatalogEntryByNameWithTypeName(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, targetName string, typeName string) (*client.CatalogEntryV2, error) {
catalogType, err := FindCatalogTypeByName(ctx, logger, cl, typeName)
if err != nil {
return nil, fmt.Errorf("finding catalog type by name for entry lookup: %s", err)
}
catalogEntry, err := FindCatalogEntryByNameWithTypeID(ctx, logger, cl, targetName, catalogType.Id)
if err != nil {
return nil, fmt.Errorf("finding catalog entry by name with type id: %s", err)
}
return catalogEntry, nil
}
func FindCatalogEntryByNameWithTypeID(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, targetName string, typeID string) (*client.CatalogEntryV2, error) {
var (
after *string
pageSize = 250
)
for {
page, err := cl.CatalogV2ListEntriesWithResponse(ctx, &client.CatalogV2ListEntriesParams{
CatalogTypeId: typeID,
PageSize: &pageSize,
After: after,
})
if err != nil {
return nil, fmt.Errorf("listing catalog entries: %s", err)
}
for _, candidate := range page.JSON200.CatalogEntries {
if candidate.Name == targetName {
return &candidate, nil
}
for _, alias := range candidate.Aliases {
if alias == targetName {
return &candidate, nil
}
}
}
if count := len(page.JSON200.CatalogEntries); count == 0 {
return nil, fmt.Errorf("failed to find catalog entry %q", targetName) // end pagination
} else {
after = lo.ToPtr(page.JSON200.CatalogEntries[count-1].Id)
}
}
}
func FindCatalogEntryByID(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, targetID string) (*client.CatalogEntryV2, error) {
res, err := cl.CatalogV2ShowEntryWithResponse(ctx, targetID)
if err != nil {
return nil, errors.Wrap(err, "listing catalog")
}
return &res.JSON200.CatalogEntry, nil
}
func ListAllCustomFields(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) ([]client.CustomFieldV2, error) {
res, err := cl.CustomFieldsV2ListWithResponse(ctx)
if err != nil {
return nil, errors.Wrap(err, "listing catalog")
}
return res.JSON200.CustomFields, nil
}
func ListAllCatalogTypes(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) ([]client.CatalogTypeV2, error) {
res, err := cl.CatalogV2ListTypesWithResponse(ctx)
if err != nil {
return nil, errors.Wrap(err, "listing catalog types")
}
return res.JSON200.CatalogTypes, nil
}
func ListAllCatalogEntries(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) ([]client.CatalogEntryV2, error) {
catalogTypes, err := ListAllCatalogTypes(ctx, logger, cl)
if err != nil {
return nil, fmt.Errorf("failed enumerating catalog types: %s", err)
}
var results []client.CatalogEntryV2
for _, catalogType := range catalogTypes {
var (
after *string
pageSize = 250
)
for {
page, err := cl.CatalogV2ListEntriesWithResponse(ctx, &client.CatalogV2ListEntriesParams{
CatalogTypeId: catalogType.Id,
PageSize: &pageSize,
After: after,
})
if err != nil {
return nil, fmt.Errorf("listing catalog entries: %q", err)
}
results = append(results, page.JSON200.CatalogEntries...)
if count := len(page.JSON200.CatalogEntries); count == 0 {
break // end pagination
} else {
after = lo.ToPtr(page.JSON200.CatalogEntries[count-1].Id)
}
}
}
return results, nil
}
func ListAllCatalogEntriesByTypeName(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, typeName string) ([]client.CatalogEntryV2, error) {
catalogType, err := FindCatalogTypeByName(ctx, logger, cl, typeName)
if err != nil {
return nil, fmt.Errorf("finding catalog type by name for entry lookup: %s", err)
}
return ListAllCatalogEntriesByTypeID(ctx, logger, cl, catalogType.Id)
}
func ListAllCatalogEntriesByTypeID(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, typeID string) ([]client.CatalogEntryV2, error) {
var (
after *string
pageSize = 250
results = []client.CatalogEntryV2{}
)
for {
page, err := cl.CatalogV2ListEntriesWithResponse(ctx, &client.CatalogV2ListEntriesParams{
CatalogTypeId: typeID,
PageSize: &pageSize,
After: after,
})
if err != nil {
return nil, fmt.Errorf("listing catalog entries: %q", err)
}
results = append(results, page.JSON200.CatalogEntries...)
if count := len(page.JSON200.CatalogEntries); count == 0 {
return results, nil // end pagination
} else {
after = lo.ToPtr(page.JSON200.CatalogEntries[count-1].Id)
}
}
}
func EditIncidentByReferenceNumber(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, reference int, newCustomFields map[string]string) (*client.IncidentV2, error) {
incident, err := FindIncidentByReferenceNumber(ctx, logger, cl, reference)
if err != nil {
return nil, fmt.Errorf("finding incident by id to show: %q", err)
}
return EditIncident(ctx, logger, cl, incident.Id, newCustomFields)
}
func ShowIncidentByID(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, id string) (*client.IncidentV2, error) {
res, err := cl.IncidentsV2ShowWithResponse(ctx, id)
if err != nil {
return nil, errors.Wrap(err, "listing catalog types")
}
return &res.JSON200.Incident, nil
}
func ShowIncidentByReference(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, reference int) (*client.IncidentV2, error) {
incident, err := FindIncidentByReferenceNumber(ctx, logger, cl, reference)
if err != nil {
return nil, errors.Wrap(err, "finding incident by id to show")
}
return ShowIncidentByID(ctx, logger, cl, incident.Id)
}
func ListAllIncidents(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses) ([]client.IncidentV2, error) {
var (
after *string
pageSize = int64(250)
results = []client.IncidentV2{}
)
for {
page, err := cl.IncidentsV2ListWithResponse(ctx, &client.IncidentsV2ListParams{
PageSize: &pageSize,
After: after,
})
if err != nil {
return nil, errors.Wrap(err, "listing incidents")
}
results = append(results, page.JSON200.Incidents...)
if count := len(page.JSON200.Incidents); count == 0 {
return results, nil // end pagination
} else {
after = lo.ToPtr(page.JSON200.Incidents[count-1].Id)
}
}
}
func FindIncidentByReferenceNumber(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, reference int) (*client.IncidentV2, error) {
incidents, err := ListAllIncidents(ctx, logger, cl)
if err != nil {
return nil, errors.Wrap(err, "listing incidents to find by reference")
}
for _, candidate := range incidents {
if candidate.Reference == fmt.Sprintf("INC-%d", reference) {
return &candidate, nil
}
}
return nil, errors.New("incident not found")
}
func EditIncident(ctx context.Context, logger kitlog.Logger, cl *client.ClientWithResponses, id string, newCustomFields map[string]string) (*client.IncidentV2, error) {
customFields, err := ListAllCustomFields(ctx, logger, cl)
if err != nil {
return nil, fmt.Errorf("failed to find custom field types: %q", err)
}
customFieldIDToType := map[string]client.CustomFieldV2FieldType{}
customFieldMap := map[string]string{}
for _, candidate := range customFields {
customFieldMap[candidate.Name] = candidate.Id
customFieldIDToType[candidate.Id] = candidate.FieldType
}
body := client.IncidentsV2EditJSONRequestBody{
Incident: client.IncidentEditPayloadV2{
CustomFieldEntries: &[]client.CustomFieldEntryPayloadV1{},
},
NotifyIncidentChannel: false,
}
for k, v := range newCustomFields {
id, ok := customFieldMap[k]
if !ok {
return nil, errors.Errorf("custom field ID for %q not found", k)
}
// empty array resets previous set value
var values = []client.CustomFieldValuePayloadV1{}
if v != "" {
var customVal client.CustomFieldValuePayloadV1
switch customFieldIDToType[id] {
case client.SingleSelect:
customVal = client.CustomFieldValuePayloadV1{
ValueCatalogEntryId: &v,
}
case client.Text:
customVal = client.CustomFieldValuePayloadV1{
ValueText: &v,
}
default:
return nil, errors.Errorf("unsupported custom field type %q", customFieldIDToType[id])
}
values = append(values, customVal)
}
*body.Incident.CustomFieldEntries = append(*body.Incident.CustomFieldEntries, client.CustomFieldEntryPayloadV1{
CustomFieldId: id,
Values: values,
})
}
litter.Dump(id)
litter.Dump("===")
litter.Dump(body)
res, err := cl.IncidentsV2EditWithResponse(ctx, id, body)
if err != nil {
return nil, errors.Wrap(err, "failed to edit incident")
}
return &res.JSON200.Incident, nil
}