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

enhance(templates): inject template name as variable #1096

Merged
merged 2 commits into from
Mar 29, 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
8 changes: 8 additions & 0 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@
format = constants.PipelineTypeGo
}

// initialize variable map if not parsed from config
if len(template.Variables) == 0 {
template.Variables = make(map[string]interface{})
}

// inject template name into variables
template.Variables["VELA_TEMPLATE_NAME"] = template.Name

parsed, _, err := c.Parse(bytes, format, template)
if err != nil {
return nil, err
Expand Down Expand Up @@ -256,7 +264,7 @@

// compileSteps executes the workflow for converting a YAML pipeline into an executable struct.
//
//nolint:dupl,lll // linter thinks the steps and stages workflows are identical

Check failure on line 267 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] compiler/native/compile.go#L267

directive `//nolint:dupl,lll // linter thinks the steps and stages workflows are identical` is unused for linter "dupl" (nolintlint)
Raw output
compiler/native/compile.go:267:1: directive `//nolint:dupl,lll // linter thinks the steps and stages workflows are identical` is unused for linter "dupl" (nolintlint)
//nolint:dupl,lll // linter thinks the steps and stages workflows are identical
^
func (c *client) compileSteps(p *yaml.Build, _pipeline *library.Pipeline, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*pipeline.Build, *library.Pipeline, error) {
var err error

Expand Down Expand Up @@ -353,7 +361,7 @@

// compileStages executes the workflow for converting a YAML pipeline into an executable struct.
//
//nolint:dupl,lll // linter thinks the steps and stages workflows are identical

Check failure on line 364 in compiler/native/compile.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] compiler/native/compile.go#L364

directive `//nolint:dupl,lll // linter thinks the steps and stages workflows are identical` is unused for linter "dupl" (nolintlint)
Raw output
compiler/native/compile.go:364:1: directive `//nolint:dupl,lll // linter thinks the steps and stages workflows are identical` is unused for linter "dupl" (nolintlint)
//nolint:dupl,lll // linter thinks the steps and stages workflows are identical
^
func (c *client) compileStages(p *yaml.Build, _pipeline *library.Pipeline, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*pipeline.Build, *library.Pipeline, error) {
var err error

Expand Down
44 changes: 25 additions & 19 deletions compiler/native/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@
buildEnv["GRADLE_USER_HOME"] = ".gradle"
buildEnv["HOME"] = "/root"
buildEnv["SHELL"] = "/bin/sh"
buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build"})
buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build", "echo gradle"})
buildEnv["bar"] = "test4"
buildEnv["star"] = "test3"

Expand Down Expand Up @@ -951,7 +951,7 @@
buildEnv["GRADLE_USER_HOME"] = ".gradle"
buildEnv["HOME"] = "/root"
buildEnv["SHELL"] = "/bin/sh"
buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build"})
buildEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew build", "echo gradle"})
buildEnv["bar"] = "test4"
buildEnv["star"] = "test3"

Expand Down Expand Up @@ -1815,7 +1815,7 @@
}

compiler.WithMetadata(m)
compiler.WithRepo(&library.Repo{PipelineType: &tt.args.pipelineType})

Check failure on line 1818 in compiler/native/compile_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] compiler/native/compile_test.go#L1818

G601: Implicit memory aliasing in for loop. (gosec)
Raw output
compiler/native/compile_test.go:1818:50: G601: Implicit memory aliasing in for loop. (gosec)
			compiler.WithRepo(&library.Repo{PipelineType: &tt.args.pipelineType})
			                                              ^

got, _, err := compiler.Compile(yaml)
if err != nil {
Expand Down Expand Up @@ -3126,7 +3126,7 @@
compiler.WithMetadata(m)

if tt.args.pipelineType != "" {
compiler.WithRepo(&library.Repo{PipelineType: &tt.args.pipelineType})

Check failure on line 3129 in compiler/native/compile_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] compiler/native/compile_test.go#L3129

G601: Implicit memory aliasing in for loop. (gosec)
Raw output
compiler/native/compile_test.go:3129:51: G601: Implicit memory aliasing in for loop. (gosec)
				compiler.WithRepo(&library.Repo{PipelineType: &tt.args.pipelineType})
				                                              ^
}

