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

fix(compiler): aggregate templates for nested pipelines #1125

Merged
merged 2 commits into from
May 10, 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
4 changes: 4 additions & 0 deletions compiler/native/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

// ExpandSteps injects the template for each
// templated step in a yaml configuration.
func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r *pipeline.RuleData, depth int) (*yaml.Build, error) {

Check failure on line 47 in compiler/native/expand.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] compiler/native/expand.go#L47

Function 'ExpandSteps' has too many statements (77 > 70) (funlen)
Raw output
compiler/native/expand.go:47: Function 'ExpandSteps' has too many statements (77 > 70) (funlen)
func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r *pipeline.RuleData, depth int) (*yaml.Build, error) {
if len(tmpls) == 0 {
return s, nil
}
Expand All @@ -60,6 +60,7 @@
secrets := s.Secrets
services := s.Services
environment := s.Environment
templates := s.Templates

if len(environment) == 0 {
environment = make(raw.StringSliceMap)
Expand Down Expand Up @@ -139,6 +140,8 @@
return s, fmt.Errorf("cannot use render_inline inside a called template (%s)", step.Template.Name)
}

templates = append(templates, tmplBuild.Templates...)

tmplBuild, err = c.ExpandSteps(tmplBuild, mapFromTemplates(tmplBuild.Templates), r, depth-1)
if err != nil {
return s, err
Expand Down Expand Up @@ -202,6 +205,7 @@
s.Secrets = secrets
s.Services = services
s.Environment = environment
s.Templates = templates

return s, nil
}
Expand Down
19 changes: 18 additions & 1 deletion compiler/native/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,19 @@ func TestNative_ExpandSteps_TemplateCallTemplate(t *testing.T) {
"star": "test3",
}

wantTemplates := yaml.TemplateSlice{
{
Name: "chain",
Source: "github.example.com/faz/baz/template_calls_template.yml",
Type: "github",
},
{
Name: "test",
Source: "github.example.com/foo/bar/long_template.yml",
Type: "github",
},
}

// run test
compiler, err := New(c)
if err != nil {
Expand All @@ -879,7 +892,7 @@ func TestNative_ExpandSteps_TemplateCallTemplate(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: globalEnvironment}, test.tmpls, new(pipeline.RuleData), compiler.TemplateDepth)
build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: globalEnvironment, Templates: yaml.TemplateSlice{test.tmpls["chain"]}}, test.tmpls, new(pipeline.RuleData), compiler.TemplateDepth)
if err != nil {
t.Errorf("ExpandSteps_Type%s returned err: %v", test.name, err)
}
Expand All @@ -899,6 +912,10 @@ func TestNative_ExpandSteps_TemplateCallTemplate(t *testing.T) {
if diff := cmp.Diff(build.Environment, wantEnvironment); diff != "" {
t.Errorf("ExpandSteps()_Type%s mismatch (-want +got):\n%s", test.name, diff)
}

if diff := cmp.Diff(build.Templates, wantTemplates); diff != "" {
t.Errorf("ExpandSteps()_Type%s mismatch (-want +got):\n%s", test.name, diff)
}
})
}
}
Expand Down
Loading