forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
constraint.go
54 lines (46 loc) · 1013 Bytes
/
constraint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package utils
import (
"github.com/rancher/types/apis/project.cattle.io/v3"
"path/filepath"
)
func MatchAll(cs *v3.Constraints, execution *v3.PipelineExecution) bool {
if cs == nil {
return true
}
m := GetEnvVarMap(execution)
return Match(cs.Branch, m[EnvGitBranch]) &&
Match(cs.Event, m[EnvEvent])
}
func Match(c *v3.Constraint, v string) bool {
if c == nil {
return true
}
if Excludes(c, v) {
return false
}
if Includes(c, v) {
return true
}
if len(c.Include) == 0 {
return true
}
return false
}
// Includes returns true if the string matches the include patterns.
func Includes(c *v3.Constraint, v string) bool {
for _, pattern := range c.Include {
if ok, _ := filepath.Match(pattern, v); ok {
return true
}
}
return false
}
// Excludes returns true if the string matches the exclude patterns.
func Excludes(c *v3.Constraint, v string) bool {
for _, pattern := range c.Exclude {
if ok, _ := filepath.Match(pattern, v); ok {
return true
}
}
return false
}