got, _, err := compiler.Compile(yaml)
Expand Down Expand Up @@ -3230,17 +3230,21 @@
},
Templates: []*yaml.Template{
{
Name: "golang",
Source: "github.example.com/github/octocat/golang_inline_stages.yml",
Format: "golang",
Type: "github",
Variables: map[string]any{"image": string("golang:latest")},
Name: "golang",
Source: "github.example.com/github/octocat/golang_inline_stages.yml",
Format: "golang",
Type: "github",
Variables: map[string]any{
"image": string("golang:latest"),
"VELA_TEMPLATE_NAME": string("golang"),
},
},
{
Name: "starlark",
Source: "github.example.com/github/octocat/starlark_inline_stages.star",
Format: "starlark",
Type: "github",
Name: "starlark",
Source: "github.example.com/github/octocat/starlark_inline_stages.star",
Format: "starlark",
Type: "github",
Variables: map[string]any{"VELA_TEMPLATE_NAME": string("starlark")},
},
},
Environment: raw.StringSliceMap{},
Expand Down Expand Up @@ -3375,16 +3379,18 @@
Environment: raw.StringSliceMap{},
Templates: yaml.TemplateSlice{
{
Name: "golang",
Source: "github.example.com/github/octocat/golang_inline_steps.yml",
Format: "golang",
Type: "github",
Name: "golang",
Source: "github.example.com/github/octocat/golang_inline_steps.yml",
Format: "golang",
Type: "github",
Variables: map[string]any{"VELA_TEMPLATE_NAME": string("golang")},
},
{
Name: "starlark",
Source: "github.example.com/github/octocat/starlark_inline_steps.star",
Format: "starlark",
Type: "github",
Name: "starlark",
Source: "github.example.com/github/octocat/starlark_inline_steps.star",
Format: "starlark",
Type: "github",
Variables: map[string]any{"VELA_TEMPLATE_NAME": string("starlark")},
},
},
},
Expand Down
8 changes: 8 additions & 0 deletions compiler/native/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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 48 in compiler/native/expand.go

View workflow job for this annotation

GitHub Actions / golangci

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

Function 'ExpandSteps' has too many statements (74 > 70) (funlen)
Raw output
compiler/native/expand.go:48: Function 'ExpandSteps' has too many statements (74 > 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 Down Expand Up @@ -120,6 +120,14 @@
return s, err
}

// initialize variable map if not parsed from config
if len(step.Template.Variables) == 0 {
step.Template.Variables = make(map[string]interface{})
}

// inject template name into variables
step.Template.Variables["VELA_TEMPLATE_NAME"] = step.Template.Name

tmplBuild, err := c.mergeTemplate(bytes, tmpl, step)
if err != nil {
return s, err
Expand Down
6 changes: 3 additions & 3 deletions compiler/native/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestNative_ExpandStages(t *testing.T) {
Pull: "always",
},
&yaml.Step{
Commands: []string{"./gradlew build"},
Commands: []string{"./gradlew build", "echo gradle"},
Environment: raw.StringSliceMap{
"GRADLE_OPTS": "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false",
"GRADLE_USER_HOME": ".gradle",
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestNative_ExpandSteps(t *testing.T) {
Pull: "always",
},
&yaml.Step{
Commands: []string{"./gradlew build"},
Commands: []string{"./gradlew build", "echo gradle"},
Environment: raw.StringSliceMap{
"GRADLE_OPTS": "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false",
"GRADLE_USER_HOME": ".gradle",
Expand Down Expand Up @@ -823,7 +823,7 @@ func TestNative_ExpandSteps_TemplateCallTemplate(t *testing.T) {
Pull: "always",
},
&yaml.Step{
Commands: []string{"./gradlew build"},
Commands: []string{"./gradlew build", "echo test"},
Environment: raw.StringSliceMap{
"GRADLE_OPTS": "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=1 -Dorg.gradle.parallel=false",
"GRADLE_USER_HOME": ".gradle",
Expand Down
1 change: 1 addition & 0 deletions compiler/native/testdata/long_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ steps:
- name: build
commands:
- ./gradlew build
- echo {{ .VELA_TEMPLATE_NAME }}
environment: {{ .environment }}
image: {{ .image }}
{{ .pull_policy }}
Expand Down
Loading