/
autotest.go
296 lines (249 loc) · 8.03 KB
/
autotest.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// 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 apistructs
import (
"fmt"
"time"
"github.com/erda-project/erda/pkg/strutil"
)
const (
AutoTestFileTreeNodeMetaKeyPipelineYml = "pipelineYml"
AutoTestFileTreeNodeMetaKeyHistoryID = "historyID"
AutoTestFileTreeNodeMetaKeySnippetAction = "snippetAction"
AutoTestFileTreeNodeMetaKeyRunParams = "runParams"
AutoTestFileTreeNodeMetaKeyExtra = "extra"
)
type AutoTestFileTreeNode struct {
Type UnifiedFileTreeNodeType
Scope string
ScopeID string
Pinode string `gorm:"type:bigint(20)"` // root dir 的 pinode 为 "0",表示无 pinode
Inode string `gorm:"type:bigint(20)"`
Name string
Desc string
CreatorID string
UpdaterID string
}
type AutoTestNodeMetaSnippetObj struct {
Alias string `json:"alias"`
Params map[string]interface{} `json:"params"`
}
type AutoTestsScope string
var (
AutoTestsScopeProject AutoTestsScope = "project"
AutoTestsScopeProjectTestPlan AutoTestsScope = "project-testplan" // 测试计划单独的目录树
)
type AutoTestCaseSavePipelineRequest struct {
Inode string `json:"inode"`
PipelineYml string `json:"pipelineYml"`
RunParams []PipelineRunParam `json:"runParams"`
IdentityInfo
}
type AutoTestCaseSavePipelineResponse struct {
Header
Data *UnifiedFileTreeNode `json:"data,omitempty"`
IdentityInfo
}
func (req AutoTestCaseSavePipelineRequest) BasicValidate() error {
if err := strutil.Validate(req.Inode, strutil.MinLenValidator(1)); err != nil {
return fmt.Errorf("invalid inode: %v", err)
}
if req.PipelineYml == "" && req.RunParams == nil {
return fmt.Errorf("nothing to save, please specify pipelineYml or runParams")
}
return nil
}
type AutoTestGlobalConfigType string
var (
AutoTestGlobalConfigTypeAPI AutoTestGlobalConfigType = "API"
AutoTestGlobalConfigTypeUI AutoTestGlobalConfigType = "UI"
)
type AutoTestGlobalConfigCreateRequest struct {
Scope string `json:"scope"`
ScopeID string `json:"scopeID"`
DisplayName string `json:"displayName"`
Desc string `json:"desc"`
APIConfig *AutoTestAPIConfig `json:"apiConfig,omitempty"`
UIConfig *AutoTestUIConfig `json:"uiConfig,omitempty"`
IdentityInfo
}
type AutoTestGlobalConfigCreateResponse struct {
Header
Data *AutoTestGlobalConfig `json:"data,omitempty"`
}
func (req AutoTestGlobalConfigCreateRequest) BasicValidate() error {
// scope
if err := strutil.Validate(req.Scope, strutil.MinLenValidator(1)); err != nil {
return fmt.Errorf("invalid scope: %v", err)
}
// scope id
if err := strutil.Validate(req.ScopeID, strutil.MinLenValidator(1)); err != nil {
return fmt.Errorf("invalid scopeID: %v", err)
}
// display name
if err := strutil.Validate(req.DisplayName, strutil.MinLenValidator(1), strutil.MaxLenValidator(191)); err != nil {
return fmt.Errorf("invalid displayname: %v", err)
}
// desc
if err := strutil.Validate(req.Desc, strutil.MaxLenValidator(512)); err != nil {
return fmt.Errorf("invalid desc: %v", err)
}
// configs
hasAtLeastOneTypeConfig := false
// api config
if req.APIConfig != nil {
hasAtLeastOneTypeConfig = true
if err := req.APIConfig.BasicValidate(); err != nil {
return fmt.Errorf("invalid apiConfig: %v", err)
}
}
// ui config
if req.UIConfig != nil {
hasAtLeastOneTypeConfig = true
if err := req.UIConfig.BasicValidate(); err != nil {
return fmt.Errorf("invalid uiConfig: %v", err)
}
}
// doesn't have any configs declared
if !hasAtLeastOneTypeConfig {
return fmt.Errorf("empty configs")
}
return nil
}
type AutoTestGlobalConfig struct {
Scope string `json:"scope"`
ScopeID string `json:"scopeID"`
Ns string `json:"ns"`
DisplayName string `json:"displayName,omitempty"`
Desc string `json:"desc,omitempty"`
CreatorID string `json:"creatorID"`
UpdaterID string `json:"updaterID"`
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
APIConfig *AutoTestAPIConfig `json:"apiConfig,omitempty"`
UIConfig *AutoTestUIConfig `json:"uiConfig,omitempty"`
}
type SortByUpdateTimeAutoTestGlobalConfigs []AutoTestGlobalConfig
func (p SortByUpdateTimeAutoTestGlobalConfigs) Len() int {
return len(p)
}
func (p SortByUpdateTimeAutoTestGlobalConfigs) Less(i, j int) bool {
return p[i].UpdatedAt.After(p[j].UpdatedAt)
}
func (p SortByUpdateTimeAutoTestGlobalConfigs) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (cfg AutoTestGlobalConfig) GetUserIDs() []string {
return strutil.DedupSlice([]string{cfg.CreatorID, cfg.UpdaterID}, true)
}
type AutoTestAPIConfig struct {
Domain string `json:"domain"`
Header map[string]string `json:"header"`
Global map[string]AutoTestConfigItem `json:"global"`
}
func (cfg AutoTestAPIConfig) BasicValidate() error {
// domain 格式校验
if cfg.Domain != "" {
if !strutil.HasPrefixes(cfg.Domain, "http://", "https://") {
return fmt.Errorf("invalid domain protocol")
}
}
// global
for key, item := range cfg.Global {
if item.Type == "" {
return fmt.Errorf("invalid type of global key: %s", key)
}
}
return nil
}
type AutoTestConfigItem struct {
Name string `json:"name"`
Type string `json:"type"`
Value string `json:"value"`
Desc string `json:"desc,omitempty"`
}
type AutoTestUIConfig struct {
}
func (cfg AutoTestUIConfig) BasicValidate() error {
return nil
}
type AutoTestGlobalConfigUpdateRequest struct {
PipelineCmsNs string `json:"ns"`
DisplayName string `json:"displayName"`
Desc string `json:"desc"`
APIConfig *AutoTestAPIConfig `json:"apiConfig,omitempty"`
UIConfig *AutoTestUIConfig `json:"uiConfig,omitempty"`
IdentityInfo
}
type AutoTestGlobalConfigUpdateResponse struct {
Header
Data *AutoTestAPIConfig `json:"data,omitempty"`
}
func (req AutoTestGlobalConfigUpdateRequest) BasicValidate() error {
// ns
if err := strutil.Validate(req.PipelineCmsNs, strutil.MinLenValidator(1)); err != nil {
return fmt.Errorf("invalid pipeline cms ns, err: %v", err)
}
// display name
if err := strutil.Validate(req.DisplayName, strutil.MaxLenValidator(191)); err != nil {
return fmt.Errorf("invalid displayname: %v", err)
}
// desc
if err := strutil.Validate(req.Desc, strutil.MaxLenValidator(512)); err != nil {
return fmt.Errorf("invalid desc: %v", err)
}
if req.APIConfig != nil {
if err := req.APIConfig.BasicValidate(); err != nil {
return fmt.Errorf("invalid apiConfig: %v", err)
}
}
if req.UIConfig != nil {
if err := req.UIConfig.BasicValidate(); err != nil {
return fmt.Errorf("invalid uiConfig: %v", err)
}
}
return nil
}
type AutoTestGlobalConfigDeleteRequest struct {
PipelineCmsNs string `json:"ns"`
IdentityInfo
}
type AutoTestGlobalConfigDeleteResponse struct {
Header
Data *AutoTestAPIConfig `json:"data,omitempty"`
}
func (req AutoTestGlobalConfigDeleteRequest) BasicValidate() error {
if err := strutil.Validate(req.PipelineCmsNs, strutil.MinLenValidator(1)); err != nil {
return fmt.Errorf("invalid ns: %v", err)
}
return nil
}
type AutoTestGlobalConfigListRequest struct {
Scope string `json:"scope"`
ScopeID string `json:"scopeID"`
IdentityInfo
}
type AutoTestGlobalConfigListResponse struct {
Header
Data []AutoTestGlobalConfig `json:"data,omitempty"`
}
func (req AutoTestGlobalConfigListRequest) BasicValidate() error {
if err := strutil.Validate(req.Scope, strutil.MinLenValidator(1)); err != nil {
return fmt.Errorf("invalid scope: %v", err)
}
if err := strutil.Validate(req.ScopeID, strutil.MinLenValidator(1)); err != nil {
return fmt.Errorf("invalid scopeID: %v", err)
}
return nil
}