From 371a64ef6a1e7a33b75c42ccd4ad5b9deb9f2042 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 30 Apr 2015 10:39:16 -0700 Subject: [PATCH] hook honors branch filter --- common/config.go | 3 +++ parser/parse.go | 8 ++++++++ server/hooks.go | 12 +++++++----- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/common/config.go b/common/config.go index 5a871896..1d9cf07e 100644 --- a/common/config.go +++ b/common/config.go @@ -78,6 +78,9 @@ func (c *Condition) MatchBranch(branch string) bool { if len(c.Branch) == 0 { return true } + if strings.HasPrefix(branch, "refs/heads/") { + branch = branch[11:] + } match, _ := filepath.Match(c.Branch, branch) return match } diff --git a/parser/parse.go b/parser/parse.go index 7543efc2..2c8390e9 100644 --- a/parser/parse.go +++ b/parser/parse.go @@ -92,3 +92,11 @@ func ParseSingle(raw string, opts *Opts) (*common.Config, error) { } return conf, err } + +func ParseCondition(raw string) (*common.Condition, error) { + c := struct { + Condition *common.Condition `yaml:"when"` + }{} + err := yaml.Unmarshal([]byte(raw), c) + return c.Condition, err +} diff --git a/server/hooks.go b/server/hooks.go index 3ceb6944..4cabcc4b 100644 --- a/server/hooks.go +++ b/server/hooks.go @@ -5,6 +5,7 @@ import ( log "github.com/Sirupsen/logrus" "github.com/drone/drone/common" + "github.com/drone/drone/parser" "github.com/drone/drone/parser/inject" "github.com/drone/drone/parser/matrix" "github.com/drone/drone/queue" @@ -122,11 +123,12 @@ func PostHook(c *gin.Context) { } // verify the branches can be built vs skipped - // s, _ := script.ParseBuild(string(yml)) - // if len(hook.PullRequest) == 0 && !s.MatchBranch(hook.Branch) { - // w.WriteHeader(http.StatusOK) - // return - // } + when, _ := parser.ParseCondition(string(raw)) + if build.Commit != nil && when != nil && !when.MatchBranch(build.Commit.Ref) { + log.Infof("ignoring hook. yaml file excludes repo and branch %s %s", repo.FullName, build.Commit.Ref) + c.AbortWithStatus(200) + return + } err = store.SetBuild(repo.FullName, build) if err != nil {