Setting for empty commits on path condition (#3708)

This commit is contained in:
Kai J 2024-05-14 16:28:14 +02:00 committed by GitHub
parent d92cf4b18e
commit faf6b33140
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 27 additions and 4 deletions

View file

@ -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`. 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 ```yaml
when: when:
- path: - path:
include: ['.woodpecker/*.yaml', '*.ini'] include: ['.woodpecker/*.yaml', '*.ini']
exclude: ['*.md', 'docs/**'] exclude: ['*.md', 'docs/**']
ignore_message: '[ALL]' ignore_message: '[ALL]'
on_empty: true
``` ```
:::info :::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` #### `evaluate`

View file

@ -69,7 +69,8 @@ type (
Path struct { Path struct {
Include []string Include []string
Exclude []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"` Include yamlBaseTypes.StringOrSlice `yaml:"include,omitempty"`
Exclude yamlBaseTypes.StringOrSlice `yaml:"exclude,omitempty"` Exclude yamlBaseTypes.StringOrSlice `yaml:"exclude,omitempty"`
IgnoreMessage string `yaml:"ignore_message,omitempty"` IgnoreMessage string `yaml:"ignore_message,omitempty"`
OnEmpty yamlBaseTypes.BoolTrue `yaml:"on_empty,omitempty"`
}{} }{}
var out2 yamlBaseTypes.StringOrSlice var out2 yamlBaseTypes.StringOrSlice
@ -340,6 +342,7 @@ func (c *Path) UnmarshalYAML(value *yaml.Node) error {
c.Exclude = out1.Exclude c.Exclude = out1.Exclude
c.IgnoreMessage = out1.IgnoreMessage c.IgnoreMessage = out1.IgnoreMessage
c.OnEmpty = out1.OnEmpty
c.Include = append( //nolint:gocritic c.Include = append( //nolint:gocritic
out1.Include, out1.Include,
out2..., out2...,
@ -361,9 +364,9 @@ func (c *Path) Match(v []string, message string) bool {
return true 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 { if len(v) == 0 {
return true return c.OnEmpty.Bool()
} }
if len(c.Exclude) > 0 && c.Excludes(v) { if len(c.Exclude) > 0 && c.Excludes(v) {

View file

@ -260,6 +260,16 @@ func TestConstraintList(t *testing.T) {
with: []string{}, with: []string{},
want: true, 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 { for _, test := range testdata {
c := parseConstraintPath(t, test.conf) c := parseConstraintPath(t, test.conf)

View file

@ -119,6 +119,7 @@ steps:
include: ['.woodpecker/*.yml', '*.ini'] include: ['.woodpecker/*.yml', '*.ini']
exclude: ['*.md', 'docs/**'] exclude: ['*.md', 'docs/**']
ignore_message: '[ALL]' ignore_message: '[ALL]'
on_empty: true
when-repo: when-repo:
image: alpine image: alpine

View file

@ -288,6 +288,9 @@
}, },
"ignore_message": { "ignore_message": {
"type": "string" "type": "string"
},
"on_empty": {
"type": "boolean"
} }
}, },
"additionalProperties": false "additionalProperties": false
@ -493,6 +496,9 @@
}, },
"ignore_message": { "ignore_message": {
"type": "string" "type": "string"
},
"on_empty": {
"type": "boolean"
} }
}, },
"additionalProperties": false "additionalProperties": false