-
Notifications
You must be signed in to change notification settings - Fork 13
/
ruleset.go
170 lines (149 loc) · 7.29 KB
/
ruleset.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SPDX-License-Identifier: Apache-2.0
package yaml
import (
"github.com/go-vela/types/constants"
"github.com/go-vela/types/pipeline"
"github.com/go-vela/types/raw"
)
type (
// Ruleset is the yaml representation of a
// ruleset block for a step in a pipeline.
Ruleset struct {
If Rules `yaml:"if,omitempty" json:"if,omitempty" jsonschema:"description=Limit execution to when all rules match.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Unless Rules `yaml:"unless,omitempty" json:"unless,omitempty" jsonschema:"description=Limit execution to when all rules do not match.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Matcher string `yaml:"matcher,omitempty" json:"matcher,omitempty" jsonschema:"enum=filepath,enum=regexp,default=filepath,description=Use the defined matching method.\nReference: coming soon"`
Operator string `yaml:"operator,omitempty" json:"operator,omitempty" jsonschema:"enum=or,enum=and,default=and,description=Whether all rule conditions must be met or just any one of them.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Continue bool `yaml:"continue,omitempty" json:"continue,omitempty" jsonschema:"default=false,description=Limits the execution of a step to continuing on any failure.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
}
// Rules is the yaml representation of the ruletypes
// from a ruleset block for a step in a pipeline.
Rules struct {
Branch []string `yaml:"branch,omitempty,flow" json:"branch,omitempty" jsonschema:"description=Limits the execution of a step to matching build branches.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Comment []string `yaml:"comment,omitempty,flow" json:"comment,omitempty" jsonschema:"description=Limits the execution of a step to matching a pull request comment.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Event []string `yaml:"event,omitempty,flow" json:"event,omitempty" jsonschema:"description=Limits the execution of a step to matching build events.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Path []string `yaml:"path,omitempty,flow" json:"path,omitempty" jsonschema:"description=Limits the execution of a step to matching files changed in a repository.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Repo []string `yaml:"repo,omitempty,flow" json:"repo,omitempty" jsonschema:"description=Limits the execution of a step to matching repos.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Status []string `yaml:"status,omitempty,flow" json:"status,omitempty" jsonschema:"enum=[failure],enum=[success],description=Limits the execution of a step to matching build statuses.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Tag []string `yaml:"tag,omitempty,flow" json:"tag,omitempty" jsonschema:"description=Limits the execution of a step to matching build tag references.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
Target []string `yaml:"target,omitempty,flow" json:"target,omitempty" jsonschema:"description=Limits the execution of a step to matching build deployment targets.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ruleset-tag"`
}
)
// ToPipeline converts the Ruleset type
// to a pipeline Ruleset type.
func (r *Ruleset) ToPipeline() *pipeline.Ruleset {
return &pipeline.Ruleset{
If: *r.If.ToPipeline(),
Unless: *r.Unless.ToPipeline(),
Matcher: r.Matcher,
Operator: r.Operator,
Continue: r.Continue,
}
}
// UnmarshalYAML implements the Unmarshaler interface for the Ruleset type.
func (r *Ruleset) UnmarshalYAML(unmarshal func(interface{}) error) error {
// simple struct we try unmarshalling to
simple := new(Rules)
// advanced struct we try unmarshalling to
advanced := new(struct {
If Rules
Unless Rules
Matcher string
Operator string
Continue bool
})
// attempt to unmarshal simple ruleset
//nolint:errcheck // intentionally not handling error
unmarshal(simple)
// attempt to unmarshal advanced ruleset
//nolint:errcheck // intentionally not handling error
unmarshal(advanced)
// set ruleset `unless` to advanced `unless` rules
r.Unless = advanced.Unless
// set ruleset `matcher` to advanced `matcher`
r.Matcher = advanced.Matcher
// set ruleset `operator` to advanced `operator`
r.Operator = advanced.Operator
// set ruleset `continue` to advanced `continue`
r.Continue = advanced.Continue
// implicitly add simple ruleset to the advanced ruleset for each rule type
advanced.If.Branch = append(advanced.If.Branch, simple.Branch...)
advanced.If.Comment = append(advanced.If.Comment, simple.Comment...)
advanced.If.Event = append(advanced.If.Event, simple.Event...)
advanced.If.Path = append(advanced.If.Path, simple.Path...)
advanced.If.Repo = append(advanced.If.Repo, simple.Repo...)
advanced.If.Status = append(advanced.If.Status, simple.Status...)
advanced.If.Tag = append(advanced.If.Tag, simple.Tag...)
advanced.If.Target = append(advanced.If.Target, simple.Target...)
// set ruleset `if` to advanced `if` rules
r.If = advanced.If
// implicitly set `matcher` field if empty for ruleset
if len(r.Matcher) == 0 {
r.Matcher = constants.MatcherFilepath
}
// implicitly set `operator` field if empty for ruleset
if len(r.Operator) == 0 {
r.Operator = constants.OperatorAnd
}
return nil
}
// ToPipeline converts the Rules
// type to a pipeline Rules type.
func (r *Rules) ToPipeline() *pipeline.Rules {
return &pipeline.Rules{
Branch: r.Branch,
Comment: r.Comment,
Event: r.Event,
Path: r.Path,
Repo: r.Repo,
Status: r.Status,
Tag: r.Tag,
Target: r.Target,
}
}
// UnmarshalYAML implements the Unmarshaler interface for the Rules type.
func (r *Rules) UnmarshalYAML(unmarshal func(interface{}) error) error {
// rules struct we try unmarshalling to
rules := new(struct {
Branch raw.StringSlice
Comment raw.StringSlice
Event raw.StringSlice
Path raw.StringSlice
Repo raw.StringSlice
Status raw.StringSlice
Tag raw.StringSlice
Target raw.StringSlice
})
// attempt to unmarshal rules
err := unmarshal(rules)
if err == nil {
r.Branch = rules.Branch
r.Comment = rules.Comment
r.Path = rules.Path
r.Repo = rules.Repo
r.Status = rules.Status
r.Tag = rules.Tag
r.Target = rules.Target
// account for users who use non-scoped pull_request event
events := []string{}
for _, e := range rules.Event {
switch e {
// backwards compatibility
// pull_request = pull_request:opened + pull_request:synchronize + pull_request:reopened
// comment = comment:created + comment:edited
case constants.EventPull:
events = append(events,
constants.EventPull+":"+constants.ActionOpened,
constants.EventPull+":"+constants.ActionSynchronize,
constants.EventPull+":"+constants.ActionReopened)
case constants.EventComment:
events = append(events,
constants.EventComment+":"+constants.ActionCreated,
constants.EventComment+":"+constants.ActionEdited)
default:
events = append(events, e)
}
}
r.Event = events
}
return err
}