From 5fa26ebb5f1b751edbe972b16c3ab8757ff2eeff Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 9 Nov 2017 13:24:35 +0800 Subject: [PATCH] UseDefaultTag for skip build if not default branch. Signed-off-by: Bo-Yi Wu --- cmd/drone-docker/main.go | 14 +++++-- docker.go | 18 +++++++++ docker_test.go | 25 ++++++++++++ tags.go | 19 ++------- tags_test.go | 85 ++++++++++++---------------------------- 5 files changed, 82 insertions(+), 79 deletions(-) create mode 100644 docker_test.go diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index b993bb9..0efd4d8 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -246,11 +246,19 @@ func run(c *cli.Context) error { } if c.Bool("tags.auto") { - plugin.Build.Tags = docker.DefaultTagSuffix( + if docker.UseDefaultTag( // return true if not default branch, or not tag c.String("commit.ref"), c.String("repo.branch"), - c.String("tags.suffix"), - ) + ) { + plugin.Build.Tags = docker.DefaultTagSuffix( + c.String("commit.ref"), + c.String("tags.suffix"), + ) + } else { + logrus.Printf("skipping automated docker build for %s", c.String("commit.ref")) + + return nil + } } return plugin.Exec() diff --git a/docker.go b/docker.go index b3d0b77..908268c 100644 --- a/docker.go +++ b/docker.go @@ -61,6 +61,24 @@ type ( } ) +func stripHeadPrefix(ref string) string { + ref = strings.TrimPrefix(ref, "refs/heads/") + return ref +} + +// UseDefaultTag for keep only default branch for latest tag +func UseDefaultTag(ref, defaultBranch string) bool { + if strings.HasPrefix(ref, "refs/tags/") { + return true + } + + if stripHeadPrefix(ref) == defaultBranch { + return true + } + + return false +} + // Exec executes the plugin step func (p Plugin) Exec() error { // start the Docker daemon server diff --git a/docker_test.go b/docker_test.go new file mode 100644 index 0000000..ffd1cf2 --- /dev/null +++ b/docker_test.go @@ -0,0 +1,25 @@ +package docker + +import "testing" + +func Test_stripHeadPrefix(t *testing.T) { + type args struct { + ref string + } + tests := []struct { + args args + want string + }{ + { + args: args{ + ref: "refs/heads/master", + }, + want: "master", + }, + } + for _, tt := range tests { + if got := stripHeadPrefix(tt.args.ref); got != tt.want { + t.Errorf("stripHeadPrefix() = %v, want %v", got, tt.want) + } + } +} diff --git a/tags.go b/tags.go index 5f33e95..5e1b36e 100644 --- a/tags.go +++ b/tags.go @@ -9,8 +9,8 @@ import ( // DefaultTagSuffix returns a set of default suggested tags // based on the commit ref with an attached suffix. -func DefaultTagSuffix(ref, defaultBranch, suffix string) []string { - tags := DefaultTags(ref, defaultBranch) +func DefaultTagSuffix(ref, suffix string) []string { + tags := DefaultTags(ref) if len(suffix) == 0 { return tags } @@ -26,18 +26,10 @@ func DefaultTagSuffix(ref, defaultBranch, suffix string) []string { // DefaultTags returns a set of default suggested tags based on // the commit ref. -func DefaultTags(ref, defaultBranch string) []string { - - if defaultBranch != "" && strings.HasPrefix(ref, "refs/heads/") { - if stripHeadPrefix(ref) != defaultBranch { - return []string{} - } - } - +func DefaultTags(ref string) []string { if !strings.HasPrefix(ref, "refs/tags/") { return []string{"latest"} } - v := stripTagPrefix(ref) version, err := semver.NewVersion(v) if err != nil { @@ -66,8 +58,3 @@ func stripTagPrefix(ref string) string { ref = strings.TrimPrefix(ref, "v") return ref } - -func stripHeadPrefix(ref string) string { - ref = strings.TrimPrefix(ref, "refs/heads/") - return ref -} diff --git a/tags_test.go b/tags_test.go index 709b6ba..9d356f8 100644 --- a/tags_test.go +++ b/tags_test.go @@ -25,29 +25,23 @@ func Test_stripTagPrefix(t *testing.T) { func TestDefaultTags(t *testing.T) { var tests = []struct { - Before string - DefaultBranch string - After []string + Before string + After []string }{ - {"", "master", []string{"latest"}}, - {"refs/heads/master", "master", []string{"latest"}}, - {"refs/tags/0.9.0", "master", []string{"0.9", "0.9.0"}}, - {"refs/tags/1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, - {"refs/tags/v1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, - {"refs/tags/v1.0.0-alpha.1", "master", []string{"1.0.0-alpha.1"}}, + {"", []string{"latest"}}, + {"refs/heads/master", []string{"latest"}}, + {"refs/tags/0.9.0", []string{"0.9", "0.9.0"}}, + {"refs/tags/1.0.0", []string{"1", "1.0", "1.0.0"}}, + {"refs/tags/v1.0.0", []string{"1", "1.0", "1.0.0"}}, + {"refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}}, // malformed or errors - {"refs/tags/x1.0.0", "master", []string{"latest"}}, - {"v1.0.0", "master", []string{"latest"}}, - - // defualt branch - {"refs/heads/master", "master", []string{"latest"}}, - {"refs/heads/test", "master", []string{}}, - {"refs/tags/v1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, + {"refs/tags/x1.0.0", []string{"latest"}}, + {"v1.0.0", []string{"latest"}}, } for _, test := range tests { - got, want := DefaultTags(test.Before, test.DefaultBranch), test.After + got, want := DefaultTags(test.Before), test.After if !reflect.DeepEqual(got, want) { t.Errorf("Got tag %v, want %v", got, want) } @@ -56,19 +50,16 @@ func TestDefaultTags(t *testing.T) { func TestDefaultTagSuffix(t *testing.T) { var tests = []struct { - Before string - DefaultBranch string - Suffix string - After []string + Before string + Suffix string + After []string }{ // without suffix { - After: []string{"latest"}, - DefaultBranch: "", + After: []string{"latest"}, }, { - Before: "refs/tags/v1.0.0", - DefaultBranch: "", + Before: "refs/tags/v1.0.0", After: []string{ "1", "1.0", @@ -77,14 +68,12 @@ func TestDefaultTagSuffix(t *testing.T) { }, // with suffix { - DefaultBranch: "", - Suffix: "linux-amd64", - After: []string{"linux-amd64"}, + Suffix: "linux-amd64", + After: []string{"linux-amd64"}, }, { - DefaultBranch: "", - Before: "refs/tags/v1.0.0", - Suffix: "linux-amd64", + Before: "refs/tags/v1.0.0", + Suffix: "linux-amd64", After: []string{ "1-linux-amd64", "1.0-linux-amd64", @@ -92,14 +81,12 @@ func TestDefaultTagSuffix(t *testing.T) { }, }, { - DefaultBranch: "", - Suffix: "nanoserver", - After: []string{"nanoserver"}, + Suffix: "nanoserver", + After: []string{"nanoserver"}, }, { - DefaultBranch: "", - Before: "refs/tags/v1.9.2", - Suffix: "nanoserver", + Before: "refs/tags/v1.9.2", + Suffix: "nanoserver", After: []string{ "1-nanoserver", "1.9-nanoserver", @@ -109,31 +96,9 @@ func TestDefaultTagSuffix(t *testing.T) { } for _, test := range tests { - got, want := DefaultTagSuffix(test.Before, test.DefaultBranch, test.Suffix), test.After + got, want := DefaultTagSuffix(test.Before, test.Suffix), test.After if !reflect.DeepEqual(got, want) { t.Errorf("Got tag %v, want %v", got, want) } } } - -func Test_stripHeadPrefix(t *testing.T) { - type args struct { - ref string - } - tests := []struct { - args args - want string - }{ - { - args: args{ - ref: "refs/heads/master", - }, - want: "master", - }, - } - for _, tt := range tests { - if got := stripHeadPrefix(tt.args.ref); got != tt.want { - t.Errorf("stripHeadPrefix() = %v, want %v", got, tt.want) - } - } -}