mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-09-01 16:05:19 +00:00
Add default event filter (#1140)
breakout from #934 when new events are added you don't have to worry that pipeline will behave different as it does now with this Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
@@ -63,7 +63,13 @@ func (when *When) Match(metadata frontend.Metadata) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return when.IsEmpty()
|
||||
|
||||
if when.IsEmpty() {
|
||||
// test against default Constraints
|
||||
empty := &Constraint{}
|
||||
return empty.Match(metadata)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (when *When) IncludesStatus(status string) bool {
|
||||
@@ -120,6 +126,16 @@ func (when *When) UnmarshalYAML(value *yaml.Node) error {
|
||||
// Match returns true if all constraints match the given input. If a single
|
||||
// constraint fails a false value is returned.
|
||||
func (c *Constraint) Match(metadata frontend.Metadata) bool {
|
||||
// if event filter is not set, set default
|
||||
if c.Event.IsEmpty() {
|
||||
c.Event.Include = []string{
|
||||
frontend.EventPush,
|
||||
frontend.EventPull,
|
||||
frontend.EventTag,
|
||||
frontend.EventDeploy,
|
||||
}
|
||||
}
|
||||
|
||||
match := c.Platform.Match(metadata.Sys.Platform) &&
|
||||
c.Environment.Match(metadata.Curr.Target) &&
|
||||
c.Event.Match(metadata.Curr.Event) &&
|
||||
@@ -140,6 +156,11 @@ func (c *Constraint) Match(metadata frontend.Metadata) bool {
|
||||
return match
|
||||
}
|
||||
|
||||
// IsEmpty return true if a constraint has no conditions
|
||||
func (c List) IsEmpty() bool {
|
||||
return len(c.Include) == 0 && len(c.Exclude) == 0
|
||||
}
|
||||
|
||||
// Match returns true if the string matches the include patterns and does not
|
||||
// match any of the exclude patterns.
|
||||
func (c *List) Match(v string) bool {
|
||||
|
@@ -383,89 +383,98 @@ func TestConstraintMap(t *testing.T) {
|
||||
|
||||
func TestConstraints(t *testing.T) {
|
||||
testdata := []struct {
|
||||
desc string
|
||||
conf string
|
||||
with frontend.Metadata
|
||||
want bool
|
||||
}{
|
||||
// no constraints, must match
|
||||
{
|
||||
desc: "no constraints, must match on default events",
|
||||
conf: "",
|
||||
with: frontend.Metadata{},
|
||||
with: frontend.Metadata{
|
||||
Curr: frontend.Build{
|
||||
Event: frontend.EventPush,
|
||||
},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
// branch constraint
|
||||
{
|
||||
desc: "global branch filter",
|
||||
conf: "{ branch: develop }",
|
||||
with: frontend.Metadata{Curr: frontend.Build{Commit: frontend.Commit{Branch: "master"}}},
|
||||
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventPush, Commit: frontend.Commit{Branch: "master"}}},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
desc: "global branch filter",
|
||||
conf: "{ branch: master }",
|
||||
with: frontend.Metadata{Curr: frontend.Build{Commit: frontend.Commit{Branch: "master"}}},
|
||||
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventPush, Commit: frontend.Commit{Branch: "master"}}},
|
||||
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,
|
||||
},
|
||||
// repo constraint
|
||||
{
|
||||
desc: "repo constraint",
|
||||
conf: "{ repo: owner/* }",
|
||||
with: frontend.Metadata{Repo: frontend.Repo{Name: "owner/repo"}},
|
||||
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventPush}, Repo: frontend.Repo{Name: "owner/repo"}},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
desc: "repo constraint",
|
||||
conf: "{ repo: octocat/* }",
|
||||
with: frontend.Metadata{Repo: frontend.Repo{Name: "owner/repo"}},
|
||||
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventPush}, Repo: frontend.Repo{Name: "owner/repo"}},
|
||||
want: false,
|
||||
},
|
||||
// ref constraint
|
||||
{
|
||||
desc: "ref constraint",
|
||||
conf: "{ ref: refs/tags/* }",
|
||||
with: frontend.Metadata{Curr: frontend.Build{Commit: frontend.Commit{Ref: "refs/tags/v1.0.0"}}},
|
||||
with: frontend.Metadata{Curr: frontend.Build{Commit: frontend.Commit{Ref: "refs/tags/v1.0.0"}, Event: frontend.EventPush}},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
desc: "ref constraint",
|
||||
conf: "{ ref: refs/tags/* }",
|
||||
with: frontend.Metadata{Curr: frontend.Build{Commit: frontend.Commit{Ref: "refs/heads/master"}}},
|
||||
with: frontend.Metadata{Curr: frontend.Build{Commit: frontend.Commit{Ref: "refs/heads/master"}, Event: frontend.EventPush}},
|
||||
want: false,
|
||||
},
|
||||
// platform constraint
|
||||
{
|
||||
desc: "platform constraint",
|
||||
conf: "{ platform: linux/amd64 }",
|
||||
with: frontend.Metadata{Sys: frontend.System{Platform: "linux/amd64"}},
|
||||
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventPush}, Sys: frontend.System{Platform: "linux/amd64"}},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
desc: "platform constraint",
|
||||
conf: "{ repo: linux/amd64 }",
|
||||
with: frontend.Metadata{Sys: frontend.System{Platform: "windows/amd64"}},
|
||||
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventPush}, Sys: frontend.System{Platform: "windows/amd64"}},
|
||||
want: false,
|
||||
},
|
||||
// instance constraint
|
||||
{
|
||||
desc: "instance constraint",
|
||||
conf: "{ instance: agent.tld }",
|
||||
with: frontend.Metadata{Sys: frontend.System{Host: "agent.tld"}},
|
||||
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventPush}, Sys: frontend.System{Host: "agent.tld"}},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
desc: "instance constraint",
|
||||
conf: "{ instance: agent.tld }",
|
||||
with: frontend.Metadata{Sys: frontend.System{Host: "beta.agent.tld"}},
|
||||
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventPush}, Sys: frontend.System{Host: "beta.agent.tld"}},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
desc: "no constraints, and event get filtered by default default event filter",
|
||||
conf: "",
|
||||
with: frontend.Metadata{
|
||||
Curr: frontend.Build{Event: "non-default"},
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, test := range testdata {
|
||||
c := parseConstraints(t, test.conf)
|
||||
got, want := c.Match(test.with), test.want
|
||||
if got != want {
|
||||
t.Errorf("Expect %+v matches %q is %v", test.with, test.conf, want)
|
||||
}
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
c := parseConstraints(t, test.conf)
|
||||
got, want := c.Match(test.with), test.want
|
||||
if got != want {
|
||||
t.Errorf("Expect %+v matches %q is %v", test.with, test.conf, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user