Skip to content

Commit

Permalink
Feature/autotest step copy paste (#2481)
Browse files Browse the repository at this point in the history
* autotest step json copy

* paste autotest step by json text
  • Loading branch information
chengjoey committed Oct 22, 2021
1 parent ed4636e commit 83868e0
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 13 deletions.
11 changes: 11 additions & 0 deletions apistructs/autotest_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package apistructs

import (
"encoding/json"
"strconv"
"time"
)
Expand Down Expand Up @@ -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"`
Expand Down
17 changes: 17 additions & 0 deletions apistructs/autotest_scene_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
14 changes: 8 additions & 6 deletions apistructs/component_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -200,6 +201,7 @@ const (
ExecuteClickRowNoOperationKey OperationKey = "clickRow"
ExecuteAddApiOperationKey OperationKey = "addApi"
ExecuteTaskBreadcrumbSelectItem OperationKey = "selectItem"
ExecuteSubmitCopyOperationKey OperationKey = "submitCopy"

//org-list
FilterOrgsOperationKey OperationKey = "filter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
},
},
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
// 前端操作是否需要触发后端
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ hierarchy:
- configSheetDrawer
- customScriptDrawer
- nestedSceneDrawer
- addCopyApiFormModal
fileExecute:
- executeHead
- executeInfo
Expand Down Expand Up @@ -247,6 +248,8 @@ components:
type: Button
addApiButton:
type: Button
addCopyApiFormModal:
type: FormModal
addCustomScriptButton:
type: Button
waitEditorDrawer:
Expand Down Expand Up @@ -376,6 +379,10 @@ rendering:
state:
- name: "sceneId"
value: "{{ fileTree.sceneId }}"
- name: addCopyApiFormModal
state:
- name: "sceneId"
value: "{{ fileTree.sceneId }}"
- name: addConfigSheetButton
state:
- name: "sceneId"
Expand Down Expand Up @@ -476,6 +483,10 @@ rendering:
state:
- name: "sceneId"
value: "{{ fileTree.sceneId }}"
- name: addCopyApiFormModal
state:
- name: "sceneId"
value: "{{ fileTree.sceneId }}"
- name: addConfigSheetButton
state:
- name: "sceneId"
Expand Down Expand Up @@ -575,6 +586,9 @@ rendering:
state:
- name: "stepId"
value: "{{ addApiButton.createStepID }}"
addCopyApiFormModal:
- name: fileInfo
- name: stages
addConfigSheetButton:
- name: fileInfo
- name: stages
Expand Down Expand Up @@ -950,6 +964,10 @@ rendering:
state:
- name: "sceneId"
value: "{{ fileTree.sceneId }}"
- name: addCopyApiFormModal
state:
- name: "sceneId"
value: "{{ fileTree.sceneId }}"
- name: addConfigSheetButton
state:
- name: "sceneId"
Expand Down

0 comments on commit 83868e0

Please sign in to comment.