Skip to content

Commit 6cc2e43

Browse files
committed
semver parsing fails pipeline step
1 parent b5598ee commit 6cc2e43

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

cmd/drone-docker/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,15 @@ func run(c *cli.Context) error {
298298
c.String("commit.ref"),
299299
c.String("repo.branch"),
300300
) {
301-
plugin.Build.Tags = docker.DefaultTagSuffix(
301+
tag, err := docker.DefaultTagSuffix(
302302
c.String("commit.ref"),
303303
c.String("tags.suffix"),
304304
)
305+
if err != nil {
306+
logrus.Printf("cannot build docker image for %s, invalid semantic version", c.String("commit.ref"))
307+
return err
308+
}
309+
plugin.Build.Tags = tag
305310
} else {
306311
logrus.Printf("skipping automated docker build for %s", c.String("commit.ref"))
307312
return nil

tags.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import (
99

1010
// DefaultTagSuffix returns a set of default suggested tags
1111
// based on the commit ref with an attached suffix.
12-
func DefaultTagSuffix(ref, suffix string) []string {
13-
tags := DefaultTags(ref)
12+
func DefaultTagSuffix(ref, suffix string) ([]string, error) {
13+
tags, err := DefaultTags(ref)
14+
if err != nil {
15+
return nil, err
16+
}
1417
if len(suffix) == 0 {
15-
return tags
18+
return tags, nil
1619
}
1720
for i, tag := range tags {
1821
if tag == "latest" {
@@ -21,7 +24,7 @@ func DefaultTagSuffix(ref, suffix string) []string {
2124
tags[i] = fmt.Sprintf("%s-%s", tag, suffix)
2225
}
2326
}
24-
return tags
27+
return tags, nil
2528
}
2629

2730
func splitOff(input string, delim string) string {
@@ -36,19 +39,19 @@ func splitOff(input string, delim string) string {
3639

3740
// DefaultTags returns a set of default suggested tags based on
3841
// the commit ref.
39-
func DefaultTags(ref string) []string {
42+
func DefaultTags(ref string) ([]string, error) {
4043
if !strings.HasPrefix(ref, "refs/tags/") {
41-
return []string{"latest"}
44+
return []string{"latest"}, nil
4245
}
4346
v := stripTagPrefix(ref)
4447
version, err := semver.NewVersion(v)
4548
if err != nil {
46-
return []string{"latest"}
49+
return []string{"latest"}, err
4750
}
4851
if version.PreRelease != "" || version.Metadata != "" {
4952
return []string{
5053
version.String(),
51-
}
54+
}, nil
5255
}
5356

5457
v = stripTagPrefix(ref)
@@ -59,13 +62,13 @@ func DefaultTags(ref string) []string {
5962
return []string{
6063
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
6164
fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch),
62-
}
65+
}, nil
6366
}
6467
return []string{
6568
fmt.Sprintf("%0*d", len(dotParts[0]), version.Major),
6669
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
6770
fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch),
68-
}
71+
}, nil
6972
}
7073

7174
// UseDefaultTag for keep only default branch for latest tag

tags_test.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,35 @@ func TestDefaultTags(t *testing.T) {
3434
{"refs/tags/1.0.0", []string{"1", "1.0", "1.0.0"}},
3535
{"refs/tags/v1.0.0", []string{"1", "1.0", "1.0.0"}},
3636
{"refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}},
37-
38-
// malformed or errors
39-
{"refs/tags/x1.0.0", []string{"latest"}},
40-
{"v1.0.0", []string{"latest"}},
4137
}
4238

4339
for _, test := range tests {
44-
got, want := DefaultTags(test.Before), test.After
40+
tags, err := DefaultTags(test.Before)
41+
if err != nil {
42+
t.Error(err)
43+
continue
44+
}
45+
got, want := tags, test.After
4546
if !reflect.DeepEqual(got, want) {
4647
t.Errorf("Got tag %v, want %v", got, want)
4748
}
4849
}
4950
}
5051

52+
func TestDefaultTagsError(t *testing.T) {
53+
var tests = []string{
54+
"refs/tags/x1.0.0",
55+
"refs/tags/20190203",
56+
}
57+
58+
for _, test := range tests {
59+
_, err := DefaultTags(test)
60+
if err == nil {
61+
t.Errorf("Expect tag error for %s", test)
62+
}
63+
}
64+
}
65+
5166
func TestDefaultTagSuffix(t *testing.T) {
5267
var tests = []struct {
5368
Before string
@@ -105,7 +120,12 @@ func TestDefaultTagSuffix(t *testing.T) {
105120
}
106121

107122
for _, test := range tests {
108-
got, want := DefaultTagSuffix(test.Before, test.Suffix), test.After
123+
tag, err := DefaultTagSuffix(test.Before, test.Suffix)
124+
if err != nil {
125+
t.Error(err)
126+
continue
127+
}
128+
got, want := tag, test.After
109129
if !reflect.DeepEqual(got, want) {
110130
t.Errorf("Got tag %v, want %v", got, want)
111131
}

0 commit comments

Comments
 (0)