2023-08-10 09:06:00 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
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"
|
2023-10-09 07:11:08 +00:00
|
|
|
"maps"
|
2023-06-04 22:15:07 +00:00
|
|
|
"path"
|
2021-06-28 21:50:35 +00:00
|
|
|
"strings"
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
"github.com/bmatcuk/doublestar/v4"
|
2023-12-04 19:53:46 +00:00
|
|
|
"github.com/expr-lang/expr"
|
2023-11-03 10:44:03 +00:00
|
|
|
"go.uber.org/multierr"
|
2021-10-12 07:25:13 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
|
|
|
|
yamlBaseTypes "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/types/base"
|
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
|
2023-10-09 07:11:08 +00:00
|
|
|
Local yamlBaseTypes.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.
|
2023-07-02 21:45:22 +00:00
|
|
|
func (when *When) Match(metadata metadata.Metadata, global bool, env map[string]string) (bool, error) {
|
2022-08-14 17:32:49 +00:00
|
|
|
for _, c := range when.Constraints {
|
2023-07-02 21:45:22 +00:00
|
|
|
match, err := c.Match(metadata, global, env)
|
2022-10-05 23:49:23 +00:00
|
|
|
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{}
|
2023-07-02 21:45:22 +00:00
|
|
|
return empty.Match(metadata, global, env)
|
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
|
|
|
}
|
|
|
|
|
2023-01-02 05:36:57 +00:00
|
|
|
func (when *When) IncludesStatusFailure() bool {
|
2022-08-14 17:32:49 +00:00
|
|
|
for _, c := range when.Constraints {
|
2023-01-02 05:36:57 +00:00
|
|
|
if c.Status.Includes("failure") {
|
2022-08-14 17:32:49 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-02 05:36:57 +00:00
|
|
|
func (when *When) IncludesStatusSuccess() bool {
|
|
|
|
// "success" acts differently than "failure" in that it's
|
|
|
|
// presumed to be included unless it's specifically not part
|
|
|
|
// of the list
|
|
|
|
if when.IsEmpty() {
|
|
|
|
return true
|
|
|
|
}
|
2022-08-14 17:32:49 +00:00
|
|
|
for _, c := range when.Constraints {
|
2023-01-02 05:36:57 +00:00
|
|
|
if len(c.Status.Include) == 0 || c.Status.Includes("success") {
|
|
|
|
return true
|
2022-08-14 17:32:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-02 05:36:57 +00:00
|
|
|
return false
|
2022-08-14 17:32:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2023-07-02 21:45:22 +00:00
|
|
|
func (c *Constraint) Match(m metadata.Metadata, global bool, env map[string]string) (bool, error) {
|
2022-09-26 07:27:20 +00:00
|
|
|
match := true
|
|
|
|
if !global {
|
|
|
|
// apply step only filters
|
2023-06-04 22:15:07 +00:00
|
|
|
match = c.Matrix.Match(m.Workflow.Matrix)
|
2022-08-29 22:36:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
match = match && c.Platform.Match(m.Sys.Platform) &&
|
|
|
|
c.Environment.Match(m.Curr.Target) &&
|
|
|
|
c.Event.Match(m.Curr.Event) &&
|
|
|
|
c.Repo.Match(path.Join(m.Repo.Owner, m.Repo.Name)) &&
|
|
|
|
c.Ref.Match(m.Curr.Commit.Ref) &&
|
|
|
|
c.Instance.Match(m.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
|
2023-06-04 22:15:07 +00:00
|
|
|
if m.Curr.Event == metadata.EventPull || m.Curr.Event == metadata.EventPush {
|
|
|
|
match = match && c.Path.Match(m.Curr.Commit.ChangedFiles, m.Curr.Commit.Message)
|
2022-01-17 13:43:30 +00:00
|
|
|
}
|
2022-08-14 17:32:49 +00:00
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
if m.Curr.Event != metadata.EventTag {
|
|
|
|
match = match && c.Branch.Match(m.Curr.Commit.Branch)
|
2022-05-16 18:04:00 +00:00
|
|
|
}
|
2022-01-17 13:43:30 +00:00
|
|
|
|
2023-06-04 22:15:07 +00:00
|
|
|
if m.Curr.Event == metadata.EventCron {
|
|
|
|
match = match && c.Cron.Match(m.Curr.Cron)
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 23:49:23 +00:00
|
|
|
if c.Evaluate != "" {
|
2023-07-02 21:45:22 +00:00
|
|
|
if env == nil {
|
|
|
|
env = m.Environ()
|
|
|
|
} else {
|
|
|
|
maps.Copy(env, m.Environ())
|
|
|
|
}
|
|
|
|
out, err := expr.Compile(c.Evaluate, expr.Env(env), expr.AllowUndefinedVariables(), expr.AsBool())
|
2022-10-05 23:49:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
result, err := expr.Run(out, env)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2024-01-12 01:01:02 +00:00
|
|
|
bresult, ok := result.(bool)
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("could not parse result: %v", result)
|
|
|
|
}
|
|
|
|
match = match && bresult
|
2022-10-05 23:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return match, nil
|
2017-03-05 07:56:08 +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 {
|
2023-10-09 07:11:08 +00:00
|
|
|
Include yamlBaseTypes.StringOrSlice
|
|
|
|
Exclude yamlBaseTypes.StringOrSlice
|
2017-03-05 07:56:08 +00:00
|
|
|
}{}
|
|
|
|
|
2023-10-09 07:11:08 +00:00
|
|
|
var out2 yamlBaseTypes.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
|
2024-01-10 14:34:44 +00:00
|
|
|
c.Include = append( //nolint:gocritic
|
2017-03-05 07:56:08 +00:00
|
|
|
out1.Include,
|
|
|
|
out2...,
|
|
|
|
)
|
2019-11-14 19:16:03 +00:00
|
|
|
|
|
|
|
if err1 != nil && err2 != nil {
|
|
|
|
y, _ := yaml.Marshal(value)
|
2024-01-10 21:56:42 +00:00
|
|
|
return fmt.Errorf("could not parse condition: %s: %w", y, multierr.Append(err1, err2))
|
2019-11-14 19:16:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-10-09 07:11:08 +00:00
|
|
|
// Exclusions are processed first. So we can include everything and then
|
2017-03-05 07:56:08 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:08:02 +00:00
|
|
|
// UnmarshalYAML unmarshal the constraint map.
|
2023-11-12 17:23:48 +00:00
|
|
|
func (c *Map) UnmarshalYAML(unmarshal func(any) 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
|
|
|
|
2023-02-01 23:08:02 +00:00
|
|
|
// UnmarshalYAML unmarshal 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 {
|
2023-10-09 07:11:08 +00:00
|
|
|
Include yamlBaseTypes.StringOrSlice `yaml:"include,omitempty"`
|
|
|
|
Exclude yamlBaseTypes.StringOrSlice `yaml:"exclude,omitempty"`
|
|
|
|
IgnoreMessage string `yaml:"ignore_message,omitempty"`
|
2021-06-28 21:50:35 +00:00
|
|
|
}{}
|
|
|
|
|
2023-10-09 07:11:08 +00:00
|
|
|
var out2 yamlBaseTypes.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
|
2024-01-10 14:34:44 +00:00
|
|
|
c.Include = append( //nolint:gocritic
|
2021-06-28 21:50:35 +00:00
|
|
|
out1.Include,
|
|
|
|
out2...,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err1 != nil && err2 != nil {
|
|
|
|
y, _ := yaml.Marshal(value)
|
2024-01-10 21:56:42 +00:00
|
|
|
return fmt.Errorf("could not parse condition: %s", y)
|
2021-06-28 21:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|