paging for gitlab

This commit is contained in:
elee 2022-03-03 23:32:45 -06:00
parent 0c8d973d7b
commit 60b32441ef
4 changed files with 98 additions and 9 deletions

3
.gitignore vendored
View File

@ -2,3 +2,6 @@ vendor/
bin/
tools/
webbundle/bindata.go
configs/
tmp/

View File

@ -0,0 +1,32 @@
version: '3.8'
services:
etcd:
image: quay.io/coreos/etcd
restart: unless-stopped
volumes:
- etcd-data:/etcd-data
environment:
ETCD_NAME: agola-etcd
ETCD_DATA_DIR: /etcd-data
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
ETCD_ADVERTISE_CLIENT_URLS: http://0.0.0.0:2379
ETCD_INITIAL_CLUSTER_TOKEN: agola-etcd-1
agola:
image: agola
restart: unless-stopped
volumes:
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock
- ./configs:/configs
- agola-data:/data/agola
- agola-tmp:/tmp/agola
ports:
- 8000:8000
command: ["serve", "--components", "all-base,executor", "--config", "/configs/config.yml"]
depends_on:
- etcd
volumes:
agola-data:
agola-tmp:
etcd-data:

View File

@ -34,6 +34,7 @@ import (
)
var (
PAGE_MAX = 100
GitlabOauth2Scopes = []string{"api"}
branchRefPrefix = "refs/heads/"
@ -279,18 +280,42 @@ func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource
}
func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {
baseOpts := gitlab.ListProjectsOptions{
MinAccessLevel: gitlab.AccessLevel(gitlab.MaintainerPermissions),
ListOptions: gitlab.ListOptions{
Page: 1,
PerPage: 100,
},
}
return c.listUserRepos(baseOpts)
}
func (c *Client) listUserRepos(fromOptions gitlab.ListProjectsOptions) ([]*gitsource.RepoInfo, error) {
repos := []*gitsource.RepoInfo{}
// get only repos with permission greater or equal to maintainer
opts := &gitlab.ListProjectsOptions{MinAccessLevel: gitlab.AccessLevel(gitlab.MaintainerPermissions)}
remoteRepos, _, err := c.client.Projects.ListProjects(opts)
opts := &fromOptions
for i := 1; i <= PAGE_MAX; i++ {
remoteRepos, pageResp, err := c.client.Projects.ListProjects(opts)
if err != nil {
return nil, errors.WithStack(err)
}
repos := []*gitsource.RepoInfo{}
for _, rr := range remoteRepos {
repos = append(repos, fromGitlabRepo(rr))
}
opts.ListOptions.Page = pageResp.NextPage
if opts.ListOptions.Page == 0 {
break
}
if len(remoteRepos) == 0 {
break
}
if pageResp.TotalPages != 0 {
if pageResp.TotalPages >= pageResp.CurrentPage {
}
}
}
return repos, nil
}

View File

@ -0,0 +1,29 @@
package gitlab
import (
"testing"
gitlab "github.com/xanzy/go-gitlab"
)
var c *Client
func init() {
c, _ = New(Opts{SkipVerify: true, APIURL: "https://gitlab.com"})
}
func TestListUserReposLimit(t *testing.T) {
baseOpts := gitlab.ListProjectsOptions{
ListOptions: gitlab.ListOptions{
Page: 1,
PerPage: 1,
},
}
repos, err := c.listUserRepos(baseOpts)
if err != nil {
t.Errorf("err getting repos %v", err)
}
if len(repos) != PAGE_MAX {
t.Errorf("page fail expected %d to be %d", len(repos), PAGE_MAX)
}
}