Skip to content

Commit

Permalink
fix(compiler): add commit field to compiler engine to handle file typ…
Browse files Browse the repository at this point in the history
…e template lite compilation (#859)
  • Loading branch information
ecrupper authored May 23, 2023
1 parent afd00ce commit ffac676
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 11 deletions.
2 changes: 2 additions & 0 deletions api/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func CreateBuild(c *gin.Context) {
p, compiled, err = compiler.FromContext(c).
Duplicate().
WithBuild(input).
WithCommit(input.GetCommit()).
WithFiles(files).
WithMetadata(m).
WithRepo(r).
Expand Down Expand Up @@ -1200,6 +1201,7 @@ func RestartBuild(c *gin.Context) {
p, compiled, err = compiler.FromContext(c).
Duplicate().
WithBuild(b).
WithCommit(b.GetCommit()).
WithFiles(files).
WithMetadata(m).
WithRepo(r).
Expand Down
2 changes: 1 addition & 1 deletion api/pipeline/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func CompilePipeline(c *gin.Context) {
r.SetPipelineType(p.GetType())

// create the compiler object
compiler := compiler.FromContext(c).Duplicate().WithMetadata(m).WithRepo(r).WithUser(u)
compiler := compiler.FromContext(c).Duplicate().WithCommit(p.GetCommit()).WithMetadata(m).WithRepo(r).WithUser(u)

// compile the pipeline
pipeline, _, err := compiler.CompileLite(p.GetData(), true, true, nil)
Expand Down
2 changes: 1 addition & 1 deletion api/pipeline/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func ExpandPipeline(c *gin.Context) {
r.SetPipelineType(p.GetType())

// create the compiler object
compiler := compiler.FromContext(c).Duplicate().WithMetadata(m).WithRepo(r).WithUser(u)
compiler := compiler.FromContext(c).Duplicate().WithCommit(p.GetCommit()).WithMetadata(m).WithRepo(r).WithUser(u)

// expand the templates in the pipeline
pipeline, _, err := compiler.CompileLite(p.GetData(), true, false, nil)
Expand Down
2 changes: 1 addition & 1 deletion api/pipeline/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func GetTemplates(c *gin.Context) {
}).Infof("reading templates from pipeline %s", entry)

// create the compiler object
compiler := compiler.FromContext(c).Duplicate().WithMetadata(m).WithRepo(r).WithUser(u)
compiler := compiler.FromContext(c).Duplicate().WithCommit(p.GetCommit()).WithMetadata(m).WithRepo(r).WithUser(u)

// parse the pipeline configuration
pipeline, _, err := compiler.Parse(p.GetData(), p.GetType(), new(yaml.Template))
Expand Down
2 changes: 1 addition & 1 deletion api/pipeline/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func ValidatePipeline(c *gin.Context) {
r.SetPipelineType(p.GetType())

// create the compiler object
compiler := compiler.FromContext(c).Duplicate().WithMetadata(m).WithRepo(r).WithUser(u)
compiler := compiler.FromContext(c).Duplicate().WithCommit(p.GetCommit()).WithMetadata(m).WithRepo(r).WithUser(u)

// capture optional template query parameter
template, err := strconv.ParseBool(c.DefaultQuery("template", "true"))
Expand Down
1 change: 1 addition & 0 deletions api/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ func PostWebhook(c *gin.Context) {
Duplicate().
WithBuild(b).
WithComment(webhook.Comment).
WithCommit(b.GetCommit()).
WithFiles(files).
WithMetadata(m).
WithRepo(repo).
Expand Down
3 changes: 3 additions & 0 deletions compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ type Engine interface {
// WithComment defines a function that sets
// the comment in the Engine.
WithComment(string) Engine
// WithCommit defines a function that sets
// the commit in the Engine.
WithCommit(string) Engine
// WithFiles defines a function that sets
// the changeset files in the Engine.
WithFiles([]string) Engine
Expand Down
2 changes: 1 addition & 1 deletion compiler/native/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) {
Org: c.repo.GetOrg(),
Repo: c.repo.GetName(),
Name: tmpl.Source,
Ref: c.build.GetCommit(),
Ref: c.commit,
}

if !c.UsePrivateGithub {
Expand Down
7 changes: 1 addition & 6 deletions compiler/native/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,6 @@ func TestNative_ExpandSteps(t *testing.T) {
set.String("github-token", "", "doc")
c := cli.NewContext(nil, set, nil)

testBuild := new(library.Build)

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

testRepo := new(library.Repo)

testRepo.SetID(1)
Expand Down Expand Up @@ -318,7 +313,7 @@ func TestNative_ExpandSteps(t *testing.T) {
t.Errorf("Creating new compiler returned err: %v", err)
}

compiler.WithBuild(testBuild).WithRepo(testRepo)
compiler.WithCommit("123abc456def").WithRepo(testRepo)

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions compiler/native/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type client struct {

build *library.Build
comment string
commit string
files []string
local bool
metadata *types.Metadata
Expand Down Expand Up @@ -131,6 +132,15 @@ func (c *client) WithComment(cmt string) compiler.Engine {
return c
}

// WithCommit sets the comment in the Engine.
func (c *client) WithCommit(cmt string) compiler.Engine {
if cmt != "" {
c.commit = cmt
}

return c
}

// WithFiles sets the changeset files in the Engine.
func (c *client) WithFiles(f []string) compiler.Engine {
if f != nil {
Expand Down
1 change: 1 addition & 0 deletions router/middleware/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func Establish() gin.HandlerFunc {
// parse and compile the pipeline configuration file
_, pipeline, err = compiler.FromContext(c).
Duplicate().
WithCommit(p).
WithMetadata(c.MustGet("metadata").(*types.Metadata)).
WithRepo(r).
WithUser(u).
Expand Down

0 comments on commit ffac676

Please sign in to comment.