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

cscli hub: handle freebsd pre-release version numbers #3423

Merged
merged 1 commit into from
Jan 23, 2025
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 cmd/crowdsec-cli/require/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
return "master"
}

csVersion := cwversion.VersionStrip()
csVersion := cwversion.BaseVersion()

Check warning on line 72 in cmd/crowdsec-cli/require/branch.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/require/branch.go#L72

Added line #L72 was not covered by tests
if csVersion == "" {
log.Warning("Crowdsec version is not set, using hub branch 'master'")
return "master"
Expand Down
18 changes: 14 additions & 4 deletions pkg/cwversion/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"fmt"
"regexp"
"strings"

"github.com/crowdsecurity/go-cs-lib/maptools"
Expand Down Expand Up @@ -57,10 +58,19 @@
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)

Check warning on line 75 in pkg/cwversion/version.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwversion/version.go#L74-L75

Added lines #L74 - L75 were not covered by tests
}
68 changes: 68 additions & 0 deletions pkg/cwversion/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
Loading