Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting for empty commits on path condition #3708

Merged
merged 6 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/docs/20-usage/20-workflow-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 commits without file changes (empty commits), you can use `on_empty` to set whether this condition should be **true** _(default)_ or **false** in these cases.
da-Kai marked this conversation as resolved.
Show resolved Hide resolved

```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`
Expand Down
9 changes: 6 additions & 3 deletions pipeline/frontend/yaml/constraint/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
)

Expand Down Expand Up @@ -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
Expand All @@ -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...,
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions pipeline/frontend/yaml/constraint/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ steps:
include: ['.woodpecker/*.yml', '*.ini']
exclude: ['*.md', 'docs/**']
ignore_message: '[ALL]'
on_empty: true

when-repo:
image: alpine
Expand Down
6 changes: 6 additions & 0 deletions pipeline/frontend/yaml/linter/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@
},
"ignore_message": {
"type": "string"
},
"on_empty": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -493,6 +496,9 @@
},
"ignore_message": {
"type": "string"
},
"on_empty": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down