-
Notifications
You must be signed in to change notification settings - Fork 4
/
workflows_utils.go
118 lines (101 loc) · 2.79 KB
/
workflows_utils.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
package workflow_handler
import (
"encoding/json"
"fmt"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/rookout/piper/pkg/conf"
"github.com/rookout/piper/pkg/git_provider"
"github.com/rookout/piper/pkg/utils"
"gopkg.in/yaml.v3"
"log"
"regexp"
"strings"
)
func CreateDAGTemplate(fileList []*git_provider.CommitFile, name string) (*v1alpha1.Template, error) {
if len(fileList) == 0 {
log.Printf("empty file list for %s", name)
return nil, nil
}
DAGs := make([]v1alpha1.DAGTask, 0)
for _, file := range fileList {
if file.Content == nil || file.Path == nil {
return nil, fmt.Errorf("missing content or path for %s", name)
}
DAGTask := make([]v1alpha1.DAGTask, 0)
jsonBytes, err := utils.ConvertYAMLListToJSONList(*file.Content)
if err != nil {
return nil, err
}
err = json.Unmarshal(jsonBytes, &DAGTask)
if err != nil {
return nil, err
}
err = ValidateDAGTasks(DAGTask)
if err != nil {
return nil, err
}
DAGs = append(DAGs, DAGTask...)
}
if len(DAGs) == 0 {
return nil, fmt.Errorf("no tasks for %s", name)
}
template := &v1alpha1.Template{
Name: name,
DAG: &v1alpha1.DAGTemplate{
Tasks: DAGs,
},
}
return template, nil
}
func AddFilesToTemplates(templates []v1alpha1.Template, files []*git_provider.CommitFile) ([]v1alpha1.Template, error) {
for _, f := range files {
t := make([]v1alpha1.Template, 0)
jsonBytes, err := utils.ConvertYAMLListToJSONList(*f.Content)
if err != nil {
return nil, err
}
err = json.Unmarshal(jsonBytes, &t)
if err != nil {
return nil, err
}
templates = append(templates, t...)
}
return templates, nil
}
func GetParameters(paramsFile *git_provider.CommitFile) ([]v1alpha1.Parameter, error) {
var params []v1alpha1.Parameter
err := yaml.Unmarshal([]byte(*paramsFile.Content), ¶ms)
if err != nil {
return nil, err
}
return params, nil
}
func IsConfigExists(cfg *conf.WorkflowsConfig, config string) bool {
_, ok := cfg.Configs[config]
return ok
}
func IsConfigsOnExitExists(cfg *conf.WorkflowsConfig, config string) bool {
return len(cfg.Configs[config].OnExit) != 0
}
func ValidateDAGTasks(tasks []v1alpha1.DAGTask) error {
for _, task := range tasks {
if task.Name == "" {
return fmt.Errorf("task name cannot be empty: %+v\n", task)
}
if task.Template == "" && task.TemplateRef == nil {
return fmt.Errorf("task template or templateRef cannot be empty: %+v\n", task)
}
}
return nil
}
func ConvertToValidString(input string) string {
// Convert to lowercase
lowercase := strings.ToLower(input)
// Replace underscores with hyphens
converted := strings.ReplaceAll(lowercase, "_", "-")
// Remove symbols except . and -
pattern := `[^a-z0-9.\-]`
re := regexp.MustCompile(pattern)
validString := re.ReplaceAllString(converted, "")
return validString
}