diff --git a/server/hook.go b/server/hook.go index 6ffd51c05..c5f0af973 100644 --- a/server/hook.go +++ b/server/hook.go @@ -33,6 +33,7 @@ import ( "github.com/laszlocph/drone-oss-08/shared/token" "github.com/laszlocph/drone-oss-08/store" + "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/frontend/yaml" "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/rpc" "github.com/laszlocph/drone-oss-08/cncd/pubsub" "github.com/laszlocph/drone-oss-08/cncd/queue" @@ -150,14 +151,10 @@ func PostHook(c *gin.Context) { return } - // verify that pipeline can be built at all - // parsedPipelineConfig, err := yaml.ParseString(conf.Data) - // if err == nil { - // if !parsedPipelineConfig.Branches.Match(build.Branch) && build.Event != model.EventTag && build.Event != model.EventDeploy { - // c.String(200, "Branch does not match restrictions defined in yaml") - // return - // } - // } + if branchFiltered(build, remoteYamlConfigs) { + c.String(200, "Branch does not match restrictions defined in yaml") + return + } if repo.IsGated { // This feature is not clear to me. Reenabling once better understood build.Status = model.StatusBlocked @@ -262,6 +259,19 @@ func PostHook(c *gin.Context) { queueBuild(build, repo, buildItems) } +func branchFiltered(build *model.Build, remoteYamlConfigs []*remote.FileMeta) bool { + for _, remoteYamlConfig := range remoteYamlConfigs { + parsedPipelineConfig, err := yaml.ParseString(string(remoteYamlConfig.Data)) + if err == nil { + if !parsedPipelineConfig.Branches.Match(build.Branch) && build.Event != model.EventTag && build.Event != model.EventDeploy { + } else { + return false + } + } + } + return true +} + func findOrPersistPipelineConfig(build *model.Build, remoteYamlConfig *remote.FileMeta) (*model.Config, error) { sha := shasum(remoteYamlConfig.Data) conf, err := Config.Storage.Config.ConfigFindIdentical(build.RepoID, sha) @@ -315,6 +325,9 @@ func publishToTopic(c *gin.Context, build *model.Build, repo *model.Repo) { func queueBuild(build *model.Build, repo *model.Repo, buildItems []*buildItem) { var tasks []*queue.Task for _, item := range buildItems { + if item.Proc.State == model.StatusSkipped { + continue + } task := new(queue.Task) task.ID = fmt.Sprint(item.Proc.ID) task.Labels = map[string]string{} diff --git a/server/procBuilder.go b/server/procBuilder.go index 5e0656d76..5eaadeefa 100644 --- a/server/procBuilder.go +++ b/server/procBuilder.go @@ -103,6 +103,10 @@ func (b *procBuilder) Build() ([]*buildItem, error) { return nil, lerr } + if !parsed.Branches.Match(b.Curr.Branch) { + proc.State = model.StatusSkipped + } + metadata.SetPlatform(parsed.Platform) ir := b.toInternalRepresentation(parsed, environ, metadata, proc.ID) @@ -220,6 +224,9 @@ func setBuildSteps(build *model.Build, buildItems []*buildItem) { PGID: gid, State: model.StatusPending, } + if item.Proc.State == model.StatusSkipped { + proc.State = model.StatusSkipped + } build.Procs = append(build.Procs, proc) } } diff --git a/server/procBuilder_test.go b/server/procBuilder_test.go index 1a885b6b6..4452dfaf4 100644 --- a/server/procBuilder_test.go +++ b/server/procBuilder_test.go @@ -158,3 +158,49 @@ runs_on: t.Fatal("Should run on failure") } } + +func TestBranchFilter(t *testing.T) { + b := procBuilder{ + Repo: &model.Repo{}, + Curr: &model.Build{Branch: "dev"}, + Last: &model.Build{}, + Netrc: &model.Netrc{}, + Secs: []*model.Secret{}, + Regs: []*model.Registry{}, + Link: "", + Yamls: []*remote.FileMeta{ + &remote.FileMeta{Data: []byte(` +pipeline: + xxx: + image: scratch + yyy: ${DRONE_COMMIT_MESSAGE} +branches: master +`)}, + &remote.FileMeta{Data: []byte(` +pipeline: + build: + image: scratch + yyy: ${DRONE_COMMIT_MESSAGE} +`)}, + }, + } + + buildItems, err := b.Build() + if err != nil { + t.Fatal(err) + } + if len(buildItems) != 2 { + t.Fatal("Should have generated 2 buildItems") + } + if buildItems[0].Proc.State != model.StatusSkipped { + t.Fatal("Should not run on dev branch") + } + for _, child := range buildItems[0].Proc.Children { + if child.State != model.StatusSkipped { + t.Fatal("Children should skipped status too") + } + } + if buildItems[1].Proc.State != model.StatusPending { + t.Fatal("Should not run on dev branch") + } +}