Improving create-outgoing webhook command (#9899)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3904c01f46
Коммит
415036ee8d
@@ -5,7 +5,6 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/model"
|
"github.com/mattermost/mattermost-server/model"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -45,8 +44,8 @@ var WebhookCreateOutgoingCmd = &cobra.Command{
|
|||||||
Use: "create-outgoing",
|
Use: "create-outgoing",
|
||||||
Short: "Create outgoing webhook",
|
Short: "Create outgoing webhook",
|
||||||
Long: "create outgoing webhook which allows external posting of messages from a specific channel",
|
Long: "create outgoing webhook which allows external posting of messages from a specific channel",
|
||||||
Example: ` webhook create-outgoing --team myteam --user myusername --display-name mywebhook --trigger-words "build\ntest" --urls http://localhost:8000/my-webhook-handler
|
Example: ` webhook create-outgoing --team myteam --user myusername --display-name mywebhook --trigger-word "build" --trigger-word "test" --url http://localhost:8000/my-webhook-handler
|
||||||
webhook create-outgoing --team myteam --channel mychannel --user myusername --display-name mywebhook --description "My cool webhook" --trigger-when 1 --trigger-words "build\ntest" --icon http://localhost:8000/my-slash-handler-bot-icon.png --urls http://localhost:8000/my-webhook-handler --content-type "application/json"`,
|
webhook create-outgoing --team myteam --channel mychannel --user myusername --display-name mywebhook --description "My cool webhook" --trigger-when start --trigger-word build --trigger-word test --icon http://localhost:8000/my-slash-handler-bot-icon.png --url http://localhost:8000/my-webhook-handler --content-type "application/json"`,
|
||||||
RunE: createOutgoingWebhookCmdF,
|
RunE: createOutgoingWebhookCmdF,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,19 +224,25 @@ func createOutgoingWebhookCmdF(command *cobra.Command, args []string) error {
|
|||||||
return errors.New("Display name is required")
|
return errors.New("Display name is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
triggerWordsString, errWords := command.Flags().GetString("trigger-words")
|
triggerWords, errWords := command.Flags().GetStringArray("trigger-word")
|
||||||
if errWords != nil || triggerWordsString == "" {
|
if errWords != nil || len(triggerWords) == 0 {
|
||||||
return errors.New("Trigger word or words required")
|
return errors.New("Trigger word or words required")
|
||||||
}
|
}
|
||||||
triggerWords := strings.Split(triggerWordsString, "\n")
|
|
||||||
|
|
||||||
callbackURLsString, errURL := command.Flags().GetString("urls")
|
callbackURLs, errURL := command.Flags().GetStringArray("url")
|
||||||
if errURL != nil || callbackURLsString == "" {
|
if errURL != nil || len(callbackURLs) == 0 {
|
||||||
return errors.New("Callback URL or URLs required")
|
return errors.New("Callback URL or URLs required")
|
||||||
}
|
}
|
||||||
callbackURLs := strings.Split(callbackURLsString, "\n")
|
|
||||||
|
|
||||||
triggerWhen, _ := command.Flags().GetInt("trigger-when")
|
triggerWhenString, _ := command.Flags().GetString("trigger-when")
|
||||||
|
var triggerWhen int
|
||||||
|
if triggerWhenString == "exact" {
|
||||||
|
triggerWhen = 0
|
||||||
|
} else if triggerWhenString == "start" {
|
||||||
|
triggerWhen = 1
|
||||||
|
} else {
|
||||||
|
return errors.New("Invalid trigger when parameter")
|
||||||
|
}
|
||||||
description, _ := command.Flags().GetString("description")
|
description, _ := command.Flags().GetString("description")
|
||||||
contentType, _ := command.Flags().GetString("content-type")
|
contentType, _ := command.Flags().GetString("content-type")
|
||||||
iconURL, _ := command.Flags().GetString("icon")
|
iconURL, _ := command.Flags().GetString("icon")
|
||||||
@@ -311,10 +316,10 @@ func init() {
|
|||||||
WebhookCreateOutgoingCmd.Flags().String("user", "", "User username, email, or ID (required)")
|
WebhookCreateOutgoingCmd.Flags().String("user", "", "User username, email, or ID (required)")
|
||||||
WebhookCreateOutgoingCmd.Flags().String("display-name", "", "Outgoing webhook display name (required)")
|
WebhookCreateOutgoingCmd.Flags().String("display-name", "", "Outgoing webhook display name (required)")
|
||||||
WebhookCreateOutgoingCmd.Flags().String("description", "", "Outgoing webhook description")
|
WebhookCreateOutgoingCmd.Flags().String("description", "", "Outgoing webhook description")
|
||||||
WebhookCreateOutgoingCmd.Flags().String("trigger-words", "", "Words to trigger webhook (word1\nword2) (required)")
|
WebhookCreateOutgoingCmd.Flags().StringArray("trigger-word", []string{}, "Word to trigger webhook (required)")
|
||||||
WebhookCreateOutgoingCmd.Flags().Int("trigger-when", 0, "When to trigger webhook (either when trigger word is first (enter 1) or when it's anywhere (enter 0))")
|
WebhookCreateOutgoingCmd.Flags().String("trigger-when", "exact", "When to trigger webhook (exact: for first word matches a trigger word exactly, start: for first word starts with a trigger word)")
|
||||||
WebhookCreateOutgoingCmd.Flags().String("icon", "", "Icon URL")
|
WebhookCreateOutgoingCmd.Flags().String("icon", "", "Icon URL")
|
||||||
WebhookCreateOutgoingCmd.Flags().String("urls", "", "Callback URLs (url1\nurl2) (required)")
|
WebhookCreateOutgoingCmd.Flags().StringArray("url", []string{}, "Callback URL (required)")
|
||||||
WebhookCreateOutgoingCmd.Flags().String("content-type", "", "Content-type")
|
WebhookCreateOutgoingCmd.Flags().String("content-type", "", "Content-type")
|
||||||
|
|
||||||
WebhookCmd.AddCommand(
|
WebhookCmd.AddCommand(
|
||||||
|
|||||||
@@ -172,28 +172,30 @@ func TestCreateOutgoingWebhook(t *testing.T) {
|
|||||||
team := th.BasicTeam.Id
|
team := th.BasicTeam.Id
|
||||||
user := th.BasicUser.Id
|
user := th.BasicUser.Id
|
||||||
displayName := "totally radical webhook"
|
displayName := "totally radical webhook"
|
||||||
triggerWords := "build\ndefenestrate"
|
triggerWord1 := "build"
|
||||||
callbackURLs := "http://localhost:8000/my-webhook-handler\nhttp://localhost:8000/my-webhook-handler2"
|
triggerWord2 := "defenestrate"
|
||||||
|
callbackURL1 := "http://localhost:8000/my-webhook-handler"
|
||||||
|
callbackURL2 := "http://localhost:8000/my-webhook-handler2"
|
||||||
|
|
||||||
// should fail because team is not specified
|
// should fail because team is not specified
|
||||||
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--display-name", displayName, "--trigger-words", triggerWords, "--urls", callbackURLs, "--user", user))
|
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--display-name", displayName, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--url", callbackURL1, "--url", callbackURL2, "--user", user))
|
||||||
|
|
||||||
// should fail because user is not specified
|
// should fail because user is not specified
|
||||||
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--team", team, "--display-name", displayName, "--trigger-words", triggerWords, "--urls", callbackURLs))
|
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--team", team, "--display-name", displayName, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--url", callbackURL1, "--url", callbackURL2))
|
||||||
|
|
||||||
// should fail because display name is not specified
|
// should fail because display name is not specified
|
||||||
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--team", team, "--trigger-words", triggerWords, "--urls", callbackURLs, "--user", user))
|
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--team", team, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--url", callbackURL1, "--url", callbackURL2, "--user", user))
|
||||||
|
|
||||||
// should fail because trigger words are not specified
|
// should fail because trigger words are not specified
|
||||||
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--team", team, "--display-name", displayName, "--urls", callbackURLs, "--user", user))
|
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--team", team, "--display-name", displayName, "--url", callbackURL1, "--url", callbackURL2, "--user", user))
|
||||||
|
|
||||||
// should fail because callback URLs are not specified
|
// should fail because callback URLs are not specified
|
||||||
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--team", team, "--display-name", displayName, "--trigger-words", triggerWords, "--user", user))
|
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--team", team, "--display-name", displayName, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--user", user))
|
||||||
|
|
||||||
// should fail because outgoing webhooks cannot be made for private channels
|
// should fail because outgoing webhooks cannot be made for private channels
|
||||||
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--team", team, "--channel", th.BasicPrivateChannel.Id, "--display-name", displayName, "--trigger-words", triggerWords, "--urls", callbackURLs, "--user", user))
|
require.Error(t, RunCommand(t, "webhook", "create-outgoing", "--team", team, "--channel", th.BasicPrivateChannel.Id, "--display-name", displayName, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--url", callbackURL1, "--url", callbackURL2, "--user", user))
|
||||||
|
|
||||||
CheckCommand(t, "webhook", "create-outgoing", "--team", team, "--channel", th.BasicChannel.Id, "--display-name", displayName, "--trigger-words", triggerWords, "--urls", callbackURLs, "--user", user)
|
CheckCommand(t, "webhook", "create-outgoing", "--team", team, "--channel", th.BasicChannel.Id, "--display-name", displayName, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--url", callbackURL1, "--url", callbackURL2, "--user", user)
|
||||||
|
|
||||||
webhooks, err := th.App.GetOutgoingWebhooksPage(0, 1000)
|
webhooks, err := th.App.GetOutgoingWebhooksPage(0, 1000)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user