From 4bbcfd74ada1a3497dd08af2470504d64f547cbd Mon Sep 17 00:00:00 2001 From: Elton Goh Jun Hao <75515229+EltonGohJH@users.noreply.github.com> Date: Thu, 4 Apr 2024 16:09:15 +0800 Subject: [PATCH] [MM-49243] - Use paged requests to request the list of teams (#26278) Co-authored-by: Mattermost Build --- server/cmd/mmctl/commands/channel.go | 4 +++- server/cmd/mmctl/commands/channel_test.go | 8 +++++++- server/cmd/mmctl/commands/command.go | 4 +++- server/cmd/mmctl/commands/command_test.go | 3 ++- server/cmd/mmctl/commands/import.go | 8 ++++---- server/cmd/mmctl/commands/utils.go | 2 ++ server/cmd/mmctl/commands/webhook.go | 4 +++- server/cmd/mmctl/commands/webhook_test.go | 16 ++++++++++++++-- 8 files changed, 38 insertions(+), 11 deletions(-) diff --git a/server/cmd/mmctl/commands/channel.go b/server/cmd/mmctl/commands/channel.go index edb4effba4..6247e5ecf1 100644 --- a/server/cmd/mmctl/commands/channel.go +++ b/server/cmd/mmctl/commands/channel.go @@ -497,7 +497,9 @@ func searchChannelCmdF(c client.Client, cmd *cobra.Command, args []string) error return errors.Errorf("channel %s was not found in team %s", args[0], teamArg) } } else { - teams, _, err := c.GetAllTeams(context.TODO(), "", 0, 9999) + teams, err := getPages(func(page, numPerPage int, etag string) ([]*model.Team, *model.Response, error) { + return c.GetAllTeams(context.TODO(), etag, page, numPerPage) + }, DefaultPageSize) if err != nil { return err } diff --git a/server/cmd/mmctl/commands/channel_test.go b/server/cmd/mmctl/commands/channel_test.go index 2a97ef77ca..deee9da73f 100644 --- a/server/cmd/mmctl/commands/channel_test.go +++ b/server/cmd/mmctl/commands/channel_test.go @@ -69,10 +69,16 @@ func (s *MmctlUnitTestSuite) TestSearchChannelCmdF() { s.client. EXPECT(). - GetAllTeams(context.TODO(), "", 0, 9999). + GetAllTeams(context.TODO(), "", 0, DefaultPageSize). Return(mockTeams, &model.Response{}, nil). Times(1) + s.client. + EXPECT(). + GetAllTeams(context.TODO(), "", 1, DefaultPageSize). + Return([]*model.Team{}, &model.Response{}, nil). + Times(1) + // first call is for the other team, that doesn't have the channel s.client. EXPECT(). diff --git a/server/cmd/mmctl/commands/command.go b/server/cmd/mmctl/commands/command.go index f76c3b1b1a..fa08b98f5a 100644 --- a/server/cmd/mmctl/commands/command.go +++ b/server/cmd/mmctl/commands/command.go @@ -188,7 +188,9 @@ func createCommandCmdF(c client.Client, cmd *cobra.Command, args []string) error func listCommandCmdF(c client.Client, cmd *cobra.Command, args []string) error { var teams []*model.Team if len(args) < 1 { - teamList, _, err := c.GetAllTeams(context.TODO(), "", 0, 10000) + teamList, err := getPages(func(page, numPerPage int, etag string) ([]*model.Team, *model.Response, error) { + return c.GetAllTeams(context.TODO(), etag, page, numPerPage) + }, DefaultPageSize) if err != nil { return err } diff --git a/server/cmd/mmctl/commands/command_test.go b/server/cmd/mmctl/commands/command_test.go index 8a8c8ed4f4..63a578d377 100644 --- a/server/cmd/mmctl/commands/command_test.go +++ b/server/cmd/mmctl/commands/command_test.go @@ -417,7 +417,8 @@ func (s *MmctlUnitTestSuite) TestCommandListCmdF() { } cmd := &cobra.Command{} - s.client.EXPECT().GetAllTeams(context.TODO(), "", 0, 10000).Return(teams, &model.Response{}, nil).Times(1) + s.client.EXPECT().GetAllTeams(context.TODO(), "", 0, DefaultPageSize).Return(teams, &model.Response{}, nil).Times(1) + s.client.EXPECT().GetAllTeams(context.TODO(), "", 1, DefaultPageSize).Return([]*model.Team{}, &model.Response{}, nil).Times(1) s.client.EXPECT().ListCommands(context.TODO(), team1ID, true).Return(team1Commands, &model.Response{}, nil).Times(1) s.client.EXPECT().ListCommands(context.TODO(), team2Id, true).Return(team2Commands, &model.Response{}, nil).Times(1) err := listCommandCmdF(s.client, cmd, []string{}) diff --git a/server/cmd/mmctl/commands/import.go b/server/cmd/mmctl/commands/import.go index f43ea52baa..02b4d473fa 100644 --- a/server/cmd/mmctl/commands/import.go +++ b/server/cmd/mmctl/commands/import.go @@ -396,7 +396,7 @@ func importValidateCmdF(command *cobra.Command, args []string) error { err := withClient(func(c client.Client, cmd *cobra.Command, args []string) error { users, err := getPages(func(page, numPerPage int, etag string) ([]*model.User, *model.Response, error) { return c.GetUsers(context.TODO(), page, numPerPage, etag) - }, 250) + }, DefaultPageSize) if err != nil { return err } @@ -410,7 +410,7 @@ func importValidateCmdF(command *cobra.Command, args []string) error { teams, err := getPages(func(page, numPerPage int, etag string) ([]*model.Team, *model.Response, error) { return c.GetAllTeams(context.TODO(), etag, page, numPerPage) - }, 250) + }, DefaultPageSize) if err != nil { return err } @@ -421,14 +421,14 @@ func importValidateCmdF(command *cobra.Command, args []string) error { publicChannels, err := getPages(func(page, numPerPage int, etag string) ([]*model.Channel, *model.Response, error) { return c.GetPublicChannelsForTeam(context.TODO(), team.Id, page, numPerPage, etag) - }, 250) + }, DefaultPageSize) if err != nil { return err } privateChannels, err := getPages(func(page, numPerPage int, etag string) ([]*model.Channel, *model.Response, error) { return c.GetPrivateChannelsForTeam(context.TODO(), team.Id, page, numPerPage, etag) - }, 250) + }, DefaultPageSize) if err != nil { return err } diff --git a/server/cmd/mmctl/commands/utils.go b/server/cmd/mmctl/commands/utils.go index 235f5b1c49..4673b40cfd 100644 --- a/server/cmd/mmctl/commands/utils.go +++ b/server/cmd/mmctl/commands/utils.go @@ -14,6 +14,8 @@ import ( "github.com/pkg/errors" ) +const DefaultPageSize = 200 + func checkInteractiveTerminal() error { fileInfo, err := os.Stdout.Stat() if err != nil { diff --git a/server/cmd/mmctl/commands/webhook.go b/server/cmd/mmctl/commands/webhook.go index be8d9f4bde..863e4478d0 100644 --- a/server/cmd/mmctl/commands/webhook.go +++ b/server/cmd/mmctl/commands/webhook.go @@ -92,7 +92,9 @@ func listWebhookCmdF(c client.Client, command *cobra.Command, args []string) err if len(args) < 1 { var err error // If no team is specified, list all teams - teams, _, err = c.GetAllTeams(context.TODO(), "", 0, 100000000) + teams, err = getPages(func(page, numPerPage int, etag string) ([]*model.Team, *model.Response, error) { + return c.GetAllTeams(context.TODO(), etag, page, numPerPage) + }, DefaultPageSize) if err != nil { return err } diff --git a/server/cmd/mmctl/commands/webhook_test.go b/server/cmd/mmctl/commands/webhook_test.go index 06a4b16ce6..5bb0eed6af 100644 --- a/server/cmd/mmctl/commands/webhook_test.go +++ b/server/cmd/mmctl/commands/webhook_test.go @@ -40,10 +40,16 @@ func (s *MmctlUnitTestSuite) TestListWebhookCmd() { s.client. EXPECT(). - GetAllTeams(context.TODO(), "", 0, 100000000). + GetAllTeams(context.TODO(), "", 0, DefaultPageSize). Return([]*model.Team{&mockTeam}, &model.Response{}, nil). Times(1) + s.client. + EXPECT(). + GetAllTeams(context.TODO(), "", 1, DefaultPageSize). + Return([]*model.Team{}, &model.Response{}, nil). + Times(1) + s.client. EXPECT(). GetIncomingWebhooksForTeam(context.TODO(), teamID, 0, 100000000, ""). @@ -114,10 +120,16 @@ func (s *MmctlUnitTestSuite) TestListWebhookCmd() { s.client. EXPECT(). - GetAllTeams(context.TODO(), "", 0, 100000000). + GetAllTeams(context.TODO(), "", 0, DefaultPageSize). Return([]*model.Team{&mockTeam}, &model.Response{}, nil). Times(1) + s.client. + EXPECT(). + GetAllTeams(context.TODO(), "", 1, DefaultPageSize). + Return([]*model.Team{}, &model.Response{}, nil). + Times(1) + s.client. EXPECT(). GetIncomingWebhooksForTeam(context.TODO(), teamID, 0, 100000000, "").