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 all commits
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
44 changes: 29 additions & 15 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,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)
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 10 additions & 2 deletions server/forge/bitbucket/fixtures/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\":[]}")
}
}
}

Expand Down Expand Up @@ -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\":[]}")
}
}
}

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
7 changes: 6 additions & 1 deletion server/forge/gitea/fixtures/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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