diff --git a/sdk/exportentities/v1/workflow.go b/sdk/exportentities/v1/workflow.go index a90f865834..595c83af8e 100644 --- a/sdk/exportentities/v1/workflow.go +++ b/sdk/exportentities/v1/workflow.go @@ -59,7 +59,7 @@ type ConditionEntry struct { LuaScript string `json:"script,omitempty" yaml:"script,omitempty"` } -//WorkflowNodeCondition represents a condition to trigger ot not a pipeline in a workflow. Operator can be =, !=, regex +// WorkflowNodeCondition represents a condition to trigger ot not a pipeline in a workflow. Operator can be =, !=, regex type PlainConditionEntry struct { Variable string `json:"variable" yaml:"variable"` Operator string `json:"operator" yaml:"operator"` @@ -79,20 +79,23 @@ func (w Workflow) Entries() map[string]NodeEntry { return w.Workflow } - singleEntry := NodeEntry{ - ApplicationName: w.ApplicationName, - EnvironmentName: w.EnvironmentName, - ProjectIntegrationName: w.ProjectIntegrationName, - PipelineName: w.PipelineName, - Conditions: w.Conditions, - When: w.When, - Payload: w.Payload, - Parameters: w.Parameters, - OneAtATime: w.OneAtATime, - } - return map[string]NodeEntry{ - w.PipelineName: singleEntry, + if w.PipelineName != "" { + singleEntry := NodeEntry{ + ApplicationName: w.ApplicationName, + EnvironmentName: w.EnvironmentName, + ProjectIntegrationName: w.ProjectIntegrationName, + PipelineName: w.PipelineName, + Conditions: w.Conditions, + When: w.When, + Payload: w.Payload, + Parameters: w.Parameters, + OneAtATime: w.OneAtATime, + } + return map[string]NodeEntry{ + w.PipelineName: singleEntry, + } } + return nil } func (w Workflow) CheckValidity() error { @@ -209,6 +212,11 @@ func (w Workflow) GetWorkflow() (*sdk.Workflow, error) { rand.Seed(time.Now().Unix()) entries := w.Entries() + + if len(entries) == 0 { + return nil, sdk.NewErrorFrom(sdk.ErrWorkflowInvalid, "a workflow must contains at least 1 pipeline") + } + var attempt int fakeID := rand.Int63n(5000) // attempt is there to avoid infinite loop, but it should not happened because we check validity and dependencies earlier diff --git a/sdk/exportentities/v1/workflow_test.go b/sdk/exportentities/v1/workflow_test.go index 4e42c13fe4..974ac21696 100644 --- a/sdk/exportentities/v1/workflow_test.go +++ b/sdk/exportentities/v1/workflow_test.go @@ -223,7 +223,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) { { name: "Simple workflow with mutex should not raise an error", fields: fields{ - Version: exportentities.ActionVersion1, + Version: exportentities.WorkflowVersion1, Name: "myworkflow", PipelineName: "pipeline", OneAtATime: &strue, @@ -249,8 +249,8 @@ func TestWorkflow_GetWorkflow(t *testing.T) { fields: fields{ Version: exportentities.ActionVersion1, Name: "myworkflow", - PipelineName: "pipeline", Description: "this is my description", + PipelineName: "pipeline", PipelineHooks: []v1.HookEntry{ { Model: "Scheduler", @@ -791,6 +791,24 @@ func TestWorkflow_GetWorkflow(t *testing.T) { }, }, }, + { + name: "Workflow v2 with no nodes", + fields: fields{ + Name: "myworkflow", + Version: exportentities.WorkflowVersion2, + Workflow: map[string]v1.NodeEntry{}, + }, + wantErr: true, + }, + { + name: "Workflow v1 with no nodes", + fields: fields{ + Name: "myworkflow", + Version: exportentities.WorkflowVersion1, + Workflow: map[string]v1.NodeEntry{}, + }, + wantErr: true, + }, } for _, tt := range tsts { diff --git a/sdk/exportentities/v2/workflow.go b/sdk/exportentities/v2/workflow.go index f40253d5d7..57269999d4 100644 --- a/sdk/exportentities/v2/workflow.go +++ b/sdk/exportentities/v2/workflow.go @@ -71,7 +71,7 @@ type ConditionEntry struct { LuaScript string `json:"script,omitempty" yaml:"script,omitempty"` } -//WorkflowNodeCondition represents a condition to trigger ot not a pipeline in a workflow. Operator can be =, !=, regex +// WorkflowNodeCondition represents a condition to trigger ot not a pipeline in a workflow. Operator can be =, !=, regex type PlainConditionEntry struct { Variable string `json:"variable" yaml:"variable"` Operator string `json:"operator" yaml:"operator"` @@ -113,7 +113,7 @@ func (h HookEntry) IsDefault(model sdk.WorkflowHookModel) bool { type ExportOptions func(w sdk.Workflow, exportedWorkflow *Workflow) error -//NewWorkflow creates a new exportable workflow +// NewWorkflow creates a new exportable workflow func NewWorkflow(ctx context.Context, w sdk.Workflow, version string, opts ...ExportOptions) (Workflow, error) { exportedWorkflow := Workflow{} exportedWorkflow.Name = w.Name @@ -433,6 +433,10 @@ func (w Workflow) GetWorkflow(ctx context.Context) (*sdk.Workflow, error) { wf.RetentionPolicy = *w.RetentionPolicy } + if len(w.Workflow) == 0 { + return nil, sdk.NewErrorFrom(sdk.ErrWorkflowInvalid, "a workflow must contains at least 1 pipeline") + } + r := rand.New(rand.NewSource(time.Now().Unix())) var attempt int fakeID := r.Int63n(5000)