2021-09-19 12:00:00 +00:00
|
|
|
// Copyright (c) 2019, Drone IO Inc.
|
|
|
|
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
|
2019-02-10 19:00:16 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
package yaml
|
|
|
|
|
2021-09-19 12:14:43 +00:00
|
|
|
import filepath "github.com/bmatcuk/doublestar/v4"
|
2019-01-22 23:44:17 +00:00
|
|
|
|
|
|
|
// Conditions defines a group of conditions.
|
|
|
|
type Conditions struct {
|
2019-06-14 01:11:18 +00:00
|
|
|
Action Condition `json:"action,omitempty"`
|
2019-04-24 14:52:15 +00:00
|
|
|
Cron Condition `json:"cron,omitempty"`
|
|
|
|
Ref Condition `json:"ref,omitempty"`
|
|
|
|
Repo Condition `json:"repo,omitempty"`
|
|
|
|
Instance Condition `json:"instance,omitempty"`
|
|
|
|
Target Condition `json:"target,omitempty"`
|
|
|
|
Event Condition `json:"event,omitempty"`
|
|
|
|
Branch Condition `json:"branch,omitempty"`
|
|
|
|
Status Condition `json:"status,omitempty"`
|
|
|
|
Paths Condition `json:"paths,omitempty"`
|
|
|
|
Matrix map[string]string `json:"matrix,omitempty"`
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Condition defines a runtime condition.
|
|
|
|
type Condition struct {
|
|
|
|
Include []string `yaml:"include,omitempty" json:"include,omitempty"`
|
|
|
|
Exclude []string `yaml:"exclude,omitempty" json:"exclude,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if the string matches the include
|
|
|
|
// patterns and does not match any of the exclude patterns.
|
|
|
|
func (c *Condition) Match(v string) bool {
|
|
|
|
if c.Excludes(v) {
|
|
|
|
return false
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if c.Includes(v) {
|
|
|
|
return true
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if len(c.Include) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Includes returns true if the string matches the include
|
|
|
|
// patterns.
|
|
|
|
func (c *Condition) Includes(v string) bool {
|
|
|
|
for _, pattern := range c.Include {
|
|
|
|
if ok, _ := filepath.Match(pattern, v); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Excludes returns true if the string matches the exclude
|
|
|
|
// patterns.
|
|
|
|
func (c *Condition) Excludes(v string) bool {
|
|
|
|
for _, pattern := range c.Exclude {
|
|
|
|
if ok, _ := filepath.Match(pattern, v); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements yml unmarshalling.
|
|
|
|
func (c *Condition) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
2023-02-08 09:14:20 +00:00
|
|
|
var (
|
|
|
|
out1 string
|
|
|
|
out2 []string
|
|
|
|
)
|
|
|
|
|
2022-04-25 10:56:05 +00:00
|
|
|
out3 := struct {
|
2019-01-22 23:44:17 +00:00
|
|
|
Include []string
|
|
|
|
Exclude []string
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err := unmarshal(&out1)
|
|
|
|
if err == nil {
|
|
|
|
c.Include = []string{out1}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:56:05 +00:00
|
|
|
_ = unmarshal(&out2)
|
|
|
|
_ = unmarshal(&out3)
|
2019-01-22 23:44:17 +00:00
|
|
|
|
2022-04-25 10:56:05 +00:00
|
|
|
out3.Include = append(
|
2019-01-22 23:44:17 +00:00
|
|
|
out3.Include,
|
|
|
|
out2...,
|
|
|
|
)
|
|
|
|
|
2022-04-25 10:56:05 +00:00
|
|
|
c.Exclude = out3.Exclude
|
|
|
|
c.Include = out3.Include
|
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
return nil
|
|
|
|
}
|