diff --git a/.erda/migrations/qa/20210928-auto-test-plan-step-groupID.sql b/.erda/migrations/qa/20210928-auto-test-plan-step-groupID.sql deleted file mode 100644 index 450b61ce252..00000000000 --- a/.erda/migrations/qa/20210928-auto-test-plan-step-groupID.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE `dice_autotest_plan_step` ADD `group_id` BIGINT(20) NOT NULL DEFAULT 0 COMMENT 'auto test plan step group'; diff --git a/apistructs/sceneset.go b/apistructs/sceneset.go index 4ea95f2db7b..d265b60a2b6 100644 --- a/apistructs/sceneset.go +++ b/apistructs/sceneset.go @@ -15,21 +15,12 @@ package apistructs import ( - "fmt" - "regexp" "strconv" "time" - - "github.com/erda-project/erda/pkg/strutil" ) -const ( - SceneSetsAutotestExecType = "sceneSets" - SceneAutotestExecType = "scene" - - SceneSetNameMaxLength int = 50 - SceneSetDescMaxLength int = 255 -) +const SceneSetsAutotestExecType = "sceneSets" +const SceneAutotestExecType = "scene" type SceneSet struct { ID uint64 `json:"id"` @@ -62,19 +53,6 @@ type SceneSetRequest struct { IdentityInfo } -func (req *SceneSetRequest) Validate() error { - if err := strutil.Validate(req.Name, strutil.MaxRuneCountValidator(SceneSetNameMaxLength)); err != nil { - return err - } - if err := strutil.Validate(req.Description, strutil.MaxRuneCountValidator(SceneSetDescMaxLength)); err != nil { - return err - } - if ok, _ := regexp.MatchString("^[a-zA-Z\u4e00-\u9fa50-9_-]*$", req.Name); !ok { - return fmt.Errorf("the name not match %s", "^[a-zA-Z\u4e00-\u9fa50-9_-]*$") - } - return nil -} - // type SceneSetUpdateRequest struct { // Name string `json:"name"` // Description string `json:"description"` diff --git a/apistructs/sceneset_test.go b/apistructs/sceneset_test.go deleted file mode 100644 index 1f4bdbdab9d..00000000000 --- a/apistructs/sceneset_test.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2021 Terminus, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package apistructs - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "k8s.io/apimachinery/pkg/util/rand" -) - -func TestSceneSetRequestValidate(t *testing.T) { - tt := []struct { - req SceneSetRequest - want bool - }{ - {SceneSetRequest{Name: rand.String(50), Description: "1"}, true}, - {SceneSetRequest{Name: rand.String(51), Description: "1"}, false}, - {SceneSetRequest{Name: "1", Description: rand.String(255)}, true}, - {SceneSetRequest{Name: "1", Description: rand.String(256)}, false}, - {SceneSetRequest{Name: "****", Description: "1"}, false}, - {SceneSetRequest{Name: "/", Description: "1"}, false}, - {SceneSetRequest{Name: "_abd1", Description: "1"}, true}, - {SceneSetRequest{Name: "_测试", Description: "1"}, true}, - {SceneSetRequest{Name: "1_测试a", Description: "1"}, true}, - {SceneSetRequest{Name: "a_测试1", Description: "1"}, true}, - } - for _, v := range tt { - assert.Equal(t, v.want, v.req.Validate() == nil) - } - -} diff --git a/apistructs/testplan_v2.go b/apistructs/testplan_v2.go index 76ea7aa907c..f4b9f78fa18 100644 --- a/apistructs/testplan_v2.go +++ b/apistructs/testplan_v2.go @@ -171,12 +171,6 @@ type TestPlanV2StepGetResponse struct { Data TestPlanV2Step `json:"data"` } -// TestPlanV2StepListResponse testplan get response -type TestPlanV2StepListResponse struct { - Header - Data []*TestPlanV2Step `json:"data"` -} - // TestPlanV2PagingResponseData testplan query response data type TestPlanV2PagingResponseData struct { Total int `json:"total"` @@ -190,7 +184,6 @@ type TestPlanV2Step struct { SceneSetName string `json:"sceneSetName"` PreID uint64 `json:"preID"` PlanID uint64 `json:"planID"` - GroupID uint64 `json:"groupID"` ID uint64 `json:"id"` } @@ -199,7 +192,6 @@ type TestPlanV2StepAddRequest struct { SceneSetID uint64 `json:"sceneSetID"` PreID uint64 `json:"preID"` TestPlanID uint64 `json:"-"` - GroupID uint64 `json:"groupID"` IdentityInfo } @@ -236,16 +228,3 @@ type TestPlanV2StepUpdateResp struct { Header Data string `json:"data"` } - -// TestPlanV2StepMoveRequest move a step in the test plan request -type TestPlanV2StepMoveRequest struct { - StepID uint64 `json:"stepID"` - LastStepID uint64 `json:"lastStepID"` - PreID uint64 `json:"preID"` - ScenesSetId uint64 `json:"scenesSetId"` - TestPlanID uint64 `json:"-"` - TargetStepID uint64 `json:"targetStepID"` - IsGroup bool `json:"isGroup"` // true: means move with group - - IdentityInfo -} diff --git a/bundle/autotest_plan.go b/bundle/autotest_plan.go index 2c43a6ad5e2..0df176f3aec 100644 --- a/bundle/autotest_plan.go +++ b/bundle/autotest_plan.go @@ -68,7 +68,7 @@ func (b *Bundle) DeleteTestPlansV2Step(req apistructs.TestPlanV2StepDeleteReques } // MoveTestPlansV2Step 移动测试计划步骤 -func (b *Bundle) MoveTestPlansV2Step(req apistructs.TestPlanV2StepMoveRequest) error { +func (b *Bundle) MoveTestPlansV2Step(req apistructs.TestPlanV2StepUpdateRequest) error { host, err := b.urls.DOP() if err != nil { return err @@ -177,7 +177,7 @@ func (b *Bundle) GetTestPlanV2(testPlanID uint64) (*apistructs.TestPlanV2GetResp return &getResp, nil } -// GetTestPlanV2Step 获取测试计划步骤 +// GetTestPlanV2 获取测试计划步骤 func (b *Bundle) GetTestPlanV2Step(stepID uint64) (*apistructs.TestPlanV2Step, error) { host, err := b.urls.DOP() if err != nil { @@ -199,30 +199,7 @@ func (b *Bundle) GetTestPlanV2Step(stepID uint64) (*apistructs.TestPlanV2Step, e return &getResp.Data, nil } -// ListTestPlanV2Step list test plan step -func (b *Bundle) ListTestPlanV2Step(testPlanID, groupID uint64) ([]*apistructs.TestPlanV2Step, error) { - host, err := b.urls.DOP() - if err != nil { - return nil, err - } - hc := b.hc - - var getResp apistructs.TestPlanV2StepListResponse - resp, err := hc.Get(host).Path(fmt.Sprintf("/api/autotests/testplans/%d/steps/actions/list-by-group-id", testPlanID)). - Param("groupID", strconv.FormatUint(groupID, 10)). - Header(httputil.InternalHeader, "bundle").Do().JSON(&getResp) - - if err != nil { - return nil, apierrors.ErrInvoke.InternalError(err) - } - if !resp.IsOK() || !getResp.Success { - return nil, toAPIError(resp.StatusCode(), getResp.Error) - } - - return getResp.Data, nil -} - -// UpdateTestPlanV2Step 获取测试计划步骤 +// GetTestPlanV2 获取测试计划步骤 func (b *Bundle) UpdateTestPlanV2Step(req apistructs.TestPlanV2StepUpdateRequest) error { host, err := b.urls.DOP() if err != nil { diff --git a/modules/dop/dao/testplan_v2_step.go b/modules/dop/dao/testplan_v2_step.go index 247d0fa78c8..5735e60dc30 100644 --- a/modules/dop/dao/testplan_v2_step.go +++ b/modules/dop/dao/testplan_v2_step.go @@ -20,7 +20,6 @@ import ( "github.com/erda-project/erda/apistructs" "github.com/erda-project/erda/pkg/database/dbengine" - "github.com/erda-project/erda/pkg/strutil" ) // TestPlanV2Step 测试计划V2步骤 @@ -29,7 +28,6 @@ type TestPlanV2Step struct { PlanID uint64 SceneSetID uint64 PreID uint64 - GroupID uint64 } // TestPlanV2StepJoin 测试计划V2步骤join测试集表 @@ -51,7 +49,6 @@ func (tps TestPlanV2StepJoin) Convert2DTO() *apistructs.TestPlanV2Step { PreID: tps.PreID, PlanID: tps.PlanID, ID: tps.ID, - GroupID: tps.GroupID, } } @@ -72,209 +69,102 @@ func (client *DBClient) GetTestPlanV2Step(ID uint64) (*TestPlanV2StepJoin, error return &step, nil } -// ListTestPlanV2Step list testPlan step -func (client *DBClient) ListTestPlanV2Step(testPlanID, groupID uint64) ([]TestPlanV2StepJoin, error) { - var step []TestPlanV2StepJoin - err := client.Where("plan_id = ?", testPlanID). - Where("group_id = ? OR id = ?", groupID, groupID). - Find(&step).Error - return step, err -} - // AddTestPlanV2Step Insert a step in the test plan -func (client *DBClient) AddTestPlanV2Step(req *apistructs.TestPlanV2StepAddRequest) (uint64, error) { - var newStepID uint64 - err := client.Transaction(func(tx *gorm.DB) error { +func (client *DBClient) AddTestPlanV2Step(req *apistructs.TestPlanV2StepAddRequest) error { + return client.Transaction(func(tx *gorm.DB) error { var preStep, nextStep TestPlanV2Step - newStep := TestPlanV2Step{PreID: req.PreID, SceneSetID: req.SceneSetID, PlanID: req.TestPlanID, GroupID: req.GroupID} + newStep := TestPlanV2Step{PreID: req.PreID, SceneSetID: req.SceneSetID, PlanID: req.TestPlanID} // Check the pre step is exist if req.PreID != 0 && tx.Where("id = ?", req.PreID).First(&preStep).Error != nil { return errors.Errorf("the pre step is not found: %d", req.PreID) } - // If the groupID of preStep is 0, set its id as groupID - if req.PreID != 0 && preStep.GroupID == 0 && req.GroupID != 0 { - preStep.GroupID = preStep.ID - if err := tx.Save(&preStep).Error; err != nil { - return err - } - } - // Find the next step - hasNextStep := true if err := tx.Where("pre_id = ?", req.PreID).Where("plan_id = ?", req.TestPlanID).First(&nextStep).Error; err != nil { if gorm.IsRecordNotFoundError(err) { - hasNextStep = false - } else { - return err + // Insert to the end or beginning + return tx.Create(&newStep).Error } + return err } - // Insert new step if err := tx.Create(&newStep).Error; err != nil { return err } - newStepID = newStep.ID - - // If req.GroupID is 0, set stepID as groupID - if req.GroupID == 0 { - newStep.GroupID = newStepID - if err := tx.Save(&newStep).Error; err != nil { - return err - } - } - // Update the order of next step - if hasNextStep { - nextStep.PreID = newStepID - return tx.Save(&nextStep).Error - } - return nil + nextStep.PreID = newStep.ID + return tx.Save(&nextStep).Error }) - - return newStepID, err } // DeleteTestPlanV2Step Delete a step in the test plan func (client *DBClient) DeleteTestPlanV2Step(req *apistructs.TestPlanV2StepDeleteRequest) error { - return client.Transaction(func(tx *gorm.DB) (err error) { + return client.Transaction(func(tx *gorm.DB) error { var step, nextStep TestPlanV2Step - defer func() { - if err == nil { - err = updateStepGroup(tx, step.GroupID) - } - }() - // Get the step - if err = tx.Where("id = ?", req.StepID).First(&step).Error; err != nil { + if err := tx.Where("id = ?", req.StepID).First(&step).Error; err != nil { return err } // Get the next step - if err = tx.Where("pre_id = ?", req.StepID).Where("plan_id = ?", req.TestPlanID).First(&nextStep).Error; err != nil { + if err := tx.Where("pre_id = ?", req.StepID).Where("plan_id = ?", req.TestPlanID).First(&nextStep).Error; err != nil { if gorm.IsRecordNotFoundError(err) { // Delete the last step - err = tx.Delete(&step).Error - return err + return tx.Delete(&step).Error } return err - } // Update next step nextStep.PreID = step.PreID - if err = tx.Save(&nextStep).Error; err != nil { + if err := tx.Save(&nextStep).Error; err != nil { return err } - err = tx.Delete(&step).Error - return err + return tx.Delete(&step).Error }) } -// MoveTestPlanV2Step move a step in the test plan -func (client *DBClient) MoveTestPlanV2Step(req *apistructs.TestPlanV2StepMoveRequest) error { - return client.Transaction(func(tx *gorm.DB) (err error) { - var oldGroupID, newGroupID uint64 - // update step groupID in the group if isGroup is false - defer func() { - if err == nil && !req.IsGroup { - groupIDs := strutil.DedupUint64Slice([]uint64{oldGroupID, newGroupID}, true) - err = updateStepGroup(tx, groupIDs...) - } - }() - - var ( - step, oldNextStep, newNextStep TestPlanV2Step - ) - - firstStepIDInGroup := req.StepID - lastStepIDInGroup := req.LastStepID - // get the first step in the group - if err = tx.Where("id = ?", firstStepIDInGroup).First(&step).Error; err != nil { +// UpdateTestPlanV2Step Update a step in the test plan +func (client *DBClient) MoveTestPlanV2Step(req *apistructs.TestPlanV2StepUpdateRequest) error { + return client.Transaction(func(tx *gorm.DB) error { + var step, oldNextStep, newNextStep TestPlanV2Step + // Get the step + if err := tx.Where("id = ?", req.StepID).First(&step).Error; err != nil { return err } - oldGroupID = step.GroupID - - // the order of the linked list has not changed - if req.PreID == step.PreID || req.PreID == lastStepIDInGroup { - if req.IsGroup { - return nil - } - goto LABEL2 - } - - // Get the old next step and update its preID if exists - if err = tx.Where("pre_id = ?", lastStepIDInGroup). - Where("plan_id = ?", req.TestPlanID).First(&oldNextStep).Error; err != nil { + // Get the old next step + if err := tx.Where("pre_id = ?", req.StepID).Where("plan_id = ?", req.TestPlanID).First(&oldNextStep).Error; err != nil { if gorm.IsRecordNotFoundError(err) { // the step was the last step goto LABEL1 } return err } + // Update oldNextStep oldNextStep.PreID = step.PreID - if err = tx.Save(&oldNextStep).Error; err != nil { + if err := tx.Save(&oldNextStep).Error; err != nil { return err } - LABEL1: // get the new next step and update its preID if exists + LABEL1: + // Get the new next step step.PreID = req.PreID - if err = tx.Where("pre_id = ?", req.PreID).Where("plan_id = ?", req.TestPlanID).First(&newNextStep).Error; err != nil { + if err := tx.Where("pre_id = ?", req.PreID).Where("plan_id = ?", req.TestPlanID).First(&newNextStep).Error; err != nil { if gorm.IsRecordNotFoundError(err) { // target step is the last step goto LABEL2 } return err } - newNextStep.PreID = lastStepIDInGroup - if err = tx.Save(&newNextStep).Error; err != nil { + // Update newNextStep + newNextStep.PreID = req.StepID + if err := tx.Save(&newNextStep).Error; err != nil { return err } - LABEL2: // update the preID of the step, and update the groupID of the step if needed - if !req.IsGroup { - if req.TargetStepID == 0 { - newGroupID = 0 - } else { // else find the groupID of targetStep - var targetStep TestPlanV2Step - if err = tx.Where("id = ?", req.TargetStepID).First(&targetStep).Error; err != nil { - return err - } - newGroupID = targetStep.GroupID - // if the groupID of targetStep is 0, set its id as groupID - if newGroupID == 0 { - targetStep.GroupID = targetStep.ID - newGroupID = targetStep.ID - if err = tx.Save(&targetStep).Error; err != nil { - return err - } - } - } - step.GroupID = newGroupID - } - err = tx.Save(&step).Error - return err + LABEL2: + return tx.Save(&step).Error }) } -// updateStepGroup update step group, set min setID in the group as groupID -func updateStepGroup(tx *gorm.DB, groupIDs ...uint64) error { - for _, v := range groupIDs { - if v == 0 { - continue - } - - var stepGroup []TestPlanV2Step - if err := tx.Where("group_id = ?", v).Order("id").Find(&stepGroup).Error; err != nil { - return err - } - if len(stepGroup) > 0 { - if err := tx.Model(&TestPlanV2Step{}).Where("group_id = ?", v).Update("group_id", stepGroup[0].ID).Error; err != nil { - return err - } - } - } - return nil -} - func (client *DBClient) UpdateTestPlanV2Step(step TestPlanV2Step) error { return client.Save(&step).Error } @@ -288,7 +178,7 @@ func (client *DBClient) GetStepByTestPlanID(testPlanID uint64, needSort bool) ([ ) if err := client.Table("dice_autotest_plan_step").Select("dice_autotest_plan_step.id, dice_autotest_plan_step.created_at, "+ "dice_autotest_plan_step.updated_at, dice_autotest_plan_step.plan_id, dice_autotest_plan_step.pre_id, "+ - "dice_autotest_plan_step.scene_set_id, dice_autotest_scene_set.name,dice_autotest_plan_step.group_id"). + "dice_autotest_plan_step.scene_set_id, dice_autotest_scene_set.name"). Joins("left join dice_autotest_scene_set on dice_autotest_plan_step.scene_set_id = dice_autotest_scene_set.id"). Where("dice_autotest_plan_step.plan_id = ?", testPlanID).Limit(1000).Scan(&steps).Count(&count).Error; err != nil { return nil, 0, err diff --git a/modules/dop/endpoints/endpoints.go b/modules/dop/endpoints/endpoints.go index 9841295357e..82c06bc3e8d 100644 --- a/modules/dop/endpoints/endpoints.go +++ b/modules/dop/endpoints/endpoints.go @@ -409,7 +409,6 @@ func (e *Endpoints) Routes() []httpserver.Endpoint { {Path: "/api/autotests/testplans/{testPlanID}/actions/move-step", Method: http.MethodPut, Handler: e.MoveTestPlanV2Step}, {Path: "/api/autotests/testplans-step/{stepID}", Method: http.MethodGet, Handler: e.GetTestPlanV2Step}, {Path: "/api/autotests/testplans-step/{stepID}", Method: http.MethodPut, Handler: e.UpdateTestPlanV2Step}, - {Path: "/api/autotests/testplans/{testPlanID}/steps/actions/list-by-group-id", Method: http.MethodGet, Handler: e.ListTestPlanV2Step}, {Path: "/api/reportsets/{pipelineID}", Method: http.MethodGet, Handler: e.queryReportSets}, diff --git a/modules/dop/endpoints/testplan_v2.go b/modules/dop/endpoints/testplan_v2.go index 777a8979ddb..4fa99731869 100644 --- a/modules/dop/endpoints/testplan_v2.go +++ b/modules/dop/endpoints/testplan_v2.go @@ -249,24 +249,24 @@ func (e *Endpoints) DeleteTestPlanV2Step(ctx context.Context, r *http.Request, v return httpserver.OkResp("succ") } -// MoveTestPlanV2Step move the test plan step +// UpdateTestPlanV2Step Update the test plan step func (e *Endpoints) MoveTestPlanV2Step(ctx context.Context, r *http.Request, vars map[string]string) (httpserver.Responser, error) { identityInfo, err := user.GetIdentityInfo(r) if err != nil { - return apierrors.ErrMoveTestPlanStep.NotLogin().ToResp(), nil + return apierrors.ErrUpdateTestPlanStep.NotLogin().ToResp(), nil } testPlanID, err := getTestPlanID(vars) if err != nil { - return apierrors.ErrMoveTestPlanStep.InvalidParameter(err).ToResp(), nil + return apierrors.ErrUpdateTestPlanStep.InvalidParameter(err).ToResp(), nil } - var req apistructs.TestPlanV2StepMoveRequest + var req apistructs.TestPlanV2StepUpdateRequest if r.ContentLength == 0 { - return apierrors.ErrMoveTestPlanStep.MissingParameter("request body").ToResp(), nil + return apierrors.ErrUpdateTestPlanStep.MissingParameter("request body").ToResp(), nil } if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - return apierrors.ErrMoveTestPlanStep.InvalidParameter(err).ToResp(), nil + return apierrors.ErrUpdateTestPlanStep.InvalidParameter(err).ToResp(), nil } req.IdentityInfo = identityInfo req.TestPlanID = testPlanID @@ -327,35 +327,6 @@ func (e *Endpoints) GetTestPlanV2Step(ctx context.Context, r *http.Request, vars return httpserver.OkResp(step) } -// ListTestPlanV2Step list TestPlan step -func (e *Endpoints) ListTestPlanV2Step(ctx context.Context, r *http.Request, vars map[string]string) (httpserver.Responser, error) { - _, err := user.GetIdentityInfo(r) - if err != nil { - return apierrors.ErrListTestPlanStep.NotLogin().ToResp(), nil - } - - testPlanID, err := strconv.ParseUint(vars["testPlanID"], 10, 64) - if err != nil { - return errorresp.ErrResp(errors.New("testPlanID id parse failed")) - } - - var groupID uint64 - groupIDStr := r.URL.Query().Get("groupID") - if groupIDStr != "" { - groupID, err = strconv.ParseUint(groupIDStr, 10, 64) - if err != nil { - return apierrors.ErrGetTestPlan.InvalidParameter(err).ToResp(), nil - } - } - - steps, err := e.autotestV2.ListTestPlanV2Step(testPlanID, groupID) - if err != nil { - return errorresp.ErrResp(err) - } - - return httpserver.OkResp(steps) -} - func getTestPlanID(vars map[string]string) (uint64, error) { testplanIDStr := vars["testPlanID"] testplanID, err := strconv.ParseUint(testplanIDStr, 10, 64) diff --git a/modules/dop/services/apierrors/errors.go b/modules/dop/services/apierrors/errors.go index 405c5e133f4..484d7e2ccef 100644 --- a/modules/dop/services/apierrors/errors.go +++ b/modules/dop/services/apierrors/errors.go @@ -238,8 +238,6 @@ var ( ErrAddTestPlanStep = err("ErrAddTestPlanStep", "添加测试计划步骤失败") ErrDeleteTestPlanStep = err("ErrDeleteTestPlanStep", "删除测试计划步骤失败") ErrUpdateTestPlanStep = err("ErrUpdateTestPlanStep", "更新测试计划步骤失败") - ErrListTestPlanStep = err("ErrListTestPlanStep", "获取测试计划步骤失败") - ErrMoveTestPlanStep = err("ErrMoveTestPlanStep", "移动测试计划步骤失败") ErrCreateTestPlanMember = err("ErrCreateTestPlanMember", "测试计划关联成员失败") ErrUpdateTestPlanMember = err("ErrUpdateTestPlanMember", "测试计划更新成员失败") ErrListTestPlanMembers = err("ErrListTestPlanMembers", "查询测试计划关联成员列表失败") diff --git a/modules/dop/services/autotest_v2/testplan_v2.go b/modules/dop/services/autotest_v2/testplan_v2.go index 07701ef38d0..c7d77d6ce4e 100644 --- a/modules/dop/services/autotest_v2/testplan_v2.go +++ b/modules/dop/services/autotest_v2/testplan_v2.go @@ -268,12 +268,20 @@ func (svc *Service) AddTestPlanV2Step(req *apistructs.TestPlanV2StepAddRequest) } } - // Check the sceneSet is exists + // Check the sceneset is exists if err := svc.db.CheckSceneSetIsExists(req.SceneSetID); err != nil { return 0, err } - return svc.db.AddTestPlanV2Step(req) + if err := svc.db.AddTestPlanV2Step(req); err != nil { + return 0, err + } + + newStep, err := svc.db.GetTestPlanV2StepByPreID(req.PreID) + if err != nil { + return 0, err + } + return newStep.ID, nil } // DeleteTestPlanV2Step Delete a step in the test plan @@ -296,15 +304,15 @@ func (svc *Service) DeleteTestPlanV2Step(req *apistructs.TestPlanV2StepDeleteReq return err } if !access.Access { - return apierrors.ErrDeleteTestPlan.AccessDenied() + return apierrors.ErrUpdateTestPlan.AccessDenied() } } return svc.db.DeleteTestPlanV2Step(req) } -// MoveTestPlanV2Step move a step in the test plan -func (svc *Service) MoveTestPlanV2Step(req *apistructs.TestPlanV2StepMoveRequest) error { +// UpdateTestPlanV2Step Update a step in the test plan +func (svc *Service) MoveTestPlanV2Step(req *apistructs.TestPlanV2StepUpdateRequest) error { testPlan, err := svc.db.GetTestPlanV2ByID(req.TestPlanID) if err != nil { return err @@ -337,6 +345,7 @@ func (svc *Service) UpdateTestPlanV2Step(req *apistructs.TestPlanV2StepUpdateReq if err != nil { return err } + step.ID = req.StepID step.SceneSetID = req.ScenesSetId plan, err := svc.db.GetTestPlanV2ByID(step.PlanID) @@ -378,21 +387,6 @@ func (svc *Service) GetTestPlanV2Step(ID uint64) (*apistructs.TestPlanV2Step, er return step.Convert2DTO(), nil } -// ListTestPlanV2Step list testPlan step -func (svc *Service) ListTestPlanV2Step(testPlanID, groupID uint64) ([]*apistructs.TestPlanV2Step, error) { - steps, err := svc.db.ListTestPlanV2Step(testPlanID, groupID) - if err != nil { - return nil, err - } - - var stepDtos []*apistructs.TestPlanV2Step - for _, v := range steps { - stepDtos = append(stepDtos, v.Convert2DTO()) - } - - return stepDtos, nil -} - // getChangedFields get changed fields func (svc *Service) getChangedFields(req *apistructs.TestPlanV2UpdateRequest, model *dao.TestPlanV2) (map[string]interface{}, error) { fields := make(map[string]interface{}, 0) @@ -424,6 +418,7 @@ func (svc *Service) getChangedFields(req *apistructs.TestPlanV2UpdateRequest, mo } func (svc *Service) ExecuteDiceAutotestTestPlan(req apistructs.AutotestExecuteTestPlansRequest) (*apistructs.PipelineDTO, error) { + testPlan, err := svc.GetTestPlanV2(req.TestPlan.ID, req.IdentityInfo) if err != nil { return nil, err @@ -432,42 +427,35 @@ func (svc *Service) ExecuteDiceAutotestTestPlan(req apistructs.AutotestExecuteTe var spec pipelineyml.Spec spec.Version = "1.1" var stagesValue []*pipelineyml.Stage - - // get steps group by groupID - stepGroupMap, groupIDs := getStepMapByGroupID(testPlan.Steps) - - for _, groupID := range groupIDs { + for _, v := range testPlan.Steps { + if v.SceneSetID <= 0 { + continue + } var specStage pipelineyml.Stage - for _, v := range stepGroupMap[groupID] { - if v.SceneSetID <= 0 { - continue - } - sceneSetJson, err := json.Marshal(v) - if err != nil { - return nil, err - } - action := map[pipelineyml.ActionType]*pipelineyml.Action{ - pipelineyml.Snippet: { - Alias: pipelineyml.ActionAlias(strconv.Itoa(int(v.ID))), - Type: pipelineyml.Snippet, + sceneSetJson, err := json.Marshal(v) + if err != nil { + return nil, err + } + specStage.Actions = append(specStage.Actions, map[pipelineyml.ActionType]*pipelineyml.Action{ + pipelineyml.Snippet: { + Alias: pipelineyml.ActionAlias(strconv.Itoa(int(v.ID))), + Type: pipelineyml.Snippet, + Labels: map[string]string{ + apistructs.AutotestSceneSet: base64.StdEncoding.EncodeToString(sceneSetJson), + apistructs.AutotestType: apistructs.AutotestSceneSet, + }, + If: expression.LeftPlaceholder + " 1 == 1 " + expression.RightPlaceholder, + SnippetConfig: &pipelineyml.SnippetConfig{ + Name: strconv.Itoa(int(v.SceneSetID)), + Source: apistructs.PipelineSourceAutoTest.String(), Labels: map[string]string{ - apistructs.AutotestSceneSet: base64.StdEncoding.EncodeToString(sceneSetJson), - apistructs.AutotestType: apistructs.AutotestSceneSet, - }, - If: expression.LeftPlaceholder + " 1 == 1 " + expression.RightPlaceholder, - SnippetConfig: &pipelineyml.SnippetConfig{ - Name: strconv.Itoa(int(v.SceneSetID)), - Source: apistructs.PipelineSourceAutoTest.String(), - Labels: map[string]string{ - apistructs.LabelAutotestExecType: apistructs.SceneSetsAutotestExecType, - apistructs.LabelSceneSetID: strconv.Itoa(int(v.SceneSetID)), - apistructs.LabelSpaceID: strconv.Itoa(int(testPlan.SpaceID)), - }, + apistructs.LabelAutotestExecType: apistructs.SceneSetsAutotestExecType, + apistructs.LabelSceneSetID: strconv.Itoa(int(v.SceneSetID)), + apistructs.LabelSpaceID: strconv.Itoa(int(testPlan.SpaceID)), }, }, - } - specStage.Actions = append(specStage.Actions, action) - } + }, + }) stagesValue = append(stagesValue, &specStage) } spec.Stages = stagesValue @@ -506,28 +494,6 @@ func (svc *Service) ExecuteDiceAutotestTestPlan(req apistructs.AutotestExecuteTe return pipelineDTO, nil } -// getStepMapByGroupID get step group by groupID -func getStepMapByGroupID(steps []*apistructs.TestPlanV2Step) (map[uint64][]*apistructs.TestPlanV2Step, []uint64) { - // stepGroupMap key: groupID, if groupID is 0, set id as groupID - stepGroupMap := make(map[uint64][]*apistructs.TestPlanV2Step, 0) - groupIDs := make([]uint64, 0) // make sure the order remains the same - for _, v := range steps { - if v.SceneSetID <= 0 { - continue - } - if v.GroupID == 0 { - v.GroupID = v.ID - } - if _, ok := stepGroupMap[v.GroupID]; ok { - stepGroupMap[v.GroupID] = append(stepGroupMap[v.GroupID], v) - } else { - stepGroupMap[v.GroupID] = []*apistructs.TestPlanV2Step{v} - groupIDs = append(groupIDs, v.GroupID) - } - } - return stepGroupMap, groupIDs -} - func (svc *Service) GetTestClusterNameBySpaceID(spaceID uint64) (string, error) { space, err := svc.db.GetAutoTestSpace(spaceID) if err != nil { diff --git a/modules/dop/services/autotest_v2/testplan_v2_test.go b/modules/dop/services/autotest_v2/testplan_v2_test.go deleted file mode 100644 index da5324fed48..00000000000 --- a/modules/dop/services/autotest_v2/testplan_v2_test.go +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright (c) 2021 Terminus, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package autotestv2 - -import ( - "reflect" - "testing" - - "github.com/erda-project/erda/apistructs" -) - -func TestGetStepMapByGroupID(t *testing.T) { - tt := []struct { - steps []*apistructs.TestPlanV2Step - wantGroupIDs []uint64 - wantStepGroupMap map[uint64][]*apistructs.TestPlanV2Step - }{ - { - steps: []*apistructs.TestPlanV2Step{ - {ID: 1, PreID: 0, GroupID: 0, SceneSetID: 1}, - {ID: 2, PreID: 1, GroupID: 2, SceneSetID: 2}, - {ID: 3, PreID: 2, GroupID: 2, SceneSetID: 3}, - {ID: 4, PreID: 3, GroupID: 4, SceneSetID: 4}, - {ID: 5, PreID: 4, GroupID: 4, SceneSetID: 5}, - {ID: 6, PreID: 5, GroupID: 4, SceneSetID: 6}, - {ID: 7, PreID: 6, GroupID: 7, SceneSetID: 7}, - }, - wantGroupIDs: []uint64{1, 2, 4, 7}, - wantStepGroupMap: map[uint64][]*apistructs.TestPlanV2Step{ - 1: {{ID: 1, PreID: 0, GroupID: 0, SceneSetID: 1}}, - 2: {{ID: 2, PreID: 1, GroupID: 2, SceneSetID: 2}, {ID: 3, PreID: 2, GroupID: 2, SceneSetID: 3}}, - 4: {{ID: 4, PreID: 3, GroupID: 4, SceneSetID: 4}, {ID: 5, PreID: 4, GroupID: 4, SceneSetID: 5}, {ID: 6, PreID: 5, GroupID: 4, SceneSetID: 6}}, - 7: {{ID: 7, PreID: 6, GroupID: 7, SceneSetID: 7}}, - }, - }, - { - steps: []*apistructs.TestPlanV2Step{ - {ID: 5, PreID: 0, GroupID: 0, SceneSetID: 1}, - {ID: 3, PreID: 5, GroupID: 3, SceneSetID: 2}, - {ID: 7, PreID: 3, GroupID: 4, SceneSetID: 3}, - {ID: 4, PreID: 7, GroupID: 4, SceneSetID: 4}, - {ID: 1, PreID: 4, GroupID: 1, SceneSetID: 5}, - {ID: 2, PreID: 1, GroupID: 1, SceneSetID: 6}, - {ID: 6, PreID: 2, GroupID: 6, SceneSetID: 7}, - }, - wantGroupIDs: []uint64{5, 3, 4, 1, 6}, - wantStepGroupMap: map[uint64][]*apistructs.TestPlanV2Step{ - 5: {{ID: 5, PreID: 0, GroupID: 0, SceneSetID: 1}}, - 3: {{ID: 3, PreID: 5, GroupID: 3, SceneSetID: 2}}, - 4: {{ID: 7, PreID: 3, GroupID: 4, SceneSetID: 3}, {ID: 4, PreID: 7, GroupID: 4, SceneSetID: 4}}, - 1: {{ID: 1, PreID: 4, GroupID: 1, SceneSetID: 5}, {ID: 2, PreID: 1, GroupID: 1, SceneSetID: 6}}, - 6: {{ID: 6, PreID: 2, GroupID: 6, SceneSetID: 7}}, - }, - }, - } - - for _, v := range tt { - stepGroupMap, groupIDs := getStepMapByGroupID(v.steps) - if !reflect.DeepEqual(v.wantGroupIDs, groupIDs) { - t.Error("fail") - } - for k, v1 := range stepGroupMap { - for i, v2 := range v1 { - if v.wantStepGroupMap[k][i].ID != v2.ID { - t.Error("fail") - } - } - } - } -} diff --git a/modules/dop/services/sceneset/sceneset.go b/modules/dop/services/sceneset/sceneset.go index 11512ec6051..d7f1b1deefe 100644 --- a/modules/dop/services/sceneset/sceneset.go +++ b/modules/dop/services/sceneset/sceneset.go @@ -16,20 +16,30 @@ package sceneset import ( "fmt" + "regexp" "github.com/erda-project/erda/apistructs" "github.com/erda-project/erda/modules/dop/dao" "github.com/erda-project/erda/modules/dop/services/apierrors" + "github.com/erda-project/erda/pkg/strutil" ) const ( - maxSize int = 200 + maxSize int = 200 + nameMaxLength int = 50 + descMaxLength int = 255 ) func (svc *Service) CreateSceneSet(req apistructs.SceneSetRequest) (uint64, error) { - if err := req.Validate(); err != nil { + if err := strutil.Validate(req.Name, strutil.MaxRuneCountValidator(nameMaxLength)); err != nil { return 0, err } + if err := strutil.Validate(req.Description, strutil.MaxRuneCountValidator(descMaxLength)); err != nil { + return 0, err + } + if ok, _ := regexp.MatchString("^[a-zA-Z\u4e00-\u9fa50-9_-]*$", req.Name); !ok { + return 0, apierrors.ErrCreateAutoTestSceneSet.InvalidState("只可输入中文、英文、数字、中划线或下划线") + } count, err := svc.db.CountSceneSetByName(req.Name, req.SpaceID) if err != nil { @@ -44,7 +54,7 @@ func (svc *Service) CreateSceneSet(req apistructs.SceneSetRequest) (uint64, erro return 0, err } if len(sets) >= maxSize { - return 0, fmt.Errorf("reach max sceneset size") + return 0, fmt.Errorf("Reach max sceneset size!") } preID := uint64(0) @@ -88,13 +98,23 @@ func (svc *Service) GetSceneSetsBySpaceID(spaceID uint64) ([]apistructs.SceneSet res = append(res, s) } + // res := make([]apistructs.SceneSet, 0, len(sceneSets)) + // for _, item := range sceneSets { + // res = append(res, *mapping(&item)) + // } return res, nil } func (svc *Service) UpdateSceneSet(setID uint64, req apistructs.SceneSetRequest) (*apistructs.SceneSet, error) { - if err := req.Validate(); err != nil { + if err := strutil.Validate(req.Name, strutil.MaxRuneCountValidator(nameMaxLength)); err != nil { return nil, err } + if err := strutil.Validate(req.Description, strutil.MaxRuneCountValidator(descMaxLength)); err != nil { + return nil, err + } + if ok, _ := regexp.MatchString("^[a-zA-Z\u4e00-\u9fa50-9_-]*$", req.Name); !ok { + return nil, apierrors.ErrCreateAutoTestSceneSet.InvalidState("只可输入中文、英文、数字、中划线或下划线") + } s, err := svc.db.GetSceneSet(setID) if err != nil { diff --git a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/addScenesSetButton/render.go b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/addScenesSetButton/render.go index 4090daeb6bc..cac3bd69d7b 100644 --- a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/addScenesSetButton/render.go +++ b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/addScenesSetButton/render.go @@ -44,7 +44,6 @@ func (ca *ComponentAction) Render(ctx context.Context, c *apistructs.Component, req.UserID = bdl.Identity.UserID req.TestPlanID = uint64(c.State["testPlanId"].(float64)) req.PreID = lastStepID - req.GroupID = 0 stepID, err := bdl.Bdl.CreateTestPlansV2Step(req) if err != nil { return err diff --git a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/model.go b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/model.go index e4cf74aed99..400908362d8 100644 --- a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/model.go +++ b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/model.go @@ -54,11 +54,9 @@ type InParams struct { } type DragParams struct { - DragGroupKey int64 `json:"dragGroupKey"` - DropGroupKey int64 `json:"dropGroupKey"` - DragKey int64 `json:"dragKey"` - DropKey int64 `json:"dropKey"` - Position int64 `json:"position"` + DragKey uint64 `json:"dragKey"` + DropKey uint64 `json:"dropKey"` + Position int64 `json:"position"` } type State struct { diff --git a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/render.go b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/render.go index 988d67e0957..43cd5e8b6ba 100644 --- a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/render.go +++ b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/render.go @@ -145,7 +145,7 @@ func (i *ComponentStageForm) Render(ctx context.Context, c *apistructs.Component return } i.Props = visible - i.Props["groupDraggable"] = true + i.Props["groupDraggable"] = false switch event.Operation { case apistructs.InitializeOperation, apistructs.RenderingOperation: err = i.RenderListStageForm() @@ -153,16 +153,7 @@ func (i *ComponentStageForm) Render(ctx context.Context, c *apistructs.Component return err } case apistructs.AutoTestSceneStepMoveItemOperationKey: - err = i.RenderItemMoveStagesForm() - if err != nil { - return err - } - err = i.RenderListStageForm() - if err != nil { - return err - } - case apistructs.AutoTestSceneStepMoveGroupOperationKey: - err = i.RenderGroupMoveStagesForm() + err = i.RenderMoveStagesForm() if err != nil { return err } @@ -196,15 +187,6 @@ func (i *ComponentStageForm) Render(ctx context.Context, c *apistructs.Component if err := i.RenderListStageForm(); err != nil { return err } - case apistructs.AutoTestSceneStepSplitOperationKey: - err = i.RenderSplitStagesForm(event.OperationData) - if err != nil { - return err - } - err = i.RenderListStageForm() - if err != nil { - return err - } } i.RenderProtocol(c, gs) return diff --git a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/stages.go b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/stages.go index a2f4a070789..47304360429 100644 --- a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/stages.go +++ b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/stages.go @@ -15,22 +15,14 @@ package stages import ( - "fmt" - - "github.com/pkg/errors" - "github.com/erda-project/erda/apistructs" ) -func (i *ComponentStageForm) RenderStage(step apistructs.TestPlanV2Step) (StageData, error) { - groupID := int(step.GroupID) - if groupID == 0 { - groupID = int(step.ID) - } +func RenderStage(index int, step apistructs.TestPlanV2Step) (StageData, error) { pd := StageData{ - Title: fmt.Sprintf("#%d 场景集: %s", step.ID, step.SceneSetName), + Title: "场景集: " + step.SceneSetName, ID: step.ID, - GroupID: groupID, + GroupID: index, Operations: make(map[string]interface{}), } o := CreateOperation{} @@ -51,29 +43,6 @@ func (i *ComponentStageForm) RenderStage(step apistructs.TestPlanV2Step) (StageD oc.Meta.ID = step.ID pd.Operations["delete"] = oc - os := OperationInfo{ - OperationBaseInfo: OperationBaseInfo{ - Icon: "split", - Key: apistructs.AutoTestSceneStepSplitOperationKey.String(), - HoverTip: "改为串行", - Disabled: true, - Reload: true, - }, - } - os.Meta.ID = step.ID - m := map[string]interface{}{ - "groupID": groupID, - } - os.Meta.Data = m - stepGroup, err := i.ctxBdl.Bdl.ListTestPlanV2Step(step.PlanID, uint64(groupID)) - if err != nil { - return StageData{}, err - } - if len(stepGroup) > 1 { - os.Disabled = false - } - pd.Operations["split"] = os - return pd, nil } @@ -83,8 +52,8 @@ func (i *ComponentStageForm) RenderListStageForm() error { return err } var list []StageData - for _, v := range rsp.Data.Steps { - stageData, err := i.RenderStage(*v) + for i, v := range rsp.Data.Steps { + stageData, err := RenderStage(i, *v) if err != nil { return err } @@ -101,12 +70,6 @@ func (i *ComponentStageForm) RenderListStageForm() error { Reload: true, }, } - omg := OperationInfo{ - OperationBaseInfo: OperationBaseInfo{ - Key: apistructs.AutoTestSceneStepMoveGroupOperationKey.String(), - Reload: true, - }, - } ocl := OperationInfo{ OperationBaseInfo: OperationBaseInfo{ Key: "clickItem", @@ -116,7 +79,6 @@ func (i *ComponentStageForm) RenderListStageForm() error { Meta: OpMetaInfo{}, } i.Operations["moveItem"] = omi - i.Operations["moveGroup"] = omg i.Operations["clickItem"] = ocl return nil @@ -127,19 +89,10 @@ func (i *ComponentStageForm) RenderCreateStagesForm(opsData interface{}) error { if err != nil { return err } - preStep, err := i.ctxBdl.Bdl.GetTestPlanV2Step(meta.ID) - if err != nil { - return err - } - groupID := preStep.GroupID - if groupID == 0 { - groupID = preStep.ID - } _, err = i.ctxBdl.Bdl.CreateTestPlansV2Step(apistructs.TestPlanV2StepAddRequest{ PreID: meta.ID, TestPlanID: i.State.TestPlanId, - GroupID: groupID, IdentityInfo: apistructs.IdentityInfo{ UserID: i.ctxBdl.Identity.UserID, }, @@ -169,174 +122,32 @@ func (i *ComponentStageForm) RenderDeleteStagesForm(opsData interface{}) error { return nil } -func (i *ComponentStageForm) RenderGroupMoveStagesForm() (err error) { +func (i *ComponentStageForm) RenderMoveStagesForm() (err error) { var ( - req apistructs.TestPlanV2StepMoveRequest + step *apistructs.TestPlanV2Step + req apistructs.TestPlanV2StepUpdateRequest ) - dragGroupKey := uint64(i.State.DragParams.DragGroupKey) - dropGroupKey := uint64(i.State.DragParams.DropGroupKey) - req.UserID = i.ctxBdl.Identity.UserID req.TestPlanID = i.State.TestPlanId - req.IsGroup = true - - stepDragGroup, err := i.ctxBdl.Bdl.ListTestPlanV2Step(i.State.TestPlanId, dragGroupKey) - if err != nil { - return err - } - if len(stepDragGroup) <= 0 { - return errors.New("the dragGroupKey is not exists") - } - firstStepDrag, lastStepDrag := findFirstLastStepInGroup(stepDragGroup) - req.StepID = firstStepDrag.ID - req.LastStepID = lastStepDrag.ID - - switch i.State.DragParams.Position { - case 0: // inside target - return nil - case -1: // in front of the target - stepDropGroup, err := i.ctxBdl.Bdl.ListTestPlanV2Step(i.State.TestPlanId, dropGroupKey) + req.StepID = i.State.DragParams.DragKey + if i.State.DragParams.Position == -1 { + step, err = i.ctxBdl.Bdl.GetTestPlanV2Step(i.State.DragParams.DropKey) if err != nil { - return err - } - if len(stepDropGroup) <= 0 { - return errors.New("the dropGroupKey is not exists") - } - firstStepDrop, _ := findFirstLastStepInGroup(stepDropGroup) - req.PreID = firstStepDrop.PreID - req.TargetStepID = firstStepDrop.ID - // the order of the linked list has not changed - if req.PreID == req.LastStepID { - return nil - } - case 1: // behind the target - stepDropGroup, err := i.ctxBdl.Bdl.ListTestPlanV2Step(i.State.TestPlanId, dropGroupKey) - if err != nil { - return err - } - if len(stepDropGroup) <= 0 { - return errors.New("the dropGroupKey is not exists") - } - _, lastStepDrop := findFirstLastStepInGroup(stepDropGroup) - req.PreID = lastStepDrop.ID - req.TargetStepID = lastStepDrop.ID - // the order of the linked list has not changed - if req.PreID == firstStepDrag.PreID { - return nil - } - default: - return errors.New("unknown position") - } - return i.ctxBdl.Bdl.MoveTestPlansV2Step(req) -} - -func findFirstLastStepInGroup(steps []*apistructs.TestPlanV2Step) (firstStep, lastStep *apistructs.TestPlanV2Step) { - stepIDMap := make(map[uint64]*apistructs.TestPlanV2Step, len(steps)) - preIDMap := make(map[uint64]*apistructs.TestPlanV2Step, len(steps)) - for _, v := range steps { - stepIDMap[v.ID] = v - preIDMap[v.PreID] = v - } - for k := range preIDMap { - if _, ok := stepIDMap[k]; !ok { - firstStep = preIDMap[k] - break + return } - } - for k := range stepIDMap { - if _, ok := preIDMap[k]; !ok { - lastStep = stepIDMap[k] - break + if step.PreID == i.State.DragParams.DragKey { + return } - } - return -} - -func (i *ComponentStageForm) RenderItemMoveStagesForm() (err error) { - var ( - step *apistructs.TestPlanV2Step - req apistructs.TestPlanV2StepMoveRequest - testPlan *apistructs.TestPlanV2GetResponse - ) - dragGroupKey := uint64(i.State.DragParams.DragGroupKey) - dropGroupKey := uint64(i.State.DragParams.DropGroupKey) - - req.UserID = i.ctxBdl.Identity.UserID - req.TestPlanID = i.State.TestPlanId - req.StepID = uint64(i.State.DragParams.DragKey) - req.LastStepID = uint64(i.State.DragParams.DragKey) - req.IsGroup = false - if i.State.DragParams.DropKey == -1 { // move to the end and be independent group - req.TargetStepID = 0 + req.PreID = step.PreID } else { - req.TargetStepID = uint64(i.State.DragParams.DropKey) - } - - // find preID - if i.State.DragParams.DropKey == -1 { // move to the end and be independent group - testPlan, err = i.ctxBdl.Bdl.GetTestPlanV2(i.State.TestPlanId) + step, err = i.ctxBdl.Bdl.GetTestPlanV2Step(i.State.DragParams.DragKey) if err != nil { - return err + return } - req.PreID = testPlan.Data.Steps[len(testPlan.Data.Steps)-1].ID - } else { - switch i.State.DragParams.Position { - case 0: // inside target - return nil - case 1: // behind the target - step, err = i.ctxBdl.Bdl.GetTestPlanV2Step(uint64(i.State.DragParams.DragKey)) - if err != nil { - return - } - req.PreID = uint64(i.State.DragParams.DropKey) - // the order of the linked list has not changed in the same group - if req.PreID == step.PreID && dragGroupKey == dropGroupKey { - return nil - } - - case -1: // in front of the target - step, err = i.ctxBdl.Bdl.GetTestPlanV2Step(uint64(i.State.DragParams.DropKey)) - if err != nil { - return - } - req.PreID = step.PreID - // the order of the linked list has not changed in the same group - if req.PreID == req.LastStepID && dragGroupKey == dropGroupKey { - return nil - } - default: - return errors.New("unknown position") + if step.PreID == i.State.DragParams.DropKey { + return } + req.PreID = i.State.DragParams.DropKey } return i.ctxBdl.Bdl.MoveTestPlansV2Step(req) } - -func (i *ComponentStageForm) RenderSplitStagesForm(opsData interface{}) (err error) { - meta, err := GetOpsInfo(opsData) - if err != nil { - return err - } - - var req apistructs.TestPlanV2StepMoveRequest - - req.UserID = i.ctxBdl.Identity.UserID - req.TestPlanID = i.State.TestPlanId - req.StepID = meta.ID - req.LastStepID = meta.ID - req.IsGroup = false - req.TargetStepID = 0 - - stepGroup, err := i.ctxBdl.Bdl.ListTestPlanV2Step(i.State.TestPlanId, uint64(meta.Data["groupID"].(float64))) - if err != nil { - return err - } - if len(stepGroup) <= 0 { - return errors.New("the groupID is not exists") - } - if len(stepGroup) == 1 { - return nil - } - _, lastStep := findFirstLastStepInGroup(stepGroup) - req.PreID = lastStep.ID - return i.ctxBdl.Bdl.MoveTestPlansV2Step(req) -} diff --git a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/stages_test.go b/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/stages_test.go deleted file mode 100644 index cd2365e1f7f..00000000000 --- a/modules/openapi/component-protocol/scenarios/auto-test-plan-detail/components/stages/stages_test.go +++ /dev/null @@ -1,354 +0,0 @@ -// Copyright (c) 2021 Terminus, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package stages - -import ( - "reflect" - "testing" - - "bou.ke/monkey" - "github.com/stretchr/testify/assert" - - "github.com/erda-project/erda/apistructs" - "github.com/erda-project/erda/bundle" - protocol "github.com/erda-project/erda/modules/openapi/component-protocol" -) - -func TestFindFirstLastStepInGroup(t *testing.T) { - tt := []struct { - steps []*apistructs.TestPlanV2Step - wantFirst uint64 - wantLast uint64 - }{ - { - steps: []*apistructs.TestPlanV2Step{ - {ID: 2, PreID: 1}, - {ID: 3, PreID: 2}, - {ID: 4, PreID: 3}, - {ID: 5, PreID: 4}, - }, - wantFirst: 2, - wantLast: 5, - }, - { - steps: []*apistructs.TestPlanV2Step{ - {ID: 4, PreID: 0}, - {ID: 2, PreID: 4}, - {ID: 3, PreID: 5}, - {ID: 5, PreID: 2}, - }, - wantFirst: 4, - wantLast: 3, - }, - { - steps: []*apistructs.TestPlanV2Step{ - {ID: 1, PreID: 0}, - }, - wantFirst: 1, - wantLast: 1, - }, - } - for _, v := range tt { - firstStep, lastStep := findFirstLastStepInGroup(v.steps) - firstStepID, lastStepID := firstStep.ID, lastStep.ID - assert.Equal(t, v.wantFirst, firstStepID) - assert.Equal(t, v.wantLast, lastStepID) - } -} - -// TestRenderItemMoveStagesFormFrontTarget [1,2] 3 move 2 to the front of 3 -func TestRenderItemMoveStagesFormFrontTarget(t *testing.T) { - var bdl *bundle.Bundle - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "GetTestPlanV2Step", func(*bundle.Bundle, uint64) (*apistructs.TestPlanV2Step, error) { - return &apistructs.TestPlanV2Step{ - SceneSetID: 1, - PreID: 1, - PlanID: 1, - GroupID: 1, - ID: 2, - }, nil - }) - defer monkey.UnpatchAll() - - i := ComponentStageForm{ - ctxBdl: protocol.ContextBundle{}, - CommonStageForm: CommonStageForm{ - State: State{ - DragParams: DragParams{ - DragGroupKey: 1, - DropGroupKey: 3, - DragKey: 2, - DropKey: 3, - Position: -1, - }, - TestPlanId: 1, - }, - }, - } - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "MoveTestPlansV2Step", func(*bundle.Bundle, apistructs.TestPlanV2StepMoveRequest) error { - return nil - }) - if i.RenderItemMoveStagesForm() != nil { - t.Error("fail") - } -} - -// TestRenderItemMoveStagesFormBehindTarget [1,2] [3] move 2 to the behind of 3 -func TestRenderItemMoveStagesFormBehindTarget(t *testing.T) { - var bdl *bundle.Bundle - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "GetTestPlanV2Step", func(*bundle.Bundle, uint64) (*apistructs.TestPlanV2Step, error) { - return &apistructs.TestPlanV2Step{ - SceneSetID: 1, - PreID: 1, - PlanID: 1, - GroupID: 1, - ID: 2, - }, nil - }) - defer monkey.UnpatchAll() - - i := ComponentStageForm{ - ctxBdl: protocol.ContextBundle{}, - CommonStageForm: CommonStageForm{ - State: State{ - DragParams: DragParams{ - DragGroupKey: 1, - DropGroupKey: 3, - DragKey: 2, - DropKey: 3, - Position: 1, - }, - TestPlanId: 1, - }, - }, - } - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "MoveTestPlansV2Step", func(*bundle.Bundle, apistructs.TestPlanV2StepMoveRequest) error { - return nil - }) - if i.RenderItemMoveStagesForm() != nil { - t.Error("fail") - } -} - -// TestRenderItemMoveStagesFormBehindTarget [1,2] [3] move 1 to the front of 2 -func TestRenderItemMoveStagesFormNoChange(t *testing.T) { - var bdl *bundle.Bundle - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "GetTestPlanV2Step", func(*bundle.Bundle, uint64) (*apistructs.TestPlanV2Step, error) { - return &apistructs.TestPlanV2Step{ - PreID: 1, - GroupID: 1, - ID: 1, - }, nil - }) - defer monkey.UnpatchAll() - - i := ComponentStageForm{ - ctxBdl: protocol.ContextBundle{}, - CommonStageForm: CommonStageForm{ - State: State{ - DragParams: DragParams{ - DragGroupKey: 1, - DropGroupKey: 1, - DragKey: 1, - DropKey: 2, - Position: -1, - }, - TestPlanId: 1, - }, - }, - } - if i.RenderItemMoveStagesForm() != nil { - t.Error("fail") - } -} - -// TestRenderItemMoveStagesFormBehindTarget2 [1,2] [3] move 2 to the behind of 1 -func TestRenderItemMoveStagesFormNoChange2(t *testing.T) { - var bdl *bundle.Bundle - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "GetTestPlanV2Step", func(*bundle.Bundle, uint64) (*apistructs.TestPlanV2Step, error) { - return &apistructs.TestPlanV2Step{ - PreID: 1, - GroupID: 1, - ID: 2, - }, nil - }) - defer monkey.UnpatchAll() - - i := ComponentStageForm{ - ctxBdl: protocol.ContextBundle{}, - CommonStageForm: CommonStageForm{ - State: State{ - DragParams: DragParams{ - DragGroupKey: 1, - DropGroupKey: 1, - DragKey: 2, - DropKey: 1, - Position: 1, - }, - TestPlanId: 1, - }, - }, - } - if i.RenderItemMoveStagesForm() != nil { - t.Error("fail") - } -} - -// TestRenderGroupMoveStagesFormNoChange [1,2] [3] move [1,2] to the front of [3] -func TestRenderGroupMoveStagesFormNoChange(t *testing.T) { - var bdl *bundle.Bundle - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "ListTestPlanV2Step", func(bdl *bundle.Bundle, planID, groupID uint64) ([]*apistructs.TestPlanV2Step, error) { - if groupID == 1 { - return []*apistructs.TestPlanV2Step{ - {ID: 1, PreID: 0}, - {ID: 2, PreID: 1}, - }, nil - } - return []*apistructs.TestPlanV2Step{ - {ID: 3, PreID: 2}, - }, nil - - }) - defer monkey.UnpatchAll() - - i := ComponentStageForm{ - ctxBdl: protocol.ContextBundle{}, - CommonStageForm: CommonStageForm{ - State: State{ - DragParams: DragParams{ - DragGroupKey: 1, - DropGroupKey: 3, - Position: -1, - }, - TestPlanId: 1, - }, - }, - } - if i.RenderGroupMoveStagesForm() != nil { - t.Error("fail") - } -} - -// TestRenderGroupMoveStagesFormNoChange2 [1,2] [3] move [3] to the behind of [1,2] -func TestRenderGroupMoveStagesFormNoChange2(t *testing.T) { - var bdl *bundle.Bundle - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "ListTestPlanV2Step", func(bdl *bundle.Bundle, planID, groupID uint64) ([]*apistructs.TestPlanV2Step, error) { - if groupID == 3 { - return []*apistructs.TestPlanV2Step{ - {ID: 3, PreID: 2}, - }, nil - } - return []*apistructs.TestPlanV2Step{ - {ID: 1, PreID: 0}, - {ID: 2, PreID: 1}, - }, nil - - }) - defer monkey.UnpatchAll() - - i := ComponentStageForm{ - ctxBdl: protocol.ContextBundle{}, - CommonStageForm: CommonStageForm{ - State: State{ - DragParams: DragParams{ - DragGroupKey: 3, - DropGroupKey: 1, - Position: 1, - }, - TestPlanId: 1, - }, - }, - } - if i.RenderGroupMoveStagesForm() != nil { - t.Error("fail") - } -} - -func TestRenderSplitStagesFormWithOneStep(t *testing.T) { - var bdl *bundle.Bundle - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "ListTestPlanV2Step", func(*bundle.Bundle, uint64, uint64) ([]*apistructs.TestPlanV2Step, error) { - return []*apistructs.TestPlanV2Step{ - { - PreID: 0, - PlanID: 1, - GroupID: 1, - ID: 1, - }, - }, nil - }) - defer monkey.UnpatchAll() - - i := ComponentStageForm{ - ctxBdl: protocol.ContextBundle{}, - CommonStageForm: CommonStageForm{ - State: State{ - DragParams: DragParams{ - DragGroupKey: 3, - DropGroupKey: 1, - Position: 1, - }, - TestPlanId: 1, - }, - }, - } - - opsData := OperationInfo{ - OperationBaseInfo: OperationBaseInfo{}, - Meta: OpMetaInfo{ - ID: 1, - Data: map[string]interface{}{ - "groupID": 1, - }, - }, - } - if i.RenderSplitStagesForm(opsData) != nil { - t.Error("fail") - } -} - -func TestRenderSplitStagesFormWithNilStep(t *testing.T) { - var bdl *bundle.Bundle - monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "ListTestPlanV2Step", func(*bundle.Bundle, uint64, uint64) ([]*apistructs.TestPlanV2Step, error) { - return nil, nil - }) - defer monkey.UnpatchAll() - - i := ComponentStageForm{ - ctxBdl: protocol.ContextBundle{}, - CommonStageForm: CommonStageForm{ - State: State{ - DragParams: DragParams{ - DragGroupKey: 3, - DropGroupKey: 1, - Position: 1, - }, - TestPlanId: 1, - }, - }, - } - - opsData := OperationInfo{ - OperationBaseInfo: OperationBaseInfo{}, - Meta: OpMetaInfo{ - ID: 1, - Data: map[string]interface{}{ - "groupID": 1, - }, - }, - } - if i.RenderSplitStagesForm(opsData).Error() != "the groupID is not exists" { - t.Error("fail") - } -}