-
Notifications
You must be signed in to change notification settings - Fork 86
Adds WIT parameter in TrackerQuery payload #2360
Changes from 3 commits
f0fc73c
65b3643
e4f054c
21ee0b5
bba6209
0524fee
5c24211
4e28161
7eafcf3
936cfc9
68498a6
3646533
72bc5c5
6648376
071fe92
97f2247
6f48605
e358263
6ddb69d
92a0e2c
f5c9755
fc1a8ba
1b36616
09a0e75
a59a40b
9fd13d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,12 +93,15 @@ func (c *TrackerqueryController) Create(ctx *app.CreateTrackerqueryContext) erro | |
} | ||
err = application.Transactional(c.db, func(appl application.Application) error { | ||
trackerQuery := remoteworkitem.TrackerQuery{ | ||
Query: ctx.Payload.Data.Attributes.Query, | ||
Schedule: ctx.Payload.Data.Attributes.Schedule, | ||
TrackerID: ctx.Payload.Data.Relationships.Tracker.Data.ID, | ||
SpaceID: *ctx.Payload.Data.Relationships.Space.Data.ID, | ||
Query: ctx.Payload.Data.Attributes.Query, | ||
Schedule: ctx.Payload.Data.Attributes.Schedule, | ||
TrackerID: ctx.Payload.Data.Relationships.Tracker.Data.ID, | ||
SpaceID: *ctx.Payload.Data.Relationships.Space.Data.ID, | ||
WorkItemTypeID: ctx.Payload.Data.Relationships.BaseType.Data.ID, | ||
} | ||
if ctx.Payload.Data.ID != nil { | ||
trackerQuery.ID = *ctx.Payload.Data.ID | ||
} | ||
trackerQuery.ID = *ctx.Payload.Data.ID | ||
tq, err := appl.TrackerQueries().Create(ctx.Context, trackerQuery) | ||
if err != nil { | ||
return errs.Wrapf(err, "failed to create tracker query %s", ctx.Payload.Data) | ||
|
@@ -157,6 +160,9 @@ func (c *TrackerqueryController) Update(ctx *app.UpdateTrackerqueryContext) erro | |
if &ctx.Payload.Data.Relationships.Tracker.Data.ID != nil { | ||
tq.TrackerID = ctx.Payload.Data.Relationships.Tracker.Data.ID | ||
} | ||
if ctx.Payload.Data.Relationships.BaseType.Data.ID == uuid.Nil { | ||
return errors.NewBadParameterError("Workitemtype_id", nil).Expected("not nil") | ||
} | ||
_, err = appl.TrackerQueries().Save(ctx.Context, *tq) | ||
if err != nil { | ||
return errs.Wrapf(err, "failed to update tracker query %s", ctx.Payload.Data.ID) | ||
|
@@ -248,6 +254,9 @@ func validateCreateTrackerQueryPayload(ctx *app.CreateTrackerqueryContext) error | |
if *ctx.Payload.Data.Relationships.Space.Data.ID == uuid.Nil { | ||
return errors.NewBadParameterError("SpaceID", nil).Expected("not nil") | ||
} | ||
if ctx.Payload.Data.Relationships.BaseType.Data.ID == uuid.Nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You hat the same check on line 163, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return errors.NewBadParameterError("Workitemtype_id", nil).Expected("not nil") | ||
} | ||
return nil | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,11 +179,10 @@ func (rest *TestTrackerQueryREST) TestCreateTrackerQuery() { | |
resource.Require(t, resource.Database) | ||
|
||
svc, _, trackerQueryCtrl := rest.SecuredController() | ||
fxt := tf.NewTestFixture(t, rest.DB, tf.Spaces(1), tf.Trackers(1)) | ||
fxt := tf.NewTestFixture(t, rest.DB, tf.Spaces(1), tf.Trackers(1), tf.WorkItemTypes(1)) | ||
assert.NotNil(t, fxt.Spaces[0], fxt.Trackers[0]) | ||
|
||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID) | ||
|
||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, fxt.WorkItemTypes[0].ID) | ||
_, tqresult := test.CreateTrackerqueryCreated(t, svc.Context, svc, trackerQueryCtrl, &tqpayload) | ||
assert.NotNil(t, tqresult) | ||
} | ||
|
@@ -193,13 +192,12 @@ func (rest *TestTrackerQueryREST) TestShowTrackerQuery() { | |
resource.Require(t, resource.Database) | ||
|
||
svc, _, trackerQueryCtrl := rest.SecuredController() | ||
fxt := tf.NewTestFixture(t, rest.DB, tf.Spaces(1), tf.Trackers(1)) | ||
fxt := tf.NewTestFixture(t, rest.DB, tf.Spaces(1), tf.Trackers(1), tf.WorkItemTypes(1)) | ||
assert.NotNil(t, fxt.Spaces[0], fxt.Trackers[0]) | ||
|
||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID) | ||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, fxt.WorkItemTypes[0].ID) | ||
|
||
_, tqresult := test.CreateTrackerqueryCreated(t, svc.Context, svc, trackerQueryCtrl, &tqpayload) | ||
|
||
_, tqr := test.ShowTrackerqueryOK(t, svc.Context, svc, trackerQueryCtrl, *tqresult.Data.ID) | ||
assert.NotNil(t, tqr) | ||
assert.Equal(t, tqresult.Data.ID, tqr.Data.ID) | ||
|
@@ -210,19 +208,17 @@ func (rest *TestTrackerQueryREST) TestUpdateTrackerQuery() { | |
resource.Require(t, resource.Database) | ||
|
||
svc, _, trackerQueryCtrl := rest.SecuredController() | ||
fxt := tf.NewTestFixture(t, rest.DB, tf.Spaces(1), tf.Trackers(1)) | ||
fxt := tf.NewTestFixture(t, rest.DB, tf.Spaces(1), tf.Trackers(1), tf.WorkItemTypes(1)) | ||
assert.NotNil(t, fxt.Spaces[0], fxt.Trackers[0]) | ||
|
||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID) | ||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, fxt.WorkItemTypes[0].ID) | ||
|
||
_, tqresult := test.CreateTrackerqueryCreated(t, svc.Context, svc, trackerQueryCtrl, &tqpayload) | ||
|
||
_, tqr := test.ShowTrackerqueryOK(t, svc.Context, svc, trackerQueryCtrl, *tqresult.Data.ID) | ||
assert.NotNil(t, tqr) | ||
assert.Equal(t, tqresult.Data.ID, tqr.Data.ID) | ||
|
||
spaceID := space.SystemSpace | ||
trackerID := fxt.Trackers[0].ID | ||
payload2 := app.UpdateTrackerqueryPayload{ | ||
Data: &app.TrackerQuery{ | ||
ID: tqr.Data.ID, | ||
|
@@ -233,15 +229,22 @@ func (rest *TestTrackerQueryREST) TestUpdateTrackerQuery() { | |
Relationships: &app.TrackerQueryRelations{ | ||
Space: &app.RelationSpaces{ | ||
kwk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Data: &app.RelationSpacesData{ | ||
ID: &spaceID, | ||
ID: &fxt.Spaces[0].ID, | ||
Type: &space.SpaceType, | ||
}, | ||
}, | ||
Tracker: &app.RelationKindUUID{ | ||
Data: &app.DataKindUUID{ | ||
ID: trackerID, | ||
ID: fxt.Trackers[0].ID, | ||
Type: remoteworkitem.APIStringTypeTrackers, | ||
}, | ||
}, | ||
BaseType: &app.RelationBaseType{ | ||
Data: &app.BaseTypeData{ | ||
ID: fxt.WorkItemTypes[0].ID, | ||
Type: APIStringTypeWorkItemType, | ||
}, | ||
}, | ||
}, | ||
Type: remoteworkitem.APIStringTypeTrackerQuery, | ||
}, | ||
|
@@ -260,14 +263,14 @@ func (rest *TestTrackerQueryREST) TestTrackerQueryListItemsNotNil() { | |
resource.Require(t, resource.Database) | ||
|
||
svc, _, trackerQueryCtrl := rest.SecuredController() | ||
fxt := tf.NewTestFixture(t, rest.DB, tf.Spaces(1), tf.Trackers(1)) | ||
fxt := tf.NewTestFixture(t, rest.DB, tf.Spaces(1), tf.Trackers(1), tf.WorkItemTypes(1)) | ||
assert.NotNil(t, fxt.Spaces[0], fxt.Trackers[0]) | ||
|
||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID) | ||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, fxt.WorkItemTypes[0].ID) | ||
_, tq1 := test.CreateTrackerqueryCreated(t, svc.Context, svc, trackerQueryCtrl, &tqpayload) | ||
assert.NotNil(t, tq1) | ||
|
||
tqpayload2 := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID) | ||
tqpayload2 := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, fxt.WorkItemTypes[0].ID) | ||
_, tq2 := test.CreateTrackerqueryCreated(t, svc.Context, svc, trackerQueryCtrl, &tqpayload2) | ||
assert.NotNil(t, tq2) | ||
|
||
|
@@ -282,10 +285,10 @@ func (rest *TestTrackerQueryREST) TestCreateTrackerQueryID() { | |
resource.Require(t, resource.Database) | ||
|
||
svc, _, trackerQueryCtrl := rest.SecuredController() | ||
fxt := tf.NewTestFixture(t, rest.DB, tf.Spaces(1), tf.Trackers(1)) | ||
fxt := tf.NewTestFixture(t, rest.DB, tf.Spaces(1), tf.Trackers(1), tf.WorkItemTypes(1)) | ||
|
||
rest.T().Run("valid - success", func(t *testing.T) { | ||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID) | ||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, fxt.WorkItemTypes[0].ID) | ||
_, trackerquery := test.CreateTrackerqueryCreated(t, svc.Context, svc, trackerQueryCtrl, &tqpayload) | ||
require.NotNil(t, trackerquery) | ||
|
||
|
@@ -294,14 +297,46 @@ func (rest *TestTrackerQueryREST) TestCreateTrackerQueryID() { | |
assert.Equal(t, trackerquery.Data.ID, result.Data.ID) | ||
}) | ||
rest.T().Run("invalid - fail", func(t *testing.T) { | ||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID) | ||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, fxt.WorkItemTypes[0].ID) | ||
invalidID := uuid.Nil | ||
tqpayload.Data.ID = &invalidID | ||
test.CreateTrackerqueryBadRequest(t, svc.Context, svc, trackerQueryCtrl, &tqpayload) | ||
}) | ||
} | ||
|
||
func newCreateTrackerQueryPayload(spaceID uuid.UUID, trackerID uuid.UUID) app.CreateTrackerqueryPayload { | ||
func (rest *TestTrackerQueryREST) TestInvalidWITinTrackerQuery() { | ||
t := rest.T() | ||
kwk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resource.Require(t, resource.Database) | ||
rest.T().Run("nil WIT in trackerquery payload", func(t *testing.T) { | ||
fxt := tf.NewTestFixture(t, rest.DB, | ||
tf.SpaceTemplates(2), | ||
kwk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tf.Spaces(1), | ||
tf.Trackers(1), | ||
) | ||
svc, _, trackerQueryCtrl := rest.SecuredController() | ||
|
||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, uuid.Nil) | ||
test.CreateTrackerqueryBadRequest(t, svc.Context, svc, trackerQueryCtrl, &tqpayload) | ||
}) | ||
|
||
rest.T().Run("disallow creation if WIT belongs to different spacetemplate", func(t *testing.T) { | ||
fxt := tf.NewTestFixture(t, rest.DB, | ||
tf.SpaceTemplates(2), | ||
tf.Spaces(1), | ||
tf.WorkItemTypes(1, func(fxt *tf.TestFixture, idx int) error { | ||
fxt.WorkItemTypes[idx].SpaceTemplateID = fxt.SpaceTemplates[1].ID | ||
return nil | ||
}), | ||
tf.Trackers(1), | ||
) | ||
svc, _, trackerQueryCtrl := rest.SecuredController() | ||
|
||
tqpayload := newCreateTrackerQueryPayload(fxt.Spaces[0].ID, fxt.Trackers[0].ID, fxt.WorkItemTypes[0].ID) | ||
test.CreateTrackerqueryBadRequest(t, svc.Context, svc, trackerQueryCtrl, &tqpayload) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't these test methods have return values that you can check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}) | ||
} | ||
|
||
func newCreateTrackerQueryPayload(spaceID uuid.UUID, trackerID uuid.UUID, witID uuid.UUID) app.CreateTrackerqueryPayload { | ||
trackerQueryID := uuid.NewV4() | ||
return app.CreateTrackerqueryPayload{ | ||
Data: &app.TrackerQuery{ | ||
|
@@ -313,7 +348,8 @@ func newCreateTrackerQueryPayload(spaceID uuid.UUID, trackerID uuid.UUID) app.Cr | |
Relationships: &app.TrackerQueryRelations{ | ||
Space: &app.RelationSpaces{ | ||
Data: &app.RelationSpacesData{ | ||
ID: &spaceID, | ||
ID: &spaceID, | ||
Type: &space.SpaceType, | ||
}, | ||
}, | ||
Tracker: &app.RelationKindUUID{ | ||
|
@@ -322,6 +358,12 @@ func newCreateTrackerQueryPayload(spaceID uuid.UUID, trackerID uuid.UUID) app.Cr | |
Type: remoteworkitem.APIStringTypeTrackers, | ||
}, | ||
}, | ||
BaseType: &app.RelationBaseType{ | ||
Data: &app.BaseTypeData{ | ||
ID: witID, | ||
Type: APIStringTypeWorkItemType, | ||
}, | ||
}, | ||
}, | ||
Type: remoteworkitem.APIStringTypeTrackerQuery, | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE tracker_queries ADD COLUMN work_item_type_id uuid REFERENCES work_item_types(id); | ||
kwk marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,39 +33,39 @@ func Upload(db *gorm.DB, tID uuid.UUID, item TrackerItemContent) error { | |
} | ||
|
||
// Map a remote work item into an WIT work item and persist it into the database. | ||
func ConvertToWorkItemModel(ctx context.Context, db *gorm.DB, tID uuid.UUID, item TrackerItemContent, providerType string, spaceID uuid.UUID) (*workitem.WorkItem, error) { | ||
func ConvertToWorkItemModel(ctx context.Context, db *gorm.DB, item TrackerItemContent, tq TrackerSchedule) (*workitem.WorkItem, error) { | ||
remoteID := item.ID | ||
content := string(item.Content) | ||
trackerItem := TrackerItem{Item: content, RemoteItemID: remoteID, TrackerID: tID} | ||
trackerItem := TrackerItem{Item: content, RemoteItemID: remoteID, TrackerID: tq.TrackerID} | ||
// Converting the remote item to a local work item | ||
remoteTrackerItemConvertFunc, ok := RemoteWorkItemImplRegistry[providerType] | ||
remoteTrackerItemConvertFunc, ok := RemoteWorkItemImplRegistry[tq.TrackerType] | ||
if !ok { | ||
return nil, BadParameterError{parameter: providerType, value: providerType} | ||
return nil, BadParameterError{parameter: tq.TrackerType, value: tq.TrackerType} | ||
} | ||
remoteTrackerItem, err := remoteTrackerItemConvertFunc(trackerItem) | ||
if err != nil { | ||
return nil, InternalError{simpleError{message: fmt.Sprintf(" Error parsing the tracker data: %s", err.Error())}} | ||
} | ||
remoteWorkItem, err := Map(remoteTrackerItem, RemoteWorkItemKeyMaps[providerType]) | ||
remoteWorkItem, err := Map(remoteTrackerItem, RemoteWorkItemKeyMaps[tq.TrackerType]) | ||
if err != nil { | ||
return nil, ConversionError{simpleError{message: fmt.Sprintf("Error mapping to local work item: %s", err.Error())}} | ||
} | ||
workItem, err := lookupIdentities(ctx, db, remoteWorkItem, providerType, spaceID) | ||
workItem, err := lookupIdentities(ctx, db, remoteWorkItem, tq) | ||
if err != nil { | ||
return nil, InternalError{simpleError{message: fmt.Sprintf("Error bind assignees: %s", err.Error())}} | ||
} | ||
return upsert(ctx, db, *workItem) | ||
} | ||
|
||
// lookupIdentities looks up creator and assignee remote identities to local identities (already existing or to be created) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a better documentation line. It makes no sense to me :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function did much more than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DhritiShikhar There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
func lookupIdentities(ctx context.Context, db *gorm.DB, remoteWorkItem RemoteWorkItem, providerType string, spaceID uuid.UUID) (*workitem.WorkItem, error) { | ||
func lookupIdentities(ctx context.Context, db *gorm.DB, remoteWorkItem RemoteWorkItem, tq TrackerSchedule) (*workitem.WorkItem, error) { | ||
identityRepository := account.NewIdentityRepository(db) | ||
//spaceSelfURL := rest.AbsoluteURL(goa.ContextRequest(ctx), app.SpaceHref(spaceID.String())) | ||
workItem := workitem.WorkItem{ | ||
// ID: remoteWorkItem.ID, | ||
Type: remoteWorkItem.Type, | ||
Type: tq.WorkItemTypeID, | ||
Fields: make(map[string]interface{}), | ||
SpaceID: spaceID, | ||
SpaceID: tq.SpaceID, | ||
} | ||
// copy all fields from remoteworkitem into result workitem | ||
for fieldName, fieldValue := range remoteWorkItem.Fields { | ||
|
@@ -77,7 +77,7 @@ func lookupIdentities(ctx context.Context, db *gorm.DB, remoteWorkItem RemoteWor | |
} | ||
creatorLogin := fieldValue.(string) | ||
creatorProfileURL := remoteWorkItem.Fields[remoteCreatorProfileURL].(string) | ||
identity, err := identityRepository.Lookup(context.Background(), creatorLogin, creatorProfileURL, providerType) | ||
identity, err := identityRepository.Lookup(context.Background(), creatorLogin, creatorProfileURL, tq.TrackerType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit sceptical for why and how the tracker type corresponds to the provider type in the identity repo. Can you please explain what both are and if it is only by accident that you can use the tracker type for it? |
||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create identity during lookup") | ||
} | ||
|
@@ -97,7 +97,7 @@ func lookupIdentities(ctx context.Context, db *gorm.DB, remoteWorkItem RemoteWor | |
assigneeProfileURLs := remoteWorkItem.Fields[RemoteAssigneeProfileURLs].([]string) | ||
for i, assigneeLogin := range assigneeLogins { | ||
assigneeProfileURL := assigneeProfileURLs[i] | ||
identity, err := identityRepository.Lookup(context.Background(), assigneeLogin, assigneeProfileURL, providerType) | ||
identity, err := identityRepository.Lookup(context.Background(), assigneeLogin, assigneeProfileURL, tq.TrackerType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -149,7 +149,7 @@ func upsert(ctx context.Context, db *gorm.DB, workItem workitem.WorkItem) (*work | |
} | ||
} else { | ||
log.Info(nil, nil, "Workitem does not exist, will be created") | ||
resultWorkItem, _, err = wir.Create(ctx, workItem.SpaceID, workitem.SystemBug, workItem.Fields, creator) | ||
resultWorkItem, _, err = wir.Create(ctx, workItem.SpaceID, workItem.Type, workItem.Fields, creator) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether the UUID should be validated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@baijum are you talking about a check if the
id
contains string or uuid?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check whether the value of
*ctx.Payload.Data.ID
is real UUID (It's not a string type)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean what if the value is some random string. It will reach the database layer and fail. How about failing early?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@baijum At design level itself its specified that the value should be a UUID (https://github.com/fabric8-services/fabric8-wit/blob/master/design/tracker_queries.go#L13)