summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur Mogozov <artur@canva.com>2018-10-19 22:04:12 +1100
committerGeorge Goldberg <george@gberg.me>2018-10-19 12:04:12 +0100
commitc3993704ef759e1527140b9ed03ed39881cf64f7 (patch)
tree0e96e6d6820adbaf12e2feda2ea5738e2b69f0a9
parentc3d536c644f102cbdc07354aa2ecf8bc8194882e (diff)
downloadchat-c3993704ef759e1527140b9ed03ed39881cf64f7.tar.gz
chat-c3993704ef759e1527140b9ed03ed39881cf64f7.tar.bz2
chat-c3993704ef759e1527140b9ed03ed39881cf64f7.zip
MM-12372 Add modify-incoming webhook command (#9683)
* MM-12372 Add modify-incoming webhook command * Review comments
-rw-r--r--cmd/mattermost/commands/webhook.go67
-rw-r--r--cmd/mattermost/commands/webhook_test.go56
2 files changed, 122 insertions, 1 deletions
diff --git a/cmd/mattermost/commands/webhook.go b/cmd/mattermost/commands/webhook.go
index 0c56af18c..d91d076a3 100644
--- a/cmd/mattermost/commands/webhook.go
+++ b/cmd/mattermost/commands/webhook.go
@@ -28,10 +28,18 @@ var WebhookCreateIncomingCmd = &cobra.Command{
Use: "create-incoming",
Short: "Create incoming webhook",
Long: "create incoming webhook which allows external posting of messages to specific channel",
- Example: " webhook create-incoming --channel [channelID] --user [userID] --description [webhookDescription] --lock-to-channel --icon [iconURL]",
+ Example: " webhook create-incoming --channel [channelID] --user [userID] --display-name [displayName] --description [webhookDescription] --lock-to-channel --icon [iconURL]",
RunE: createIncomingWebhookCmdF,
}
+var WebhookModifyIncomingCmd = &cobra.Command{
+ Use: "modify-incoming",
+ Short: "Modify incoming webhook",
+ Long: "Modify existing incoming webhook by changing its title, description, channel or icon url",
+ Example: " webhook modify-incoming [webhookID] --channel [channelID] --display-name [displayName] --description [webhookDescription] --lock-to-channel --icon [iconURL]",
+ RunE: modifyIncomingWebhookCmdF,
+}
+
func listWebhookCmdF(command *cobra.Command, args []string) error {
app, err := InitDBCommandContextCobra(command)
if err != nil {
@@ -119,6 +127,56 @@ func createIncomingWebhookCmdF(command *cobra.Command, args []string) error {
return nil
}
+func modifyIncomingWebhookCmdF(command *cobra.Command, args []string) error {
+ app, err := InitDBCommandContextCobra(command)
+ if err != nil {
+ return err
+ }
+ defer app.Shutdown()
+
+ if len(args) < 1 {
+ return errors.New("WebhookID is not specified")
+ }
+
+ webhookArg := args[0]
+ oldHook, getErr := app.GetIncomingWebhook(webhookArg)
+ if getErr != nil {
+ return errors.New("Unable to find webhook '" + webhookArg + "'")
+ }
+
+ updatedHook := oldHook
+
+ channelArg, _ := command.Flags().GetString("channel")
+ if channelArg != "" {
+ channel := getChannelFromChannelArg(app, channelArg)
+ if channel == nil {
+ return errors.New("Unable to find channel '" + channelArg + "'")
+ }
+ updatedHook.ChannelId = channel.Id
+ }
+
+ displayName, _ := command.Flags().GetString("display-name")
+ if displayName != "" {
+ updatedHook.DisplayName = displayName
+ }
+ description, _ := command.Flags().GetString("description")
+ if description != "" {
+ updatedHook.Description = description
+ }
+ iconUrl, _ := command.Flags().GetString("icon")
+ if iconUrl != "" {
+ updatedHook.IconURL = iconUrl
+ }
+ channelLocked, _ := command.Flags().GetBool("lock-to-channel")
+ updatedHook.ChannelLocked = channelLocked
+
+ if _, err := app.UpdateIncomingWebhook(oldHook, updatedHook); err != nil {
+ return err
+ }
+
+ return nil
+}
+
func init() {
WebhookCreateIncomingCmd.Flags().String("channel", "", "Channel ID")
WebhookCreateIncomingCmd.Flags().String("user", "", "User ID")
@@ -127,9 +185,16 @@ func init() {
WebhookCreateIncomingCmd.Flags().String("icon", "", "Icon URL")
WebhookCreateIncomingCmd.Flags().Bool("lock-to-channel", false, "Lock to channel")
+ WebhookModifyIncomingCmd.Flags().String("channel", "", "Channel ID")
+ WebhookModifyIncomingCmd.Flags().String("display-name", "", "Incoming webhook display name")
+ WebhookModifyIncomingCmd.Flags().String("description", "", "Incoming webhook description")
+ WebhookModifyIncomingCmd.Flags().String("icon", "", "Icon URL")
+ WebhookModifyIncomingCmd.Flags().Bool("lock-to-channel", false, "Lock to channel")
+
WebhookCmd.AddCommand(
WebhookListCmd,
WebhookCreateIncomingCmd,
+ WebhookModifyIncomingCmd,
)
RootCmd.AddCommand(WebhookCmd)
diff --git a/cmd/mattermost/commands/webhook_test.go b/cmd/mattermost/commands/webhook_test.go
index 1eea36b76..979109676 100644
--- a/cmd/mattermost/commands/webhook_test.go
+++ b/cmd/mattermost/commands/webhook_test.go
@@ -5,6 +5,7 @@ package commands
import (
"github.com/stretchr/testify/require"
+ "strconv"
"strings"
"testing"
@@ -94,3 +95,58 @@ func TestCreateIncomingWebhook(t *testing.T) {
t.Fatal("Failed to create incoming webhook")
}
}
+
+func TestModifyIncomingWebhook(t *testing.T) {
+ th := api4.Setup().InitBasic().InitSystemAdmin()
+ defer th.TearDown()
+
+ th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableIncomingWebhooks = true })
+ th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOutgoingWebhooks = true })
+ th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnablePostUsernameOverride = true })
+ th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnablePostIconOverride = true })
+
+ defaultRolePermissions := th.SaveDefaultRolePermissions()
+ defer func() {
+ th.RestoreDefaultRolePermissions(defaultRolePermissions)
+ }()
+ th.AddPermissionToRole(model.PERMISSION_MANAGE_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
+ th.RemovePermissionFromRole(model.PERMISSION_MANAGE_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
+
+ description := "myhookincdesc"
+ displayName := "myhookincname"
+
+ incomingWebhook := &model.IncomingWebhook{
+ ChannelId: th.BasicChannel.Id,
+ DisplayName: displayName,
+ Description: description,
+ }
+
+ oldHook, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, incomingWebhook)
+ if err != nil {
+ t.Fatal("unable to create incoming webhooks")
+ }
+ defer func() {
+ th.App.DeleteIncomingWebhook(oldHook.Id)
+ }()
+
+ // should fail because you need to specify valid incoming webhook
+ require.Error(t, RunCommand(t, "webhook", "modify-incoming", "doesnotexist"))
+ // should fail because you need to specify valid channel
+ require.Error(t, RunCommand(t, "webhook", "modify-incoming", oldHook.Id, "--channel", th.BasicTeam.Name+":doesnotexist"))
+
+ modifiedDescription := "myhookincdesc2"
+ modifiedDisplayName := "myhookincname2"
+ modifiedIconUrl := "myhookincicon2"
+ modifiedChannelLocked := true
+ modifiedChannelId := th.BasicChannel2.Id
+
+ CheckCommand(t, "webhook", "modify-incoming", oldHook.Id, "--channel", modifiedChannelId, "--description", modifiedDescription, "--display-name", modifiedDisplayName, "--icon", modifiedIconUrl, "--lock-to-channel", strconv.FormatBool(modifiedChannelLocked))
+
+ modifiedHook, err := th.App.GetIncomingWebhook(oldHook.Id)
+ if err != nil {
+ t.Fatal("unable to retrieve modified incoming webhook")
+ }
+ if modifiedHook.DisplayName != modifiedDisplayName || modifiedHook.Description != modifiedDescription || modifiedHook.IconURL != modifiedIconUrl || modifiedHook.ChannelLocked != modifiedChannelLocked || modifiedHook.ChannelId != modifiedChannelId {
+ t.Fatal("Failed to update incoming webhook")
+ }
+}