Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pagination utils #2633

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions server/forge/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -187,20 +187,28 @@ 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)
Expand Down Expand Up @@ -305,11 +313,17 @@ 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{})
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)
}
Expand Down
18 changes: 6 additions & 12 deletions server/forge/bitbucket/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion server/forge/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down