From 4935dc536e9be4da2cb49f05ab7ca68d57c8ffae Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Thu, 23 Jan 2025 09:29:29 +0100 Subject: [PATCH] cscli hub: handle freebsd pre-release version numbers (#3423) --- cmd/crowdsec-cli/require/branch.go | 2 +- pkg/cwversion/version.go | 18 ++++++-- pkg/cwversion/version_test.go | 68 ++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 pkg/cwversion/version_test.go diff --git a/cmd/crowdsec-cli/require/branch.go b/cmd/crowdsec-cli/require/branch.go index 09acc0fef8a..ab9b8e50bdc 100644 --- a/cmd/crowdsec-cli/require/branch.go +++ b/cmd/crowdsec-cli/require/branch.go @@ -69,7 +69,7 @@ func chooseBranch(ctx context.Context, cfg *csconfig.Config) string { return "master" } - csVersion := cwversion.VersionStrip() + csVersion := cwversion.BaseVersion() if csVersion == "" { log.Warning("Crowdsec version is not set, using hub branch 'master'") return "master" diff --git a/pkg/cwversion/version.go b/pkg/cwversion/version.go index 2cb7de13e18..87d855444e7 100644 --- a/pkg/cwversion/version.go +++ b/pkg/cwversion/version.go @@ -2,6 +2,7 @@ package cwversion import ( "fmt" + "regexp" "strings" "github.com/crowdsecurity/go-cs-lib/maptools" @@ -57,10 +58,19 @@ func FullString() string { return ret } -// VersionStrip remove the tag from the version string, used to match with a hub branch -func VersionStrip() string { - ret := strings.Split(version.Version, "~") - ret = strings.Split(ret[0], "-") +// StripTags removes any tag (-rc, ~foo3, .r1, etc) from a version string +func StripTags(version string) string { + reVersion := regexp.MustCompile(`^v(\d+)\.(\d+)\.(\d+)`) + ret := reVersion.FindStringSubmatch(version) + + if len(ret) == 0 { + return version + } return ret[0] } + +// BaseVersion returns the version number used to match a hub branch. +func BaseVersion() string { + return StripTags(version.Version) +} diff --git a/pkg/cwversion/version_test.go b/pkg/cwversion/version_test.go new file mode 100644 index 00000000000..13293d4a479 --- /dev/null +++ b/pkg/cwversion/version_test.go @@ -0,0 +1,68 @@ +package cwversion + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestStripTags(t *testing.T) { + tests := []struct { + name string + input string + want string + }{ + { + name: "no tag, valid version v1.2.3", + input: "v1.2.3", + want: "v1.2.3", + }, + { + name: "tag appended with dash", + input: "v1.2.3-rc1", + want: "v1.2.3", + }, + { + name: "tag appended with tilde", + input: "v1.2.3~foo3", + want: "v1.2.3", + }, + { + name: "tag appended with dot", + input: "v1.2.3.r1", + want: "v1.2.3", + }, + { + name: "tag appended directly", + input: "v1.2.3r1", + want: "v1.2.3", + }, + { + name: "multiple digits in version", + input: "v10.20.30-rc2", + want: "v10.20.30", + }, + { + name: "invalid version (no 'v' prefix)", + input: "1.2.3-tag", + want: "1.2.3-tag", + }, + { + name: "random string", + input: "some-random-string", + want: "some-random-string", + }, + { + name: "freebsd pre-release", + input: "v1.6.5.r1", + want: "v1.6.5", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := StripTags(tt.input) + require.Equal(t, tt.want, got) + }) + } +}