From f3fc6d11fa11c9b8c73554c79ca55470073bb098 Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Wed, 4 Oct 2017 19:08:59 +0100 Subject: PLT-7218: CLI to move slash commands between teams. (#7574) --- store/sqlstore/command_store.go | 21 +++++++++++++++++++++ store/sqlstore/command_store_test.go | 36 ++++++++++++++++++++++++++++++++++++ store/store.go | 2 ++ 3 files changed, 59 insertions(+) (limited to 'store') diff --git a/store/sqlstore/command_store.go b/store/sqlstore/command_store.go index f156daab1..8284f889b 100644 --- a/store/sqlstore/command_store.go +++ b/store/sqlstore/command_store.go @@ -119,6 +119,27 @@ func (s SqlCommandStore) GetByTeam(teamId string) store.StoreChannel { return storeChannel } +func (s SqlCommandStore) GetByTrigger(teamId string, trigger string) store.StoreChannel { + storeChannel := make(store.StoreChannel, 1) + + go func() { + result := store.StoreResult{} + + var command model.Command + + if err := s.GetReplica().SelectOne(&command, "SELECT * FROM Commands WHERE TeamId = :TeamId AND `Trigger` = :Trigger AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId, "Trigger": trigger}); err != nil { + result.Err = model.NewAppError("SqlCommandStore.GetByTrigger", "store.sql_command.get_by_trigger.app_error", nil, "teamId="+teamId+", trigger="+trigger+", err="+err.Error(), http.StatusInternalServerError) + } + + result.Data = &command + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlCommandStore) Delete(commandId string, time int64) store.StoreChannel { storeChannel := make(store.StoreChannel, 1) diff --git a/store/sqlstore/command_store_test.go b/store/sqlstore/command_store_test.go index 85eed64cc..407eae72e 100644 --- a/store/sqlstore/command_store_test.go +++ b/store/sqlstore/command_store_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/store" ) func TestCommandStoreSave(t *testing.T) { @@ -82,6 +83,41 @@ func TestCommandStoreGetByTeam(t *testing.T) { } } +func TestCommandStoreGetByTrigger(t *testing.T) { + ss := Setup() + + o1 := &model.Command{} + o1.CreatorId = model.NewId() + o1.Method = model.COMMAND_METHOD_POST + o1.TeamId = model.NewId() + o1.URL = "http://nowhere.com/" + o1.Trigger = "trigger1" + + o2 := &model.Command{} + o2.CreatorId = model.NewId() + o2.Method = model.COMMAND_METHOD_POST + o2.TeamId = model.NewId() + o2.URL = "http://nowhere.com/" + o2.Trigger = "trigger1" + + o1 = (<-ss.Command().Save(o1)).Data.(*model.Command) + o2 = (<-ss.Command().Save(o2)).Data.(*model.Command) + + if r1 := <-ss.Command().GetByTrigger(o1.TeamId, o1.Trigger); r1.Err != nil { + t.Fatal(r1.Err) + } else { + if r1.Data.(*model.Command).Id != o1.Id { + t.Fatal("invalid returned command") + } + } + + store.Must(ss.Command().Delete(o1.Id, model.GetMillis())) + + if result := <-ss.Command().GetByTrigger(o1.TeamId, o1.Trigger); result.Err == nil { + t.Fatal("no commands should have returned") + } +} + func TestCommandStoreDelete(t *testing.T) { ss := Setup() diff --git a/store/store.go b/store/store.go index 9694921c8..bc9aa8f1a 100644 --- a/store/store.go +++ b/store/store.go @@ -7,6 +7,7 @@ import ( "time" l4g "github.com/alecthomas/log4go" + "github.com/mattermost/mattermost-server/model" ) @@ -322,6 +323,7 @@ type CommandStore interface { Save(webhook *model.Command) StoreChannel Get(id string) StoreChannel GetByTeam(teamId string) StoreChannel + GetByTrigger(teamId string, trigger string) StoreChannel Delete(commandId string, time int64) StoreChannel PermanentDeleteByTeam(teamId string) StoreChannel PermanentDeleteByUser(userId string) StoreChannel -- cgit v1.2.3-1-g7c22