Skip to content

Commit

Permalink
fix(compiler): aggregate templates for nested pipelines (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper authored May 10, 2024
1 parent 8a744e4 commit 4421e30
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions compiler/native/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r *
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 @@ func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r *
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 @@ func (c *client) ExpandSteps(s *yaml.Build, tmpls map[string]*yaml.Template, r *
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

0 comments on commit 4421e30

Please sign in to comment.