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
34 changes: 34 additions & 0 deletions compiler/native/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/go-vela/types/constants"

"github.com/go-vela/server/compiler/registry"
"github.com/go-vela/server/compiler/template/native"
"github.com/go-vela/server/compiler/template/starlark"
"github.com/spf13/afero"
Expand Down Expand Up @@ -212,6 +213,39 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) {
}
}

case strings.EqualFold(tmpl.Type, "file"):
src := &registry.Source{
Org: c.repo.GetOrg(),
Repo: c.repo.GetName(),
Name: tmpl.Source,
Ref: c.build.GetCommit(),
}

if !c.UsePrivateGithub {
logrus.WithFields(logrus.Fields{
"org": src.Org,
"repo": src.Repo,
"path": src.Name,
}).Tracef("Using GitHub client to pull template")

bytes, err = c.Github.Template(nil, src)
if err != nil {
return bytes, err
}
} else {
logrus.WithFields(logrus.Fields{
"org": src.Org,
"repo": src.Repo,
"path": src.Name,
}).Tracef("Using authenticated GitHub client to pull template")

// use private (authenticated) github instance to pull from
bytes, err = c.PrivateGithub.Template(c.user, src)
if err != nil {
return bytes, err
}
}

default:
return bytes, fmt.Errorf("unsupported template type: %v", tmpl.Type)
}
Expand Down
76 changes: 56 additions & 20 deletions compiler/native/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"reflect"
"testing"

"github.com/go-vela/types/library"
"github.com/go-vela/types/raw"
"github.com/go-vela/types/yaml"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -190,11 +191,40 @@ func TestNative_ExpandSteps(t *testing.T) {
set.String("github-token", "", "doc")
c := cli.NewContext(nil, set, nil)

tmpls := map[string]*yaml.Template{
"gradle": {
Name: "gradle",
Source: "github.example.com/foo/bar/template.yml",
Type: "github",
testBuild := new(library.Build)

testBuild.SetID(1)
testBuild.SetCommit("123abc456def")

testRepo := new(library.Repo)

testRepo.SetID(1)
testRepo.SetOrg("foo")
testRepo.SetName("bar")

tests := []struct {
name string
tmpls map[string]*yaml.Template
}{
{
name: "GitHub",
tmpls: map[string]*yaml.Template{
"gradle": {
Name: "gradle",
Source: "github.example.com/foo/bar/template.yml",
Type: "github",
},
},
},
{
name: "File",
tmpls: map[string]*yaml.Template{
"gradle": {
Name: "gradle",
Source: "template.yml",
Type: "file",
},
},
},
}

Expand Down Expand Up @@ -287,25 +317,31 @@ func TestNative_ExpandSteps(t *testing.T) {
t.Errorf("Creating new compiler returned err: %v", err)
}

build, err := compiler.ExpandSteps(&yaml.Build{Steps: steps, Services: yaml.ServiceSlice{}, Environment: globalEnvironment}, tmpls)
if err != nil {
t.Errorf("ExpandSteps returned err: %v", err)
}
compiler.WithBuild(testBuild).WithRepo(testRepo)

if diff := cmp.Diff(build.Steps, wantSteps); diff != "" {
t.Errorf("ExpandSteps() mismatch (-want +got):\n%s", diff)
}
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)
if err != nil {
t.Errorf("ExpandSteps_Type%s returned err: %v", test.name, err)
}

if diff := cmp.Diff(build.Secrets, wantSecrets); diff != "" {
t.Errorf("ExpandSteps() mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(build.Steps, wantSteps); diff != "" {
t.Errorf("ExpandSteps()_Type%s mismatch (-want +got):\n%s", test.name, diff)
}

if diff := cmp.Diff(build.Services, wantServices); diff != "" {
t.Errorf("ExpandSteps() mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(build.Secrets, wantSecrets); diff != "" {
t.Errorf("ExpandSteps()_Type%s mismatch (-want +got):\n%s", test.name, diff)
}

if diff := cmp.Diff(build.Environment, wantEnvironment); diff != "" {
t.Errorf("ExpandSteps() mismatch (-want +got):\n%s", diff)
if diff := cmp.Diff(build.Services, wantServices); diff != "" {
t.Errorf("ExpandSteps()_Type%s mismatch (-want +got):\n%s", test.name, diff)
}

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

Expand Down