forked from fabric8-services/fabric8-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_repository.go
416 lines (381 loc) · 14.9 KB
/
link_repository.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
package link
import (
"fmt"
"strconv"
"time"
"context"
"github.com/fabric8io/almighty-core/errors"
"github.com/fabric8io/almighty-core/gormsupport"
"github.com/fabric8io/almighty-core/log"
"github.com/fabric8io/almighty-core/workitem"
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
errs "github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)
// End points
const (
EndpointWorkItemTypes = "workitemtypes"
EndpointWorkItems = "workitems"
EndpointWorkItemLinkCategories = "workitemlinkcategories"
EndpointWorkItemLinkTypes = "workitemlinktypes"
EndpointWorkItemLinks = "workitemlinks"
)
// WorkItemLinkRepository encapsulates storage & retrieval of work item links
type WorkItemLinkRepository interface {
Create(ctx context.Context, sourceID, targetID uint64, linkTypeID uuid.UUID, creatorID uuid.UUID) (*WorkItemLink, error)
Load(ctx context.Context, ID uuid.UUID) (*WorkItemLink, error)
List(ctx context.Context) ([]WorkItemLink, error)
ListByWorkItemID(ctx context.Context, wiIDStr string) ([]WorkItemLink, error)
DeleteRelatedLinks(ctx context.Context, wiIDStr string, suppressorID uuid.UUID) error
Delete(ctx context.Context, ID uuid.UUID, suppressorID uuid.UUID) error
Save(ctx context.Context, linkCat WorkItemLink, modifierID uuid.UUID) (*WorkItemLink, error)
ListWorkItemChildren(ctx context.Context, parent string) ([]workitem.WorkItem, error)
WorkItemHasChildren(ctx context.Context, parent string) (bool, error)
}
// NewWorkItemLinkRepository creates a work item link repository based on gorm
func NewWorkItemLinkRepository(db *gorm.DB) *GormWorkItemLinkRepository {
return &GormWorkItemLinkRepository{
db: db,
workItemRepo: workitem.NewWorkItemRepository(db),
workItemTypeRepo: workitem.NewWorkItemTypeRepository(db),
workItemLinkTypeRepo: NewWorkItemLinkTypeRepository(db),
revisionRepo: NewRevisionRepository(db),
}
}
// GormWorkItemLinkRepository implements WorkItemLinkRepository using gorm
type GormWorkItemLinkRepository struct {
db *gorm.DB
workItemRepo *workitem.GormWorkItemRepository
workItemTypeRepo *workitem.GormWorkItemTypeRepository
workItemLinkTypeRepo *GormWorkItemLinkTypeRepository
revisionRepo *GormWorkItemLinkRevisionRepository
}
// ValidateCorrectSourceAndTargetType returns an error if the Path of
// the source WIT as defined by the work item link type is not part of
// the actual source's WIT; the same applies for the target.
func (r *GormWorkItemLinkRepository) ValidateCorrectSourceAndTargetType(ctx context.Context, sourceID, targetID uint64, linkTypeID uuid.UUID) error {
linkType, err := r.workItemLinkTypeRepo.Load(ctx, linkTypeID)
if err != nil {
return errs.WithStack(err)
}
// Fetch the source work item
source, err := r.workItemRepo.LoadFromDB(ctx, strconv.FormatUint(sourceID, 10))
if err != nil {
return errs.WithStack(err)
}
// Fetch the target work item
target, err := r.workItemRepo.LoadFromDB(ctx, strconv.FormatUint(targetID, 10))
if err != nil {
return errs.WithStack(err)
}
// Fetch the concrete work item types of the target and the source.
sourceWorkItemType, err := r.workItemTypeRepo.LoadTypeFromDB(ctx, source.Type)
if err != nil {
return errs.WithStack(err)
}
targetWorkItemType, err := r.workItemTypeRepo.LoadTypeFromDB(ctx, target.Type)
if err != nil {
return errs.WithStack(err)
}
// Check type paths
if !sourceWorkItemType.IsTypeOrSubtypeOf(linkType.SourceTypeID) {
return errors.NewBadParameterError("source work item type", source.Type)
}
if !targetWorkItemType.IsTypeOrSubtypeOf(linkType.TargetTypeID) {
return errors.NewBadParameterError("target work item type", target.Type)
}
return nil
}
// CheckParentExists returns error if there is an attempt to create more than 1 parent of a workitem.
func (r *GormWorkItemLinkRepository) CheckParentExists(ctx context.Context, targetID uint64, linkType *WorkItemLinkType) (bool, error) {
query := fmt.Sprintf(`
SELECT EXISTS (
SELECT 1 FROM %[1]s
WHERE
link_type_id=$1
AND target_id=$2
AND deleted_at IS NULL
)`, WorkItemLink{}.TableName())
row := r.db.CommonDB().QueryRow(query, linkType.ID, targetID)
var exists bool
if err := row.Scan(&exists); err != nil {
return false, errs.Wrapf(err, "failed to check if a parent exists for the work item %d", targetID)
}
return exists, nil
}
func (r *GormWorkItemLinkRepository) ValidateTopology(ctx context.Context, targetID uint64, linkType *WorkItemLinkType) error {
// check to disallow multiple parents in tree topology
if linkType.Topology == TopologyTree {
parentExists, err := r.CheckParentExists(ctx, targetID, linkType)
if err != nil {
log.Error(ctx, map[string]interface{}{
"wilt_id": linkType.ID,
"target_id": targetID,
"err": err,
}, "failed to check if the work item %s has a parent work item", targetID)
return errs.Wrapf(err, "failed to check if the work item %s has a parent work item", targetID)
}
if parentExists {
log.Error(ctx, map[string]interface{}{
"wilt_id": linkType.ID,
"target_id": targetID,
"err": err,
}, "unable to create work item link because a topology of type \"%s\" only allows one parent to exist and the target %d already a parent", TopologyTree, targetID)
return errors.NewBadParameterError("linkTypeID + targetID", fmt.Sprintf("%s + %d", linkType.ID, targetID)).Expected("single parent in tree topology")
}
}
return nil
}
// Create creates a new work item link in the repository.
// Returns BadParameterError, ConversionError or InternalError
func (r *GormWorkItemLinkRepository) Create(ctx context.Context, sourceID, targetID uint64, linkTypeID uuid.UUID, creatorID uuid.UUID) (*WorkItemLink, error) {
link := &WorkItemLink{
SourceID: sourceID,
TargetID: targetID,
LinkTypeID: linkTypeID,
}
if err := link.CheckValidForCreation(); err != nil {
return nil, errs.WithStack(err)
}
// Fetch the link type
linkType, err := r.workItemLinkTypeRepo.Load(ctx, linkTypeID)
if err != nil {
return nil, errs.Wrap(err, "failed to load link type")
}
if err := r.ValidateCorrectSourceAndTargetType(ctx, sourceID, targetID, linkType.ID); err != nil {
return nil, errs.WithStack(err)
}
if err := r.ValidateTopology(ctx, targetID, linkType); err != nil {
return nil, errs.WithStack(err)
}
db := r.db.Create(link)
if db.Error != nil {
if gormsupport.IsUniqueViolation(db.Error, "work_item_links_unique_idx") {
// TODO(kwk): Make NewBadParameterError a variadic function to avoid this ugliness ;)
return nil, errors.NewBadParameterError("data.relationships.source_id + data.relationships.target_id + data.relationships.link_type_id", sourceID).Expected("unique")
}
return nil, errors.NewInternalError(db.Error.Error())
}
// save a revision of the created work item link
if err := r.revisionRepo.Create(ctx, creatorID, RevisionTypeCreate, *link); err != nil {
return nil, errs.Wrapf(err, "error while creating work item")
}
return link, nil
}
// Load returns the work item link for the given ID.
// Returns NotFoundError, ConversionError or InternalError
func (r *GormWorkItemLinkRepository) Load(ctx context.Context, ID uuid.UUID) (*WorkItemLink, error) {
log.Info(ctx, map[string]interface{}{
"wil_id": ID,
}, "Loading work item link")
result := WorkItemLink{}
db := r.db.Where("id=?", ID).Find(&result)
if db.RecordNotFound() {
log.Error(ctx, map[string]interface{}{
"wil_id": ID,
}, "work item link not found")
return nil, errors.NewNotFoundError("work item link", ID.String())
}
if db.Error != nil {
return nil, errors.NewInternalError(db.Error.Error())
}
return &result, nil
}
// ListByWorkItemID returns the work item links that have wiID as source or target.
// TODO: Handle pagination
func (r *GormWorkItemLinkRepository) ListByWorkItemID(ctx context.Context, wiIDStr string) ([]WorkItemLink, error) {
var modelLinks []WorkItemLink
wi, err := r.workItemRepo.LoadFromDB(ctx, wiIDStr)
if err != nil {
return nil, errs.WithStack(err)
}
// Now fetch all links for that work item
db := r.db.Model(modelLinks).Where("? IN (source_id, target_id)", wi.ID).Find(&modelLinks)
if db.Error != nil {
return nil, db.Error
}
return modelLinks, nil
}
// List returns all work item links if wiID is nil; otherwise the work item links are returned
// that have wiID as source or target.
// TODO: Handle pagination
func (r *GormWorkItemLinkRepository) List(ctx context.Context) ([]WorkItemLink, error) {
var modelLinks []WorkItemLink
db := r.db.Find(&modelLinks)
if db.Error != nil {
return nil, db.Error
}
return modelLinks, nil
}
// Delete deletes the work item link with the given id
// returns NotFoundError or InternalError
func (r *GormWorkItemLinkRepository) Delete(ctx context.Context, linkID uuid.UUID, suppressorID uuid.UUID) error {
var lnk = WorkItemLink{}
tx := r.db.Where("id = ?", linkID).Find(&lnk)
if tx.RecordNotFound() {
return errors.NewNotFoundError("work item link", linkID.String())
}
r.deleteLink(ctx, lnk, suppressorID)
return nil
}
// DeleteRelatedLinks deletes all links in which the source or target equals the
// given work item ID.
func (r *GormWorkItemLinkRepository) DeleteRelatedLinks(ctx context.Context, wiIDStr string, suppressorID uuid.UUID) error {
log.Info(ctx, map[string]interface{}{
"workitem_id": wiIDStr,
}, "Deleting the links related to work item")
wiID, err := strconv.ParseUint(wiIDStr, 10, 64)
if err != nil {
// treat as not found: clients don't know it must be a uint64
return errors.NewNotFoundError("work item link", wiIDStr)
}
var workitemLinks = []WorkItemLink{}
r.db.Where("? in (source_id, target_id)", wiID).Find(&workitemLinks)
// delete one by one to trigger the creation of a new work item link revision
for _, workitemLink := range workitemLinks {
r.deleteLink(ctx, workitemLink, suppressorID)
}
return nil
}
// Delete deletes the work item link with the given id
// returns NotFoundError or InternalError
func (r *GormWorkItemLinkRepository) deleteLink(ctx context.Context, lnk WorkItemLink, suppressorID uuid.UUID) error {
log.Info(ctx, map[string]interface{}{
"wil_id": lnk.ID,
}, "Deleting the work item link")
tx := r.db.Delete(&lnk)
if tx.RowsAffected == 0 {
return errors.NewNotFoundError("work item link", lnk.ID.String())
}
if tx.Error != nil {
log.Error(ctx, map[string]interface{}{
"wil_id": lnk.ID,
"err": tx.Error,
}, "unable to delete work item link")
return errors.NewInternalError(tx.Error.Error())
}
// save a revision of the deleted work item link
if err := r.revisionRepo.Create(ctx, suppressorID, RevisionTypeDelete, lnk); err != nil {
return errs.Wrapf(err, "error while deleting work item")
}
return nil
}
// Save updates the given work item link in storage. Version must be the same as the one int the stored version.
// returns NotFoundError, VersionConflictError, ConversionError or InternalError
func (r *GormWorkItemLinkRepository) Save(ctx context.Context, linkToSave WorkItemLink, modifierID uuid.UUID) (*WorkItemLink, error) {
log.Info(ctx, map[string]interface{}{
"wil_id": linkToSave.LinkTypeID,
}, "Saving workitem link with type = %s", linkToSave.LinkTypeID)
existingLink := WorkItemLink{}
db := r.db.Model(&existingLink).Where("id=?", linkToSave.ID).First(&existingLink)
if db.RecordNotFound() {
log.Error(ctx, map[string]interface{}{
"wil_id": linkToSave.ID,
}, "work item link not found")
return nil, errors.NewNotFoundError("work item link", linkToSave.ID.String())
}
if db.Error != nil {
log.Error(ctx, map[string]interface{}{
"wil_id": linkToSave.ID,
"err": db.Error,
}, "unable to find work item link")
return nil, errors.NewInternalError(db.Error.Error())
}
if existingLink.Version != linkToSave.Version {
return nil, errors.NewVersionConflictError("version conflict")
}
linkToSave.Version = linkToSave.Version + 1
linkTypeToSave, err := r.workItemLinkTypeRepo.Load(ctx, linkToSave.LinkTypeID)
if err != nil {
return nil, errs.Wrap(err, "failed to load link type")
}
if err := r.ValidateCorrectSourceAndTargetType(ctx, linkToSave.SourceID, linkToSave.TargetID, linkTypeToSave.ID); err != nil {
return nil, errs.WithStack(err)
}
if err := r.ValidateTopology(ctx, linkToSave.TargetID, linkTypeToSave); err != nil {
return nil, errs.WithStack(err)
}
// save
db = r.db.Save(&linkToSave)
if db.Error != nil {
log.Error(ctx, map[string]interface{}{
"wil_id": linkToSave.ID,
"err": db.Error,
}, "unable to save work item link")
return nil, errors.NewInternalError(db.Error.Error())
}
// save a revision of the modified work item link
if err := r.revisionRepo.Create(ctx, modifierID, RevisionTypeUpdate, linkToSave); err != nil {
return nil, errs.Wrapf(err, "error while saving work item")
}
log.Info(ctx, map[string]interface{}{
"wil_id": linkToSave.ID,
}, "Work item link updated")
return &linkToSave, nil
}
// ListWorkItemChildren get all child work items
func (r *GormWorkItemLinkRepository) ListWorkItemChildren(ctx context.Context, parent string) ([]workitem.WorkItem, error) {
defer goa.MeasureSince([]string{"goa", "db", "workitem", "children", "query"}, time.Now())
where := fmt.Sprintf(`
id in (
SELECT target_id FROM %s
WHERE source_id = ? AND link_type_id IN (
SELECT id FROM %s WHERE forward_name = 'parent of'
)
)`, WorkItemLink{}.TableName(), WorkItemLinkType{}.TableName())
db := r.db.Model(&workitem.WorkItemStorage{}).Where(where, parent)
rows, err := db.Rows()
if err != nil {
return nil, err
}
defer rows.Close()
result := []workitem.WorkItemStorage{}
for rows.Next() {
value := workitem.WorkItemStorage{}
db.ScanRows(rows, &value)
result = append(result, value)
}
res := make([]workitem.WorkItem, len(result))
for index, value := range result {
wiType, err := r.workItemTypeRepo.LoadTypeFromDB(ctx, value.Type)
if err != nil {
return nil, errors.NewInternalError(err.Error())
}
modelWI, err := workitem.ConvertWorkItemStorageToModel(wiType, &value)
if err != nil {
return nil, errors.NewInternalError(err.Error())
}
res[index] = *modelWI
}
return res, nil
}
// WorkItemHasChildren returns true if the given parent work item has children;
// otherwise false is returned
func (r *GormWorkItemLinkRepository) WorkItemHasChildren(ctx context.Context, parent string) (bool, error) {
defer goa.MeasureSince([]string{"goa", "db", "workitem", "has", "children"}, time.Now())
query := fmt.Sprintf(`
SELECT EXISTS (
SELECT 1 FROM %[1]s WHERE id in (
SELECT target_id FROM %[2]s
WHERE source_id = $1 AND link_type_id IN (
SELECT id FROM %[3]s WHERE forward_name = 'parent of'
)
)
)`,
workitem.WorkItemStorage{}.TableName(),
WorkItemLink{}.TableName(),
WorkItemLinkType{}.TableName())
var hasChildren bool
db := r.db.CommonDB()
stmt, err := db.Prepare(query)
if err != nil {
return false, errs.Wrapf(err, "failed prepare statement: %s", query)
}
defer stmt.Close()
err = stmt.QueryRow(parent).Scan(&hasChildren)
if err != nil {
return false, errs.Wrapf(err, "failed to check if work item %s has children: %s", parent, query)
}
return hasChildren, nil
}