Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate semver rule to vc #90

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ toolchain go1.23.1
replace github.com/distribution/reference => github.com/distribution/reference v0.5.0

require (
github.com/Masterminds/semver/v3 v3.3.0
github.com/bombsimon/logrusr/v4 v4.1.0
github.com/chi-middleware/logrus-logger v0.3.0
github.com/containers/image/v5 v5.32.2
Expand All @@ -23,6 +22,7 @@ require (
github.com/ory/dockertest/v3 v3.11.0
github.com/prometheus/client_golang v1.20.5
github.com/reugn/go-quartz v0.13.0
github.com/shipengqi/vc v0.2.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/thanhpk/randstr v1.0.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
Expand Down Expand Up @@ -202,6 +200,8 @@ github.com/reugn/go-quartz v0.13.0 h1:0eMxvj28Qu1npIDdN9Mzg9hwyksGH6XJt4Cz0QB8EU
github.com/reugn/go-quartz v0.13.0/go.mod h1:0ghKksELp8MJ4h84T203aTHRF3Kug5BrxEW3ErBvhzY=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/shipengqi/vc v0.2.0 h1:o12S/csSz9siuTU2EmpzKnaHa7LuYxDA5zdjjSEv6F4=
github.com/shipengqi/vc v0.2.0/go.mod h1:OzfQDNheQAkQm5BZ2ZTiRjKhirVn4yaGUFGsN8C4IPY=
github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand Down
52 changes: 22 additions & 30 deletions internal/rules/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"sort"

"github.com/Masterminds/semver/v3"
"github.com/shipengqi/vc"

"github.com/orange-cloudavenue/kube-image-updater/internal/log"
)
Expand Down Expand Up @@ -35,10 +35,14 @@ func init() {
register(SemverPatch, &semverPatch{})
}

var funcParseSemVer = func(s string) (vc.Comparable, error) {
return vc.NewSemverStr(s)
}

// ! semver-major rule

func (s *semverMajor) Evaluate() (matchWithRule bool, newTag string, err error) {
x, err := semver.NewVersion(s.actualTag)
x, err := vc.NewSemverStr(s.actualTag)
if err != nil {
return false, "", err
}
Expand All @@ -47,19 +51,15 @@ func (s *semverMajor) Evaluate() (matchWithRule bool, newTag string, err error)
sort.Sort(sort.Reverse(sort.StringSlice(s.tags)))

for _, t := range s.tags {
ac, err := semver.NewVersion(t)
if err != nil {
log.WithError(err).WithField("tag", t).Error("Error parsing actual tag")
continue
}

v, err := semver.NewConstraint(fmt.Sprintf("^%s", x.IncMajor()))
// Original x = 1.0.0
// >=2.0.0
v, err := vc.NewConstraint(fmt.Sprintf(">=%s", x.IncMajor()), funcParseSemVer)
if err != nil {
log.WithError(err).WithField("constraint", x.IncMajor()).Error("Error parsing constraint")
log.WithError(err).WithField("constraint", x.IncMinor()).Error("Error parsing constraint")
continue
}

if v.Check(ac) {
if ok, _ := v.CheckString(t); ok {
s.SetNewTag(t)
return true, t, nil
}
Expand All @@ -71,7 +71,7 @@ func (s *semverMajor) Evaluate() (matchWithRule bool, newTag string, err error)
// ! semver-minor rule

func (s *semverMinor) Evaluate() (matchWithRule bool, newTag string, err error) {
x, err := semver.NewVersion(s.actualTag)
x, err := vc.NewSemverStr(s.actualTag)
if err != nil {
return false, "", err
}
Expand All @@ -80,19 +80,15 @@ func (s *semverMinor) Evaluate() (matchWithRule bool, newTag string, err error)
sort.Sort(sort.Reverse(sort.StringSlice(s.tags)))

for _, t := range s.tags {
ac, err := semver.NewVersion(t)
if err != nil {
log.WithError(err).WithField("tag", t).Error("Error parsing actual tag")
continue
}

v, err := semver.NewConstraint(fmt.Sprintf("^%s", x.IncMinor()))
// Original x = 1.0.0
// >=1.1.0 <2
v, err := vc.NewConstraint(fmt.Sprintf(">=%s <%s", x.IncMinor(), x.IncMajor()), funcParseSemVer)
if err != nil {
log.WithError(err).WithField("constraint", x.IncMinor()).Error("Error parsing constraint")
continue
}

if v.Check(ac) {
if ok, _ := v.CheckString(t); ok {
s.SetNewTag(t)
return true, t, nil
}
Expand All @@ -104,7 +100,7 @@ func (s *semverMinor) Evaluate() (matchWithRule bool, newTag string, err error)
// ! semver-patch rule

func (s *semverPatch) Evaluate() (matchWithRule bool, newTag string, err error) {
x, err := semver.NewVersion(s.actualTag)
x, err := vc.NewSemverStr(s.actualTag)
if err != nil {
return false, "", err
}
Expand All @@ -113,19 +109,15 @@ func (s *semverPatch) Evaluate() (matchWithRule bool, newTag string, err error)
sort.Sort(sort.Reverse(sort.StringSlice(s.tags)))

for _, t := range s.tags {
ac, err := semver.NewVersion(t)
if err != nil {
log.WithError(err).WithField("tag", t).Error("Error parsing actual tag")
continue
}

v, err := semver.NewConstraint(fmt.Sprintf(">=%s <%s", x.IncPatch(), x.IncMinor()))
// Original x = 1.0.0
// >=1.0.1 <1.1.0
v, err := vc.NewConstraint(fmt.Sprintf(">=%s <%s", x.IncPatch(), x.IncMinor()), funcParseSemVer)
if err != nil {
log.WithError(err).WithField("constraint", x.IncPatch()).Error("Error parsing constraint")
log.WithError(err).WithField("constraint", x.IncMinor()).Error("Error parsing constraint")
continue
}

if v.Check(ac) {
if ok, _ := v.CheckString(t); ok {
s.SetNewTag(t)
return true, t, nil
}
Expand Down