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 {