8000 fix: use specific struct for ascode conditions (#4959) · ovh/cds@b209414 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit b209414

Browse files
authored
fix: use specific struct for ascode conditions (#4959)
1 parent 99c0d33 commit b209414

File tree

3 files changed

+61
-30
lines changed

3 files changed

+61
-30
lines changed

sdk/exportentities/workflow.go

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ type Workflow struct {
3131
Workflow map[string]NodeEntry `json:"workflow,omitempty" yaml:"workflow,omitempty" jsonschema_description:"Workflow nodes list."`
3232
Hooks map[string][]HookEntry `json:"hooks,omitempty" yaml:"hooks,omitempty" jsonschema_description:"Workflow hooks list."`
3333
// this will be filled for simple workflows
34-
OneAtATime *bool `json:"one_at_a_time,omitempty" yaml:"one_at_a_time,omitempty" jsonschema_description:"Set to true if you want to limit the execution of this node to one at a time."`
35-
Conditions *sdk.WorkflowNodeConditions `json:"conditions,omitempty" yaml:"conditions,omitempty" jsonschema_description:"Conditions to run this node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/run-conditions."`
36-
When []string `json:"when,omitempty" yaml:"when,omitempty" jsonschema_description:"Set manual and status condition (ex: 'success')."` //This is used only for manual and success condition
37-
PipelineName string `json:"pipeline,omitempty" yaml:"pipeline,omitempty" jsonschema_description:"The name of a pipeline used for pipeline node."`
38-
Payload map[string]interface{} `json:"payload,omitempty" yaml:"payload,omitempty"`
39-
Parameters map[string]string `json:"parameters,omitempty" yaml:"parameters,omitempty" jsonschema_description:"List of parameters for the workflow."`
40-
ApplicationName string `json:"application,omitempty" yaml:"application,omitempty" jsonschema_description:"The application to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
41-
EnvironmentName string `json:"environment,omitempty" yaml:"environment,omitempty" jsonschema_description:"The environment to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
42-
ProjectIntegrationName string `json:"integration,omitempty" yaml:"integration,omitempty" jsonschema_description:"The integration to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
43-
PipelineHooks []HookEntry `json:"pipeline_hooks,omitempty" yaml:"pipeline_hooks,omitempty"`
34+
OneAtATime *bool `json:"one_at_a_time,omitempty" yaml:"one_at_a_time,omitempty" jsonschema_description:"Set to true if you want to limit the execution of this node to one at a time."`
35+
Conditions *ConditionEntry `json:"conditions,omitempty" yaml:"conditions,omitempty" jsonschema_description:"Conditions to run this node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/run-conditions."`
36+
When []string `json:"when,omitempty" yaml:"when,omitempty" jsonschema_description:"Set manual and status condition (ex: 'success')."` //This is used only for manual and success condition
37+
PipelineName string `json:"pipeline,omitempty" yaml:"pipeline,omitempty" jsonschema_description:"The name of a pipeline used for pipeline node."`
38+
Payload map[string]interface{} `json:"payload,omitempty" yaml:"payload,omitempty"`
39+
Parameters map[string]string `json:"parameters,omitempty" yaml:"parameters,omitempty" jsonschema_description:"List of parameters for the workflow."`
40+
ApplicationName string `json:"application,omitempty" yaml:"application,omitempty" jsonschema_description:"The application to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
41+
EnvironmentName string `json:"environment,omitempty" yaml:"environment,omitempty" jsonschema_description:"The environment to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
42+
ProjectIntegrationName string `json:"integration,omitempty" yaml:"integration,omitempty" jsonschema_description:"The integration to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
43+
PipelineHooks []HookEntry `json:"pipeline_hooks,omitempty" yaml:"pipeline_hooks,omitempty"`
4444
// extra workflow data
4545
Permissions map[string]int `json:"permissions,omitempty" yaml:"permissions,omitempty" jsonschema_description:"The permissions for the workflow (ex: myGroup: 7).\nhttps://ovh.github.io/cds/docs/concepts/permissions"`
4646
Metadata map[string]string `json:"metadata,omitempty" yaml:"metadata,omitempty"`
@@ -66,20 +66,32 @@ type WorkflowPulledItem struct {
6666

6767
// NodeEntry represents a node as code
6868
type NodeEntry struct {
69-
ID int64 `json:"-" yaml:"-"`
70-
DependsOn []string `json:"depends_on,omitempty" yaml:"depends_on,omitempty" jsonschema_description:"Names of the parent nodes, can be pipelines, forks or joins."`
71-
Conditions *sdk.WorkflowNodeConditions `json:"conditions,omitempty" yaml:"conditions,omitempty" jsonschema_description:"Conditions to run this node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/run-conditions."`
72-
When []string `json:"when,omitempty" yaml:"when,omitempty" jsonschema_description:"Set manual and status condition (ex: 'success')."` //This is used only for manual and success condition
73-
PipelineName string `json:"pipeline,omitempty" yaml:"pipeline,omitempty" jsonschema_description:"The name of a pipeline used for pipeline node."`
74-
ApplicationName string `json:"application,omitempty" yaml:"application,omitempty" jsonschema_description:"The application to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
75-
EnvironmentName string `json:"environment,omitempty" yaml:"environment,omitempty" jsonschema_description:"The environment to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
76-
ProjectIntegrationName string `json:"integration,omitempty" yaml:"integration,omitempty" jsonschema_description:"The integration to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
77-
OneAtATime *bool `json:"one_at_a_time,omitempty" yaml:"one_at_a_time,omitempty" jsonschema_description:"Set to true if you want to limit the execution of this node to one at a time."`
78-
Payload map[string]interface{} `json:"payload,omitempty" yaml:"payload,omitempty"`
79-
Parameters map[string]string `json:"parameters,omitempty" yaml:"parameters,omitempty" jsonschema_description:"List of parameters for the workflow."`
80-
OutgoingHookModelName string `json:"trigger,omitempty" yaml:"trigger,omitempty"`
81-
OutgoingHookConfig map[string]string `json:"config,omitempty" yaml:"config,omitempty"`
82-
Permissions map[string]int `json:"permissions,omitempty" yaml:"permissions,omitempty" jsonschema_description:"The permissions for the node (ex: myGroup: 7).\nhttps://ovh.github.io/cds/docs/concepts/permissions"`
69+
ID int64 `json:"-" yaml:"-"`
70+
DependsOn []string `json:"depends_on,omitempty" yaml:"depends_on,omitempty" jsonschema_description:"Names of the parent nodes, can be pipelines, forks or joins."`
71+
Conditions *ConditionEntry `json:"conditions,omitempty" yaml:"conditions,omitempty" jsonschema_description:"Conditions to run this node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/run-conditions."`
72+
When []string `json:"when,omitempty" yaml:"when,omitempty" jsonschema_description:"Set manual and status condition (ex: 'success')."` //This is used only for manual and success condition
73+
PipelineName string `json:"pipeline,omitempty" yaml:"pipeline,omitempty" jsonschema_description:"The name of a pipeline used for pipeline node."`
74+
ApplicationName string `json:"application,omitempty" yaml:"application,omitempty" jsonschema_description:"The application to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
75+
EnvironmentName string `json:"environment,omitempty" yaml:"environment,omitempty" jsonschema_description:"The environment to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
76+
ProjectIntegrationName string `json:"integration,omitempty" yaml:"integration,omitempty" jsonschema_description:"The integration to use in the context of the node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/pipeline-context"`
77+
OneAtATime *bool `json:"one_at_a_time,omitempty" yaml:"one_at_a_time,omitempty" jsonschema_description:"Set to true if you want to limit the execution of this node to one at a time."`
78+
Payload map[string]interface{} `json:"payload,omitempty" yaml:"payload,omitempty"`
79+
Parameters map[string]string `json:"parameters,omitempty" yaml:"parameters,omitempty" jsonschema_description:"List of parameters for the workflow."`
80+
OutgoingHookModelName string `json:"trigger,omitempty" yaml:"trigger,omitempty"`
81+
OutgoingHookConfig map[string]string `json:"config,omitempty" yaml:"config,omitempty"`
82+
Permissions map[string]int `json:"permissions,omitempty" yaml:"permissions,omitempty" jsonschema_description:"The permissions for the node (ex: myGroup: 7).\nhttps://ovh.github.io/cds/docs/concepts/permissions"`
83+
}
84+
85+
type ConditionEntry struct {
86+
PlainConditions []PlainConditionEntry `json:"plain,omitempty" yaml:"check,omitempty"`
87+
LuaScript string `json:"script,omitempty" yaml:"script,omitempty"`
88+
}
89+
90+
//WorkflowNodeCondition represents a condition to trigger ot not a pipeline in a workflow. Operator can be =, !=, regex
91+
type PlainConditionEntry struct {
92+
Variable string `json:"variable" yaml:"variable"`
93+
Operator string `json:"operator" yaml:"operator"`
94+
Value string `json:"value" yaml:"value"`
8395
}
8496

8597
// HookEntry represents a hook as code
@@ -154,10 +166,17 @@ func craftNodeEntry(w sdk.Workflow, n sdk.Node) (NodeEntry, error) {
154166
}
155167

156168
if len(conditions) > 0 || n.Context.Conditions.LuaScript != "" {
157-
entry.Conditions = &sdk.WorkflowNodeConditions{
158-
PlainConditions: conditions,
169+
entry.Conditions = &ConditionEntry{
170+
PlainConditions: make([]PlainConditionEntry, 0, len(conditions)),
159171
LuaScript: n.Context.Conditions.LuaScript,
160172
}
173+
for _, c := range conditions {
174+
entry.Conditions.PlainConditions = append(entry.Conditions.PlainConditions, PlainConditionEntry{
175+
Value: c.Value,
176+
Operator: c.Operator,
177+
Variable: c.Variable,
178+
})
179+
}
161180
}
162181

163182
if n.Context.ApplicationName != "" {
@@ -626,7 +645,17 @@ func (e *NodeEntry) getNode(name string, w *sdk.Workflow) (*sdk.Node, error) {
626645
}
627646

628647
if e.Conditions != nil {
629-
node.Context.Conditions = *e.Conditions
648+
node.Context.Condi A3E2 tions = sdk.WorkflowNodeConditions{
649+
PlainConditions: make([]sdk.WorkflowNodeCondition, 0, len(e.Conditions.PlainConditions)),
650+
LuaScript: e.Conditions.LuaScript,
651+
}
652+
for _, c := range e.Conditions.PlainConditions {
653+
node.Context.Conditions.PlainConditions = append(node.Context.Conditions.PlainConditions, sdk.WorkflowNodeCondition{
654+
Variable: c.Variable,
655+
Operator: c.Operator,
656+
Value: c.Value,
657+
})
658+
}
630659
}
631660

632661
if len(e.Payload) > 0 {

sdk/exportentities/workflow_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestWorkflow_checkDependencies(t *testing.T) {
2222
Version string
2323
Workflow map[string]exportentities.NodeEntry
2424
Hooks map[string][]exportentities.HookEntry
25-
Conditions *sdk.WorkflowNodeConditions
25+
Conditions *exportentities.ConditionEntry
2626
When []string
2727
PipelineName string
2828
ApplicationName string
@@ -114,7 +114,7 @@ func TestWorkflow_checkValidity(t *testing.T) {
114114
Workflow map[string]exportentities.NodeEntry
115115
Hooks map[string][]exportentities.HookEntry
116116
DependsOn []string
117-
Conditions *sdk.WorkflowNodeConditions
117+
Conditions *exportentities.ConditionEntry
118118
When []string
119119
PipelineName string
120120
ApplicationName string
@@ -201,7 +201,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
201201
Workflow map[string]exportentities.NodeEntry
202202
Hooks map[string][]exportentities.HookEntry
203203
DependsOn []string
204-
Conditions *sdk.WorkflowNodeConditions
204+
Conditions *exportentities.ConditionEntry
205205
When []string
206206
PipelineName string
207207
ApplicationName string

tests/fixtures/ITSCWRKFLW6/ITSCWRKFLW6.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: "ITSCWRKFLW6-WORKFLOW"
22
version: v1.0
33
pipeline: clonetagbis
4+
conditions:
5+
script: return git_tag == 'v0.27.0'
46
payload:
57
git.author: ""
68
git.tag: "v0.27.0"

0 commit comments

Comments
 (0)
0