Skip to content
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
3 changes: 2 additions & 1 deletion api/pipeline/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/go-vela/server/util"
"github.com/go-vela/types"
"github.com/go-vela/types/library"
"github.com/go-vela/types/yaml"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -97,7 +98,7 @@ func GetTemplates(c *gin.Context) {
compiler := compiler.FromContext(c).Duplicate().WithMetadata(m).WithRepo(r).WithUser(u)

// parse the pipeline configuration
pipeline, _, err := compiler.Parse(p.GetData(), p.GetType(), map[string]interface{}{})
pipeline, _, err := compiler.Parse(p.GetData(), p.GetType(), new(yaml.Template))
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("unable to parse pipeline %s: %w", entry, err))

Expand Down
2 changes: 1 addition & 1 deletion compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Engine interface {

// Parse defines a function that converts
// an object to a yaml configuration.
Parse(interface{}, string, map[string]interface{}) (*yaml.Build, []byte, error)
Parse(interface{}, string, *yaml.Template) (*yaml.Build, []byte, error)

// ParseRaw defines a function that converts
// an object to a string.
Expand Down
6 changes: 3 additions & 3 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type ModifyResponse struct {

// Compile produces an executable pipeline from a yaml configuration.
func (c *client) Compile(v interface{}) (*pipeline.Build, *library.Pipeline, error) {
p, data, err := c.Parse(v, c.repo.GetPipelineType(), map[string]interface{}{})
p, data, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func (c *client) Compile(v interface{}) (*pipeline.Build, *library.Pipeline, err

// CompileLite produces a partial of an executable pipeline from a yaml configuration.
func (c *client) CompileLite(v interface{}, template, substitute bool, localTemplates []string) (*yaml.Build, *library.Pipeline, error) {
p, data, err := c.Parse(v, c.repo.GetPipelineType(), map[string]interface{}{})
p, data, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func (c *client) compileInline(p *yaml.Build, localTemplates []string) (*yaml.Bu
format = constants.PipelineTypeGo
}

parsed, _, err := c.Parse(bytes, format, template.Variables)
parsed, _, err := c.Parse(bytes, format, template)
if err != nil {
return nil, err
}
Expand Down
236 changes: 236 additions & 0 deletions compiler/native/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,242 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) {
}
}

// Test evaluation of `vela "tempalate_name"` function.
func TestNative_Compile_StepsPipelineTemplate_VelaFunction_TemplateName(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
_, engine := gin.CreateTestContext(resp)

// setup mock server
engine.GET("/api/v3/repos/:org/:repo/contents/:path", func(c *gin.Context) {
body, err := convertFileToGithubResponse(c.Param("path"))
if err != nil {
t.Error(err)
}
c.JSON(http.StatusOK, body)
})

s := httptest.NewServer(engine)
defer s.Close()

// setup types
set := flag.NewFlagSet("test", 0)
set.Bool("github-driver", true, "doc")
set.String("github-url", s.URL, "doc")
set.String("github-token", "", "doc")
c := cli.NewContext(nil, set, nil)

m := &types.Metadata{
Database: &types.Database{
Driver: "foo",
Host: "foo",
},
Queue: &types.Queue{
Channel: "foo",
Driver: "foo",
Host: "foo",
},
Source: &types.Source{
Driver: "foo",
Host: "foo",
},
Vela: &types.Vela{
Address: "foo",
WebAddress: "foo",
},
}

setupEnv := environment(nil, m, nil, nil)

helloEnv := environment(nil, m, nil, nil)
helloEnv["HOME"] = "/root"
helloEnv["SHELL"] = "/bin/sh"
helloEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"echo sample"})

want := &pipeline.Build{
Version: "1",
ID: "__0",
Metadata: pipeline.Metadata{
Clone: true,
Template: false,
Environment: []string{"steps", "services", "secrets"},
},
Steps: pipeline.ContainerSlice{
&pipeline.Container{
ID: "step___0_init",
Directory: "/vela/src/foo//",
Environment: setupEnv,
Image: "#init",
Name: "init",
Number: 1,
Pull: "not_present",
},
&pipeline.Container{
ID: "step___0_clone",
Directory: "/vela/src/foo//",
Environment: setupEnv,
Image: "target/vela-git:v0.5.1",
Name: "clone",
Number: 2,
Pull: "not_present",
},
&pipeline.Container{
ID: "step___0_sample_hello",
Directory: "/vela/src/foo//",
Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"},
Entrypoint: []string{"/bin/sh", "-c"},
Environment: helloEnv,
Image: "sample",
Name: "sample_hello",
Number: 3,
Pull: "not_present",
},
},
}

// run test
yaml, err := os.ReadFile("testdata/template_name.yml")
if err != nil {
t.Errorf("Reading yaml file return err: %v", err)
}

compiler, err := New(c)
if err != nil {
t.Errorf("Creating compiler returned err: %v", err)
}

compiler.WithMetadata(m)

got, _, err := compiler.Compile(yaml)
if err != nil {
t.Errorf("Compile returned err: %v", err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Compile() mismatch (-want +got):\n%s", diff)
}
}

// Test evaluation of `vela "tempalate_name"` function on a inline template.
func TestNative_Compile_StepsPipelineTemplate_VelaFunction_TemplateName_Inline(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
_, engine := gin.CreateTestContext(resp)

// setup mock server
engine.GET("/api/v3/repos/:org/:repo/contents/:path", func(c *gin.Context) {
body, err := convertFileToGithubResponse(c.Param("path"))
if err != nil {
t.Error(err)
}
c.JSON(http.StatusOK, body)
})

s := httptest.NewServer(engine)
defer s.Close()

// setup types
set := flag.NewFlagSet("test", 0)
set.Bool("github-driver", true, "doc")
set.String("github-url", s.URL, "doc")
set.String("github-token", "", "doc")
c := cli.NewContext(nil, set, nil)

m := &types.Metadata{
Database: &types.Database{
Driver: "foo",
Host: "foo",
},
Queue: &types.Queue{
Channel: "foo",
Driver: "foo",
Host: "foo",
},
Source: &types.Source{
Driver: "foo",
Host: "foo",
},
Vela: &types.Vela{
Address: "foo",
WebAddress: "foo",
},
}

setupEnv := environment(nil, m, nil, nil)

helloEnv := environment(nil, m, nil, nil)
helloEnv["HOME"] = "/root"
helloEnv["SHELL"] = "/bin/sh"
helloEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"echo inline_templatename"})

want := &pipeline.Build{
Version: "1",
ID: "__0",
Metadata: pipeline.Metadata{
Clone: true,
Template: false,
Environment: []string{"steps", "services", "secrets"},
},
Steps: pipeline.ContainerSlice{
&pipeline.Container{
ID: "step___0_init",
Directory: "/vela/src/foo//",
Environment: setupEnv,
Image: "#init",
Name: "init",
Number: 1,
Pull: "not_present",
},
&pipeline.Container{
ID: "step___0_clone",
Directory: "/vela/src/foo//",
Environment: setupEnv,
Image: "target/vela-git:v0.5.1",
Name: "clone",
Number: 2,
Pull: "not_present",
},
&pipeline.Container{
ID: "step___0_inline_templatename_hello",
Directory: "/vela/src/foo//",
Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"},
Entrypoint: []string{"/bin/sh", "-c"},
Environment: helloEnv,
Image: "inline_templatename",
Name: "inline_templatename_hello",
Number: 3,
Pull: "not_present",
},
},
}

// run test
yaml, err := os.ReadFile("testdata/template_name_inline.yml")
if err != nil {
t.Errorf("Reading yaml file return err: %v", err)
}

compiler, err := New(c)
if err != nil {
t.Errorf("Creating compiler returned err: %v", err)
}

compiler.WithMetadata(m)

got, _, err := compiler.Compile(yaml)
if err != nil {
t.Errorf("Compile returned err: %v", err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Compile() mismatch (-want +got):\n%s", diff)
}
}

func TestNative_Compile_InvalidType(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)
Expand Down
6 changes: 3 additions & 3 deletions compiler/native/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *client) ParseRaw(v interface{}) (string, error) {
}

// Parse converts an object to a yaml configuration.
func (c *client) Parse(v interface{}, pipelineType string, variables map[string]interface{}) (*types.Build, []byte, error) {
func (c *client) Parse(v interface{}, pipelineType string, template *types.Template) (*types.Build, []byte, error) {
var (
p *types.Build
raw []byte
Expand All @@ -59,7 +59,7 @@ func (c *client) Parse(v interface{}, pipelineType string, variables map[string]
// capture the raw pipeline configuration
raw = []byte(parsedRaw)

p, err = native.RenderBuild(parsedRaw, c.EnvironmentBuild(), variables)
p, err = native.RenderBuild(template.Name, parsedRaw, c.EnvironmentBuild(), template.Variables)
if err != nil {
return nil, raw, err
}
Expand All @@ -73,7 +73,7 @@ func (c *client) Parse(v interface{}, pipelineType string, variables map[string]
// capture the raw pipeline configuration
raw = []byte(parsedRaw)

p, err = starlark.RenderBuild(parsedRaw, c.EnvironmentBuild(), variables)
p, err = starlark.RenderBuild(template.Name, parsedRaw, c.EnvironmentBuild(), template.Variables)
if err != nil {
return nil, raw, err
}
Expand Down
Loading