Skip to content

Commit

Permalink
paste autotest step by json text
Browse files Browse the repository at this point in the history
  • Loading branch information
chengjoey committed Oct 22, 2021
1 parent 8704229 commit edef4df
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 6 deletions.
7 changes: 6 additions & 1 deletion apistructs/autotest_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ type AutoTestSceneStep struct {
}

func (a *AutoTestSceneStep) ToJsonCopyText() string {
b, _ := json.Marshal(a)
dat := map[string]interface{}{
"type": a.Type,
"method": a.Method,
"value": a.Value,
}
b, _ := json.MarshalIndent(dat, "", "\t")
return string(b)
}

Expand Down
1 change: 1 addition & 0 deletions apistructs/component_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,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 @@ -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

0 comments on commit edef4df

Please # to comment.