Factor out sideeffects

This commit is contained in:
Laszlo Fogas 2019-07-22 13:43:18 +02:00
parent b7f61fc81d
commit ed51df29b3
4 changed files with 16 additions and 10 deletions

View file

@ -344,6 +344,7 @@ func PostApproval(c *gin.Context) {
store.UpdateBuild(c, build) store.UpdateBuild(c, build)
return return
} }
build = setBuildStepsOnBuild(b.Curr, buildItems)
err = store.FromContext(c).ProcCreate(build.Procs) err = store.FromContext(c).ProcCreate(build.Procs)
if err != nil { if err != nil {
@ -558,6 +559,7 @@ func PostBuild(c *gin.Context) {
c.JSON(500, build) c.JSON(500, build)
return return
} }
build = setBuildStepsOnBuild(b.Curr, buildItems)
err = store.FromContext(c).ProcCreate(build.Procs) err = store.FromContext(c).ProcCreate(build.Procs)
if err != nil { if err != nil {

View file

@ -177,7 +177,7 @@ func PostHook(c *gin.Context) {
return return
} }
if zeroSteps(*build, remoteYamlConfigs) { if zeroSteps(build, remoteYamlConfigs) {
c.String(200, "Step conditions yield zero runnable steps") c.String(200, "Step conditions yield zero runnable steps")
return return
} }
@ -261,6 +261,7 @@ func PostHook(c *gin.Context) {
store.UpdateBuild(c, build) store.UpdateBuild(c, build)
return return
} }
build = setBuildStepsOnBuild(b.Curr, buildItems)
err = store.FromContext(c).ProcCreate(build.Procs) err = store.FromContext(c).ProcCreate(build.Procs)
if err != nil { if err != nil {
@ -298,11 +299,10 @@ func branchFiltered(build *model.Build, remoteYamlConfigs []*remote.FileMeta) bo
return true return true
} }
// uses pass by value as procBuilder has side effects on build. Something to be fixed func zeroSteps(build *model.Build, remoteYamlConfigs []*remote.FileMeta) bool {
func zeroSteps(build model.Build, remoteYamlConfigs []*remote.FileMeta) bool {
b := procBuilder{ b := procBuilder{
Repo: &model.Repo{}, Repo: &model.Repo{},
Curr: &build, Curr: build,
Last: &model.Build{}, Last: &model.Build{},
Netrc: &model.Netrc{}, Netrc: &model.Netrc{},
Secs: []*model.Secret{}, Secs: []*model.Secret{},

View file

@ -128,14 +128,11 @@ func (b *procBuilder) Build() ([]*buildItem, error) {
item.Labels = map[string]string{} item.Labels = map[string]string{}
} }
b.Curr.Procs = append(b.Curr.Procs, proc)
items = append(items, item) items = append(items, item)
pidSequence++ pidSequence++
} }
} }
setBuildSteps(b.Curr, items)
return items, nil return items, nil
} }
@ -214,8 +211,12 @@ func (b *procBuilder) toInternalRepresentation(parsed *yaml.Config, environ map[
).Compile(parsed) ).Compile(parsed)
} }
func setBuildSteps(build *model.Build, buildItems []*buildItem) { func setBuildStepsOnBuild(build *model.Build, buildItems []*buildItem) *model.Build {
pcounter := len(buildItems) for _, item := range buildItems {
build.Procs = append(build.Procs, item.Proc)
}
pcounter := len(build.Procs)
for _, item := range buildItems { for _, item := range buildItems {
for _, stage := range item.Config.Stages { for _, stage := range item.Config.Stages {
var gid int var gid int
@ -239,6 +240,8 @@ func setBuildSteps(build *model.Build, buildItems []*buildItem) {
} }
} }
} }
return build
} }
// return the metadata from the cli context. // return the metadata from the cli context.

View file

@ -262,7 +262,8 @@ pipeline:
}, },
} }
_, err := b.Build() buildItems, err := b.Build()
build = setBuildStepsOnBuild(build, buildItems)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }