diff --git a/apistructs/autotest_scene.go b/apistructs/autotest_scene.go index 1eb6436808e..29847d9923d 100644 --- a/apistructs/autotest_scene.go +++ b/apistructs/autotest_scene.go @@ -15,6 +15,7 @@ package apistructs import ( + "encoding/json" "strconv" "time" ) @@ -102,6 +103,16 @@ type AutoTestSceneStep struct { APISpecID uint64 `json:"apiSpecID"` // api集市id } +func (a *AutoTestSceneStep) ToJsonCopyText() string { + dat := map[string]interface{}{ + "type": a.Type, + "method": a.Method, + "value": a.Value, + } + b, _ := json.MarshalIndent(dat, "", "\t") + return string(b) +} + type AutotestSceneRequest struct { AutoTestSceneParams Name string `json:"name,omitempty"` diff --git a/apistructs/autotest_scene_test.go b/apistructs/autotest_scene_test.go index a9c8ba5fe88..8eeca71a3ec 100644 --- a/apistructs/autotest_scene_test.go +++ b/apistructs/autotest_scene_test.go @@ -40,3 +40,20 @@ func TestAutoTestRunWait(t *testing.T) { assert.Equal(t, 2, jsonWait.WaitTimeSec) assert.Equal(t, 2, envWait.WaitTimeSec) } + +func TestToJsonCopyText(t *testing.T) { + jsonStr := `{ + "method": "", + "type": "API", + "value": "{\"apiSpec\":{\"asserts\":[],\"body\":{\"content\":null,\"type\":\"\"},\"headers\":null,\"id\":\"\",\"method\":\"GET\",\"name\":\"a\",\"out_params\":[],\"params\":[{\"key\":\"d\",\"value\":\"${{ params.a }}\"}],\"url\":\"www.xxx.com?d=${{ params.a }}\"},\"loop\":null}" +}` + step := AutoTestSceneStep{ + SpaceID: 1, + SceneID: 1, + CreatorID: "123", + Type: StepTypeAPI, + Value: "{\"apiSpec\":{\"asserts\":[],\"body\":{\"content\":null,\"type\":\"\"},\"headers\":null,\"id\":\"\",\"method\":\"GET\",\"name\":\"a\",\"out_params\":[],\"params\":[{\"key\":\"d\",\"value\":\"${{ params.a }}\"}],\"url\":\"www.xxx.com?d=${{ params.a }}\"},\"loop\":null}", + } + + assert.Equal(t, jsonStr, step.ToJsonCopyText()) +} diff --git a/apistructs/component_protocol.go b/apistructs/component_protocol.go index 5a84e2e302c..ead9313ecb0 100644 --- a/apistructs/component_protocol.go +++ b/apistructs/component_protocol.go @@ -161,12 +161,13 @@ const ( // autotest scene output AutoTestSceneOutputUpdateOperationKey OperationKey = "save" // autotest scene step - AutoTestSceneStepCreateOperationKey OperationKey = "addParallelAPI" - AutoTestSceneStepCopyOperationKey OperationKey = "copyParallelAPI" - AutoTestSceneStepMoveItemOperationKey OperationKey = "moveItem" - AutoTestSceneStepMoveGroupOperationKey OperationKey = "moveGroup" - AutoTestSceneStepDeleteOperationKey OperationKey = "deleteAPI" - AutoTestSceneStepSplitOperationKey OperationKey = "standalone" + AutoTestSceneStepCreateOperationKey OperationKey = "addParallelAPI" + AutoTestSceneStepCopyOperationKey OperationKey = "copyParallelAPI" + AutoTestSceneStepCopyAsJsonOperationKey OperationKey = "copyAsJson" + AutoTestSceneStepMoveItemOperationKey OperationKey = "moveItem" + AutoTestSceneStepMoveGroupOperationKey OperationKey = "moveGroup" + AutoTestSceneStepDeleteOperationKey OperationKey = "deleteAPI" + AutoTestSceneStepSplitOperationKey OperationKey = "standalone" //auto-test scene set ListSceneSetOperationKey OperationKey = "ListSceneSet" @@ -199,6 +200,7 @@ const ( ExecuteClickRowNoOperationKey OperationKey = "clickRow" ExecuteAddApiOperationKey OperationKey = "addApi" ExecuteTaskBreadcrumbSelectItem OperationKey = "selectItem" + ExecuteSubmitCopyOperationKey OperationKey = "submitCopy" //org-list FilterOrgsOperationKey OperationKey = "filter" diff --git a/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/addApiButton/render.go b/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/addApiButton/render.go index a09de96ac18..bcd0e7d2794 100644 --- a/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/addApiButton/render.go +++ b/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/addApiButton/render.go @@ -59,11 +59,34 @@ func (ca *ComponentAction) Render(ctx context.Context, c *apistructs.Component, c.Type = "Button" c.Props = map[string]interface{}{ "text": "+ 接口", - } - c.Operations = map[string]interface{}{ - "click": map[string]interface{}{ - "key": "addApi", - "reload": true, + "menu": []interface{}{ + map[string]interface{}{ + "key": "addApi", + "operations": map[string]interface{}{ + "click": map[string]interface{}{ + "key": "addApi", + "reload": true, + }, + }, + "text": "表单添加", + }, + map[string]interface{}{ + "key": "addByCopyText", + "text": "粘贴文本添加", + "operations": map[string]interface{}{ + "click": map[string]interface{}{ + "key": "addByCopyText", + "reload": false, + "command": map[string]interface{}{ + "key": "set", + "target": "addCopyApiFormModal", + "state": map[string]interface{}{ + "visible": true, + }, + }, + }, + }, + }, }, } } diff --git a/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/addCopyApiFormModal/render.go b/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/addCopyApiFormModal/render.go new file mode 100644 index 00000000000..c6b5875451d --- /dev/null +++ b/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/addCopyApiFormModal/render.go @@ -0,0 +1,118 @@ +// 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 addCopyApiFormModal + +import ( + "context" + "encoding/json" + + "github.com/erda-project/erda/apistructs" + protocol "github.com/erda-project/erda/modules/openapi/component-protocol" +) + +type ComponentAction struct{} + +type State struct { + SceneID uint64 `json:"sceneId"` + Visible bool `json:"visible"` + FormData struct { + ApiText string `json:"apiText"` + } + CreateStepID uint64 `json:"createStepID"` +} + +func (ca *ComponentAction) Render(ctx context.Context, c *apistructs.Component, scenario apistructs.ComponentProtocolScenario, event apistructs.ComponentEvent, gs *apistructs.GlobalStateData) error { + bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle) + v, err := json.Marshal(c.State) + if err != nil { + return err + } + var state State + if err := json.Unmarshal(v, &state); err != nil { + return err + } + switch event.Operation { + case apistructs.ExecuteSubmitCopyOperationKey: + copyText := state.FormData.ApiText + scene, err := bdl.Bdl.GetAutoTestScene(apistructs.AutotestSceneRequest{ + IdentityInfo: apistructs.IdentityInfo{ + UserID: bdl.Identity.UserID, + }, + AutoTestSceneParams: apistructs.AutoTestSceneParams{ + ID: state.SceneID, + }, + Target: -1, + GroupID: -1, + Type: apistructs.StepTypeAPI, + SceneID: state.SceneID, + }) + if err != nil { + return err + } + var req apistructs.AutotestSceneRequest + if err := json.Unmarshal([]byte(copyText), &req); err != nil { + return err + } + req.ID = 0 + req.Target = -1 + req.GroupID = -1 + req.SceneID = scene.ID + req.SpaceID = scene.SpaceID + req.UserID = bdl.Identity.UserID + req.CreatorID = bdl.Identity.UserID + req.UpdaterID = bdl.Identity.UserID + req.APISpecID = 0 + req.RefSetID = 0 + req.PreType = "" + req.Position = 0 + req.IsGroup = false + stepID, err := bdl.Bdl.CreateAutoTestSceneStep(req) + if err != nil { + return err + } + c.State["createStepID"] = stepID + c.State["visible"] = false + case apistructs.InitializeOperation, apistructs.RenderingOperation: + c.Props = map[string]interface{}{ + "width": 850, + "title": "按文本添加", + "fields": []interface{}{ + map[string]interface{}{ + "component": "textarea", + "componentProps": map[string]interface{}{ + "autoSize": map[string]interface{}{ + "minRows": 8, + "maxRows": 15, + }, + }, + "key": "apiText", + "label": "API文本", + "required": true, + }, + }, + } + c.Operations = map[string]interface{}{ + "submit": map[string]interface{}{ + "key": "submitCopy", + "reload": true, + }, + } + } + return nil +} + +func RenderCreator() protocol.CompRender { + return &ComponentAction{} +} diff --git a/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/stages/model.go b/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/stages/model.go index 30d06e3072c..a8a36f36046 100644 --- a/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/stages/model.go +++ b/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/stages/model.go @@ -80,8 +80,10 @@ type OperationBaseInfo struct { Icon string `json:"icon"` HoverTip string `json:"hoverTip"` HoverShow bool `json:"hoverShow"` + Group string `json:"group"` // 操作展示名称 - Text string `json:"text"` + Text string `json:"text"` + CopyText string `json:"copyText"` // 确认提示 Confirm string `json:"confirm,omitempty"` // 前端操作是否需要触发后端 diff --git a/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/stages/stages.go b/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/stages/stages.go index d20943aba09..61679f8e069 100644 --- a/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/stages/stages.go +++ b/modules/openapi/component-protocol/scenarios/auto-test-scenes/components/stages/stages.go @@ -85,11 +85,13 @@ func RenderStage(groupID uint64, step apistructs.AutoTestSceneStep) (StageData, o := CreateOperation{} o.Key = apistructs.AutoTestSceneStepCopyOperationKey.String() o.Icon = "fz1" - o.HoverTip = "复制接口" + //o.HoverTip = "复制接口" + o.Text = "复制接口" o.Disabled = false o.Reload = true o.HoverShow = true o.Meta.ID = step.ID + o.Group = "copy" pd.Operations["copy"] = o o2 := CreateOperation{} @@ -101,6 +103,18 @@ func RenderStage(groupID uint64, step apistructs.AutoTestSceneStep) (StageData, o2.HoverShow = true o2.Meta.ID = step.ID pd.Operations["add"] = o2 + + o3 := CreateOperation{} + o3.Key = apistructs.AutoTestSceneStepCopyAsJsonOperationKey.String() + o3.Icon = "fz1" + o3.Reload = false + o3.Text = "复制Json格式" + o3.Key = "copyAsJson" + o3.Disabled = false + o3.Group = "copy" + o3.Meta.ID = step.ID + o3.CopyText = step.ToJsonCopyText() + pd.Operations["copyAsJson"] = o3 } os := OperationInfo{ diff --git a/modules/openapi/component-protocol/scenarios/auto-test-scenes/protocol.yml b/modules/openapi/component-protocol/scenarios/auto-test-scenes/protocol.yml index 073c36c1218..b195e6603c1 100644 --- a/modules/openapi/component-protocol/scenarios/auto-test-scenes/protocol.yml +++ b/modules/openapi/component-protocol/scenarios/auto-test-scenes/protocol.yml @@ -53,6 +53,7 @@ hierarchy: - configSheetDrawer - customScriptDrawer - nestedSceneDrawer + - addCopyApiFormModal fileExecute: - executeHead - executeInfo @@ -238,6 +239,8 @@ components: type: Button addApiButton: type: Button + addCopyApiFormModal: + type: FormModal addCustomScriptButton: type: Button waitEditorDrawer: @@ -367,6 +370,10 @@ rendering: state: - name: "sceneId" value: "{{ fileTree.sceneId }}" + - name: addCopyApiFormModal + state: + - name: "sceneId" + value: "{{ fileTree.sceneId }}" - name: addConfigSheetButton state: - name: "sceneId" @@ -467,6 +474,10 @@ rendering: state: - name: "sceneId" value: "{{ fileTree.sceneId }}" + - name: addCopyApiFormModal + state: + - name: "sceneId" + value: "{{ fileTree.sceneId }}" - name: addConfigSheetButton state: - name: "sceneId" @@ -566,6 +577,9 @@ rendering: state: - name: "stepId" value: "{{ addApiButton.createStepID }}" + addCopyApiFormModal: + - name: fileInfo + - name: stages addConfigSheetButton: - name: fileInfo - name: stages @@ -940,6 +954,10 @@ rendering: state: - name: "sceneId" value: "{{ fileTree.sceneId }}" + - name: addCopyApiFormModal + state: + - name: "sceneId" + value: "{{ fileTree.sceneId }}" - name: addConfigSheetButton state: - name: "sceneId"