github.com/drone/runner-go@v1.12.0/manifest/cond.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 package manifest 6 7 import filepath "github.com/bmatcuk/doublestar" 8 9 // Match provides match creteria for evaluation. 10 type Match struct { 11 Action string 12 Branch string 13 Cron string 14 Event string 15 Instance string 16 Ref string 17 Repo string 18 Target string 19 Paths []string 20 } 21 22 // Conditions defines a group of conditions. 23 type Conditions struct { 24 Action Condition `json:"action,omitempty"` 25 Cron Condition `json:"cron,omitempty"` 26 Ref Condition `json:"ref,omitempty"` 27 Repo Condition `json:"repo,omitempty"` 28 Instance Condition `json:"instance,omitempty"` 29 Target Condition `json:"target,omitempty"` 30 Event Condition `json:"event,omitempty"` 31 Branch Condition `json:"branch,omitempty"` 32 Status Condition `json:"status,omitempty"` 33 Paths Condition `json:"paths,omitempty"` 34 } 35 36 // Match returns true if the string matches the include 37 // patterns and does not match any of the exclude patterns. 38 func (c Conditions) Match(m Match) bool { 39 return c.Cron.Match(m.Cron) && 40 c.Ref.Match(m.Ref) && 41 c.Repo.Match(m.Repo) && 42 c.Instance.Match(m.Instance) && 43 c.Target.Match(m.Target) && 44 c.Event.Match(m.Event) && 45 c.Branch.Match(m.Branch) && 46 c.Action.Match(m.Action) 47 } 48 49 // Condition defines a runtime condition. 50 type Condition struct { 51 Include []string `yaml:"include,omitempty" json:"include,omitempty"` 52 Exclude []string `yaml:"exclude,omitempty" json:"exclude,omitempty"` 53 } 54 55 // Match returns true if the string matches the include 56 // patterns and does not match any of the exclude patterns. 57 func (c *Condition) Match(v string) bool { 58 if c.Excludes(v) { 59 return false 60 } 61 if c.Includes(v) { 62 return true 63 } 64 if len(c.Include) == 0 { 65 return true 66 } 67 return false 68 } 69 70 // Includes returns true if the string matches the include 71 // patterns. 72 func (c *Condition) Includes(v string) bool { 73 for _, pattern := range c.Include { 74 if ok, _ := filepath.Match(pattern, v); ok { 75 return true 76 } 77 } 78 return false 79 } 80 81 // Excludes returns true if the string matches the exclude 82 // patterns. 83 func (c *Condition) Excludes(v string) bool { 84 for _, pattern := range c.Exclude { 85 if ok, _ := filepath.Match(pattern, v); ok { 86 return true 87 } 88 } 89 return false 90 } 91 92 // UnmarshalYAML implements yml unmarshalling. 93 func (c *Condition) UnmarshalYAML(unmarshal func(interface{}) error) error { 94 var out1 string 95 var out2 []string 96 var out3 = struct { 97 Include []string 98 Exclude []string 99 }{} 100 101 err := unmarshal(&out1) 102 if err == nil { 103 c.Include = []string{out1} 104 return nil 105 } 106 107 unmarshal(&out2) 108 unmarshal(&out3) 109 110 c.Exclude = out3.Exclude 111 c.Include = append( 112 out3.Include, 113 out2..., 114 ) 115 116 return nil 117 }