MM-12371 Add webhook list command (#9528)

* Added start of the webhook command

* start of unit tests

* created a simple hook for unit test

* added outgoing as well

* have it all working

* Add license headers to the files

* Addressing code review, fixed print reverted sql change
Этот коммит содержится в:
Ben Echols
2018-10-04 12:16:25 -06:00
коммит произвёл Jesús Espino
родитель 108fabc67d
Коммит 939ee15013
2 изменённых файлов: 135 добавлений и 0 удалений

84
cmd/mattermost/commands/webhook.go Обычный файл
Просмотреть файл

@@ -0,0 +1,84 @@
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package commands
import (
"fmt"
"github.com/mattermost/mattermost-server/model"
"github.com/spf13/cobra"
)
var WebhookCmd = &cobra.Command{
Use: "webhook",
Short: "Management of webhooks",
}
var WebhookListCmd = &cobra.Command{
Use: "list",
Short: "List webhooks",
Long: "list all webhooks",
Example: " webhook list myteam",
RunE: listWebhookCmdF,
}
func listWebhookCmdF(command *cobra.Command, args []string) error {
app, err := InitDBCommandContextCobra(command)
if err != nil {
return err
}
defer app.Shutdown()
var teams []*model.Team
if len(args) < 1 {
var getErr error
// If no team is specified, list all teams
teams, getErr = app.GetAllTeams()
if getErr != nil {
return getErr
}
} else {
teams = getTeamsFromTeamArgs(app, args)
}
for i, team := range teams {
if team == nil {
CommandPrintErrorln("Unable to find team '" + args[i] + "'")
continue
}
// Fetch all hooks with a very large limit so we get them all.
incomingResult := app.Srv.Store.Webhook().GetIncomingByTeam(team.Id, 0, 100000000)
outgoingResult := app.Srv.Store.Webhook().GetOutgoingByTeam(team.Id, 0, 100000000)
if result := <-incomingResult; result.Err == nil {
CommandPrettyPrintln(fmt.Sprintf("Incoming webhooks for %s (%s):", team.DisplayName, team.Name))
hooks := result.Data.([]*model.IncomingWebhook)
for _, hook := range hooks {
CommandPrettyPrintln("\t" + hook.DisplayName + " (" + hook.Id + ")")
}
} else {
CommandPrintErrorln("Unable to list incoming webhooks for '" + args[i] + "'")
}
if result := <-outgoingResult; result.Err == nil {
hooks := result.Data.([]*model.OutgoingWebhook)
CommandPrettyPrintln(fmt.Sprintf("Outgoing webhooks for %s (%s):", team.DisplayName, team.Name))
for _, hook := range hooks {
CommandPrettyPrintln("\t" + hook.DisplayName + " (" + hook.Id + ")")
}
} else {
CommandPrintErrorln("Unable to list outgoing webhooks for '" + args[i] + "'")
}
}
return nil
}
func init() {
WebhookCmd.AddCommand(
WebhookListCmd,
)
RootCmd.AddCommand(WebhookCmd)
}

51
cmd/mattermost/commands/webhook_test.go Обычный файл
Просмотреть файл

@@ -0,0 +1,51 @@
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package commands
import (
"strings"
"testing"
"github.com/mattermost/mattermost-server/api4"
"github.com/mattermost/mattermost-server/model"
)
func TestListWebhooks(t *testing.T) {
th := api4.Setup().InitBasic().InitSystemAdmin()
defer th.TearDown()
adminClient := th.SystemAdminClient
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)
dispName := "myhookinc"
hook := &model.IncomingWebhook{DisplayName: dispName, ChannelId: th.BasicChannel.Id, TeamId: th.BasicChannel.TeamId}
_, resp := adminClient.CreateIncomingWebhook(hook)
api4.CheckNoError(t, resp)
dispName2 := "myhookout"
outHook := &model.OutgoingWebhook{DisplayName: dispName2, ChannelId: th.BasicChannel.Id, TeamId: th.BasicChannel.TeamId, CallbackURLs: []string{"http://nowhere.com"}, Username: "some-user-name", IconURL: "http://some-icon-url/"}
_, resp = adminClient.CreateOutgoingWebhook(outHook)
api4.CheckNoError(t, resp)
output := CheckCommand(t, "webhook", "list", th.BasicTeam.Name)
if !strings.Contains(string(output), dispName) {
t.Fatal("should have incoming webhooks")
}
if !strings.Contains(string(output), dispName2) {
t.Fatal("should have outgoing webhooks")
}
}