Use pagination utils (#2633)

1. replace custom pagination code with pagination util
2. add pagination if it could be necessary (hook deactivation)
This commit is contained in:
qwerty287 2023-10-23 18:44:25 +02:00 committed by GitHub
parent 4803160f27
commit 9232db06dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 31 deletions

View file

@ -158,11 +158,11 @@ func (c *config) Repo(ctx context.Context, u *model.User, remoteID model.ForgeRe
if remoteID.IsValid() {
name = string(remoteID)
}
repos, err := c.Repos(ctx, u)
if err != nil {
return nil, err
}
if len(owner) == 0 {
if owner == "" {
repos, err := c.Repos(ctx, u)
if err != nil {
return nil, err
}
for _, repo := range repos {
if string(repo.ForgeRemoteID) == name {
owner = repo.Owner
@ -187,20 +187,26 @@ func (c *config) Repo(ctx context.Context, u *model.User, remoteID model.ForgeRe
func (c *config) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error) {
client := c.newClient(ctx, u)
var all []*model.Repo
resp, err := client.ListWorkspaces(&internal.ListWorkspacesOpts{
PageLen: 100,
Role: "member",
workspaces, err := shared_utils.Paginate(func(page int) ([]*internal.Workspace, error) {
resp, err := client.ListWorkspaces(&internal.ListWorkspacesOpts{
Page: page,
PageLen: 100,
Role: "member",
})
if err != nil {
return nil, err
}
return resp.Values, nil
})
if err != nil {
return all, err
return nil, err
}
for _, workspace := range resp.Values {
var all []*model.Repo
for _, workspace := range workspaces {
repos, err := client.ListReposAll(workspace.Slug)
if err != nil {
return all, err
return nil, err
}
for _, repo := range repos {
perm, err := client.GetPermission(repo.FullName)
@ -305,11 +311,19 @@ func (c *config) Activate(ctx context.Context, u *model.User, r *model.Repo, lin
func (c *config) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
client := c.newClient(ctx, u)
hooks, err := client.ListHooks(r.Owner, r.Name, &internal.ListOpts{})
hooks, err := shared_utils.Paginate(func(page int) ([]*internal.Hook, error) {
hooks, err := client.ListHooks(r.Owner, r.Name, &internal.ListOpts{
Page: page,
})
if err != nil {
return nil, err
}
return hooks.Values, nil
})
if err != nil {
return err
}
hook := matchingHooks(hooks.Values, link)
hook := matchingHooks(hooks, link)
if hook != nil {
return client.DeleteHook(r.Owner, r.Name, hook.UUID)
}

View file

@ -103,7 +103,11 @@ func getRepoHooks(c *gin.Context) {
case "hook_empty":
c.String(200, "{}")
default:
c.String(200, repoHookPayload)
if c.Query("page") == "" || c.Query("page") == "1" {
c.String(200, repoHookPayload)
} else {
c.String(200, "{\"values\":[]}")
}
}
}
@ -174,7 +178,11 @@ func getUserRepos(c *gin.Context) {
case "Bearer repos_not_found", "Bearer 70efdf2e":
c.String(404, "")
default:
c.String(200, userRepoPayload)
if c.Query("page") == "" || c.Query("page") == "1" {
c.String(200, userRepoPayload)
} else {
c.String(200, "{\"values\":[]}")
}
}
}

View file

@ -23,6 +23,8 @@ import (
"net/http"
"net/url"
shared_utils "github.com/woodpecker-ci/woodpecker/shared/utils"
"golang.org/x/oauth2"
"golang.org/x/oauth2/bitbucket"
)
@ -110,21 +112,13 @@ func (c *Client) ListRepos(workspace string, opts *ListOpts) (*RepoResp, error)
}
func (c *Client) ListReposAll(workspace string) ([]*Repo, error) {
page := 1
var repos []*Repo
for {
return shared_utils.Paginate(func(page int) ([]*Repo, error) {
resp, err := c.ListRepos(workspace, &ListOpts{Page: page, PageLen: 100})
if err != nil {
return repos, err
return nil, err
}
repos = append(repos, resp.Values...)
if len(resp.Next) == 0 {
break
}
page = resp.Page + 1
}
return repos, nil
return resp.Values, nil
})
}
func (c *Client) FindHook(owner, name, id string) (*Hook, error) {

View file

@ -41,7 +41,12 @@ func Handler() http.Handler {
}
func listRepoHooks(c *gin.Context) {
c.String(200, listRepoHookPayloads)
page := c.Query("page")
if page != "" && page != "1" {
c.String(200, "[]")
} else {
c.String(200, listRepoHookPayloads)
}
}
func getRepo(c *gin.Context) {

View file

@ -411,7 +411,15 @@ func (c *Gitea) Deactivate(ctx context.Context, u *model.User, r *model.Repo, li
return err
}
hooks, _, err := client.ListRepoHooks(r.Owner, r.Name, gitea.ListHooksOptions{})
hooks, err := shared_utils.Paginate(func(page int) ([]*gitea.Hook, error) {
hooks, _, err := client.ListRepoHooks(r.Owner, r.Name, gitea.ListHooksOptions{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: c.perPage(ctx),
},
})
return hooks, err
})
if err != nil {
return err
}