Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature/autotest step copy paste #2481

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -199,6 +200,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 @@ -53,6 +53,7 @@ hierarchy:
- configSheetDrawer
- customScriptDrawer
- nestedSceneDrawer
- addCopyApiFormModal
fileExecute:
- executeHead
- executeInfo
Expand Down Expand Up @@ -238,6 +239,8 @@ components:
type: Button
addApiButton:
type: Button
addCopyApiFormModal:
type: FormModal
addCustomScriptButton:
type: Button
waitEditorDrawer:
Expand Down Expand Up @@ -367,6 +370,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 @@ -467,6 +474,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 @@ -566,6 +577,9 @@ rendering:
state:
- name: "stepId"
value: "{{ addApiButton.createStepID }}"
addCopyApiFormModal:
- name: fileInfo
- name: stages
addConfigSheetButton:
- name: fileInfo
- name: stages
Expand Down Expand Up @@ -940,6 +954,10 @@ rendering:
state:
- name: "sceneId"
value: "{{ fileTree.sceneId }}"
- name: addCopyApiFormModal
state:
- name: "sceneId"
value: "{{ fileTree.sceneId }}"
- name: addConfigSheetButton
state:
- name: "sceneId"
Expand Down