Enhance SemVersion detection (#52)

close #51

Reviewed-on: https://codeberg.org/woodpecker-plugins/plugin-docker-buildx/pulls/52
Reviewed-by: Lauris BH <lafriks@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Co-committed-by: 6543 <6543@obermui.de>
This commit is contained in:
6543 2022-12-21 17:11:09 +00:00 committed by Lauris BH
parent 186b87cf88
commit ccd5834d4c
5 changed files with 70 additions and 44 deletions

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.18
require (
codeberg.org/6543/go-yaml2json v0.3.0
github.com/coreos/go-semver v0.3.0
github.com/6543/go-version v1.3.1
github.com/drone-plugins/drone-plugin-lib v0.4.0
github.com/joho/godotenv v1.4.0
github.com/sirupsen/logrus v1.9.0

5
go.sum
View File

@ -1,10 +1,10 @@
codeberg.org/6543/go-yaml2json v0.3.0 h1:BlvjmY0Gous8P+rr8aBdgPYnIfUAqFepF8q7Tp0R5t8=
codeberg.org/6543/go-yaml2json v0.3.0/go.mod h1:mz61q14LWF4ZABrgMEDMmk3t9dPi6zgR1uBh2VKV2RQ=
github.com/6543/go-version v1.3.1 h1:HvOp+Telns7HWJ2Xo/05YXQSB2bE0WmVgbHqwMPZT4U=
github.com/6543/go-version v1.3.1/go.mod h1:oqFAHCwtLVUTLdhQmVZWYvaHXTdsbB4SY85at64SQEo=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
@ -66,7 +66,6 @@ golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f/go.mod h1:SgwaegtQh8clI
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -5,7 +5,7 @@ import (
"strings"
"time"
"github.com/coreos/go-semver/semver"
"github.com/6543/go-version"
)
// Labels returns list of labels to use for image
@ -22,8 +22,8 @@ func (p *Plugin) Labels() []string {
if p.pipeline.Commit.SHA != "" {
l = append(l, fmt.Sprintf("org.opencontainers.image.revision=%s", p.pipeline.Commit.SHA))
}
if p.settings.Build.Ref != "" && strings.HasPrefix(p.settings.Build.Ref, "refs/tags/") {
v, err := semver.NewVersion(strings.TrimPrefix(p.settings.Build.Ref[10:], "v"))
if p.settings.Build.Ref != "" && strings.HasPrefix(p.settings.Build.Ref, tagRefPrefix) {
v, err := version.NewSemver(stripTagPrefix(p.settings.Build.Ref))
if err == nil && v != nil {
l = append(l, fmt.Sprintf("org.opencontainers.image.version=%s", v.String()))
}

View File

@ -5,9 +5,13 @@ import (
"regexp"
"strings"
"github.com/coreos/go-semver/semver"
"github.com/6543/go-version"
)
const tagRefPrefix = "refs/tags/"
var dateRegex = regexp.MustCompile(`^(\d{8}|\d{4}-\d{2}-\d{2})$`)
// DefaultTagSuffix returns a set of default suggested tags
// based on the commit ref with an attached suffix.
func DefaultTagSuffix(ref, defaultTag, suffix string) ([]string, error) {
@ -41,41 +45,59 @@ func splitOff(input, delim string) string {
// DefaultTags returns a set of default suggested tags based on
// the commit ref.
func DefaultTags(ref, defaultTag string) ([]string, error) {
if !strings.HasPrefix(ref, "refs/tags/") {
// check if no tag event
if !strings.HasPrefix(ref, tagRefPrefix) {
return []string{defaultTag}, nil
}
v := stripTagPrefix(ref)
version, err := semver.NewVersion(v)
// else it's an tag event
tagString := stripTagPrefix(ref)
// check if date
if dateRegex.MatchString(tagString) {
return []string{tagString}, nil
}
version, err := version.NewSemver(tagString)
// if no semversion return default tag and error
if err != nil {
return []string{defaultTag}, err
}
if version.PreRelease != "" || version.Metadata != "" {
vParts := version.Segments()
major, minor, patch := vParts[0], vParts[1], vParts[2]
// if prerelease or version with metadata, only use this strict version
if version.Prerelease() != "" || version.Metadata() != "" {
return []string{
version.String(),
}, nil
}
v = stripTagPrefix(ref)
v = splitOff(splitOff(v, "+"), "-")
dotParts := strings.SplitN(v, ".", 3)
if version.Major == 0 {
// check if version is acutaly a date (%Y%m%d) ... and only return that if so
if major > 999 && major < 10000 && minor == 0 && patch == 0 {
return []string{
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch),
fmt.Sprintf("%d", major),
}, nil
}
if major == 0 {
return []string{
fmt.Sprintf("%d.%d", major, minor),
fmt.Sprintf("%d.%d.%d", major, minor, patch),
}, nil
}
return []string{
fmt.Sprintf("%0*d", len(dotParts[0]), version.Major),
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
fmt.Sprintf("%0*d.%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor, len(dotParts[2]), version.Patch),
fmt.Sprintf("%d", major),
fmt.Sprintf("%d.%d", major, minor),
fmt.Sprintf("%d.%d.%d", major, minor, patch),
}, nil
}
// UseDefaultTag for keep only default branch for latest tag
// return true if tag event or default branch
func UseDefaultTag(ref, defaultBranch string) bool {
return strings.HasPrefix(ref, "refs/tags/") ||
return strings.HasPrefix(ref, tagRefPrefix) ||
stripHeadPrefix(ref) == defaultBranch
}
@ -84,9 +106,7 @@ func stripHeadPrefix(ref string) string {
}
func stripTagPrefix(ref string) string {
ref = strings.TrimPrefix(ref, "refs/tags/")
ref = strings.TrimPrefix(ref, "v")
return ref
return strings.TrimPrefix(ref, tagRefPrefix)
}
func isSingleTag(tag string) bool {

View File

@ -3,6 +3,8 @@ package plugin
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_stripTagPrefix(t *testing.T) {
@ -11,8 +13,8 @@ func Test_stripTagPrefix(t *testing.T) {
After string
}{
{"refs/tags/1.0.0", "1.0.0"},
{"refs/tags/v1.0.0", "1.0.0"},
{"v1.0.0", "1.0.0"},
{"refs/tags/v1.0.0", "v1.0.0"},
{"v1.0.0", "v1.0.0"},
}
for _, test := range tests {
@ -29,12 +31,20 @@ func TestDefaultTags(t *testing.T) {
Before string
After []string
}{
// no tag event
{"latest", "", []string{"latest"}},
{"latest", "refs/heads/master", []string{"latest"}},
// tag event with semver
{"latest", "refs/tags/0.9.0", []string{"0.9", "0.9.0"}},
{"latest", "refs/tags/1.0.0", []string{"1", "1.0", "1.0.0"}},
{"latest", "refs/tags/v1.0.0", []string{"1", "1.0", "1.0.0"}},
{"latest", "refs/tags/v1.2.3-rc1", []string{"1.2.3-rc1"}},
{"latest", "refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}},
{"latest", "refs/tags/v20221221", []string{"20221221", "20221221.0", "20221221.0.0"}},
{"latest", "refs/tags/v2022-12-21", []string{"2022.0.0-12-21"}},
// tag event with date
{"latest", "refs/tags/20221221", []string{"20221221"}},
{"latest", "refs/tags/2022-12-21", []string{"2022-12-21"}},
}
for _, test := range tests {
@ -61,14 +71,14 @@ func TestDefaultTagsError(t *testing.T) {
},
{
DefaultTag: "latest",
Before: "refs/tags/20190203",
Before: "refs/tags/2a",
},
}
for _, test := range tests {
_, err := DefaultTags(test.Before, test.DefaultTag)
tags, err := DefaultTags(test.Before, test.DefaultTag)
if err == nil {
t.Errorf("Expect tag error for %s", test)
t.Errorf("Expect tag error for %s, got tags %v", test, tags)
}
}
}
@ -186,8 +196,8 @@ func TestDefaultTagSuffix(t *testing.T) {
Suffix: "nanoserver",
After: []string{
"18-nanoserver",
"18.06-nanoserver",
"18.06.0-nanoserver",
"18.6-nanoserver",
"18.6.0-nanoserver",
},
},
{
@ -197,22 +207,19 @@ func TestDefaultTagSuffix(t *testing.T) {
Suffix: "nanoserver",
After: []string{
"18-nanoserver",
"18.06-nanoserver",
"18.06.0-nanoserver",
"18.6-nanoserver",
"18.6.0-nanoserver",
},
},
}
for _, test := range tests {
tag, err := DefaultTagSuffix(test.Before, test.DefaultTag, test.Suffix)
if err != nil {
t.Error(err)
continue
}
got, want := tag, test.After
if !reflect.DeepEqual(got, want) {
t.Errorf("%q. Got tag %v, want %v", test.Name, got, want)
}
t.Run(test.Name, func(t *testing.T) {
tags, err := DefaultTagSuffix(test.Before, test.DefaultTag, test.Suffix)
if assert.NoError(t, err) {
assert.EqualValues(t, test.After, tags)
}
})
}
}