MM-12368 Add create webhook-incoming command (#9566)
Этот коммит содержится в:
коммит произвёл
George Goldberg
родитель
80153ef873
Коммит
cedf6488e4
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -23,6 +24,14 @@ var WebhookListCmd = &cobra.Command{
|
||||
RunE: listWebhookCmdF,
|
||||
}
|
||||
|
||||
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]",
|
||||
RunE: createIncomingWebhookCmdF,
|
||||
}
|
||||
|
||||
func listWebhookCmdF(command *cobra.Command, args []string) error {
|
||||
app, err := InitDBCommandContextCobra(command)
|
||||
if err != nil {
|
||||
@@ -75,9 +84,52 @@ func listWebhookCmdF(command *cobra.Command, args []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func createIncomingWebhookCmdF(command *cobra.Command, args []string) error {
|
||||
app, err := InitDBCommandContextCobra(command)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer app.Shutdown()
|
||||
|
||||
channelArg, _ := command.Flags().GetString("channel")
|
||||
channel := getChannelFromChannelArg(app, channelArg)
|
||||
if channel == nil {
|
||||
return errors.New("Unable to find channel '" + channelArg + "'")
|
||||
}
|
||||
|
||||
userArg, _ := command.Flags().GetString("user")
|
||||
user := getUserFromUserArg(app, userArg)
|
||||
displayName, _ := command.Flags().GetString("display-name")
|
||||
description, _ := command.Flags().GetString("description")
|
||||
iconUrl, _ := command.Flags().GetString("icon")
|
||||
channelLocked, _ := command.Flags().GetBool("lock-to-channel")
|
||||
|
||||
incomingWebhook := &model.IncomingWebhook{
|
||||
ChannelId: channel.Id,
|
||||
DisplayName: displayName,
|
||||
Description: description,
|
||||
IconURL: iconUrl,
|
||||
ChannelLocked: channelLocked,
|
||||
}
|
||||
|
||||
if _, err := app.CreateIncomingWebhookForChannel(user.Id, channel, incomingWebhook); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
WebhookCreateIncomingCmd.Flags().String("channel", "", "Channel ID")
|
||||
WebhookCreateIncomingCmd.Flags().String("user", "", "User ID")
|
||||
WebhookCreateIncomingCmd.Flags().String("display-name", "", "Incoming webhook display name")
|
||||
WebhookCreateIncomingCmd.Flags().String("description", "", "Incoming webhook description")
|
||||
WebhookCreateIncomingCmd.Flags().String("icon", "", "Icon URL")
|
||||
WebhookCreateIncomingCmd.Flags().Bool("lock-to-channel", false, "Lock to channel")
|
||||
|
||||
WebhookCmd.AddCommand(
|
||||
WebhookListCmd,
|
||||
WebhookCreateIncomingCmd,
|
||||
)
|
||||
|
||||
RootCmd.AddCommand(WebhookCmd)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -49,3 +50,47 @@ func TestListWebhooks(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestCreateIncomingWebhook(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)
|
||||
|
||||
// should fail because you need to specify valid channel
|
||||
require.Error(t, RunCommand(t, "webhook", "create-incoming"))
|
||||
require.Error(t, RunCommand(t, "webhook", "create-incoming", "--channel", th.BasicTeam.Name+":doesnotexist"))
|
||||
|
||||
// should fail because you need to specify valid user
|
||||
require.Error(t, RunCommand(t, "webhook", "create-incoming", "--channel", th.BasicChannel.Id))
|
||||
require.Error(t, RunCommand(t, "webhook", "create-incoming", "--channel", th.BasicChannel.Id, "--user", "doesnotexist"))
|
||||
|
||||
description := "myhookinc"
|
||||
displayName := "myhookinc"
|
||||
CheckCommand(t, "webhook", "create-incoming", "--channel", th.BasicChannel.Id, "--user", th.BasicUser.Email, "--description", description, "--display-name", displayName)
|
||||
|
||||
webhooks, err := th.App.GetIncomingWebhooksPage(0, 1000)
|
||||
if err != nil {
|
||||
t.Fatal("unable to retrieve incoming webhooks")
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, webhook := range webhooks {
|
||||
if webhook.Description == description && webhook.UserId == th.BasicUser.Id {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("Failed to create incoming webhook")
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user