From faf6b331401d4f6f0d97b7b24919aefe3af92bc3 Mon Sep 17 00:00:00 2001 From: Kai J <99220919+da-Kai@users.noreply.github.com> Date: Tue, 14 May 2024 16:28:14 +0200 Subject: [PATCH] Setting for empty commits on path condition (#3708) --- docs/docs/20-usage/20-workflow-syntax.md | 5 ++++- pipeline/frontend/yaml/constraint/constraint.go | 9 ++++++--- pipeline/frontend/yaml/constraint/constraint_test.go | 10 ++++++++++ .../yaml/linter/schema/.woodpecker/test-when.yaml | 1 + pipeline/frontend/yaml/linter/schema/schema.json | 6 ++++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/docs/docs/20-usage/20-workflow-syntax.md b/docs/docs/20-usage/20-workflow-syntax.md index df3bfc035..7b966fc48 100644 --- a/docs/docs/20-usage/20-workflow-syntax.md +++ b/docs/docs/20-usage/20-workflow-syntax.md @@ -402,16 +402,19 @@ when: You can use [glob patterns](https://github.com/bmatcuk/doublestar#patterns) to match the changed files and specify if the step should run if a file matching that pattern has been changed `include` or if some files have **not** been changed `exclude`. +For pipelines without file changes (empty commits or on events without file changes like `tag`), you can use `on_empty` to set whether this condition should be **true** _(default)_ or **false** in these cases. + ```yaml when: - path: include: ['.woodpecker/*.yaml', '*.ini'] exclude: ['*.md', 'docs/**'] ignore_message: '[ALL]' + on_empty: true ``` :::info -Passing a defined ignore-message like `[ALL]` inside the commit message will ignore all path conditions. +Passing a defined ignore-message like `[ALL]` inside the commit message will ignore all path conditions and the `on_empty` setting. ::: #### `evaluate` diff --git a/pipeline/frontend/yaml/constraint/constraint.go b/pipeline/frontend/yaml/constraint/constraint.go index 9c562e4a7..736f571ba 100644 --- a/pipeline/frontend/yaml/constraint/constraint.go +++ b/pipeline/frontend/yaml/constraint/constraint.go @@ -69,7 +69,8 @@ type ( Path struct { Include []string Exclude []string - IgnoreMessage string `yaml:"ignore_message,omitempty"` + IgnoreMessage string `yaml:"ignore_message,omitempty"` + OnEmpty yamlBaseTypes.BoolTrue `yaml:"on_empty,omitempty"` } ) @@ -331,6 +332,7 @@ func (c *Path) UnmarshalYAML(value *yaml.Node) error { Include yamlBaseTypes.StringOrSlice `yaml:"include,omitempty"` Exclude yamlBaseTypes.StringOrSlice `yaml:"exclude,omitempty"` IgnoreMessage string `yaml:"ignore_message,omitempty"` + OnEmpty yamlBaseTypes.BoolTrue `yaml:"on_empty,omitempty"` }{} var out2 yamlBaseTypes.StringOrSlice @@ -340,6 +342,7 @@ func (c *Path) UnmarshalYAML(value *yaml.Node) error { c.Exclude = out1.Exclude c.IgnoreMessage = out1.IgnoreMessage + c.OnEmpty = out1.OnEmpty c.Include = append( //nolint:gocritic out1.Include, out2..., @@ -361,9 +364,9 @@ func (c *Path) Match(v []string, message string) bool { return true } - // always match if there are no commit files (empty commit) + // return value based on 'on_empty', if there are no commit files (empty commit) if len(v) == 0 { - return true + return c.OnEmpty.Bool() } if len(c.Exclude) > 0 && c.Excludes(v) { diff --git a/pipeline/frontend/yaml/constraint/constraint_test.go b/pipeline/frontend/yaml/constraint/constraint_test.go index 834e7f6bc..60936d8c7 100644 --- a/pipeline/frontend/yaml/constraint/constraint_test.go +++ b/pipeline/frontend/yaml/constraint/constraint_test.go @@ -260,6 +260,16 @@ func TestConstraintList(t *testing.T) { with: []string{}, want: true, }, + { + conf: "{ include: [ README.md ], on_empty: false }", + with: []string{}, + want: false, + }, + { + conf: "{ include: [ README.md ], on_empty: true }", + with: []string{}, + want: true, + }, } for _, test := range testdata { c := parseConstraintPath(t, test.conf) diff --git a/pipeline/frontend/yaml/linter/schema/.woodpecker/test-when.yaml b/pipeline/frontend/yaml/linter/schema/.woodpecker/test-when.yaml index 867989285..b08385a77 100644 --- a/pipeline/frontend/yaml/linter/schema/.woodpecker/test-when.yaml +++ b/pipeline/frontend/yaml/linter/schema/.woodpecker/test-when.yaml @@ -119,6 +119,7 @@ steps: include: ['.woodpecker/*.yml', '*.ini'] exclude: ['*.md', 'docs/**'] ignore_message: '[ALL]' + on_empty: true when-repo: image: alpine diff --git a/pipeline/frontend/yaml/linter/schema/schema.json b/pipeline/frontend/yaml/linter/schema/schema.json index f5869c15d..f735c0ea3 100644 --- a/pipeline/frontend/yaml/linter/schema/schema.json +++ b/pipeline/frontend/yaml/linter/schema/schema.json @@ -288,6 +288,9 @@ }, "ignore_message": { "type": "string" + }, + "on_empty": { + "type": "boolean" } }, "additionalProperties": false @@ -493,6 +496,9 @@ }, "ignore_message": { "type": "string" + }, + "on_empty": { + "type": "boolean" } }, "additionalProperties": false