Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: fetch all repo from vcs (#1379)

STEVENcfa1263c84db
diff --git a/plugin/vcs/github/github.go b/plugin/vcs/github/github.go
index 9f67757a8d..7af4a8df3a 100644
--- a/plugin/vcs/github/github.go
+++ b/plugin/vcs/github/github.go
@@ -174,9 +174,8 @@ func (p *Provider) ExchangeOAuthToken(ctx context.Context, _ string, oauthExchan
return oauthResp.toVCSOAuthToken(), nil
}
-// FetchRepositoryList fetched all repositories in which the authenticated user
-// has a maintainer role.
-func (p *Provider) FetchRepositoryList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) ([]*vcs.Repository, error) {
+// FetchAllRepositoryList fetches all repositories where the authenticated user has a maintainer role.
+func (p *Provider) FetchAllRepositoryList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) ([]*vcs.Repository, error) {
return nil, errors.New("not implemented yet")
}
diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.go
index ad4b369fdd..4757967323 100644
--- a/plugin/vcs/gitlab/gitlab.go
+++ b/plugin/vcs/gitlab/gitlab.go
@@ -281,12 +281,36 @@ func (p *Provider) ExchangeOAuthToken(ctx context.Context, instanceURL string, o
return oauthResp.toVCSOAuthToken(), nil
}
-// FetchRepositoryList fetched all repositories in which the authenticated user
-// has a maintainer role.
-func (p *Provider) FetchRepositoryList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) ([]*vcs.Repository, error) {
+// FetchAllRepositoryList fetches all repositories where the authenticated user has a maintainer role.
+func (p *Provider) FetchAllRepositoryList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) ([]*vcs.Repository, error) {
+ repoList := []*vcs.Repository{}
+ // page is the current requesting page index, starts from 1.
+ // perPage is the number of items to list per page (default: 20, max: 100).
+ // refer: https://docs.gitlab.com/ee/api/#offset-based-pagination
+ page := 1
+ perPage := 40
+
+ for {
+ temp, err := p.fetchOffsetRepositoryList(ctx, oauthCtx, instanceURL, page, perPage)
+ if err != nil {
+ return nil, err
+ }
+
+ repoList = append(repoList, temp...)
+ if len(temp) < perPage {
+ break
+ }
+ page++
+ }
+
+ return repoList, nil
+}
+
+// fetchOffsetRepositoryList fetches repositories at page `page` with `perPage` items per page.
+func (p *Provider) fetchOffsetRepositoryList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, page, perPage int) ([]*vcs.Repository, error) {
// We will use user's token to create webhook in the project, which requires the
// token owner to be at least the project maintainer(40).
- url := fmt.Sprintf("%s/projects?membership=true&simple=true&min_access_level=40", p.APIURL(instanceURL))
+ url := fmt.Sprintf("%s/projects?membership=true&simple=true&min_access_level=40&page=%d&per_page=%d", p.APIURL(instanceURL), page, perPage)
code, body, err := oauth.Get(
ctx,
p.client,
@@ -307,7 +331,7 @@ func (p *Provider) FetchRepositoryList(ctx context.Context, oauthCtx common.Oaut
}
if code == http.StatusNotFound {
- return nil, common.Errorf(common.NotFound, fmt.Errorf("failed to fetch repository list from GitLab instance %s", instanceURL))
+ return nil, common.Errorf(common.NotFound, fmt.Errorf("repository list for GitLab instance %s not found", instanceURL))
} else if code >= 300 {
return nil, fmt.Errorf("failed to read repository list from GitLab instance %s, status code: %d",
instanceURL,
@@ -330,6 +354,7 @@ func (p *Provider) FetchRepositoryList(ctx context.Context, oauthCtx common.Oaut
}
repos = append(repos, repo)
}
+
return repos, nil
}
diff --git a/plugin/vcs/vcs.go b/plugin/vcs/vcs.go
index a543ba8b12..0600a64e58 100644
--- a/plugin/vcs/vcs.go
+++ b/plugin/vcs/vcs.go
@@ -171,7 +171,7 @@ type Provider interface {
//
// oauthCtx: OAuth context to write the file content
// instanceURL: VCS instance URL
- FetchRepositoryList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) ([]*Repository, error)
+ FetchAllRepositoryList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) ([]*Repository, error)
// Fetch the repository file list
//
diff --git a/server/vcs.go b/server/vcs.go
index e287d4680b..68a464fceb 100644
--- a/server/vcs.go
+++ b/server/vcs.go
@@ -162,7 +162,7 @@ func (s *Server) registerVCSRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Failed to find VCS, ID: %v", id))
}
- repoList, err := vcs.Get(vcsFound.Type, vcs.ProviderConfig{Logger: s.l}).FetchRepositoryList(
+ repoList, err := vcs.Get(vcsFound.Type, vcs.ProviderConfig{Logger: s.l}).FetchAllRepositoryList(
ctx,
common.OauthContext{
ClientID: vcsFound.ApplicationID,