diff --git a/server/forge/bitbucket/bitbucket.go b/server/forge/bitbucket/bitbucket.go index d10583bcb5..5b691f8956 100644 --- a/server/forge/bitbucket/bitbucket.go +++ b/server/forge/bitbucket/bitbucket.go @@ -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) } diff --git a/server/forge/bitbucket/fixtures/handler.go b/server/forge/bitbucket/fixtures/handler.go index b200bb219d..707a6dd732 100644 --- a/server/forge/bitbucket/fixtures/handler.go +++ b/server/forge/bitbucket/fixtures/handler.go @@ -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\":[]}") + } } } diff --git a/server/forge/bitbucket/internal/client.go b/server/forge/bitbucket/internal/client.go index 04eca60919..7159ad0c59 100644 --- a/server/forge/bitbucket/internal/client.go +++ b/server/forge/bitbucket/internal/client.go @@ -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 - } - repos = append(repos, resp.Values...) - if len(resp.Next) == 0 { - break + return nil, err } - page = resp.Page + 1 - } - return repos, nil + return resp.Values, nil + }) } func (c *Client) FindHook(owner, name, id string) (*Hook, error) { diff --git a/server/forge/gitea/fixtures/handler.go b/server/forge/gitea/fixtures/handler.go index 41742fcfa3..6f0dbeb144 100644 --- a/server/forge/gitea/fixtures/handler.go +++ b/server/forge/gitea/fixtures/handler.go @@ -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) { diff --git a/server/forge/gitea/gitea.go b/server/forge/gitea/gitea.go index 0d2f218863..7e4a322fda 100644 --- a/server/forge/gitea/gitea.go +++ b/server/forge/gitea/gitea.go @@ -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 }