2022-01-17 13:43:30 +00:00
|
|
|
package constraint
|
2017-03-05 07:56:08 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-14 19:16:03 +00:00
|
|
|
"fmt"
|
2021-06-28 21:50:35 +00:00
|
|
|
"strings"
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2022-10-05 23:49:23 +00:00
|
|
|
"github.com/antonmedv/expr"
|
2021-09-24 14:29:26 +00:00
|
|
|
"github.com/bmatcuk/doublestar/v4"
|
2021-10-12 07:25:13 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2021-09-24 11:18:34 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types"
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-08-14 17:32:49 +00:00
|
|
|
// When defines a set of runtime constraints.
|
|
|
|
When struct {
|
|
|
|
// If true then read from a list of constraint
|
|
|
|
Constraints []Constraint
|
|
|
|
}
|
|
|
|
|
|
|
|
Constraint struct {
|
2022-01-17 13:43:30 +00:00
|
|
|
Ref List
|
|
|
|
Repo List
|
|
|
|
Instance List
|
|
|
|
Platform List
|
|
|
|
Environment List
|
|
|
|
Event List
|
|
|
|
Branch List
|
2022-08-31 22:36:32 +00:00
|
|
|
Cron List
|
2022-01-17 13:43:30 +00:00
|
|
|
Status List
|
|
|
|
Matrix Map
|
2017-05-06 01:15:47 +00:00
|
|
|
Local types.BoolTrue
|
2022-01-17 13:43:30 +00:00
|
|
|
Path Path
|
2022-10-05 23:49:23 +00:00
|
|
|
Evaluate string `yaml:"evaluate,omitempty"`
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 13:43:30 +00:00
|
|
|
// List defines a runtime constraint for exclude & include string slices.
|
|
|
|
List struct {
|
2017-03-05 07:56:08 +00:00
|
|
|
Include []string
|
|
|
|
Exclude []string
|
|
|
|
}
|
|
|
|
|
2022-01-17 13:43:30 +00:00
|
|
|
// Map defines a runtime constraint for exclude & include map strings.
|
|
|
|
Map struct {
|
2017-03-05 07:56:08 +00:00
|
|
|
Include map[string]string
|
|
|
|
Exclude map[string]string
|
|
|
|
}
|
2021-06-28 21:50:35 +00:00
|
|
|
|
2022-01-17 13:43:30 +00:00
|
|
|
// Path defines a runtime constrain for exclude & include paths.
|
|
|
|
Path struct {
|
2021-06-28 21:50:35 +00:00
|
|
|
Include []string
|
|
|
|
Exclude []string
|
|
|
|
IgnoreMessage string `yaml:"ignore_message,omitempty"`
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
2022-08-14 17:32:49 +00:00
|
|
|
func (when *When) IsEmpty() bool {
|
|
|
|
return len(when.Constraints) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if at least one of the internal constraints is true.
|
2022-10-05 23:49:23 +00:00
|
|
|
func (when *When) Match(metadata frontend.Metadata, global bool) (bool, error) {
|
2022-08-14 17:32:49 +00:00
|
|
|
for _, c := range when.Constraints {
|
2022-10-05 23:49:23 +00:00
|
|
|
match, err := c.Match(metadata, global)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if match {
|
|
|
|
return true, nil
|
2022-08-14 17:32:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-29 22:36:37 +00:00
|
|
|
|
|
|
|
if when.IsEmpty() {
|
|
|
|
// test against default Constraints
|
|
|
|
empty := &Constraint{}
|
2022-09-26 07:27:20 +00:00
|
|
|
return empty.Match(metadata, global)
|
2022-08-29 22:36:37 +00:00
|
|
|
}
|
2022-10-05 23:49:23 +00:00
|
|
|
return false, nil
|
2022-08-14 17:32:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (when *When) IncludesStatus(status string) bool {
|
|
|
|
for _, c := range when.Constraints {
|
|
|
|
if c.Status.Includes(status) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (when *When) ExcludesStatus(status string) bool {
|
|
|
|
for _, c := range when.Constraints {
|
|
|
|
if !c.Status.Excludes(status) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(when.Constraints) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// False if (any) non local
|
|
|
|
func (when *When) IsLocal() bool {
|
|
|
|
for _, c := range when.Constraints {
|
|
|
|
if !c.Local.Bool() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (when *When) UnmarshalYAML(value *yaml.Node) error {
|
2022-08-15 12:39:08 +00:00
|
|
|
switch value.Kind {
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
if err := value.Decode(&when.Constraints); err != nil {
|
2022-08-14 17:32:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-15 12:39:08 +00:00
|
|
|
case yaml.MappingNode:
|
2022-08-14 17:32:49 +00:00
|
|
|
c := Constraint{}
|
2022-08-15 12:39:08 +00:00
|
|
|
if err := value.Decode(&c); err != nil {
|
2022-08-14 17:32:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
when.Constraints = append(when.Constraints, c)
|
|
|
|
|
2022-08-15 12:39:08 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("not supported yaml kind: %v", value.Kind)
|
2022-08-14 17:32:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 12:39:08 +00:00
|
|
|
return nil
|
2022-08-14 17:32:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
// Match returns true if all constraints match the given input. If a single
|
|
|
|
// constraint fails a false value is returned.
|
2022-10-05 23:49:23 +00:00
|
|
|
func (c *Constraint) Match(metadata frontend.Metadata, global bool) (bool, error) {
|
2022-09-26 07:27:20 +00:00
|
|
|
match := true
|
|
|
|
if !global {
|
|
|
|
c.SetDefaultEventFilter()
|
|
|
|
|
|
|
|
// apply step only filters
|
|
|
|
match = c.Matrix.Match(metadata.Job.Matrix)
|
2022-08-29 22:36:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 07:27:20 +00:00
|
|
|
match = match && c.Platform.Match(metadata.Sys.Platform) &&
|
2017-03-05 07:56:08 +00:00
|
|
|
c.Environment.Match(metadata.Curr.Target) &&
|
|
|
|
c.Event.Match(metadata.Curr.Event) &&
|
|
|
|
c.Repo.Match(metadata.Repo.Name) &&
|
2017-09-14 23:53:52 +00:00
|
|
|
c.Ref.Match(metadata.Curr.Commit.Ref) &&
|
2022-09-26 07:27:20 +00:00
|
|
|
c.Instance.Match(metadata.Sys.Host)
|
2022-01-17 13:43:30 +00:00
|
|
|
|
2022-08-14 17:32:49 +00:00
|
|
|
// changed files filter apply only for pull-request and push events
|
2022-01-17 13:43:30 +00:00
|
|
|
if metadata.Curr.Event == frontend.EventPull || metadata.Curr.Event == frontend.EventPush {
|
|
|
|
match = match && c.Path.Match(metadata.Curr.Commit.ChangedFiles, metadata.Curr.Commit.Message)
|
|
|
|
}
|
2022-08-14 17:32:49 +00:00
|
|
|
|
2022-05-16 18:04:00 +00:00
|
|
|
if metadata.Curr.Event != frontend.EventTag {
|
|
|
|
match = match && c.Branch.Match(metadata.Curr.Commit.Branch)
|
|
|
|
}
|
2022-01-17 13:43:30 +00:00
|
|
|
|
2022-08-31 22:36:32 +00:00
|
|
|
if metadata.Curr.Event == frontend.EventCron {
|
|
|
|
match = match && c.Cron.Match(metadata.Curr.Cron)
|
|
|
|
}
|
|
|
|
|
2022-10-05 23:49:23 +00:00
|
|
|
if c.Evaluate != "" {
|
|
|
|
env := metadata.Environ()
|
|
|
|
out, err := expr.Compile(c.Evaluate, expr.Env(env), expr.AsBool())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
result, err := expr.Run(out, env)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
match = match && result.(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
return match, nil
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 07:27:20 +00:00
|
|
|
// SetDefaultEventFilter set default e event filter if not event filter is already set
|
|
|
|
func (c *Constraint) SetDefaultEventFilter() {
|
|
|
|
if c.Event.IsEmpty() {
|
|
|
|
c.Event.Include = []string{
|
|
|
|
frontend.EventPush,
|
|
|
|
frontend.EventPull,
|
|
|
|
frontend.EventTag,
|
|
|
|
frontend.EventDeploy,
|
2022-09-27 09:05:00 +00:00
|
|
|
frontend.EventManual,
|
2022-09-26 07:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 22:36:37 +00:00
|
|
|
// IsEmpty return true if a constraint has no conditions
|
|
|
|
func (c List) IsEmpty() bool {
|
|
|
|
return len(c.Include) == 0 && len(c.Exclude) == 0
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
// Match returns true if the string matches the include patterns and does not
|
|
|
|
// match any of the exclude patterns.
|
2022-01-17 13:43:30 +00:00
|
|
|
func (c *List) Match(v string) bool {
|
2017-03-05 07:56:08 +00:00
|
|
|
if c.Excludes(v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.Includes(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(c.Include) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Includes returns true if the string matches the include patterns.
|
2022-01-17 13:43:30 +00:00
|
|
|
func (c *List) Includes(v string) bool {
|
2017-03-05 07:56:08 +00:00
|
|
|
for _, pattern := range c.Include {
|
2022-01-17 13:43:30 +00:00
|
|
|
if ok, _ := doublestar.Match(pattern, v); ok {
|
2017-03-05 07:56:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Excludes returns true if the string matches the exclude patterns.
|
2022-01-17 13:43:30 +00:00
|
|
|
func (c *List) Excludes(v string) bool {
|
2017-03-05 07:56:08 +00:00
|
|
|
for _, pattern := range c.Exclude {
|
2022-01-17 13:43:30 +00:00
|
|
|
if ok, _ := doublestar.Match(pattern, v); ok {
|
2017-03-05 07:56:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML unmarshals the constraint.
|
2022-01-17 13:43:30 +00:00
|
|
|
func (c *List) UnmarshalYAML(value *yaml.Node) error {
|
2022-01-05 20:50:23 +00:00
|
|
|
out1 := struct {
|
2021-10-30 15:52:02 +00:00
|
|
|
Include types.Stringorslice
|
|
|
|
Exclude types.Stringorslice
|
2017-03-05 07:56:08 +00:00
|
|
|
}{}
|
|
|
|
|
2021-10-30 15:52:02 +00:00
|
|
|
var out2 types.Stringorslice
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2019-11-14 19:16:03 +00:00
|
|
|
err1 := value.Decode(&out1)
|
|
|
|
err2 := value.Decode(&out2)
|
2017-03-05 07:56:08 +00:00
|
|
|
|
|
|
|
c.Exclude = out1.Exclude
|
|
|
|
c.Include = append(
|
|
|
|
out1.Include,
|
|
|
|
out2...,
|
|
|
|
)
|
2019-11-14 19:16:03 +00:00
|
|
|
|
|
|
|
if err1 != nil && err2 != nil {
|
|
|
|
y, _ := yaml.Marshal(value)
|
|
|
|
return fmt.Errorf("Could not parse condition: %s", y)
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if the params matches the include key values and does not
|
|
|
|
// match any of the exclude key values.
|
2022-01-17 13:43:30 +00:00
|
|
|
func (c *Map) Match(params map[string]string) bool {
|
2017-03-05 07:56:08 +00:00
|
|
|
// when no includes or excludes automatically match
|
|
|
|
if len(c.Include) == 0 && len(c.Exclude) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2022-08-14 17:32:49 +00:00
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
// exclusions are processed first. So we can include everything and then
|
|
|
|
// selectively include others.
|
|
|
|
if len(c.Exclude) != 0 {
|
|
|
|
var matches int
|
|
|
|
|
|
|
|
for key, val := range c.Exclude {
|
2022-01-17 13:43:30 +00:00
|
|
|
if ok, _ := doublestar.Match(val, params[key]); ok {
|
2017-03-05 07:56:08 +00:00
|
|
|
matches++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if matches == len(c.Exclude) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for key, val := range c.Include {
|
2022-01-17 13:43:30 +00:00
|
|
|
if ok, _ := doublestar.Match(val, params[key]); !ok {
|
2017-03-05 07:56:08 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML unmarshals the constraint map.
|
2022-01-17 13:43:30 +00:00
|
|
|
func (c *Map) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
2017-03-05 07:56:08 +00:00
|
|
|
out1 := struct {
|
|
|
|
Include map[string]string
|
|
|
|
Exclude map[string]string
|
|
|
|
}{
|
|
|
|
Include: map[string]string{},
|
|
|
|
Exclude: map[string]string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
out2 := map[string]string{}
|
|
|
|
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = unmarshal(&out1) // it contains include and exclude statement
|
|
|
|
_ = unmarshal(&out2) // it contains no include/exclude statement, assume include as default
|
2017-03-05 07:56:08 +00:00
|
|
|
|
|
|
|
c.Include = out1.Include
|
|
|
|
c.Exclude = out1.Exclude
|
|
|
|
for k, v := range out2 {
|
|
|
|
c.Include[k] = v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-28 21:50:35 +00:00
|
|
|
|
|
|
|
// UnmarshalYAML unmarshals the constraint.
|
2022-01-17 13:43:30 +00:00
|
|
|
func (c *Path) UnmarshalYAML(value *yaml.Node) error {
|
2022-01-05 20:50:23 +00:00
|
|
|
out1 := struct {
|
2021-10-30 15:52:02 +00:00
|
|
|
Include types.Stringorslice `yaml:"include,omitempty"`
|
|
|
|
Exclude types.Stringorslice `yaml:"exclude,omitempty"`
|
|
|
|
IgnoreMessage string `yaml:"ignore_message,omitempty"`
|
2021-06-28 21:50:35 +00:00
|
|
|
}{}
|
|
|
|
|
2021-10-30 15:52:02 +00:00
|
|
|
var out2 types.Stringorslice
|
2021-06-28 21:50:35 +00:00
|
|
|
|
|
|
|
err1 := value.Decode(&out1)
|
|
|
|
err2 := value.Decode(&out2)
|
|
|
|
|
|
|
|
c.Exclude = out1.Exclude
|
|
|
|
c.IgnoreMessage = out1.IgnoreMessage
|
|
|
|
c.Include = append(
|
|
|
|
out1.Include,
|
|
|
|
out2...,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err1 != nil && err2 != nil {
|
|
|
|
y, _ := yaml.Marshal(value)
|
|
|
|
return fmt.Errorf("Could not parse condition: %s", y)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if file paths in string slice matches the include and not exclude patterns
|
2022-08-14 17:32:49 +00:00
|
|
|
// or if commit message contains ignore message.
|
2022-01-17 13:43:30 +00:00
|
|
|
func (c *Path) Match(v []string, message string) bool {
|
2021-06-28 21:50:35 +00:00
|
|
|
// ignore file pattern matches if the commit message contains a pattern
|
|
|
|
if len(c.IgnoreMessage) > 0 && strings.Contains(strings.ToLower(message), strings.ToLower(c.IgnoreMessage)) {
|
|
|
|
return true
|
|
|
|
}
|
2022-08-14 17:32:49 +00:00
|
|
|
|
2021-06-28 21:50:35 +00:00
|
|
|
// always match if there are no commit files (empty commit)
|
|
|
|
if len(v) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Exclude) > 0 && c.Excludes(v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(c.Include) > 0 && !c.Includes(v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Includes returns true if the string matches any of the include patterns.
|
2022-01-17 13:43:30 +00:00
|
|
|
func (c *Path) Includes(v []string) bool {
|
2021-06-28 21:50:35 +00:00
|
|
|
for _, pattern := range c.Include {
|
|
|
|
for _, file := range v {
|
2021-09-21 04:18:43 +00:00
|
|
|
if ok, _ := doublestar.Match(pattern, file); ok {
|
2021-06-28 21:50:35 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Excludes returns true if the string matches any of the exclude patterns.
|
2022-01-17 13:43:30 +00:00
|
|
|
func (c *Path) Excludes(v []string) bool {
|
2021-06-28 21:50:35 +00:00
|
|
|
for _, pattern := range c.Exclude {
|
|
|
|
for _, file := range v {
|
2021-09-21 04:18:43 +00:00
|
|
|
if ok, _ := doublestar.Match(pattern, file); ok {
|
2021-06-28 21:50:35 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|