replace commit branch with refs

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-11-09 10:05:50 +08:00
parent 3c1cfa628f
commit 0927e34a03
3 changed files with 50 additions and 37 deletions

View File

@ -193,13 +193,7 @@ func main() {
EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "commit.branch", Name: "repo.branch",
Value: "master",
Usage: "git commit branch",
EnvVar: "DRONE_COMMIT_BRANCH",
},
cli.StringFlag{
Name: "default.branch",
Usage: "repository default branch", Usage: "repository default branch",
EnvVar: "DRONE_REPO_BRANCH", EnvVar: "DRONE_REPO_BRANCH",
}, },
@ -222,8 +216,7 @@ func run(c *cli.Context) error {
Build: docker.Build{ Build: docker.Build{
Remote: c.String("remote.url"), Remote: c.String("remote.url"),
Name: c.String("commit.sha"), Name: c.String("commit.sha"),
Branch: c.String("commit.branch"), DefaultBranch: c.String("repo.branch"),
DefaultBranch: c.String("default.branch"),
Dockerfile: c.String("dockerfile"), Dockerfile: c.String("dockerfile"),
Context: c.String("context"), Context: c.String("context"),
Tags: c.StringSlice("tags"), Tags: c.StringSlice("tags"),
@ -256,8 +249,7 @@ func run(c *cli.Context) error {
plugin.Build.Tags = docker.DefaultTagSuffix( plugin.Build.Tags = docker.DefaultTagSuffix(
c.String("commit.ref"), c.String("commit.ref"),
c.String("tags.suffix"), c.String("tags.suffix"),
c.String("commit.branch"), c.String("repo.branch"),
c.String("default.branch"),
) )
} }

15
tags.go
View File

@ -9,8 +9,8 @@ import (
// DefaultTagSuffix returns a set of default suggested tags // DefaultTagSuffix returns a set of default suggested tags
// based on the commit ref with an attached suffix. // based on the commit ref with an attached suffix.
func DefaultTagSuffix(ref, suffix, commitBranch, defaultBranch string) []string { func DefaultTagSuffix(ref, suffix, defaultBranch string) []string {
tags := DefaultTags(ref, commitBranch, defaultBranch) tags := DefaultTags(ref, defaultBranch)
if len(suffix) == 0 { if len(suffix) == 0 {
return tags return tags
} }
@ -26,11 +26,13 @@ func DefaultTagSuffix(ref, suffix, commitBranch, defaultBranch string) []string
// DefaultTags returns a set of default suggested tags based on // DefaultTags returns a set of default suggested tags based on
// the commit ref. // the commit ref.
func DefaultTags(ref, commitBranch, defaultBranch string) []string { func DefaultTags(ref, defaultBranch string) []string {
if defaultBranch != "" && commitBranch != defaultBranch && !strings.HasPrefix(ref, "refs/tags/") { if defaultBranch != "" && strings.HasPrefix(ref, "refs/heads/") {
if stripHeadPrefix(ref) != defaultBranch {
return []string{} return []string{}
} }
}
if !strings.HasPrefix(ref, "refs/tags/") { if !strings.HasPrefix(ref, "refs/tags/") {
return []string{"latest"} return []string{"latest"}
@ -64,3 +66,8 @@ func stripTagPrefix(ref string) string {
ref = strings.TrimPrefix(ref, "v") ref = strings.TrimPrefix(ref, "v")
return ref return ref
} }
func stripHeadPrefix(ref string) string {
ref = strings.TrimPrefix(ref, "refs/heads/")
return ref
}

View File

@ -26,29 +26,28 @@ func Test_stripTagPrefix(t *testing.T) {
func TestDefaultTags(t *testing.T) { func TestDefaultTags(t *testing.T) {
var tests = []struct { var tests = []struct {
Before string Before string
CommitBranch string
DefaultBranch string DefaultBranch string
After []string After []string
}{ }{
{"", "master", "", []string{"latest"}}, {"", "master", []string{"latest"}},
{"refs/heads/master", "master", "", []string{"latest"}}, {"refs/heads/master", "master", []string{"latest"}},
{"refs/tags/0.9.0", "master", "", []string{"0.9", "0.9.0"}}, {"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/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", "master", []string{"1", "1.0", "1.0.0"}},
{"refs/tags/v1.0.0-alpha.1", "master", "", []string{"1.0.0-alpha.1"}}, {"refs/tags/v1.0.0-alpha.1", "master", []string{"1.0.0-alpha.1"}},
// malformed or errors // malformed or errors
{"refs/tags/x1.0.0", "master", "", []string{"latest"}}, {"refs/tags/x1.0.0", "master", []string{"latest"}},
{"v1.0.0", "master", "", []string{"latest"}}, {"v1.0.0", "master", []string{"latest"}},
// defualt branch // defualt branch
{"refs/heads/master", "master", "master", []string{"latest"}}, {"refs/heads/master", "master", []string{"latest"}},
{"refs/heads/test", "test", "master", []string{}}, {"refs/heads/test", "master", []string{}},
{"refs/tags/v1.0.0", "v1.0.0", "master", []string{"1", "1.0", "1.0.0"}}, {"refs/tags/v1.0.0", "master", []string{"1", "1.0", "1.0.0"}},
} }
for _, test := range tests { for _, test := range tests {
got, want := DefaultTags(test.Before, test.CommitBranch, test.DefaultBranch), test.After got, want := DefaultTags(test.Before, test.DefaultBranch), test.After
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("Got tag %v, want %v", got, want) t.Errorf("Got tag %v, want %v", got, want)
} }
@ -58,7 +57,6 @@ func TestDefaultTags(t *testing.T) {
func TestDefaultTagSuffix(t *testing.T) { func TestDefaultTagSuffix(t *testing.T) {
var tests = []struct { var tests = []struct {
Before string Before string
CommitBranch string
DefaultBranch string DefaultBranch string
Suffix string Suffix string
After []string After []string
@ -66,12 +64,10 @@ func TestDefaultTagSuffix(t *testing.T) {
// without suffix // without suffix
{ {
After: []string{"latest"}, After: []string{"latest"},
CommitBranch: "master",
DefaultBranch: "", DefaultBranch: "",
}, },
{ {
Before: "refs/tags/v1.0.0", Before: "refs/tags/v1.0.0",
CommitBranch: "master",
DefaultBranch: "", DefaultBranch: "",
After: []string{ After: []string{
"1", "1",
@ -81,13 +77,11 @@ func TestDefaultTagSuffix(t *testing.T) {
}, },
// with suffix // with suffix
{ {
CommitBranch: "master",
DefaultBranch: "", DefaultBranch: "",
Suffix: "linux-amd64", Suffix: "linux-amd64",
After: []string{"linux-amd64"}, After: []string{"linux-amd64"},
}, },
{ {
CommitBranch: "master",
DefaultBranch: "", DefaultBranch: "",
Before: "refs/tags/v1.0.0", Before: "refs/tags/v1.0.0",
Suffix: "linux-amd64", Suffix: "linux-amd64",
@ -98,13 +92,11 @@ func TestDefaultTagSuffix(t *testing.T) {
}, },
}, },
{ {
CommitBranch: "master",
DefaultBranch: "", DefaultBranch: "",
Suffix: "nanoserver", Suffix: "nanoserver",
After: []string{"nanoserver"}, After: []string{"nanoserver"},
}, },
{ {
CommitBranch: "master",
DefaultBranch: "", DefaultBranch: "",
Before: "refs/tags/v1.9.2", Before: "refs/tags/v1.9.2",
Suffix: "nanoserver", Suffix: "nanoserver",
@ -117,9 +109,31 @@ func TestDefaultTagSuffix(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
got, want := DefaultTagSuffix(test.Before, test.Suffix, test.CommitBranch, test.DefaultBranch), test.After got, want := DefaultTagSuffix(test.Before, test.Suffix, test.DefaultBranch), test.After
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("Got tag %v, want %v", 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)
}
}
}