forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
75 lines (65 loc) · 1.86 KB
/
filter.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package transform
import (
"github.com/drone/drone/model"
"github.com/drone/drone/yaml"
)
// ChangeFilter is a transform function that alters status constraints set to
// change and replaces with the opposite of the prior status.
func ChangeFilter(conf *yaml.Config, prev string) {
for _, step := range conf.Pipeline {
for i, status := range step.Constraints.Status.Include {
if status != "change" && status != "changed" {
continue
}
if prev == model.StatusFailure {
step.Constraints.Status.Include[i] = model.StatusSuccess
} else {
step.Constraints.Status.Include[i] = model.StatusFailure
}
}
}
}
// DefaultFilter is a transform function that applies default Filters to each
// step in the Yaml specification file.
func DefaultFilter(conf *yaml.Config) {
for _, step := range conf.Pipeline {
defaultStatus(step)
defaultEvent(step)
}
}
// defaultStatus sets default status conditions.
func defaultStatus(c *yaml.Container) {
if !isEmpty(c.Constraints.Status) {
return
}
c.Constraints.Status.Include = []string{
model.StatusSuccess,
}
}
// defaultEvent sets default event conditions.
func defaultEvent(c *yaml.Container) {
if !isEmpty(c.Constraints.Event) {
return
}
if isPlugin(c) && !isClone(c) {
c.Constraints.Event.Exclude = []string{
model.EventPull,
}
}
}
// helper function returns true if the step is a clone step.
func isEmpty(c yaml.Constraint) bool {
return len(c.Include) == 0 && len(c.Exclude) == 0
}
// helper function returns true if the step is a plugin step.
func isPlugin(c *yaml.Container) bool {
return len(c.Commands) == 0 || len(c.Vargs) != 0
}
// helper function returns true if the step is a command step.
func isCommand(c *yaml.Container) bool {
return len(c.Commands) != 0
}
// helper function returns true if the step is a clone step.
func isClone(c *yaml.Container) bool {
return c.Name == "clone"
}