-
Notifications
You must be signed in to change notification settings - Fork 375
/
render.go
208 lines (190 loc) · 5.73 KB
/
render.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package spaceFormModal
import (
"context"
"encoding/json"
"fmt"
"regexp"
"github.com/erda-project/erda/apistructs"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
spec "github.com/erda-project/erda/modules/openapi/component-protocol/component_spec/form_modal"
"github.com/erda-project/erda/pkg/strutil"
)
type SpaceFormModal struct {
CtxBdl protocol.ContextBundle
Type string `json:"type"`
Props spec.Props `json:"props"`
Operations map[string]interface{} `json:"operations"`
State State `json:"state"`
}
type State struct {
Reload bool `json:"reload"`
Visible bool `json:"visible"`
FormData map[string]interface{} `json:"formData"`
}
type operation struct {
Key string `json:"key"`
Reload bool `json:"reload"`
}
type inParams struct {
ProjectID int64 `json:"projectId"`
}
type AutoTestSpace struct {
ID uint64 `json:"id"`
ProjectID int64 `json:"projectId"`
Name string `json:"name"`
Desc string `json:"desc"`
}
const (
regular = "^[a-z\u4e00-\u9fa5A-Z0-9_-]+( )+(.)*$"
)
func (a *SpaceFormModal) Render(ctx context.Context, c *apistructs.Component, scenario apistructs.ComponentProtocolScenario, event apistructs.ComponentEvent, gs *apistructs.GlobalStateData) error {
a.CtxBdl = ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
a.Props.Fields = []spec.Field{
{
Key: "name",
Label: "空间名",
Component: "input",
Required: true,
ComponentProps: spec.ComponentProps{
MaxLength: 50,
},
Rules: []spec.FieldRule{
{
Pattern: `/^[.a-z\u4e00-\u9fa5A-Z0-9_-\s]*$/`,
Msg: "可输入中文、英文、数字、中划线或下划线",
},
},
},
{
Key: "desc",
Label: "描述",
Component: "textarea",
Required: false,
ComponentProps: spec.ComponentProps{
MaxLength: 1000,
},
},
}
a.Operations["submit"] = operation{
Key: "submit",
Reload: true,
}
if a.CtxBdl.InParams == nil {
return fmt.Errorf("params is empty")
}
inParamsBytes, err := json.Marshal(a.CtxBdl.InParams)
if err != nil {
return fmt.Errorf("failed to marshal inParams, inParams:%+v, err:%v", a.CtxBdl.InParams, err)
}
var inParams inParams
if err := json.Unmarshal(inParamsBytes, &inParams); err != nil {
return err
}
// listen on operation
switch event.Operation {
case apistructs.AutoTestSpaceSubmitOperationKey:
if _, ok := a.State.FormData["id"]; ok {
if err := a.handlerUpdateOperation(a.CtxBdl, c, inParams, event); err != nil {
return err
}
} else {
if err := a.handlerCreateOperation(a.CtxBdl, c, inParams, event); err != nil {
return err
}
}
a.State.Reload = true
a.State.Visible = false
}
return nil
}
func (a *SpaceFormModal) handlerCreateOperation(bdl protocol.ContextBundle, c *apistructs.Component, inParams inParams, event apistructs.ComponentEvent) error {
cond := AutoTestSpace{}
filterCond, ok := c.State["formData"]
if ok {
filterCondS, err := json.Marshal(filterCond)
if err != nil {
return err
}
if err := json.Unmarshal(filterCondS, &cond); err != nil {
return err
}
}
if err := strutil.Validate(cond.Desc, strutil.MaxRuneCountValidator(1000)); err != nil {
return err
}
if err := strutil.Validate(cond.Name, strutil.MaxRuneCountValidator(50)); err != nil {
return err
}
reg := regexp.MustCompile("^[a-zA-Z0-9_.|\\-|\\s|\u4e00-\u9fa5]+$")
if reg == nil { //解释失败,返回nil
return fmt.Errorf("regexp err")
}
if !reg.MatchString(cond.Name) {
return fmt.Errorf("请输入中文、英文、数字、中划线或下划线")
}
err := bdl.Bdl.CreateTestSpace(cond.Name, inParams.ProjectID, cond.Desc, bdl.Identity.UserID)
if err != nil {
return err
}
return nil
}
func (a *SpaceFormModal) handlerUpdateOperation(bdl protocol.ContextBundle, c *apistructs.Component, inParams inParams, event apistructs.ComponentEvent) error {
cond := AutoTestSpace{}
filterCond, ok := c.State["formData"]
if ok {
filterCondS, err := json.Marshal(filterCond)
if err != nil {
return err
}
if err := json.Unmarshal(filterCondS, &cond); err != nil {
return err
}
}
if err := strutil.Validate(cond.Desc, strutil.MaxRuneCountValidator(1000)); err != nil {
return err
}
if err := strutil.Validate(cond.Name, strutil.MaxRuneCountValidator(50)); err != nil {
return err
}
reg := regexp.MustCompile("^[a-zA-Z0-9_.|\\-|\\s|\u4e00-\u9fa5]+$")
if reg == nil { //解释失败,返回nil
return fmt.Errorf("regexp err")
}
if !reg.MatchString(cond.Name) {
return fmt.Errorf("请输入中文、英文、数字、中划线或下划线")
}
res, err := bdl.Bdl.GetTestSpace(cond.ID)
if err != nil {
return err
}
if res.Status != apistructs.TestSpaceOpen {
return fmt.Errorf("当前状态不允许编辑")
}
err = bdl.Bdl.UpdateTestSpace(cond.Name, cond.ID, cond.Desc, bdl.Identity.UserID)
if err != nil {
return err
}
return nil
}
func RenderCreator() protocol.CompRender {
return &SpaceFormModal{
CtxBdl: protocol.ContextBundle{},
Type: "",
Operations: map[string]interface{}{},
State: State{
Reload: false,
},
}
}