Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty pipeline validation #1020

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions cli/run_util_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ workflows:
c: {}
`

const emptyPipeline = `
format_version: '13'
pipelines:
dag:
workflows: {}
workflows:
a: {}
`

func TestValidation(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -185,19 +194,8 @@ func TestValidation(t *testing.T) {
config: validStagedPipeline,
},
{
name: "The last pipeline is invalid",
config: `
format_version: '13'
pipelines:
pipeline1:
workflows:
a: {}
pipeline2:
workflows: {}
workflows:
a: {}
`,
wantErr: "failed to get Bitrise config (bitrise.yml) from base 64 data: Failed to parse bitrise config, error: pipeline (pipeline2) should have at least 1 stage or workflow",
name: "Empty pipeline",
config: emptyPipeline,
},
}
for _, tt := range tests {
Expand Down
11 changes: 9 additions & 2 deletions models/models_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,15 @@ func validatePipelines(config *BitriseDataModel) ([]string, error) {

if hasStages && hasWorkflows {
return pipelineWarnings, fmt.Errorf("pipeline (%s) has both stages and workflows", pipelineID)
} else if !hasStages && !hasWorkflows {
return pipelineWarnings, fmt.Errorf("pipeline (%s) should have at least 1 stage or workflow", pipelineID)
}

// A pipeline is considered valid if it has neither stages nor workflows.
// This is useful for the WFE to be able to save a pipeline that is not yet fully defined.
if !hasStages && !hasWorkflows {
warning := fmt.Sprintf("pipeline (%s) should have at least 1 stage or workflow", pipelineID)
pipelineWarnings = append(pipelineWarnings, warning)

continue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider adding a warning in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed it and then I forgot to add it. Thanks for the reminder.

}

if hasStages {
Expand Down
96 changes: 66 additions & 30 deletions models/models_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,36 +376,6 @@ func TestValidateConfig(t *testing.T) {
require.Equal(t, "invalid pipeline ID (pi/id): doesn't conform to: [A-Za-z0-9-_.]", warnings[0])
}

t.Log("Invalid bitriseData - pipeline does not have any stages")
{
bitriseData := BitriseDataModel{
FormatVersion: "1.4.0",
Pipelines: map[string]PipelineModel{
"pipeline1": PipelineModel{
Stages: []StageListItemModel{},
},
},
}

warnings, err := bitriseData.Validate()
require.EqualError(t, err, "pipeline (pipeline1) should have at least 1 stage or workflow")
require.Equal(t, 0, len(warnings))
}

t.Log("Invalid bitriseData - pipeline does not have stages key defined")
{
bitriseData := BitriseDataModel{
FormatVersion: "1.4.0",
Pipelines: map[string]PipelineModel{
"pipeline1": PipelineModel{},
},
}

warnings, err := bitriseData.Validate()
require.EqualError(t, err, "pipeline (pipeline1) should have at least 1 stage or workflow")
require.Equal(t, 0, len(warnings))
}

t.Log("Invalid bitriseData - pipeline stage does not exist")
{
bitriseData := BitriseDataModel{
Expand Down Expand Up @@ -638,6 +608,72 @@ func TestValidateConfig(t *testing.T) {
}
}

func TestValidatePipelines(t *testing.T) {
tests := []struct {
name string
config string
wantWarns []string
wantErr string
}{
{
name: "empty pipelines",
config: `
format_version: 11
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git

pipelines:
pipeline1:
workflows: {}
pipeline2:
stages: []
`,
wantWarns: []string{
"pipeline (pipeline1) should have at least 1 stage or workflow",
"pipeline (pipeline2) should have at least 1 stage or workflow",
},
},
{
name: "mixed pipeline",
config: `
format_version: 11
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git

pipelines:
pipeline1:
workflows:
workflow1: {}
stages:
- stage1: {}
stages:
stage1:
workflow1: {}
workflows:
workflow1: {}
`,
wantErr: "pipeline (pipeline1) has both stages and workflows",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var config BitriseDataModel
require.NoError(t, yaml.Unmarshal([]byte(tt.config), &config))

warns, err := config.Validate()
if len(tt.wantWarns) > 0 {
require.Equal(t, tt.wantWarns, warns)
} else {
require.Empty(t, warns)
}

if tt.wantErr != "" {
require.EqualError(t, err, tt.wantErr)
} else {
require.NoError(t, err)
}
})
}
}

func TestValidateConfig_Containers(t *testing.T) {
tests := []struct {
name string
Expand Down