diff --git a/pipeline/ruleset.go b/pipeline/ruleset.go index b0004a5d..2a22e6a4 100644 --- a/pipeline/ruleset.go +++ b/pipeline/ruleset.go @@ -29,6 +29,7 @@ type ( Repo Ruletype `json:"repo,omitempty" yaml:"repo,omitempty"` Status Ruletype `json:"status,omitempty" yaml:"status,omitempty"` Tag Ruletype `json:"tag,omitempty" yaml:"tag,omitempty"` + Target Ruletype `json:"target,omitempty" yaml:"target,omitempty"` } // Ruletype is the pipeline representation of an element @@ -45,6 +46,7 @@ type ( Repo string `json:"repo,omitempty" yaml:"repo,omitempty"` Status string `json:"status,omitempty" yaml:"status,omitempty"` Tag string `json:"tag,omitempty" yaml:"tag,omitempty"` + Target string `json:"target,omitempty" yaml:"target,omitempty"` } ) @@ -89,7 +91,8 @@ func (r *Rules) Empty() bool { len(r.Path) == 0 && len(r.Repo) == 0 && len(r.Status) == 0 && - len(r.Tag) == 0 { + len(r.Tag) == 0 && + len(r.Target) == 0 { return true } @@ -127,7 +130,8 @@ func (r *Rules) Match(from *RuleData, op string) bool { r.Path.MatchOr(p) || r.Repo.MatchOr(from.Repo) || status || - r.Tag.MatchOr(from.Tag) { + r.Tag.MatchOr(from.Tag) || + r.Target.MatchOr(from.Target) { return true } } @@ -151,7 +155,8 @@ func (r *Rules) Match(from *RuleData, op string) bool { r.Path.MatchAnd(p) && r.Repo.MatchAnd(from.Repo) && status && - r.Tag.MatchAnd(from.Tag) { + r.Tag.MatchAnd(from.Tag) && + r.Target.MatchAnd(from.Target) { return true } } @@ -175,7 +180,8 @@ func (r *Rules) Match(from *RuleData, op string) bool { r.Path.MatchOr("") || r.Repo.MatchOr(from.Repo) || status || - r.Tag.MatchOr(from.Tag) { + r.Tag.MatchOr(from.Tag) || + r.Target.MatchOr(from.Target) { return true } @@ -195,7 +201,8 @@ func (r *Rules) Match(from *RuleData, op string) bool { r.Path.MatchAnd("") && r.Repo.MatchAnd(from.Repo) && status && - r.Tag.MatchAnd(from.Tag) { + r.Tag.MatchAnd(from.Tag) && + r.Target.MatchAnd(from.Target) { return true } diff --git a/pipeline/ruleset_test.go b/pipeline/ruleset_test.go index 156fe125..68e99916 100644 --- a/pipeline/ruleset_test.go +++ b/pipeline/ruleset_test.go @@ -20,120 +20,130 @@ func TestPipeline_Ruleset_Match(t *testing.T) { // If with and operator { ruleset: &Ruleset{If: Rules{Branch: []string{"master"}}}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, { ruleset: &Ruleset{If: Rules{Branch: []string{"master"}}}, - data: &RuleData{Branch: "dev", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, { ruleset: &Ruleset{If: Rules{Branch: []string{"master"}, Event: []string{"push"}}}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, { ruleset: &Ruleset{If: Rules{Branch: []string{"master"}, Event: []string{"push"}}}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, { ruleset: &Ruleset{If: Rules{Path: []string{"foo.txt", "/foo/bar.txt"}}}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Path: []string{}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Path: []string{}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, { ruleset: &Ruleset{If: Rules{Comment: []string{"rerun"}}}, - data: &RuleData{Branch: "dev", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, { ruleset: &Ruleset{If: Rules{Comment: []string{"rerun"}}}, - data: &RuleData{Branch: "dev", Comment: "ok to test", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Comment: "ok to test", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, + want: false, + }, + { + ruleset: &Ruleset{If: Rules{Event: []string{"deployment"}, Target: []string{"production"}}}, + data: &RuleData{Branch: "dev", Comment: "", Event: "deployment", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: "production"}, + want: true, + }, + { + ruleset: &Ruleset{If: Rules{Event: []string{"deployment"}, Target: []string{"production"}}}, + data: &RuleData{Branch: "dev", Comment: "", Event: "deployment", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: "stage"}, want: false, }, { ruleset: &Ruleset{If: Rules{Status: []string{"success", "failure"}}}, - data: &RuleData{Branch: "dev", Comment: "ok to test", Event: "push", Repo: "octocat/hello-world", Status: "failure", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Comment: "ok to test", Event: "push", Repo: "octocat/hello-world", Status: "failure", Tag: "refs/heads/master", Target: ""}, want: true, }, // If with or operator { ruleset: &Ruleset{If: Rules{Branch: []string{"master"}, Event: []string{"push"}}, Operator: "or"}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, { ruleset: &Ruleset{If: Rules{Branch: []string{"master"}, Event: []string{"push"}}, Operator: "or"}, - data: &RuleData{Branch: "dev", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, { ruleset: &Ruleset{If: Rules{Branch: []string{"master"}, Event: []string{"push"}}, Operator: "or"}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, { ruleset: &Ruleset{If: Rules{Branch: []string{"master"}, Event: []string{"push"}}, Operator: "or"}, - data: &RuleData{Branch: "dev", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, { ruleset: &Ruleset{If: Rules{Path: []string{"foo.txt", "/foo/bar.txt"}}, Operator: "or"}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Path: []string{}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Path: []string{}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, // Unless with and operator { ruleset: &Ruleset{Unless: Rules{Branch: []string{"master"}}}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, { ruleset: &Ruleset{Unless: Rules{Branch: []string{"master"}}}, - data: &RuleData{Branch: "dev", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, { ruleset: &Ruleset{Unless: Rules{Branch: []string{"master"}, Event: []string{"push"}}}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, { ruleset: &Ruleset{Unless: Rules{Branch: []string{"master"}, Event: []string{"push"}}}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, { ruleset: &Ruleset{Unless: Rules{Path: []string{"foo.txt", "/foo/bar.txt"}}}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Path: []string{}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Path: []string{}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, // Unless with or operator { ruleset: &Ruleset{Unless: Rules{Branch: []string{"master"}, Event: []string{"push"}}, Operator: "or"}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, { ruleset: &Ruleset{Unless: Rules{Branch: []string{"master"}, Event: []string{"push"}}, Operator: "or"}, - data: &RuleData{Branch: "dev", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Comment: "rerun", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, { ruleset: &Ruleset{Unless: Rules{Branch: []string{"master"}, Event: []string{"push"}}, Operator: "or"}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, { ruleset: &Ruleset{Unless: Rules{Branch: []string{"master"}, Event: []string{"push"}}, Operator: "or"}, - data: &RuleData{Branch: "dev", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Comment: "rerun", Event: "pull_request", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, { ruleset: &Ruleset{Unless: Rules{Path: []string{"foo.txt", "/foo/bar.txt"}}, Operator: "or"}, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Path: []string{}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "pull_request", Path: []string{}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: true, }, // Advanced Rulesets @@ -145,7 +155,7 @@ func TestPipeline_Ruleset_Match(t *testing.T) { }, Operator: "or", }, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "tag", Repo: "octocat/hello-world", Status: "pending", Tag: "release/*"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "tag", Repo: "octocat/hello-world", Status: "pending", Tag: "release/*", Target: ""}, want: true, }, { @@ -156,7 +166,7 @@ func TestPipeline_Ruleset_Match(t *testing.T) { }, Operator: "or", }, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "tag", Repo: "octocat/hello-world", Status: "pending", Tag: "release/*"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "tag", Repo: "octocat/hello-world", Status: "pending", Tag: "release/*", Target: ""}, want: true, }, { @@ -167,7 +177,7 @@ func TestPipeline_Ruleset_Match(t *testing.T) { }, Operator: "or", }, - data: &RuleData{Branch: "master", Comment: "rerun", Event: "tag", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Comment: "rerun", Event: "tag", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, want: false, }, } @@ -217,100 +227,100 @@ func TestPipeline_Rules_Match(t *testing.T) { // Empty { rules: &Rules{}, - data: &RuleData{Branch: "master", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "and", want: true, }, { rules: &Rules{}, - data: &RuleData{Branch: "master", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "or", want: false, }, // and operator { rules: &Rules{Branch: []string{"master"}}, - data: &RuleData{Branch: "master", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "and", want: true, }, { rules: &Rules{Branch: []string{"master"}}, - data: &RuleData{Branch: "dev", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "and", want: false, }, { rules: &Rules{Branch: []string{"master"}, Event: []string{"push"}}, - data: &RuleData{Branch: "master", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "and", want: true, }, { rules: &Rules{Branch: []string{"master"}, Event: []string{"push"}}, - data: &RuleData{Branch: "master", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "and", want: false, }, { rules: &Rules{Path: []string{"foob.txt"}}, - data: &RuleData{Branch: "master", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "and", want: false, }, { rules: &Rules{Status: []string{"success", "failure"}}, - data: &RuleData{Branch: "master", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Tag: "refs/heads/master", Target: ""}, operator: "and", want: true, }, // or operator { rules: &Rules{Branch: []string{"master"}, Event: []string{"push"}}, - data: &RuleData{Branch: "master", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "or", want: true, }, { rules: &Rules{Branch: []string{"master"}, Event: []string{"push"}}, - data: &RuleData{Branch: "dev", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Event: "push", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "or", want: true, }, { rules: &Rules{Branch: []string{"master"}, Event: []string{"push"}}, - data: &RuleData{Branch: "master", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "or", want: true, }, { rules: &Rules{Branch: []string{"master"}, Event: []string{"push"}}, - data: &RuleData{Branch: "dev", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "or", want: false, }, { rules: &Rules{Path: []string{"foob.txt"}}, - data: &RuleData{Branch: "dev", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "dev", Event: "pull_request", Path: []string{"foo.txt", "/foo/bar.txt"}, Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "or", want: false, }, // Advanced Rulesets { rules: &Rules{Event: []string{"push", "pull_request"}, Tag: []string{"release/*"}}, - data: &RuleData{Branch: "master", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "release/*"}, + data: &RuleData{Branch: "master", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "release/*", Target: ""}, operator: "or", want: true, }, { rules: &Rules{Event: []string{"push", "pull_request"}, Tag: []string{"release/*"}}, - data: &RuleData{Branch: "master", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "release/*"}, + data: &RuleData{Branch: "master", Event: "push", Repo: "octocat/hello-world", Status: "pending", Tag: "release/*", Target: ""}, operator: "or", want: true, }, { rules: &Rules{Event: []string{"push", "pull_request"}, Tag: []string{"release/*"}}, - data: &RuleData{Branch: "master", Event: "tag", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master"}, + data: &RuleData{Branch: "master", Event: "tag", Repo: "octocat/hello-world", Status: "pending", Tag: "refs/heads/master", Target: ""}, operator: "or", want: false, }, @@ -342,7 +352,7 @@ func TestPipeline_Ruletype_MatchAnd(t *testing.T) { // Branch {rule: []string{"master"}, pattern: "master", want: true}, {rule: []string{"master"}, pattern: "dev", want: false}, - // Branch + // Comment {rule: []string{"ok to test"}, pattern: "ok to test", want: true}, {rule: []string{"ok to test"}, pattern: "rerun", want: false}, // Event @@ -357,6 +367,9 @@ func TestPipeline_Ruletype_MatchAnd(t *testing.T) { // Tag {rule: []string{"release/*"}, pattern: "release/*", want: true}, {rule: []string{"release/*"}, pattern: "stage/*", want: false}, + // Target + {rule: []string{"production"}, pattern: "production", want: true}, + {rule: []string{"stage"}, pattern: "production", want: false}, } // run test @@ -400,6 +413,9 @@ func TestPipeline_Ruletype_MatchOr(t *testing.T) { // Tag {rule: []string{"release/*"}, pattern: "release/*", want: true}, {rule: []string{"release/*"}, pattern: "stage/*", want: false}, + // Target + {rule: []string{"production"}, pattern: "production", want: true}, + {rule: []string{"stage"}, pattern: "production", want: false}, } // run test diff --git a/yaml/ruleset.go b/yaml/ruleset.go index 57681eb0..408f9de2 100644 --- a/yaml/ruleset.go +++ b/yaml/ruleset.go @@ -29,6 +29,7 @@ type ( Repo []string `yaml:"repo,omitempty"` Status []string `yaml:"status,omitempty"` Tag []string `yaml:"tag,omitempty"` + Target []string `yaml:"target,omitempty"` } ) @@ -76,6 +77,7 @@ func (r *Ruleset) UnmarshalYAML(unmarshal func(interface{}) error) error { advanced.If.Repo = append(advanced.If.Repo, simple.Repo...) advanced.If.Status = append(advanced.If.Status, simple.Status...) advanced.If.Tag = append(advanced.If.Tag, simple.Tag...) + advanced.If.Target = append(advanced.If.Target, simple.Target...) // set ruleset `if` to advanced `if` rules r.If = advanced.If @@ -99,6 +101,7 @@ func (r *Rules) ToPipeline() *pipeline.Rules { Repo: r.Repo, Status: r.Status, Tag: r.Tag, + Target: r.Target, } } @@ -113,6 +116,7 @@ func (r *Rules) UnmarshalYAML(unmarshal func(interface{}) error) error { Repo raw.StringSlice Status raw.StringSlice Tag raw.StringSlice + Target raw.StringSlice }) // attempt to unmarshal rules @@ -125,6 +129,7 @@ func (r *Rules) UnmarshalYAML(unmarshal func(interface{}) error) error { r.Repo = []string(rules.Repo) r.Status = []string(rules.Status) r.Tag = []string(rules.Tag) + r.Target = []string(rules.Target) } return err diff --git a/yaml/ruleset_test.go b/yaml/ruleset_test.go index 36da0fa7..da4ce6fd 100644 --- a/yaml/ruleset_test.go +++ b/yaml/ruleset_test.go @@ -26,6 +26,7 @@ func TestYaml_Ruleset_ToPipeline(t *testing.T) { Repo: slice, Status: slice, Tag: slice, + Target: slice, }, Unless: pipeline.Rules{ Branch: slice, @@ -35,6 +36,7 @@ func TestYaml_Ruleset_ToPipeline(t *testing.T) { Repo: slice, Status: slice, Tag: slice, + Target: slice, }, Operator: str, Continue: false, @@ -49,6 +51,7 @@ func TestYaml_Ruleset_ToPipeline(t *testing.T) { Repo: slice, Status: slice, Tag: slice, + Target: slice, }, Unless: Rules{ Branch: slice, @@ -58,6 +61,7 @@ func TestYaml_Ruleset_ToPipeline(t *testing.T) { Repo: slice, Status: slice, Tag: slice, + Target: slice, }, Operator: str, Continue: false, @@ -146,6 +150,7 @@ func TestYaml_Rules_ToPipeline(t *testing.T) { Repo: slice, Status: slice, Tag: slice, + Target: slice, } r := &Rules{ @@ -156,6 +161,7 @@ func TestYaml_Rules_ToPipeline(t *testing.T) { Repo: slice, Status: slice, Tag: slice, + Target: slice, } // run test