From e1571287af2dc2df8cc325dcd4c5cf1dac90460a Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 23 Oct 2023 15:51:27 +0200 Subject: [PATCH 1/3] Use pagination utils --- server/forge/bitbucket/bitbucket.go | 42 +++++++++++++++-------- server/forge/bitbucket/internal/client.go | 18 ++++------ server/forge/gitea/gitea.go | 10 +++++- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/server/forge/bitbucket/bitbucket.go b/server/forge/bitbucket/bitbucket.go index d10583bcb5..6e7d21a9bb 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,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) @@ -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) } diff --git a/server/forge/bitbucket/internal/client.go b/server/forge/bitbucket/internal/client.go index 04eca60919..931b16c058 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/gitea.go b/server/forge/gitea/gitea.go index 0d2f218863..6a76f862dc 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 } From 0fb422ec53173d64a2ef8a66f5ab22f4887d4be0 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 23 Oct 2023 16:10:44 +0200 Subject: [PATCH 2/3] fix lint --- server/forge/bitbucket/bitbucket.go | 16 +++++++--------- server/forge/bitbucket/internal/client.go | 2 +- server/forge/gitea/gitea.go | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/server/forge/bitbucket/bitbucket.go b/server/forge/bitbucket/bitbucket.go index 6e7d21a9bb..0a70ae3a74 100644 --- a/server/forge/bitbucket/bitbucket.go +++ b/server/forge/bitbucket/bitbucket.go @@ -187,10 +187,9 @@ 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) - - workspaces, err := shared_utils.Paginate(func (page int) ([]*internal.Workspace, error) { + workspaces, err := shared_utils.Paginate(func(page int) ([]*internal.Workspace, error) { resp, err := client.ListWorkspaces(&internal.ListWorkspacesOpts{ - Page: page, + Page: page, PageLen: 100, Role: "member", }) @@ -199,7 +198,6 @@ func (c *config) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error } return resp.Values, nil }) - if err != nil { return nil, err } @@ -314,11 +312,11 @@ func (c *config) Deactivate(ctx context.Context, u *model.User, r *model.Repo, l client := c.newClient(ctx, u) 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 + 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 diff --git a/server/forge/bitbucket/internal/client.go b/server/forge/bitbucket/internal/client.go index 931b16c058..7159ad0c59 100644 --- a/server/forge/bitbucket/internal/client.go +++ b/server/forge/bitbucket/internal/client.go @@ -112,7 +112,7 @@ func (c *Client) ListRepos(workspace string, opts *ListOpts) (*RepoResp, error) } func (c *Client) ListReposAll(workspace string) ([]*Repo, error) { - return shared_utils.Paginate(func (page int) ([]*Repo, error) { + return shared_utils.Paginate(func(page int) ([]*Repo, error) { resp, err := c.ListRepos(workspace, &ListOpts{Page: page, PageLen: 100}) if err != nil { return nil, err diff --git a/server/forge/gitea/gitea.go b/server/forge/gitea/gitea.go index 6a76f862dc..7e4a322fda 100644 --- a/server/forge/gitea/gitea.go +++ b/server/forge/gitea/gitea.go @@ -411,10 +411,10 @@ func (c *Gitea) Deactivate(ctx context.Context, u *model.User, r *model.Repo, li return err } - hooks, err := shared_utils.Paginate(func (page int) ([]*gitea.Hook, error) { + 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, + Page: page, PageSize: c.perPage(ctx), }, }) From 1dff430f827517522851a6a32a47a9db137b5f96 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 23 Oct 2023 18:32:07 +0200 Subject: [PATCH 3/3] Fix tests --- server/forge/bitbucket/bitbucket.go | 4 +++- server/forge/bitbucket/fixtures/handler.go | 12 ++++++++++-- server/forge/gitea/fixtures/handler.go | 7 ++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/server/forge/bitbucket/bitbucket.go b/server/forge/bitbucket/bitbucket.go index 0a70ae3a74..5b691f8956 100644 --- a/server/forge/bitbucket/bitbucket.go +++ b/server/forge/bitbucket/bitbucket.go @@ -312,7 +312,9 @@ func (c *config) Deactivate(ctx context.Context, u *model.User, r *model.Repo, l client := c.newClient(ctx, u) hooks, err := shared_utils.Paginate(func(page int) ([]*internal.Hook, error) { - hooks, err := client.ListHooks(r.Owner, r.Name, &internal.ListOpts{}) + hooks, err := client.ListHooks(r.Owner, r.Name, &internal.ListOpts{ + Page: page, + }) if err != nil { return nil, err } 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/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) {