forked from drone-plugins/drone-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement default tags logic drone-plugins#150
- Loading branch information
1 parent
3727b33
commit 6f5d6e2
Showing
5 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package docker | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/coreos/go-semver/semver" | ||
) | ||
|
||
// Default tags returns a set of default suggested tags based on | ||
// the commit ref. | ||
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 { | ||
return []string{"latest"} | ||
} | ||
if version.PreRelease != "" || version.Metadata != "" { | ||
return []string{ | ||
version.String(), | ||
} | ||
} | ||
if version.Major == 0 { | ||
return []string{ | ||
fmt.Sprintf("%d.%d", version.Major, version.Minor), | ||
fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.Patch), | ||
} | ||
} | ||
return []string{ | ||
fmt.Sprint(version.Major), | ||
fmt.Sprintf("%d.%d", version.Major, version.Minor), | ||
fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.Patch), | ||
} | ||
} | ||
|
||
func stripTagPrefix(ref string) string { | ||
ref = strings.TrimPrefix(ref, "refs/tags/") | ||
ref = strings.TrimPrefix(ref, "v") | ||
return ref | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package docker | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_stripTagPrefix(t *testing.T) { | ||
var tests = []struct { | ||
Before string | ||
After string | ||
}{ | ||
{"refs/tags/1.0.0", "1.0.0"}, | ||
{"refs/tags/v1.0.0", "1.0.0"}, | ||
{"v1.0.0", "1.0.0"}, | ||
} | ||
|
||
for _, test := range tests { | ||
got, want := stripTagPrefix(test.Before), test.After | ||
if got != want { | ||
t.Errorf("Got tag %s, want %s", got, want) | ||
} | ||
} | ||
} | ||
|
||
func Test_defaultTags(t *testing.T) { | ||
var tests = []struct { | ||
Before string | ||
After []string | ||
}{ | ||
{"", []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", []string{"latest"}}, | ||
{"v1.0.0", []string{"latest"}}, | ||
} | ||
|
||
for _, test := range tests { | ||
got, want := DefaultTags(test.Before), test.After | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("Got tag %v, want %v", got, want) | ||
} | ||
} | ||
} |