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

feat(api): template skip empty files #5763

Merged
merged 1 commit into from
Mar 15, 2021
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
27 changes: 21 additions & 6 deletions engine/api/workflowtemplate/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ func Parse(wt sdk.WorkflowTemplate) (sdk.WorkflowTemplateParsed, error) {
// Execute returns yaml file from template.
func Execute(wt sdk.WorkflowTemplate, instance sdk.WorkflowTemplateInstance) (exportentities.WorkflowComponents, error) {
result := exportentities.WorkflowComponents{
Pipelines: make([]exportentities.PipelineV1, len(wt.Pipelines)),
Applications: make([]exportentities.Application, len(wt.Applications)),
Environments: make([]exportentities.Environment, len(wt.Environments)),
Pipelines: make([]exportentities.PipelineV1, 0, len(wt.Pipelines)),
Applications: make([]exportentities.Application, 0, len(wt.Applications)),
Environments: make([]exportentities.Environment, 0, len(wt.Environments)),
}

data := map[string]interface{}{
Expand Down Expand Up @@ -198,29 +198,44 @@ func Execute(wt sdk.WorkflowTemplate, instance sdk.WorkflowTemplateInstance) (ex
if err != nil {
return result, err
}
if err := yaml.Unmarshal([]byte(pipelineYaml), &result.Pipelines[i]); err != nil {
if pipelineYaml == "" {
continue
}
var pip exportentities.PipelineV1
if err := yaml.Unmarshal([]byte(pipelineYaml), &pip); err != nil {
return result, sdk.NewError(sdk.ErrWrongRequest, fmt.Errorf("cannot parse generated pipeline: %v", err))
}
result.Pipelines = append(result.Pipelines, pip)
}

for i := range parsedTemplate.Applications {
applicationYaml, err := executeTemplate(parsedTemplate.Applications[i], data)
if err != nil {
return result, err
}
if err := yaml.Unmarshal([]byte(applicationYaml), &result.Applications[i]); err != nil {
if applicationYaml == "" {
continue
}
var app exportentities.Application
if err := yaml.Unmarshal([]byte(applicationYaml), &app); err != nil {
return result, sdk.NewError(sdk.ErrWrongRequest, fmt.Errorf("cannot parse generated application: %v", err))
}
result.Applications = append(result.Applications, app)
}

for i := range parsedTemplate.Environments {
environmentYaml, err := executeTemplate(parsedTemplate.Environments[i], data)
if err != nil {
return result, err
}
if err := yaml.Unmarshal([]byte(environmentYaml), &result.Environments[i]); err != nil {
if environmentYaml == "" {
continue
}
var env exportentities.Environment
if err := yaml.Unmarshal([]byte(environmentYaml), &env); err != nil {
return result, sdk.NewError(sdk.ErrWrongRequest, fmt.Errorf("cannot parse generated environment: %v", err))
}
result.Environments = append(result.Environments, env)
}

return result, nil
Expand Down
46 changes: 44 additions & 2 deletions engine/api/workflowtemplate/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ values:
}

res, err := workflowtemplate.Execute(tmpl, instance)
assert.Nil(t, err)
require.NoError(t, err)

buf, err := yaml.Marshal(res.Workflow)
require.NoError(t, err)
Expand Down Expand Up @@ -176,7 +176,7 @@ name: Environment-[[if .id]]`)),
}

_, err := workflowtemplate.Parse(tmpl)
assert.NotNil(t, err)
require.Error(t, err)
e := sdk.ExtractHTTPError(err)
assert.Equal(t, sdk.ErrCannotParseTemplate.ID, e.ID)
errs := []sdk.WorkflowTemplateError{{
Expand All @@ -198,3 +198,45 @@ name: Environment-[[if .id]]`)),
}}
assert.Equal(t, errs, e.Data)
}

func TestExecuteTemplateWithEmptyElements(t *testing.T) {
tmpl := sdk.WorkflowTemplate{
ID: 42,
Parameters: []sdk.WorkflowTemplateParameter{
{Key: "withEnv", Type: sdk.ParameterTypeBoolean, Required: true},
},
Workflow: base64.StdEncoding.EncodeToString([]byte(`
name: [[.name]]
description: Test simple workflow with error
version: v1.0`)),
Environments: []sdk.EnvironmentTemplate{{
Value: base64.StdEncoding.EncodeToString([]byte(`[[if .params.withEnv ]]
name: Environment-[[.id]]
[[end]]`)),
}},
}

res, err := workflowtemplate.Execute(tmpl, sdk.WorkflowTemplateInstance{
ID: 5,
Request: sdk.WorkflowTemplateRequest{
WorkflowName: "my-workflow",
Parameters: map[string]string{
"withEnv": "true",
},
},
})
require.NoError(t, err)
assert.Len(t, res.Environments, 1)

res, err = workflowtemplate.Execute(tmpl, sdk.WorkflowTemplateInstance{
ID: 5,
Request: sdk.WorkflowTemplateRequest{
WorkflowName: "my-workflow",
Parameters: map[string]string{
"withEnv": "false",
},
},
})
require.NoError(t, err)
assert.Len(t, res.Environments, 0)
}