This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
make_functions.go
513 lines (493 loc) · 18.6 KB
/
make_functions.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package testfixture
import (
"math/rand"
"strings"
"github.com/fabric8-services/fabric8-wit/account"
"github.com/fabric8-services/fabric8-wit/app"
"github.com/fabric8-services/fabric8-wit/area"
"github.com/fabric8-services/fabric8-wit/codebase"
"github.com/fabric8-services/fabric8-wit/comment"
"github.com/fabric8-services/fabric8-wit/iteration"
"github.com/fabric8-services/fabric8-wit/label"
"github.com/fabric8-services/fabric8-wit/remoteworkitem"
"github.com/fabric8-services/fabric8-wit/rendering"
"github.com/fabric8-services/fabric8-wit/space"
testsupport "github.com/fabric8-services/fabric8-wit/test"
"github.com/fabric8-services/fabric8-wit/workitem"
"github.com/fabric8-services/fabric8-wit/workitem/link"
errs "github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)
func makeIdentities(fxt *TestFixture) error {
if fxt.info[kindIdentities] == nil {
return nil
}
fxt.Identities = make([]*account.Identity, fxt.info[kindIdentities].numInstances)
for i := range fxt.Identities {
fxt.Identities[i] = &account.Identity{
Username: testsupport.CreateRandomValidTestName("John Doe "),
ProviderType: "test provider",
}
if err := fxt.runCustomizeEntityFuncs(i, kindIdentities); err != nil {
return errs.WithStack(err)
}
err := testsupport.CreateTestIdentityForAccountIdentity(fxt.db, fxt.Identities[i])
if err != nil {
return errs.Wrapf(err, "failed to create identity: %+v", fxt.Identities[i])
}
}
return nil
}
func makeWorkItemLinkCategories(fxt *TestFixture) error {
if fxt.info[kindWorkItemLinkCategories] == nil {
return nil
}
fxt.WorkItemLinkCategories = make([]*link.WorkItemLinkCategory, fxt.info[kindWorkItemLinkCategories].numInstances)
wilcRepo := link.NewWorkItemLinkCategoryRepository(fxt.db)
for i := range fxt.WorkItemLinkCategories {
desc := "some description"
fxt.WorkItemLinkCategories[i] = &link.WorkItemLinkCategory{
Name: testsupport.CreateRandomValidTestName("link category "),
Description: &desc,
}
if err := fxt.runCustomizeEntityFuncs(i, kindWorkItemLinkCategories); err != nil {
return errs.WithStack(err)
}
_, err := wilcRepo.Create(fxt.ctx, fxt.WorkItemLinkCategories[i])
if err != nil {
return errs.Wrapf(err, "failed to create work item link category: %+v", fxt.WorkItemLinkCategories[i])
}
}
return nil
}
func makeSpaces(fxt *TestFixture) error {
if fxt.info[kindSpaces] == nil {
return nil
}
fxt.Spaces = make([]*space.Space, fxt.info[kindSpaces].numInstances)
spaceRepo := space.NewRepository(fxt.db)
for i := range fxt.Spaces {
fxt.Spaces[i] = &space.Space{
Name: testsupport.CreateRandomValidTestName("space "),
Description: "Some description",
}
if !fxt.isolatedCreation {
fxt.Spaces[i].OwnerId = fxt.Identities[0].ID
}
if err := fxt.runCustomizeEntityFuncs(i, kindSpaces); err != nil {
return errs.WithStack(err)
}
if fxt.isolatedCreation {
if fxt.Spaces[i].OwnerId == uuid.Nil {
return errs.New("you must specify an owner ID for each space")
}
}
_, err := spaceRepo.Create(fxt.ctx, fxt.Spaces[i])
if err != nil {
return errs.Wrapf(err, "failed to create space: %+v", fxt.Spaces[i])
}
}
return nil
}
func makeWorkItemLinkTypes(fxt *TestFixture) error {
if fxt.info[kindWorkItemLinkTypes] == nil {
return nil
}
fxt.WorkItemLinkTypes = make([]*link.WorkItemLinkType, fxt.info[kindWorkItemLinkTypes].numInstances)
wiltRepo := link.NewWorkItemLinkTypeRepository(fxt.db)
for i := range fxt.WorkItemLinkTypes {
desc := "some description"
fxt.WorkItemLinkTypes[i] = &link.WorkItemLinkType{
Name: testsupport.CreateRandomValidTestName("work item link type "),
Description: &desc,
Topology: link.TopologyTree,
ForwardName: "forward name (e.g. blocks)",
ReverseName: "reverse name (e.g. blocked by)",
}
if !fxt.isolatedCreation {
fxt.WorkItemLinkTypes[i].SpaceID = fxt.Spaces[0].ID
fxt.WorkItemLinkTypes[i].LinkCategoryID = fxt.WorkItemLinkCategories[0].ID
}
if err := fxt.runCustomizeEntityFuncs(i, kindWorkItemLinkTypes); err != nil {
return errs.WithStack(err)
}
if fxt.isolatedCreation {
if fxt.WorkItemLinkTypes[i].SpaceID == uuid.Nil {
return errs.New("you must specify a space for each work item link type")
}
if fxt.WorkItemLinkTypes[i].LinkCategoryID == uuid.Nil {
return errs.New("you must specify a link category for each work item link type")
}
}
_, err := wiltRepo.Create(fxt.ctx, fxt.WorkItemLinkTypes[i])
if err != nil {
return errs.Wrapf(err, "failed to create work item link type: %+v", fxt.WorkItemLinkTypes[i])
}
}
return nil
}
func makeIterations(fxt *TestFixture) error {
if fxt.info[kindIterations] == nil {
return nil
}
fxt.Iterations = make([]*iteration.Iteration, fxt.info[kindIterations].numInstances)
iterationRepo := iteration.NewIterationRepository(fxt.db)
for i := range fxt.Iterations {
desc := "Some description"
fxt.Iterations[i] = &iteration.Iteration{
Name: testsupport.CreateRandomValidTestName("iteration "),
Description: &desc,
}
if !fxt.isolatedCreation {
fxt.Iterations[i].SpaceID = fxt.Spaces[0].ID
}
if err := fxt.runCustomizeEntityFuncs(i, kindIterations); err != nil {
return errs.WithStack(err)
}
if fxt.isolatedCreation {
if fxt.Iterations[i].SpaceID == uuid.Nil {
return errs.New("you must specify a space ID for each iteration")
}
}
err := iterationRepo.Create(fxt.ctx, fxt.Iterations[i])
if err != nil {
return errs.Wrapf(err, "failed to create iteration: %+v", fxt.Iterations[i])
}
}
return nil
}
func makeAreas(fxt *TestFixture) error {
if fxt.info[kindAreas] == nil {
return nil
}
fxt.Areas = make([]*area.Area, fxt.info[kindAreas].numInstances)
areaRepo := area.NewAreaRepository(fxt.db)
for i := range fxt.Areas {
fxt.Areas[i] = &area.Area{
Name: testsupport.CreateRandomValidTestName("area "),
}
if !fxt.isolatedCreation {
fxt.Areas[i].SpaceID = fxt.Spaces[0].ID
}
if err := fxt.runCustomizeEntityFuncs(i, kindAreas); err != nil {
return errs.WithStack(err)
}
if fxt.isolatedCreation {
if fxt.Areas[i].SpaceID == uuid.Nil {
return errs.New("you must specify a space ID for each area")
}
}
err := areaRepo.Create(fxt.ctx, fxt.Areas[i])
if err != nil {
return errs.Wrapf(err, "failed to create area: %+v", fxt.Areas[i])
}
}
return nil
}
func makeCodebases(fxt *TestFixture) error {
if fxt.info[kindCodebases] == nil {
return nil
}
fxt.Codebases = make([]*codebase.Codebase, fxt.info[kindCodebases].numInstances)
codebaseRepo := codebase.NewCodebaseRepository(fxt.db)
for i := range fxt.Codebases {
stackID := "golang-default"
fxt.Codebases[i] = &codebase.Codebase{
Type: "git",
StackID: &stackID,
LastUsedWorkspace: "my-used-last-workspace",
URL: "git@github.com:fabric8-services/fabric8-wit.git",
}
if !fxt.isolatedCreation {
fxt.Codebases[i].SpaceID = fxt.Spaces[0].ID
}
if err := fxt.runCustomizeEntityFuncs(i, kindCodebases); err != nil {
return errs.WithStack(err)
}
if fxt.isolatedCreation {
if fxt.Codebases[i].SpaceID == uuid.Nil {
return errs.New("you must specify a space ID for each codebase")
}
}
err := codebaseRepo.Create(fxt.ctx, fxt.Codebases[i])
if err != nil {
return errs.Wrapf(err, "failed to create codebase: %+v", fxt.Codebases[i])
}
}
return nil
}
func makeWorkItemTypes(fxt *TestFixture) error {
if fxt.info[kindWorkItemTypes] == nil {
return nil
}
fxt.WorkItemTypes = make([]*workitem.WorkItemType, fxt.info[kindWorkItemTypes].numInstances)
witRepo := workitem.NewWorkItemTypeRepository(fxt.db)
for i := range fxt.WorkItemTypes {
desc := "this work item type was automatically generated"
fxt.WorkItemTypes[i] = &workitem.WorkItemType{
Name: testsupport.CreateRandomValidTestName("work item type "),
Description: &desc,
Icon: "fa-bug",
Fields: map[string]workitem.FieldDefinition{
workitem.SystemTitle: {Type: workitem.SimpleType{Kind: "string"}, Required: true, Label: "Title", Description: "The title text of the work item"},
workitem.SystemDescription: {Type: workitem.SimpleType{Kind: "markup"}, Required: false, Label: "Description", Description: "A descriptive text of the work item"},
workitem.SystemCreator: {Type: workitem.SimpleType{Kind: "user"}, Required: true, Label: "Creator", Description: "The user that created the work item"},
workitem.SystemRemoteItemID: {Type: workitem.SimpleType{Kind: "string"}, Required: false, Label: "Remote item", Description: "The ID of the remote work item"},
workitem.SystemCreatedAt: {Type: workitem.SimpleType{Kind: "instant"}, Required: false, Label: "Created at", Description: "The date and time when the work item was created"},
workitem.SystemUpdatedAt: {Type: workitem.SimpleType{Kind: "instant"}, Required: false, Label: "Updated at", Description: "The date and time when the work item was last updated"},
workitem.SystemOrder: {Type: workitem.SimpleType{Kind: "float"}, Required: false, Label: "Execution Order", Description: "Execution Order of the workitem."},
workitem.SystemIteration: {Type: workitem.SimpleType{Kind: "iteration"}, Required: false, Label: "Iteration", Description: "The iteration to which the work item belongs"},
workitem.SystemArea: {Type: workitem.SimpleType{Kind: "area"}, Required: false, Label: "Area", Description: "The area to which the work item belongs"},
workitem.SystemCodebase: {Type: workitem.SimpleType{Kind: "codebase"}, Required: false, Label: "Codebase", Description: "Contains codebase attributes to which this WI belongs to"},
workitem.SystemAssignees: {
Type: &workitem.ListType{
SimpleType: workitem.SimpleType{Kind: workitem.KindList},
ComponentType: workitem.SimpleType{Kind: workitem.KindUser}},
Required: false,
Label: "Assignees",
Description: "The users that are assigned to the work item",
},
workitem.SystemLabels: {
Type: &workitem.ListType{
SimpleType: workitem.SimpleType{Kind: workitem.KindList},
ComponentType: workitem.SimpleType{Kind: workitem.KindLabel},
},
Required: false,
Label: "Labels",
Description: "List of labels attached to the work item",
},
workitem.SystemState: {
Type: &workitem.EnumType{
SimpleType: workitem.SimpleType{Kind: workitem.KindEnum},
BaseType: workitem.SimpleType{Kind: workitem.KindString},
Values: []interface{}{
workitem.SystemStateNew,
workitem.SystemStateOpen,
workitem.SystemStateInProgress,
workitem.SystemStateResolved,
workitem.SystemStateClosed,
},
},
Required: true,
Label: "State",
Description: "The state of the work item",
},
},
}
if !fxt.isolatedCreation {
fxt.WorkItemTypes[i].SpaceID = fxt.Spaces[0].ID
}
if err := fxt.runCustomizeEntityFuncs(i, kindWorkItemTypes); err != nil {
return errs.WithStack(err)
}
if fxt.isolatedCreation {
if fxt.WorkItemTypes[i].SpaceID == uuid.Nil {
return errs.New("you must specify a space ID for each work item type")
}
}
_, err := witRepo.CreateFromModel(fxt.ctx, fxt.WorkItemTypes[i])
if err != nil {
return errs.Wrapf(err, "failed to create work item type %+v", fxt.WorkItemTypes[i])
}
}
return nil
}
func makeWorkItems(fxt *TestFixture) error {
if fxt.info[kindWorkItems] == nil {
return nil
}
fxt.WorkItems = make([]*workitem.WorkItem, fxt.info[kindWorkItems].numInstances)
wiRepo := workitem.NewWorkItemRepository(fxt.db)
for i := range fxt.WorkItems {
fxt.WorkItems[i] = &workitem.WorkItem{
Fields: map[string]interface{}{
workitem.SystemTitle: testsupport.CreateRandomValidTestName("work item "),
workitem.SystemState: workitem.SystemStateNew,
},
}
if !fxt.isolatedCreation {
fxt.WorkItems[i].SpaceID = fxt.Spaces[0].ID
fxt.WorkItems[i].Type = fxt.WorkItemTypes[0].ID
fxt.WorkItems[i].Fields[workitem.SystemCreator] = fxt.Identities[0].ID.String()
}
if err := fxt.runCustomizeEntityFuncs(i, kindWorkItems); err != nil {
return errs.WithStack(err)
}
if fxt.isolatedCreation {
if fxt.WorkItems[i].SpaceID == uuid.Nil {
return errs.New("you must specify a space ID for each work item")
}
if fxt.WorkItems[i].Type == uuid.Nil {
return errs.New("you must specify a work item type ID for each work item")
}
_, ok := fxt.WorkItems[i].Fields[workitem.SystemCreator]
if !ok {
return errs.Errorf("you must specify a work creator ID for the \"%s\" field in %+v", workitem.SystemCreator, fxt.WorkItems[i].Fields)
}
}
creatorIDStr, ok := fxt.WorkItems[i].Fields[workitem.SystemCreator].(string)
if !ok {
return errs.Errorf("failed to convert \"%s\" field to string in %+v", workitem.SystemCreator, fxt.WorkItems[i].Fields)
}
creatorID, err := uuid.FromString(creatorIDStr)
if err != nil {
return errs.Wrapf(err, "failed to convert \"%s\" field to uuid.UUID", workitem.SystemCreator)
}
wi, err := wiRepo.Create(fxt.ctx, fxt.WorkItems[i].SpaceID, fxt.WorkItems[i].Type, fxt.WorkItems[i].Fields, creatorID)
if err != nil {
return errs.Wrapf(err, "failed to create work item: %+v", fxt.WorkItems[i])
}
fxt.WorkItems[i] = wi
}
return nil
}
func makeWorkItemLinks(fxt *TestFixture) error {
if fxt.info[kindWorkItemLinks] == nil {
return nil
}
fxt.WorkItemLinks = make([]*link.WorkItemLink, fxt.info[kindWorkItemLinks].numInstances)
wilRepo := link.NewWorkItemLinkRepository(fxt.db)
for i := range fxt.WorkItemLinks {
fxt.WorkItemLinks[i] = &link.WorkItemLink{}
if !fxt.isolatedCreation {
fxt.WorkItemLinks[i].LinkTypeID = fxt.WorkItemLinkTypes[0].ID
// this is the logic that ensures, each work item is only appearing
// in one link
fxt.WorkItemLinks[i].SourceID = fxt.WorkItems[2*i].ID
fxt.WorkItemLinks[i].TargetID = fxt.WorkItems[2*i+1].ID
}
if err := fxt.runCustomizeEntityFuncs(i, kindWorkItemLinks); err != nil {
return errs.WithStack(err)
}
if fxt.isolatedCreation {
if fxt.WorkItemLinks[i].LinkTypeID == uuid.Nil {
return errs.New("you must specify a work item link type for each work item link")
}
if fxt.WorkItemLinks[i].SourceID == uuid.Nil {
return errs.New("you must specify a source work item for each work item link")
}
if fxt.WorkItemLinks[i].TargetID == uuid.Nil {
return errs.New("you must specify a target work item for each work item link")
}
}
// default choice for creatorID: take it from the creator of the source work item
sourceWI, err := workitem.NewWorkItemRepository(fxt.db).LoadByID(fxt.ctx, fxt.WorkItemLinks[i].SourceID)
if err != nil {
return errs.Wrapf(err, "failed to load the source work item in order to fetch a creator ID for the link")
}
creatorIDStr, ok := sourceWI.Fields[workitem.SystemCreator].(string)
if !ok {
return errs.Errorf("failed to fetch the %s field from the source work item %s", workitem.SystemCreator, fxt.WorkItemLinks[i].SourceID)
}
creatorID, err := uuid.FromString(creatorIDStr)
if err != nil {
return errs.Wrapf(err, "failed to convert the string \"%s\" to a uuid.UUID object", creatorIDStr)
}
wilt, err := wilRepo.Create(fxt.ctx, fxt.WorkItemLinks[i].SourceID, fxt.WorkItemLinks[i].TargetID, fxt.WorkItemLinks[i].LinkTypeID, creatorID)
if err != nil {
return errs.Wrapf(err, "failed to create work item link: %+v", fxt.WorkItemLinks[i])
}
fxt.WorkItemLinks[i] = wilt
}
return nil
}
func makeComments(fxt *TestFixture) error {
if fxt.info[kindComments] == nil {
return nil
}
fxt.Comments = make([]*comment.Comment, fxt.info[kindComments].numInstances)
commentRepo := comment.NewRepository(fxt.db)
for i := range fxt.Comments {
loremIpsum := `Lorem ipsum dolor sitamet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.`
fxt.Comments[i] = &comment.Comment{
Markup: rendering.SystemMarkupMarkdown,
Body: loremIpsum,
}
if !fxt.isolatedCreation {
fxt.Comments[i].ParentID = fxt.WorkItems[0].ID
fxt.Comments[i].Creator = fxt.Identities[0].ID
}
if err := fxt.runCustomizeEntityFuncs(i, kindComments); err != nil {
return errs.WithStack(err)
}
if fxt.isolatedCreation {
if fxt.Comments[i].ParentID == uuid.Nil {
return errs.New("you must specify a parent work item ID for each comment")
}
if fxt.Comments[i].Creator == uuid.Nil {
return errs.New("you must specify a creator ID for each comment")
}
}
err := commentRepo.Create(fxt.ctx, fxt.Comments[i], fxt.Comments[i].Creator)
if err != nil {
return errs.Wrapf(err, "failed to create comment: %+v", fxt.Comments[i])
}
}
return nil
}
func makeLabels(fxt *TestFixture) error {
if fxt.info[kindLabels] == nil {
return nil
}
fxt.Labels = make([]*label.Label, fxt.info[kindLabels].numInstances)
labelRrepo := label.NewLabelRepository(fxt.db)
randColor := func() string {
colorBits := []string{"0", "1", "2", "3", "4", "5", "6", "a", "b", "c", "d", "e", "f"}
strArr := make([]string, 6)
for i := range strArr {
strArr[i] = colorBits[rand.Intn(len(colorBits))]
}
return "#" + strings.Join(strArr, "")
}
for i := range fxt.Labels {
fxt.Labels[i] = &label.Label{
Name: testsupport.CreateRandomValidTestName("label "),
TextColor: randColor(),
BackgroundColor: randColor(),
BorderColor: randColor(),
}
if !fxt.isolatedCreation {
fxt.Labels[i].SpaceID = fxt.Spaces[0].ID
}
if err := fxt.runCustomizeEntityFuncs(i, kindLabels); err != nil {
return errs.WithStack(err)
}
if fxt.isolatedCreation {
if fxt.Labels[i].SpaceID == uuid.Nil {
return errs.New("you must specify a space ID for each label")
}
}
err := labelRrepo.Create(fxt.ctx, fxt.Labels[i])
if err != nil {
return errs.Wrapf(err, "failed to create label: %+v", fxt.Labels[i])
}
}
return nil
}
func makeTrackers(fxt *TestFixture) error {
if fxt.info[kindTrackers] == nil {
return nil
}
fxt.Trackers = make([]*app.Tracker, fxt.info[kindTrackers].numInstances)
trackerRepo := remoteworkitem.NewTrackerRepository(fxt.db)
for i := range fxt.Trackers {
fxt.Trackers[i] = &app.Tracker{
URL: "https://api.github.com/",
Type: remoteworkitem.ProviderGithub,
}
if err := fxt.runCustomizeEntityFuncs(i, kindTrackers); err != nil {
return errs.WithStack(err)
}
tracker, err := trackerRepo.Create(fxt.ctx, fxt.Trackers[i].URL, fxt.Trackers[i].Type)
if err != nil {
return errs.Wrapf(err, "failed to create tracker: %+v", fxt.Trackers[i])
}
fxt.Trackers[i] = tracker
}
return nil
}