From 90a25cc06caa352e369704147aa0ca7d5f27f743 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 16 Jan 2022 20:00:33 +0100 Subject: [PATCH 01/11] pipeline compiler: use const for strings --- pipeline/frontend/yaml/compiler/compiler.go | 27 +++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/pipeline/frontend/yaml/compiler/compiler.go b/pipeline/frontend/yaml/compiler/compiler.go index 807cbbd379..cbff32ec79 100644 --- a/pipeline/frontend/yaml/compiler/compiler.go +++ b/pipeline/frontend/yaml/compiler/compiler.go @@ -2,6 +2,7 @@ package compiler import ( "fmt" + "strings" backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types" "github.com/woodpecker-ci/woodpecker/pipeline/frontend" @@ -11,6 +12,16 @@ import ( // TODO(bradrydzewski) compiler should handle user-defined volumes from YAML // TODO(bradrydzewski) compiler should handle user-defined networks from YAML +const ( + windowsPrefix = "windows/" + + defaultCloneImage = "woodpeckerci/plugin-git:latest" + defaultCloneName = "clone" + + networkDriverNAT = "nat" + networkDriverBridge = "bridge" +) + type Registry struct { Hostname string Username string @@ -77,15 +88,15 @@ func (c *Compiler) Compile(conf *yaml.Config) *backend.Config { }) // create a default network - if c.metadata.Sys.Arch == "windows/amd64" { + if strings.HasPrefix(c.metadata.Sys.Arch, windowsPrefix) { config.Networks = append(config.Networks, &backend.Network{ Name: fmt.Sprintf("%s_default", c.prefix), - Driver: "nat", + Driver: networkDriverNAT, }) } else { config.Networks = append(config.Networks, &backend.Network{ Name: fmt.Sprintf("%s_default", c.prefix), - Driver: "bridge", + Driver: networkDriverBridge, }) } @@ -110,17 +121,17 @@ func (c *Compiler) Compile(conf *yaml.Config) *backend.Config { // add default clone step if !c.local && len(conf.Clone.Containers) == 0 && !conf.SkipClone { container := &yaml.Container{ - Name: "clone", - Image: "woodpeckerci/plugin-git:latest", + Name: defaultCloneName, + Image: defaultCloneImage, Settings: map[string]interface{}{"depth": "0"}, Environment: c.cloneEnv, } name := fmt.Sprintf("%s_clone", c.prefix) - step := c.createProcess(name, container, "clone") + step := c.createProcess(name, container, defaultCloneName) stage := new(backend.Stage) stage.Name = name - stage.Alias = "clone" + stage.Alias = defaultCloneName stage.Steps = append(stage.Steps, step) config.Stages = append(config.Stages, stage) @@ -134,7 +145,7 @@ func (c *Compiler) Compile(conf *yaml.Config) *backend.Config { stage.Alias = container.Name name := fmt.Sprintf("%s_clone_%d", c.prefix, i) - step := c.createProcess(name, container, "clone") + step := c.createProcess(name, container, defaultCloneName) for k, v := range c.cloneEnv { step.Environment[k] = v } From 08063c3f1713b316976d71783f9c55976eba7d9b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 16 Jan 2022 20:33:38 +0100 Subject: [PATCH 02/11] re-enable environment constraint tests --- pipeline/frontend/yaml/constraint.go | 4 ++-- pipeline/frontend/yaml/constraint_test.go | 28 ++++++++++------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/pipeline/frontend/yaml/constraint.go b/pipeline/frontend/yaml/constraint.go index d0b5aa636a..c8f3e835a8 100644 --- a/pipeline/frontend/yaml/constraint.go +++ b/pipeline/frontend/yaml/constraint.go @@ -136,7 +136,7 @@ func (c *ConstraintMap) Match(params map[string]string) bool { var matches int for key, val := range c.Exclude { - if params[key] == val { + if ok, _ := doublestar.Match(val, params[key]); ok { matches++ } } @@ -145,7 +145,7 @@ func (c *ConstraintMap) Match(params map[string]string) bool { } } for key, val := range c.Include { - if params[key] != val { + if ok, _ := doublestar.Match(val, params[key]); !ok { return false } } diff --git a/pipeline/frontend/yaml/constraint_test.go b/pipeline/frontend/yaml/constraint_test.go index 40373c42f9..e53eadbd22 100644 --- a/pipeline/frontend/yaml/constraint_test.go +++ b/pipeline/frontend/yaml/constraint_test.go @@ -285,11 +285,10 @@ func TestConstraintMap(t *testing.T) { with: map[string]string{"GOLANG": "1.7", "REDIS": "3.0"}, want: false, }, - // TODO(bradrydzewski) eventually we should enable wildcard matching { conf: "{ GOLANG: 1.7, REDIS: 3.* }", with: map[string]string{"GOLANG": "1.7", "REDIS": "3.0"}, - want: false, + want: true, }, // include syntax { @@ -368,10 +367,7 @@ func TestConstraintMap(t *testing.T) { } for _, test := range testdata { c := parseConstraintMap(t, test.conf) - got, want := c.Match(test.with), test.want - if got != want { - t.Errorf("Expect %q matches %q is %v", test.with, test.conf, want) - } + assert.Equal(t, test.want, c.Match(test.with), "config: '%s', with: '%s'", test.conf, test.with) } } @@ -399,16 +395,16 @@ func TestConstraints(t *testing.T) { want: true, }, // environment constraint - // { - // conf: "{ branch: develop }", - // with: frontend.Metadata{Curr: frontend.Build{Commit: frontend.Commit{Branch: "master"}}}, - // want: false, - // }, - // { - // conf: "{ branch: master }", - // with: frontend.Metadata{Curr: frontend.Build{Commit: frontend.Commit{Branch: "master"}}}, - // want: true, - // }, + { + conf: "{ branch: develop }", + with: frontend.Metadata{Curr: frontend.Build{Commit: frontend.Commit{Branch: "master"}}}, + want: false, + }, + { + conf: "{ branch: master }", + with: frontend.Metadata{Curr: frontend.Build{Commit: frontend.Commit{Branch: "master"}}}, + want: true, + }, // repo constraint { conf: "{ repo: owner/* }", From 21e4fe59e23b53341f57df8628053a8b390084d5 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 16 Jan 2022 20:41:03 +0100 Subject: [PATCH 03/11] use doublestar (glob pattern) for all constrains --- pipeline/frontend/yaml/constraint.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pipeline/frontend/yaml/constraint.go b/pipeline/frontend/yaml/constraint.go index c8f3e835a8..ea5cc8dd97 100644 --- a/pipeline/frontend/yaml/constraint.go +++ b/pipeline/frontend/yaml/constraint.go @@ -2,7 +2,6 @@ package yaml import ( "fmt" - "path/filepath" "strings" "github.com/bmatcuk/doublestar/v4" @@ -80,7 +79,7 @@ func (c *Constraint) Match(v string) bool { // Includes returns true if the string matches the include patterns. func (c *Constraint) Includes(v string) bool { for _, pattern := range c.Include { - if ok, _ := filepath.Match(pattern, v); ok { + if ok, _ := doublestar.Match(pattern, v); ok { return true } } @@ -90,7 +89,7 @@ func (c *Constraint) Includes(v string) bool { // Excludes returns true if the string matches the exclude patterns. func (c *Constraint) Excludes(v string) bool { for _, pattern := range c.Exclude { - if ok, _ := filepath.Match(pattern, v); ok { + if ok, _ := doublestar.Match(pattern, v); ok { return true } } From ea32d95f49bc0ec95946ffcc805bb8fba7e21d70 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 16 Jan 2022 20:46:14 +0100 Subject: [PATCH 04/11] more tests --- pipeline/frontend/yaml/constraint_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pipeline/frontend/yaml/constraint_test.go b/pipeline/frontend/yaml/constraint_test.go index e53eadbd22..072487430c 100644 --- a/pipeline/frontend/yaml/constraint_test.go +++ b/pipeline/frontend/yaml/constraint_test.go @@ -290,6 +290,16 @@ func TestConstraintMap(t *testing.T) { with: map[string]string{"GOLANG": "1.7", "REDIS": "3.0"}, want: true, }, + { + conf: "{ GOLANG: 1.7, BRANCH: release/**/test }", + with: map[string]string{"GOLANG": "1.7", "BRANCH": "release/v1.12.1//test"}, + want: true, + }, + { + conf: "{ GOLANG: 1.7, BRANCH: release/**/test }", + with: map[string]string{"GOLANG": "1.7", "BRANCH": "release/v1.12.1/qest"}, + want: false, + }, // include syntax { conf: "include: { GOLANG: 1.7 }", From 743f7cd88c3b0a5618eb565606dc5b094aae6ef0 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 16 Jan 2022 20:52:38 +0100 Subject: [PATCH 05/11] move constraint code into own package --- pipeline/frontend/yaml/config.go | 3 ++- .../yaml/{ => constraint}/constraint.go | 2 +- .../yaml/{ => constraint}/constraint_test.go | 2 +- pipeline/frontend/yaml/container.go | 3 ++- pipeline/frontend/yaml/container_test.go | 17 +++++++++-------- 5 files changed, 15 insertions(+), 12 deletions(-) rename pipeline/frontend/yaml/{ => constraint}/constraint.go (99%) rename pipeline/frontend/yaml/{ => constraint}/constraint_test.go (99%) diff --git a/pipeline/frontend/yaml/config.go b/pipeline/frontend/yaml/config.go index 27c4517a76..eaa78d5687 100644 --- a/pipeline/frontend/yaml/config.go +++ b/pipeline/frontend/yaml/config.go @@ -3,6 +3,7 @@ package yaml import ( "gopkg.in/yaml.v3" + "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/constraint" "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types" ) @@ -11,7 +12,7 @@ type ( Config struct { Cache types.Stringorslice Platform string - Branches Constraint + Branches constraint.Constraint Workspace Workspace Clone Containers Pipeline Containers diff --git a/pipeline/frontend/yaml/constraint.go b/pipeline/frontend/yaml/constraint/constraint.go similarity index 99% rename from pipeline/frontend/yaml/constraint.go rename to pipeline/frontend/yaml/constraint/constraint.go index ea5cc8dd97..7843d912ae 100644 --- a/pipeline/frontend/yaml/constraint.go +++ b/pipeline/frontend/yaml/constraint/constraint.go @@ -1,4 +1,4 @@ -package yaml +package constraint import ( "fmt" diff --git a/pipeline/frontend/yaml/constraint_test.go b/pipeline/frontend/yaml/constraint/constraint_test.go similarity index 99% rename from pipeline/frontend/yaml/constraint_test.go rename to pipeline/frontend/yaml/constraint/constraint_test.go index 072487430c..7621f1f162 100644 --- a/pipeline/frontend/yaml/constraint_test.go +++ b/pipeline/frontend/yaml/constraint/constraint_test.go @@ -1,4 +1,4 @@ -package yaml +package constraint import ( "testing" diff --git a/pipeline/frontend/yaml/container.go b/pipeline/frontend/yaml/container.go index 9b30356f05..8543e7b424 100644 --- a/pipeline/frontend/yaml/container.go +++ b/pipeline/frontend/yaml/container.go @@ -5,6 +5,7 @@ import ( "gopkg.in/yaml.v3" + "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/constraint" "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types" ) @@ -57,7 +58,7 @@ type ( Volumes types.Volumes `yaml:"volumes,omitempty"` Secrets Secrets `yaml:"secrets,omitempty"` Sysctls types.SliceorMap `yaml:"sysctls,omitempty"` - Constraints Constraints `yaml:"when,omitempty"` + Constraints constraint.Constraints `yaml:"when,omitempty"` Settings map[string]interface{} `yaml:"settings"` // Deprecated Vargs map[string]interface{} `yaml:",inline"` // TODO: remove deprecated with v0.16.0 diff --git a/pipeline/frontend/yaml/container_test.go b/pipeline/frontend/yaml/container_test.go index ad7708ec7b..257f0fde70 100644 --- a/pipeline/frontend/yaml/container_test.go +++ b/pipeline/frontend/yaml/container_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" + "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/constraint" "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types" ) @@ -109,8 +110,8 @@ func TestUnmarshalContainer(t *testing.T) { {Source: "/etc/configs", Destination: "/etc/configs/", AccessMode: "ro"}, }, }, - Constraints: Constraints{ - Branch: Constraint{ + Constraints: constraint.Constraints{ + Branch: constraint.Constraint{ Include: []string{"master"}, }, }, @@ -188,9 +189,9 @@ func TestUnmarshalContainers(t *testing.T) { "tag": stringsToInterface("next", "latest"), "dry_run": true, }, - Constraints: Constraints{ - Event: Constraint{Include: []string{"push"}}, - Branch: Constraint{Include: []string{"${CI_REPO_DEFAULT_BRANCH}"}}, + Constraints: constraint.Constraints{ + Event: constraint.Constraint{Include: []string{"push"}}, + Branch: constraint.Constraint{Include: []string{"${CI_REPO_DEFAULT_BRANCH}"}}, }, }, }, @@ -216,9 +217,9 @@ func TestUnmarshalContainers(t *testing.T) { "dockerfile": "docker/Dockerfile.cli", "tag": stringsToInterface("next"), }, - Constraints: Constraints{ - Event: Constraint{Include: []string{"push"}}, - Branch: Constraint{Include: []string{"${CI_REPO_DEFAULT_BRANCH}"}}, + Constraints: constraint.Constraints{ + Event: constraint.Constraint{Include: []string{"push"}}, + Branch: constraint.Constraint{Include: []string{"${CI_REPO_DEFAULT_BRANCH}"}}, }, }, }, From 3896ce18b7bd3b35ff4916d721d376917aa43eff Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 16 Jan 2022 21:00:20 +0100 Subject: [PATCH 06/11] dont relay on remote impl - filter path match based on event tag at constraints --- pipeline/frontend/yaml/constraint/constraint.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pipeline/frontend/yaml/constraint/constraint.go b/pipeline/frontend/yaml/constraint/constraint.go index 7843d912ae..a53db6afbf 100644 --- a/pipeline/frontend/yaml/constraint/constraint.go +++ b/pipeline/frontend/yaml/constraint/constraint.go @@ -50,15 +50,21 @@ type ( // Match returns true if all constraints match the given input. If a single // constraint fails a false value is returned. func (c *Constraints) Match(metadata frontend.Metadata) bool { - return c.Platform.Match(metadata.Sys.Arch) && + match := c.Platform.Match(metadata.Sys.Arch) && c.Environment.Match(metadata.Curr.Target) && c.Event.Match(metadata.Curr.Event) && c.Branch.Match(metadata.Curr.Commit.Branch) && c.Repo.Match(metadata.Repo.Name) && c.Ref.Match(metadata.Curr.Commit.Ref) && c.Instance.Match(metadata.Sys.Host) && - c.Matrix.Match(metadata.Job.Matrix) && - c.Path.Match(metadata.Curr.Commit.ChangedFiles, metadata.Curr.Commit.Message) + c.Matrix.Match(metadata.Job.Matrix) + + // changed files filter do not apply for tag event + if metadata.Curr.Event != frontend.EventTag { + match = match && c.Path.Match(metadata.Curr.Commit.ChangedFiles, metadata.Curr.Commit.Message) + } + + return match } // Match returns true if the string matches the include patterns and does not From 3970b9e00ade7e92a65eea9adbce3bf976423671 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 16 Jan 2022 22:23:14 +0100 Subject: [PATCH 07/11] fix lint issue --- pipeline/frontend/yaml/config.go | 2 +- .../frontend/yaml/constraint/constraint.go | 52 +++++++++---------- .../yaml/constraint/constraint_test.go | 12 ++--- pipeline/frontend/yaml/container_test.go | 10 ++-- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/pipeline/frontend/yaml/config.go b/pipeline/frontend/yaml/config.go index eaa78d5687..9296828dad 100644 --- a/pipeline/frontend/yaml/config.go +++ b/pipeline/frontend/yaml/config.go @@ -12,7 +12,7 @@ type ( Config struct { Cache types.Stringorslice Platform string - Branches constraint.Constraint + Branches constraint.List Workspace Workspace Clone Containers Pipeline Containers diff --git a/pipeline/frontend/yaml/constraint/constraint.go b/pipeline/frontend/yaml/constraint/constraint.go index a53db6afbf..6247a31ce1 100644 --- a/pipeline/frontend/yaml/constraint/constraint.go +++ b/pipeline/frontend/yaml/constraint/constraint.go @@ -14,33 +14,33 @@ import ( type ( // Constraints defines a set of runtime constraints. Constraints struct { - Ref Constraint - Repo Constraint - Instance Constraint - Platform Constraint - Environment Constraint - Event Constraint - Branch Constraint - Status Constraint - Matrix ConstraintMap + Ref List + Repo List + Instance List + Platform List + Environment List + Event List + Branch List + Status List + Matrix Map Local types.BoolTrue - Path ConstraintPath + Path Path } - // Constraint defines a runtime constraint. - Constraint struct { + // List defines a runtime constraint for exclude & include string slices. + List struct { Include []string Exclude []string } - // ConstraintMap defines a runtime constraint map. - ConstraintMap struct { + // Map defines a runtime constraint for exclude & include map. + Map struct { Include map[string]string Exclude map[string]string } - // ConstraintPath defines a runtime constrain for paths - ConstraintPath struct { + // Path defines a runtime constrain for exclude & include paths. + Path struct { Include []string Exclude []string IgnoreMessage string `yaml:"ignore_message,omitempty"` @@ -69,7 +69,7 @@ func (c *Constraints) Match(metadata frontend.Metadata) bool { // Match returns true if the string matches the include patterns and does not // match any of the exclude patterns. -func (c *Constraint) Match(v string) bool { +func (c *List) Match(v string) bool { if c.Excludes(v) { return false } @@ -83,7 +83,7 @@ func (c *Constraint) Match(v string) bool { } // Includes returns true if the string matches the include patterns. -func (c *Constraint) Includes(v string) bool { +func (c *List) Includes(v string) bool { for _, pattern := range c.Include { if ok, _ := doublestar.Match(pattern, v); ok { return true @@ -93,7 +93,7 @@ func (c *Constraint) Includes(v string) bool { } // Excludes returns true if the string matches the exclude patterns. -func (c *Constraint) Excludes(v string) bool { +func (c *List) Excludes(v string) bool { for _, pattern := range c.Exclude { if ok, _ := doublestar.Match(pattern, v); ok { return true @@ -103,7 +103,7 @@ func (c *Constraint) Excludes(v string) bool { } // UnmarshalYAML unmarshals the constraint. -func (c *Constraint) UnmarshalYAML(value *yaml.Node) error { +func (c *List) UnmarshalYAML(value *yaml.Node) error { out1 := struct { Include types.Stringorslice Exclude types.Stringorslice @@ -130,7 +130,7 @@ func (c *Constraint) UnmarshalYAML(value *yaml.Node) error { // Match returns true if the params matches the include key values and does not // match any of the exclude key values. -func (c *ConstraintMap) Match(params map[string]string) bool { +func (c *Map) Match(params map[string]string) bool { // when no includes or excludes automatically match if len(c.Include) == 0 && len(c.Exclude) == 0 { return true @@ -158,7 +158,7 @@ func (c *ConstraintMap) Match(params map[string]string) bool { } // UnmarshalYAML unmarshals the constraint map. -func (c *ConstraintMap) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (c *Map) UnmarshalYAML(unmarshal func(interface{}) error) error { out1 := struct { Include map[string]string Exclude map[string]string @@ -181,7 +181,7 @@ func (c *ConstraintMap) UnmarshalYAML(unmarshal func(interface{}) error) error { } // UnmarshalYAML unmarshals the constraint. -func (c *ConstraintPath) UnmarshalYAML(value *yaml.Node) error { +func (c *Path) UnmarshalYAML(value *yaml.Node) error { out1 := struct { Include types.Stringorslice `yaml:"include,omitempty"` Exclude types.Stringorslice `yaml:"exclude,omitempty"` @@ -210,7 +210,7 @@ func (c *ConstraintPath) UnmarshalYAML(value *yaml.Node) error { // Match returns true if file paths in string slice matches the include and not exclude patterns // or if commit message contains ignore message. -func (c *ConstraintPath) Match(v []string, message string) bool { +func (c *Path) Match(v []string, message string) bool { // 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 @@ -230,7 +230,7 @@ func (c *ConstraintPath) Match(v []string, message string) bool { } // Includes returns true if the string matches any of the include patterns. -func (c *ConstraintPath) Includes(v []string) bool { +func (c *Path) Includes(v []string) bool { for _, pattern := range c.Include { for _, file := range v { if ok, _ := doublestar.Match(pattern, file); ok { @@ -242,7 +242,7 @@ func (c *ConstraintPath) Includes(v []string) bool { } // Excludes returns true if the string matches any of the exclude patterns. -func (c *ConstraintPath) Excludes(v []string) bool { +func (c *Path) Excludes(v []string) bool { for _, pattern := range c.Exclude { for _, file := range v { if ok, _ := doublestar.Match(pattern, file); ok { diff --git a/pipeline/frontend/yaml/constraint/constraint_test.go b/pipeline/frontend/yaml/constraint/constraint_test.go index 7621f1f162..5eb9e46582 100644 --- a/pipeline/frontend/yaml/constraint/constraint_test.go +++ b/pipeline/frontend/yaml/constraint/constraint_test.go @@ -475,20 +475,20 @@ func parseConstraints(t *testing.T, s string) *Constraints { return c } -func parseConstraint(t *testing.T, s string) *Constraint { - c := &Constraint{} +func parseConstraint(t *testing.T, s string) *List { + c := &List{} assert.NoError(t, yaml.Unmarshal([]byte(s), c)) return c } -func parseConstraintMap(t *testing.T, s string) *ConstraintMap { - c := &ConstraintMap{} +func parseConstraintMap(t *testing.T, s string) *Map { + c := &Map{} assert.NoError(t, yaml.Unmarshal([]byte(s), c)) return c } -func parseConstraintPath(t *testing.T, s string) *ConstraintPath { - c := &ConstraintPath{} +func parseConstraintPath(t *testing.T, s string) *Path { + c := &Path{} assert.NoError(t, yaml.Unmarshal([]byte(s), c)) return c } diff --git a/pipeline/frontend/yaml/container_test.go b/pipeline/frontend/yaml/container_test.go index 257f0fde70..52cd0d82c2 100644 --- a/pipeline/frontend/yaml/container_test.go +++ b/pipeline/frontend/yaml/container_test.go @@ -111,7 +111,7 @@ func TestUnmarshalContainer(t *testing.T) { }, }, Constraints: constraint.Constraints{ - Branch: constraint.Constraint{ + Branch: constraint.List{ Include: []string{"master"}, }, }, @@ -190,8 +190,8 @@ func TestUnmarshalContainers(t *testing.T) { "dry_run": true, }, Constraints: constraint.Constraints{ - Event: constraint.Constraint{Include: []string{"push"}}, - Branch: constraint.Constraint{Include: []string{"${CI_REPO_DEFAULT_BRANCH}"}}, + Event: constraint.List{Include: []string{"push"}}, + Branch: constraint.List{Include: []string{"${CI_REPO_DEFAULT_BRANCH}"}}, }, }, }, @@ -218,8 +218,8 @@ func TestUnmarshalContainers(t *testing.T) { "tag": stringsToInterface("next"), }, Constraints: constraint.Constraints{ - Event: constraint.Constraint{Include: []string{"push"}}, - Branch: constraint.Constraint{Include: []string{"${CI_REPO_DEFAULT_BRANCH}"}}, + Event: constraint.List{Include: []string{"push"}}, + Branch: constraint.List{Include: []string{"${CI_REPO_DEFAULT_BRANCH}"}}, }, }, }, From 0ce26b451301c06f524d5ec760ac27e78a33a757 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 16 Jan 2022 22:31:57 +0100 Subject: [PATCH 08/11] update docs --- docs/docs/20-usage/22-conditional-execution.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/docs/20-usage/22-conditional-execution.md b/docs/docs/20-usage/22-conditional-execution.md index bc73a8b84f..14b739b5e9 100644 --- a/docs/docs/20-usage/22-conditional-execution.md +++ b/docs/docs/20-usage/22-conditional-execution.md @@ -159,7 +159,10 @@ when: ## `path` -> NOTE: This feature is currently only available for GitHub and Gitea repositories. +:::info +This feature is currently only available for GitHub and Gitea without pull support. +It ignores tag events. +::: Execute a step only on a pipeline with certain files being changed: From 9f112e6f32bacef2b7d485e6a829022183403d7a Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 17 Jan 2022 12:18:50 +0100 Subject: [PATCH 09/11] Apply suggestions from code review Co-authored-by: Anbraten --- docs/docs/20-usage/22-conditional-execution.md | 5 +++-- pipeline/frontend/yaml/constraint/constraint.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/docs/20-usage/22-conditional-execution.md b/docs/docs/20-usage/22-conditional-execution.md index 14b739b5e9..76dbe9dc41 100644 --- a/docs/docs/20-usage/22-conditional-execution.md +++ b/docs/docs/20-usage/22-conditional-execution.md @@ -160,8 +160,9 @@ when: ## `path` :::info -This feature is currently only available for GitHub and Gitea without pull support. -It ignores tag events. +This feature is currently only available for GitHub, Gitlab and Gitea. +Pull requests aren't supported at the moment ([#697](https://github.com/woodpecker-ci/woodpecker/pull/697)). +Path conditions are ignored for tag events. ::: Execute a step only on a pipeline with certain files being changed: diff --git a/pipeline/frontend/yaml/constraint/constraint.go b/pipeline/frontend/yaml/constraint/constraint.go index 6247a31ce1..0aa935f9ef 100644 --- a/pipeline/frontend/yaml/constraint/constraint.go +++ b/pipeline/frontend/yaml/constraint/constraint.go @@ -33,7 +33,7 @@ type ( Exclude []string } - // Map defines a runtime constraint for exclude & include map. + // Map defines a runtime constraint for exclude & include map strings. Map struct { Include map[string]string Exclude map[string]string From 44fb65c85ab335e26c0ad1fca2d24c006c1f6922 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 17 Jan 2022 13:39:00 +0100 Subject: [PATCH 10/11] more specific Co-authored-by: Anbraten --- pipeline/frontend/yaml/constraint/constraint.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pipeline/frontend/yaml/constraint/constraint.go b/pipeline/frontend/yaml/constraint/constraint.go index 0aa935f9ef..e7df69ab91 100644 --- a/pipeline/frontend/yaml/constraint/constraint.go +++ b/pipeline/frontend/yaml/constraint/constraint.go @@ -59,8 +59,8 @@ func (c *Constraints) Match(metadata frontend.Metadata) bool { c.Instance.Match(metadata.Sys.Host) && c.Matrix.Match(metadata.Job.Matrix) - // changed files filter do not apply for tag event - if metadata.Curr.Event != frontend.EventTag { + // changed files filter do only apply for pull and push events + if metadata.Curr.Event == frontend.EventPull || metadata.Curr.Event == frontend.EventPush { match = match && c.Path.Match(metadata.Curr.Commit.ChangedFiles, metadata.Curr.Commit.Message) } From f17fa046b5f65a1d26c9eb412efd3f665f4be396 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 17 Jan 2022 14:41:45 +0100 Subject: [PATCH 11/11] Update pipeline/frontend/yaml/constraint/constraint.go Co-authored-by: Anbraten --- pipeline/frontend/yaml/constraint/constraint.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipeline/frontend/yaml/constraint/constraint.go b/pipeline/frontend/yaml/constraint/constraint.go index e7df69ab91..64305d9268 100644 --- a/pipeline/frontend/yaml/constraint/constraint.go +++ b/pipeline/frontend/yaml/constraint/constraint.go @@ -59,7 +59,7 @@ func (c *Constraints) Match(metadata frontend.Metadata) bool { c.Instance.Match(metadata.Sys.Host) && c.Matrix.Match(metadata.Job.Matrix) - // changed files filter do only apply for pull and push events + // changed files filter do only apply for pull-request and push events if metadata.Curr.Event == frontend.EventPull || metadata.Curr.Event == frontend.EventPush { match = match && c.Path.Match(metadata.Curr.Commit.ChangedFiles, metadata.Curr.Commit.Message) }