-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use keyset pagination only when it is supported (#755)
* fix: use keyset pagination only when it is supported #744 switched to keyset pagination for the `/api/v4/projects/:id/jobs` API endpoint, but this feature is only available in GitLab 15.9 and later (https://docs.gitlab.com/ee/api/jobs.html#list-project-jobs). This commit retrieves the instance metadata at startup to determine whether keyset pagination is available. If the metadata is not available or returns a version < 15.9, offset pagination will be used. * chore: Use golang.org/x/mod/semver for version parsing
- Loading branch information
Showing
10 changed files
with
307 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
|
||
goGitlab "github.com/xanzy/go-gitlab" | ||
|
||
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/gitlab" | ||
) | ||
|
||
func (c *Controller) GetGitLabMetadata(ctx context.Context) error { | ||
options := []goGitlab.RequestOptionFunc{goGitlab.WithContext(ctx)} | ||
|
||
metadata, _, err := c.Gitlab.Metadata.GetMetadata(options...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if metadata.Version != "" { | ||
c.Gitlab.UpdateVersion(gitlab.NewGitLabVersion(metadata.Version)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package controller | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/config" | ||
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/gitlab" | ||
) | ||
|
||
func TestGetGitLabMetadataSuccess(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data string | ||
expectedVersion gitlab.GitLabVersion | ||
}{ | ||
{ | ||
name: "successful parse", | ||
data: ` | ||
{ | ||
"version":"16.7.0-pre", | ||
"revision":"3fe364fe754", | ||
"kas":{ | ||
"enabled":true, | ||
"externalUrl":"wss://kas.gitlab.com", | ||
"version":"v16.7.0-rc2" | ||
}, | ||
"enterprise":true | ||
} | ||
`, | ||
expectedVersion: gitlab.NewGitLabVersion("v16.7.0-pre"), | ||
}, | ||
{ | ||
name: "unsuccessful parse", | ||
data: ` | ||
{ | ||
"revision":"3fe364fe754" | ||
} | ||
`, | ||
expectedVersion: gitlab.NewGitLabVersion(""), | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctx, c, mux, srv := newTestController(config.Config{}) | ||
defer srv.Close() | ||
|
||
mux.HandleFunc("/api/v4/metadata", | ||
func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, tc.data) | ||
}) | ||
|
||
assert.NoError(t, c.GetGitLabMetadata(ctx)) | ||
assert.Equal(t, tc.expectedVersion, c.Gitlab.Version()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package gitlab | ||
|
||
import ( | ||
"strings" | ||
|
||
"golang.org/x/mod/semver" | ||
) | ||
|
||
type GitLabVersion struct { | ||
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.Version == "" { | ||
return false | ||
} | ||
|
||
return semver.Compare(v.Version, "v15.9.0") >= 0 | ||
} |
Oops, something went wrong.