Skip to content

Commit

Permalink
fix(templates): handle nil PrivateGitHub (#1034)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper authored Jan 5, 2024
1 parent 4906635 commit de5cf10
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
9 changes: 9 additions & 0 deletions compiler/native/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) {
"host": src.Host,
}).Tracef("Using authenticated GitHub client to pull template")

// verify private GitHub is actually set up
if c.PrivateGithub == nil {
return nil, fmt.Errorf("unable to fetch template %s: missing credentials", src.Name)
}

// use private (authenticated) github instance to pull from
bytes, err = c.PrivateGithub.Template(c.user, src)
if err != nil {
Expand Down Expand Up @@ -307,6 +312,10 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) {
"path": src.Name,
}).Tracef("Using authenticated GitHub client to pull template")

if c.PrivateGithub == nil {
return nil, fmt.Errorf("unable to fetch template %s: missing credentials", src.Name)
}

// use private (authenticated) github instance to pull from
bytes, err = c.PrivateGithub.Template(c.user, src)
if err != nil {
Expand Down
34 changes: 32 additions & 2 deletions compiler/native/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,43 @@ func TestNative_ExpandStages(t *testing.T) {
"bar": "test4",
}

// run test
// run test -- missing private github
compiler, err := New(c)
if err != nil {
t.Errorf("Creating new compiler returned err: %v", err)
}

build, err := compiler.ExpandStages(&yaml.Build{Stages: stages, Services: yaml.ServiceSlice{}, Environment: raw.StringSliceMap{}}, tmpls, new(pipeline.RuleData))
compiler.PrivateGithub = nil
_, err = compiler.ExpandStages(
&yaml.Build{
Stages: stages,
Services: yaml.ServiceSlice{},
Environment: raw.StringSliceMap{},
},
tmpls,
new(pipeline.RuleData),
)

if err == nil {
t.Errorf("ExpandStages should have returned error with empty PrivateGitHub")
}

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

build, err := compiler.ExpandStages(
&yaml.Build{
Stages: stages,
Services: yaml.ServiceSlice{},
Environment: raw.StringSliceMap{},
},
tmpls,
new(pipeline.RuleData),
)

if err != nil {
t.Errorf("ExpandStages returned err: %v", err)
}
Expand Down

0 comments on commit de5cf10

Please sign in to comment.