diff --git a/plugin/publish/npm.go b/plugin/publish/npm.go index d4b2cbb40..c01126e3a 100644 --- a/plugin/publish/npm.go +++ b/plugin/publish/npm.go @@ -3,6 +3,7 @@ package publish import ( "fmt" + "github.com/drone/drone/plugin/condition" "github.com/drone/drone/shared/build/buildfile" ) @@ -40,7 +41,7 @@ type NPM struct { // Registers the published package with the given tag Tag string `yaml:"tag,omitempty"` - Branch string `yaml:"branch,omitempty"` + Condition *condition.Condition `yaml:"when,omitempty"` } func (n *NPM) Write(f *buildfile.Buildfile) { @@ -71,3 +72,7 @@ func (n *NPM) Write(f *buildfile.Buildfile) { f.WriteCmd(fmt.Sprintf(npmPublishCmd, n.Folder)) } + +func (n *NPM) GetCondition() *condition.Condition { + return n.Condition +} diff --git a/plugin/publish/publish.go b/plugin/publish/publish.go index 4c8e859a9..86d861f33 100644 --- a/plugin/publish/publish.go +++ b/plugin/publish/publish.go @@ -1,6 +1,7 @@ package publish import ( + "github.com/drone/drone/plugin/condition" "github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/repo" ) @@ -17,22 +18,36 @@ type Publish struct { func (p *Publish) Write(f *buildfile.Buildfile, r *repo.Repo) { // S3 - if p.S3 != nil && (len(p.S3.Branch) == 0 || (len(p.S3.Branch) > 0 && r.Branch == p.S3.Branch)) { + if p.S3 != nil && match(p.S3.GetCondition(), r) { p.S3.Write(f) } // Swift - if p.Swift != nil && (len(p.Swift.Branch) == 0 || (len(p.Swift.Branch) > 0 && r.Branch == p.Swift.Branch)) { + if p.Swift != nil && match(p.Swift.GetCondition(), r) { p.Swift.Write(f) } // PyPI - if p.PyPI != nil && (len(p.PyPI.Branch) == 0 || (len(p.PyPI.Branch) > 0 && r.Branch == p.PyPI.Branch)) { + if p.PyPI != nil && match(p.PyPI.GetCondition(), r) { p.PyPI.Write(f) } // NPM - if p.NPM != nil && (len(p.NPM.Branch) == 0 || (len(p.NPM.Branch) > 0 && r.Branch == p.NPM.Branch)) { + if p.NPM != nil && match(p.NPM.GetCondition(), r) { p.NPM.Write(f) } } + +func match(c *condition.Condition, r *repo.Repo) bool { + switch { + case c == nil: + return true + case !c.MatchBranch(r.Branch): + return false + case !c.MatchOwner(r.Name): + return false + case !c.MatchPullRequest(r.PR): + return false + } + return true +} diff --git a/plugin/publish/pypi.go b/plugin/publish/pypi.go index 21d00cd8a..60f1e295e 100644 --- a/plugin/publish/pypi.go +++ b/plugin/publish/pypi.go @@ -3,6 +3,7 @@ package publish import ( "fmt" + "github.com/drone/drone/plugin/condition" "github.com/drone/drone/shared/build/buildfile" ) @@ -37,7 +38,8 @@ type PyPI struct { Password string `yaml:"password,omitempty"` Formats []string `yaml:"formats,omitempty"` Repository string `yaml:"repository,omitempty"` - Branch string `yaml:"branch,omitempty"` + + Condition *condition.Condition `yaml:"when,omitempty"` } func (p *PyPI) Write(f *buildfile.Buildfile) { @@ -83,3 +85,7 @@ func (p *PyPI) BuildFormatStr() string { } return fmtStr[:len(fmtStr)-1] } + +func (p *PyPI) GetCondition() *condition.Condition { + return p.Condition +} diff --git a/plugin/publish/s3.go b/plugin/publish/s3.go index aae1724d8..634d44e0f 100644 --- a/plugin/publish/s3.go +++ b/plugin/publish/s3.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/drone/drone/plugin/condition" "github.com/drone/drone/shared/build/buildfile" ) @@ -47,7 +48,7 @@ type S3 struct { // Recursive uploads Recursive bool `yaml:"recursive"` - Branch string `yaml:"branch,omitempty"` + Condition *condition.Condition `yaml:"when,omitempty"` } func (s *S3) Write(f *buildfile.Buildfile) { @@ -94,3 +95,7 @@ func (s *S3) Write(f *buildfile.Buildfile) { f.WriteCmd(fmt.Sprintf(`aws s3 cp %s s3://%s/%s --acl %s --region %s`, s.Source, s.Bucket, s.Target, s.Access, s.Region)) } } + +func (s *S3) GetCondition() *condition.Condition { + return s.Condition +} diff --git a/plugin/publish/swift.go b/plugin/publish/swift.go index 1589da7a5..d56b3e739 100644 --- a/plugin/publish/swift.go +++ b/plugin/publish/swift.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/drone/drone/plugin/condition" "github.com/drone/drone/shared/build/buildfile" ) @@ -34,7 +35,7 @@ type Swift struct { // object name if source is a file Target string `yaml:"target,omitempty"` - Branch string `yaml:"branch,omitempty"` + Condition *condition.Condition `yaml:"when,omitempty"` } func (s *Swift) Write(f *buildfile.Buildfile) { @@ -65,3 +66,7 @@ func (s *Swift) Write(f *buildfile.Buildfile) { f.WriteCmd(fmt.Sprintf(`swiftly put -i %s %s%s`, s.Source, s.Container, target)) } + +func (s *Swift) GetCondition() *condition.Condition { + return s.Condition +}