Skip to content

Commit

Permalink
chore: Use golang.org/x/mod/semver for version parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
stanhu committed Dec 4, 2023
1 parent 45dc3ed commit 73dd2d3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 52 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ require (
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq
golang.org/x/exp v0.0.0-20231127185646-65229373498e h1:Gvh4YaCaXNs6dKTlfgismwWZKyjVZXwOPfIyUaqU3No=
golang.org/x/exp v0.0.0-20231127185646-65229373498e/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand Down
31 changes: 1 addition & 30 deletions pkg/controller/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,12 @@ package controller

import (
"context"
"regexp"
"strconv"

goGitlab "github.com/xanzy/go-gitlab"

"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/gitlab"
)

func parseVersionRegex(version string) (int, int, int, string) {
regexPattern := `^(\d+)\.(\d+)\.(\d+)(?:-(.*))?$`
r := regexp.MustCompile(regexPattern)

matches := r.FindStringSubmatch(version)

if matches == nil {
return 0, 0, 0, "Invalid version format"
}

major, _ := strconv.Atoi(matches[1])
minor, _ := strconv.Atoi(matches[2])
patch, _ := strconv.Atoi(matches[3])

suffix := matches[4]

return major, minor, patch, suffix
}

func (c *Controller) GetGitLabMetadata(ctx context.Context) error {
options := []goGitlab.RequestOptionFunc{goGitlab.WithContext(ctx)}

Expand All @@ -38,15 +17,7 @@ func (c *Controller) GetGitLabMetadata(ctx context.Context) error {
}

if metadata.Version != "" {
major, minor, patch, suffix := parseVersionRegex(metadata.Version)
c.Gitlab.UpdateVersion(
gitlab.GitLabVersion{
Major: major,
Minor: minor,
Patch: patch,
Suffix: suffix,
},
)
c.Gitlab.UpdateVersion(gitlab.NewGitLabVersion(metadata.Version))
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ func TestGetGitLabMetadataSuccess(t *testing.T) {
"enterprise":true
}
`,
expectedVersion: gitlab.GitLabVersion{Major: 16, Minor: 7, Patch: 0, Suffix: "pre"},
expectedVersion: gitlab.NewGitLabVersion("v16.7.0-pre"),
},
{
name: "unsuccessful parse",
data: `
{
"version":"16.7"
"revision":"3fe364fe754"
}
`,
expectedVersion: gitlab.GitLabVersion{Major: 0, Minor: 0, Patch: 0, Suffix: "Invalid version format"},
expectedVersion: gitlab.NewGitLabVersion(""),
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/gitlab/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ func TestListRefMostRecentJobs(t *testing.T) {
defer server.Close()

if tc.keysetPagination {
c.UpdateVersion(GitLabVersion{Major: 16, Minor: 0})
c.UpdateVersion(NewGitLabVersion("16.0.0"))
} else {
c.UpdateVersion(GitLabVersion{Major: 15, Minor: 0})
c.UpdateVersion(NewGitLabVersion("15.0.0"))
}

ref := schemas.Ref{
Expand Down
32 changes: 21 additions & 11 deletions pkg/gitlab/version.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package gitlab

import (
"strings"

"golang.org/x/mod/semver"
)

type GitLabVersion struct {
Major int
Minor int
Patch int
Suffix string
Version string
}

func NewGitLabVersion(version string) GitLabVersion {
ver := ""
if strings.HasPrefix(version, "v") {
ver = version
} else if version != "" {
ver = "v" + version
}

return GitLabVersion{Version: ver}
}

// PipelineJobsKeysetPaginationSupported returns true if the GitLab instance
// is running 15.9 or later.
func (v GitLabVersion) PipelineJobsKeysetPaginationSupported() bool {
if v.Major == 0 {
if v.Version == "" {
return false
} else if v.Major < 15 {
return false
} else if v.Major > 15 {
return true
} else {
return v.Minor >= 9
}

return semver.Compare(v.Version, "v15.9.0") >= 0
}
22 changes: 16 additions & 6 deletions pkg/gitlab/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,42 @@ func TestPipelineJobsKeysetPaginationSupported(t *testing.T) {
}{
{
name: "unknown",
version: GitLabVersion{Major: 0, Minor: 0, Patch: 0, Suffix: ""},
version: NewGitLabVersion(""),
expectedResult: false,
},
{
name: "15.8.0",
version: GitLabVersion{Major: 15, Minor: 8, Patch: 0, Suffix: ""},
version: NewGitLabVersion("15.8.0"),
expectedResult: false,
},
{
name: "v15.8.0",
version: NewGitLabVersion("v15.8.0"),
expectedResult: false,
},
{
name: "15.9.0",
version: GitLabVersion{Major: 15, Minor: 9, Patch: 0, Suffix: ""},
version: NewGitLabVersion("15.9.0"),
expectedResult: true,
},
{
name: "v15.9.0",
version: NewGitLabVersion("v15.9.0"),
expectedResult: true,
},
{
name: "15.9.1",
version: GitLabVersion{Major: 15, Minor: 9, Patch: 1, Suffix: ""},
version: NewGitLabVersion("15.9.1"),
expectedResult: true,
},
{
name: "15.10.2",
version: GitLabVersion{Major: 15, Minor: 10, Patch: 12, Suffix: ""},
version: NewGitLabVersion("15.10.2"),
expectedResult: true,
},
{
name: "16.0.0",
version: GitLabVersion{Major: 16, Minor: 0, Patch: 0, Suffix: ""},
version: NewGitLabVersion("16.0.0"),
expectedResult: true,
},
}
Expand Down

0 comments on commit 73dd2d3

Please sign in to comment.