diff options
-rw-r--r-- | app/channel.go | 15 | ||||
-rw-r--r-- | app/channel_test.go | 14 | ||||
-rw-r--r-- | cmd/mattermost/commands/channel.go | 42 | ||||
-rw-r--r-- | cmd/mattermost/commands/channel_test.go | 14 | ||||
-rw-r--r-- | tests/test-config.json | 2 |
5 files changed, 86 insertions, 1 deletions
diff --git a/app/channel.go b/app/channel.go index f9e77d737..ce183e6a9 100644 --- a/app/channel.go +++ b/app/channel.go @@ -159,6 +159,21 @@ func (a *App) CreateChannelWithUser(channel *model.Channel, userId string) (*mod return rchannel, nil } +// RenameChannel is used to rename the channel Name and the DisplayName fields +func (a *App) RenameChannel(channel *model.Channel, newChannelName string, newDisplayName string) (*model.Channel, *model.AppError) { + channel.Name = newChannelName + if newDisplayName != "" { + channel.DisplayName = newDisplayName + } + + newChannel, err := a.UpdateChannel(channel) + if err != nil { + return nil, err + } + + return newChannel, nil +} + func (a *App) CreateChannel(channel *model.Channel, addMember bool) (*model.Channel, *model.AppError) { if result := <-a.Srv.Store.Channel().Save(channel, *a.Config().TeamSettings.MaxChannelsPerTeam); result.Err != nil { return nil, result.Err diff --git a/app/channel_test.go b/app/channel_test.go index 4e6aaaf52..82c9e07ad 100644 --- a/app/channel_test.go +++ b/app/channel_test.go @@ -605,3 +605,17 @@ func TestFillInChannelProps(t *testing.T) { } }) } + +func TestRenameChannel(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN) + + channel, err := th.App.RenameChannel(channel, "newchannelname", "New Display Name") + if err != nil { + t.Fatal("Failed to update channel name. Error: " + err.Error()) + } + assert.Equal(t, "newchannelname", channel.Name) + assert.Equal(t, "New Display Name", channel.DisplayName) +} diff --git a/cmd/mattermost/commands/channel.go b/cmd/mattermost/commands/channel.go index 478466a34..cbafac6b5 100644 --- a/cmd/mattermost/commands/channel.go +++ b/cmd/mattermost/commands/channel.go @@ -26,6 +26,14 @@ var ChannelCreateCmd = &cobra.Command{ RunE: createChannelCmdF, } +var ChannelRenameCmd = &cobra.Command{ + Use: "rename", + Short: "Rename a channel", + Long: `Rename a channel.`, + Example: `" channel rename myteam:mychannel newchannelname --display_name "New Display Name"`, + RunE: renameChannelCmdF, +} + var RemoveChannelUsersCmd = &cobra.Command{ Use: "remove [channel] [users]", Short: "Remove users from channel", @@ -115,6 +123,8 @@ func init() { ModifyChannelCmd.Flags().Bool("public", false, "Convert the channel to a public channel") ModifyChannelCmd.Flags().String("username", "", "Required. Username who changes the channel privacy.") + ChannelRenameCmd.Flags().String("display_name", "", "Channel Display Name") + ChannelCmd.AddCommand( ChannelCreateCmd, RemoveChannelUsersCmd, @@ -125,6 +135,7 @@ func init() { MoveChannelsCmd, RestoreChannelsCmd, ModifyChannelCmd, + ChannelRenameCmd, ) RootCmd.AddCommand(ChannelCmd) @@ -493,3 +504,34 @@ func modifyChannelCmdF(command *cobra.Command, args []string) error { return nil } + +func renameChannelCmdF(command *cobra.Command, args []string) error { + a, err := InitDBCommandContextCobra(command) + var newDisplayName, newChannelName string + if err != nil { + return err + } + defer a.Shutdown() + + if len(args) < 2 { + return errors.New("Not enough arguments.") + } + + channel := getChannelFromChannelArg(a, args[0]) + if channel == nil { + return errors.New("Unable to find channel '" + args[0] + "'") + } + + newChannelName = args[1] + newDisplayName, errdn := command.Flags().GetString("display_name") + if errdn != nil { + return errdn + } + + _, errch := a.RenameChannel(channel, newChannelName, newDisplayName) + if errch != nil { + return errors.New("Error in updating channel from " + channel.Name + " to " + newChannelName + err.Error()) + } + + return nil +} diff --git a/cmd/mattermost/commands/channel_test.go b/cmd/mattermost/commands/channel_test.go index 8fec971ca..9632ee432 100644 --- a/cmd/mattermost/commands/channel_test.go +++ b/cmd/mattermost/commands/channel_test.go @@ -9,6 +9,7 @@ import ( "github.com/mattermost/mattermost-server/api4" "github.com/mattermost/mattermost-server/model" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -113,3 +114,16 @@ func TestCreateChannel(t *testing.T) { name = name + "-private" CheckCommand(t, "channel", "create", "--display_name", name, "--team", th.BasicTeam.Name, "--private", "--name", name) } + +func TestRenameChannel(t *testing.T) { + th := api4.Setup().InitBasic() + defer th.TearDown() + + channel := th.CreatePublicChannel() + CheckCommand(t, "channel", "rename", th.BasicTeam.Name+":"+channel.Name, "newchannelname10", "--display_name", "New Display Name") + + // Get the channel from the DB + updatedChannel, _ := th.App.GetChannel(channel.Id) + assert.Equal(t, "newchannelname10", updatedChannel.Name) + assert.Equal(t, "New Display Name", updatedChannel.DisplayName) +} diff --git a/tests/test-config.json b/tests/test-config.json index d22a377d1..8b6181fd2 100644 --- a/tests/test-config.json +++ b/tests/test-config.json @@ -409,4 +409,4 @@ "TimezoneSettings": { "SupportedTimezonesPath": "timezones.json" } -}
\ No newline at end of file +} |