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

gateway: add cursor pagination #427

Closed
Closed
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
17 changes: 13 additions & 4 deletions cmd/agola/cmd/orgmemberlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/sorintlab/errors"
"github.com/spf13/cobra"

gwapitypes "agola.io/agola/services/gateway/api/types"
gwclient "agola.io/agola/services/gateway/client"
)

Expand Down Expand Up @@ -56,13 +57,21 @@ func init() {

func orgMemberList(cmd *cobra.Command, args []string) error {
gwclient := gwclient.NewClient(gatewayURL, token)
var orgMembersAll []*gwapitypes.OrgMemberResponse

orgMembers, _, err := gwclient.GetOrgMembers(context.TODO(), orgMemberListOpts.orgname)
if err != nil {
return errors.Wrapf(err, "failed to get organization member")
hasMoreData := true
var cursor string
for hasMoreData {
orgMembers, _, err := gwclient.GetOrgMembers(context.TODO(), orgMemberListOpts.orgname, false, 0, "")
if err != nil {
return errors.Wrapf(err, "failed to get organization member")
}
orgMembersAll = append(orgMembersAll, orgMembers.OrgMembers...)
cursor = orgMembers.Cursor
hasMoreData = cursor != ""
}

out, err := json.MarshalIndent(orgMembers, "", "\t")
out, err := json.MarshalIndent(orgMembersAll, "", "\t")
if err != nil {
return errors.WithStack(err)
}
Expand Down
16 changes: 11 additions & 5 deletions cmd/agola/cmd/projectlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"context"
"fmt"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/sorintlab/errors"
"github.com/spf13/cobra"

gwapitypes "agola.io/agola/services/gateway/api/types"
Expand Down Expand Up @@ -63,12 +63,18 @@ func printProjects(projects []*gwapitypes.ProjectResponse) {
func projectList(cmd *cobra.Command, args []string) error {
gwclient := gwclient.NewClient(gatewayURL, token)

projects, _, err := gwclient.GetProjectGroupProjects(context.TODO(), projectListOpts.parentPath)
if err != nil {
return errors.WithStack(err)
var projectsAll []*gwapitypes.ProjectResponse
hasMoreData := true
var cursor string
for hasMoreData {
projectsResp, _, err := gwclient.GetProjectGroupProjects(context.TODO(), projectListOpts.parentPath, 0, cursor)
projectsAll = append(projectsAll, projectsResp.Projects...)
if err != nil {
return errors.WithStack(err)
}
}

printProjects(projects)
printProjects(projectsAll)

return nil
}
32 changes: 24 additions & 8 deletions cmd/agola/cmd/projectsecretlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,37 @@ func secretList(cmd *cobra.Command, ownertype string, args []string) error {

func printSecrets(ownertype, description string, tree, removeoverridden bool) error {

var err error
var secrets []*gwapitypes.SecretResponse
var secretsAll []*gwapitypes.SecretResponse

gwclient := gwclient.NewClient(gatewayURL, token)

switch ownertype {
case "project":
secrets, _, err = gwclient.GetProjectSecrets(context.TODO(), secretListOpts.parentRef, tree, removeoverridden)
hasMoreData := true
var cursor string
for hasMoreData {
secrets, _, err := gwclient.GetProjectSecrets(context.TODO(), secretListOpts.parentRef, tree, removeoverridden, "", false, 0, cursor)
if err != nil {
return errors.Wrapf(err, "failed to list %s secrets", ownertype)
}
secretsAll = append(secretsAll, secrets.Secrets...)
cursor = secrets.Cursor
hasMoreData = cursor != ""
}
case "projectgroup":
secrets, _, err = gwclient.GetProjectGroupSecrets(context.TODO(), secretListOpts.parentRef, tree, removeoverridden)
}
if err != nil {
return errors.Wrapf(err, "failed to list %s secrets", ownertype)
hasMoreData := true
var cursor string
for hasMoreData {
secrets, _, err := gwclient.GetProjectGroupSecrets(context.TODO(), secretListOpts.parentRef, tree, removeoverridden, "", false, 0, cursor)
if err != nil {
return errors.Wrapf(err, "failed to list %s secrets", ownertype)
}
secretsAll = append(secretsAll, secrets.Secrets...)
cursor = secrets.Cursor
hasMoreData = cursor != ""
}
}
prettyJSON, err := json.MarshalIndent(secrets, "", "\t")
prettyJSON, err := json.MarshalIndent(secretsAll, "", "\t")
if err != nil {
return errors.Wrapf(err, "failed to convert %s secrets to json", ownertype)
}
Expand Down
32 changes: 24 additions & 8 deletions cmd/agola/cmd/projectvariablelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,37 @@ func variableList(cmd *cobra.Command, ownertype string, args []string) error {

func printVariables(ownertype, description string, tree, removeoverridden bool) error {

var err error
var variables []*gwapitypes.VariableResponse
var variablesAll []*gwapitypes.VariableResponse

gwclient := gwclient.NewClient(gatewayURL, token)

switch ownertype {
case "project":
variables, _, err = gwclient.GetProjectVariables(context.TODO(), variableListOpts.parentRef, tree, removeoverridden)
hasMoreData := true
var cursor string
for hasMoreData {
variables, _, err := gwclient.GetProjectVariables(context.TODO(), variableListOpts.parentRef, tree, removeoverridden, "", false, 0, cursor)
if err != nil {
return errors.Wrapf(err, "failed to list %s variables", ownertype)
}
variablesAll = append(variablesAll, variables.Variables...)
cursor = variables.Cursor
hasMoreData = cursor != ""
}
case "projectgroup":
variables, _, err = gwclient.GetProjectGroupVariables(context.TODO(), variableListOpts.parentRef, tree, removeoverridden)
}
if err != nil {
return errors.Wrapf(err, "failed to list %s variables", ownertype)
hasMoreData := true
var cursor string
for hasMoreData {
variables, _, err := gwclient.GetProjectGroupVariables(context.TODO(), variableListOpts.parentRef, tree, removeoverridden, "", false, 0, cursor)
if err != nil {
return errors.Wrapf(err, "failed to list %s variables", ownertype)
}
variablesAll = append(variablesAll, variables.Variables...)
cursor = variables.Cursor
hasMoreData = cursor != ""
}
}
prettyJSON, err := json.MarshalIndent(variables, "", "\t")
prettyJSON, err := json.MarshalIndent(variablesAll, "", "\t")
if err != nil {
return errors.Wrapf(err, "failed to convert %s variables to json", ownertype)
}
Expand Down
22 changes: 18 additions & 4 deletions cmd/agola/cmd/remotesourcelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,26 @@ func printRemoteSources(remoteSources []*gwapitypes.RemoteSourceResponse) {
func remoteSourceList(cmd *cobra.Command, args []string) error {
gwclient := gwclient.NewClient(gatewayURL, token)

remouteSources, _, err := gwclient.GetRemoteSources(context.TODO(), remoteSourceListOpts.start, remoteSourceListOpts.limit, false)
if err != nil {
return errors.WithStack(err)
var remouteSourcesAll []*gwapitypes.RemoteSourceResponse
if runListOpts.limit == 0 {
hasMoreData := true
var cursor string
for hasMoreData {
remouteSourcesResp, _, err := gwclient.GetRemoteSources(context.TODO(), remoteSourceListOpts.start, 0, false, cursor)
if err != nil {
return errors.WithStack(err)
}
remouteSourcesAll = append(remouteSourcesAll, remouteSourcesResp.RemoteSources...)
}
} else {
remouteSourcesResp, _, err := gwclient.GetRemoteSources(context.TODO(), remoteSourceListOpts.start, remoteSourceListOpts.limit, false, "")
if err != nil {
return errors.WithStack(err)
}
remouteSourcesAll = remouteSourcesResp.RemoteSources
}

printRemoteSources(remouteSources)
printRemoteSources(remouteSourcesAll)

return nil
}
46 changes: 37 additions & 9 deletions cmd/agola/cmd/runlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,47 @@ func runList(cmd *cobra.Command, args []string) error {

isProject := !flags.Changed("username")

var runsResp []*gwapitypes.RunsResponse
var err error
var runsAll []*gwapitypes.Runs
if isProject {
runsResp, _, err = gwclient.GetProjectRuns(context.TODO(), runListOpts.projectRef, runListOpts.phaseFilter, nil, runListOpts.start, runListOpts.limit, false)
if runListOpts.limit == 0 {
hasMoreData := true
var cursor string
for hasMoreData {
runsResp, _, err := gwclient.GetProjectRuns(context.TODO(), runListOpts.projectRef, runListOpts.phaseFilter, nil, runListOpts.start, 0, false, cursor)
if err != nil {
return errors.WithStack(err)
}
runsAll = append(runsAll, runsResp.Runs...)
}
} else {
runsResp, _, err := gwclient.GetProjectRuns(context.TODO(), runListOpts.projectRef, runListOpts.phaseFilter, nil, runListOpts.start, runListOpts.limit, false, "")
if err != nil {
return errors.WithStack(err)
}
runsAll = runsResp.Runs
}
} else {
runsResp, _, err = gwclient.GetUserRuns(context.TODO(), runListOpts.username, runListOpts.phaseFilter, nil, runListOpts.start, runListOpts.limit, false)
}
if err != nil {
return errors.WithStack(err)
if runListOpts.limit == 0 {
hasMoreData := true
var cursor string
for hasMoreData {
runsResp, _, err := gwclient.GetUserRuns(context.TODO(), runListOpts.username, runListOpts.phaseFilter, nil, runListOpts.start, 0, false, cursor)
if err != nil {
return errors.WithStack(err)
}
runsAll = append(runsAll, runsResp.Runs...)
}
} else {
runsResp, _, err := gwclient.GetUserRuns(context.TODO(), runListOpts.username, runListOpts.phaseFilter, nil, runListOpts.start, runListOpts.limit, false, "")
if err != nil {
return errors.WithStack(err)
}
runsAll = runsResp.Runs
}
}

runs := make([]*runDetails, len(runsResp))
for i, runResponse := range runsResp {
runs := make([]*runDetails, len(runsAll))
for i, runResponse := range runsAll {
var err error
var run *gwapitypes.RunResponse
if isProject {
Expand Down
22 changes: 18 additions & 4 deletions cmd/agola/cmd/userlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,26 @@ func printUsers(users []*gwapitypes.PrivateUserResponse) {
func userList(cmd *cobra.Command, args []string) error {
gwclient := gwclient.NewClient(gatewayURL, token)

users, _, err := gwclient.GetUsers(context.TODO(), userListOpts.start, userListOpts.limit, false)
if err != nil {
return errors.WithStack(err)
var usersAll []*gwapitypes.PrivateUserResponse
if runListOpts.limit == 0 {
hasMoreData := true
var cursor string
for hasMoreData {
usersResp, _, err := gwclient.GetUsers(context.TODO(), userListOpts.start, 0, false, cursor)
if err != nil {
return errors.WithStack(err)
}
usersAll = append(usersAll, usersResp.Users...)
}
} else {
usersResp, _, err := gwclient.GetUsers(context.TODO(), userListOpts.start, userListOpts.limit, false, "")
if err != nil {
return errors.WithStack(err)
}
usersAll = usersResp.Users
}

printUsers(users)
printUsers(usersAll)

return nil
}
6 changes: 3 additions & 3 deletions internal/services/configstore/action/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func orgMemberResponse(orgUser *db.OrgUser) *OrgMemberResponse {
}
}

func (h *ActionHandler) GetOrgMembers(ctx context.Context, orgRef string) ([]*OrgMemberResponse, error) {
func (h *ActionHandler) GetOrgMembers(ctx context.Context, orgRef string, start string, limit int, asc bool) ([]*OrgMemberResponse, error) {
var orgUsers []*db.OrgUser
err := h.d.Do(ctx, func(tx *sql.Tx) error {
var err error
Expand All @@ -50,7 +50,7 @@ func (h *ActionHandler) GetOrgMembers(ctx context.Context, orgRef string) ([]*Or
return util.NewAPIError(util.ErrNotExist, errors.Errorf("org %q doesn't exist", orgRef))
}

orgUsers, err = h.d.GetOrgUsers(tx, org.ID)
orgUsers, err = h.d.GetOrgUsers(tx, org.ID, start, limit, asc)
return errors.WithStack(err)
})
if err != nil {
Expand Down Expand Up @@ -211,7 +211,7 @@ func (h *ActionHandler) DeleteOrg(ctx context.Context, orgRef string) error {
return errors.WithStack(err)
}
for _, subgroup := range subgroups {
projects, err := h.d.GetProjectGroupProjects(tx, subgroup.ID)
projects, err := h.d.GetProjectGroupProjects(tx, subgroup.ID, "", 0)
if err != nil {
return errors.WithStack(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/services/configstore/action/projectgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (h *ActionHandler) GetProjectGroupSubgroups(ctx context.Context, projectGro
return projectGroups, nil
}

func (h *ActionHandler) GetProjectGroupProjects(ctx context.Context, projectGroupRef string) ([]*types.Project, error) {
func (h *ActionHandler) GetProjectGroupProjects(ctx context.Context, projectGroupRef string, start string, limit int) ([]*types.Project, error) {
var projects []*types.Project
err := h.d.Do(ctx, func(tx *sql.Tx) error {
var err error
Expand All @@ -80,7 +80,7 @@ func (h *ActionHandler) GetProjectGroupProjects(ctx context.Context, projectGrou
return util.NewAPIError(util.ErrNotExist, errors.Errorf("project group %q doesn't exist", projectGroupRef))
}

projects, err = h.d.GetProjectGroupProjects(tx, projectGroup.ID)
projects, err = h.d.GetProjectGroupProjects(tx, projectGroup.ID, start, limit)
return errors.WithStack(err)
})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/services/configstore/action/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ func (h *ActionHandler) GetSecret(ctx context.Context, secretID string) (*types.
return secret, nil
}

func (h *ActionHandler) GetSecrets(ctx context.Context, parentKind types.ObjectKind, parentRef string, tree bool) ([]*types.Secret, error) {
func (h *ActionHandler) GetSecrets(ctx context.Context, parentKind types.ObjectKind, parentRef string, tree bool, startSecretName string, asc bool, limit int) ([]*types.Secret, error) {
var secrets []*types.Secret
err := h.d.Do(ctx, func(tx *sql.Tx) error {
parentID, err := h.ResolveObjectID(tx, parentKind, parentRef)
if err != nil {
return errors.WithStack(err)
}
if tree {
secrets, err = h.d.GetSecretsTree(tx, parentKind, parentID)
secrets, err = h.d.GetSecretsTree(tx, parentKind, parentID, startSecretName, asc, limit)
} else {
secrets, err = h.d.GetSecrets(tx, parentID)
secrets, err = h.d.GetSecrets(tx, parentID, startSecretName, asc, limit)
}
return errors.WithStack(err)
})
Expand Down
6 changes: 3 additions & 3 deletions internal/services/configstore/action/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ import (
"agola.io/agola/services/configstore/types"
)

func (h *ActionHandler) GetVariables(ctx context.Context, parentKind types.ObjectKind, parentRef string, tree bool) ([]*types.Variable, error) {
func (h *ActionHandler) GetVariables(ctx context.Context, parentKind types.ObjectKind, parentRef string, tree bool, startVariableName string, asc bool, limit int) ([]*types.Variable, error) {
var variables []*types.Variable
err := h.d.Do(ctx, func(tx *sql.Tx) error {
parentID, err := h.ResolveObjectID(tx, parentKind, parentRef)
if err != nil {
return errors.WithStack(err)
}
if tree {
variables, err = h.d.GetVariablesTree(tx, parentKind, parentID)
variables, err = h.d.GetVariablesTree(tx, parentKind, parentID, startVariableName, asc, limit)
} else {
variables, err = h.d.GetVariables(tx, parentID)
variables, err = h.d.GetVariables(tx, parentID, startVariableName, asc, limit)
}
return errors.WithStack(err)
})
Expand Down
Loading