diff --git a/compiler/native/expand.go b/compiler/native/expand.go index c4395d6dc..e17d03161 100644 --- a/compiler/native/expand.go +++ b/compiler/native/expand.go @@ -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" @@ -212,6 +213,39 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) { } } + case strings.EqualFold(tmpl.Type, "file"): + src := ®istry.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) } diff --git a/compiler/native/expand_test.go b/compiler/native/expand_test.go index d4f9f8d7c..ae3fc32b7 100644 --- a/compiler/native/expand_test.go +++ b/compiler/native/expand_test.go @@ -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" @@ -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", + }, + }, }, } @@ -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) + } + }) } }