MM-26057 - Add CreateCommand plugin API (#14916)

Automatic Merge
Этот коммит содержится в:
Christopher Poile
2020-07-31 11:40:15 -04:00
коммит произвёл GitHub
родитель 61cc7a8680
Коммит c5c6a5ce53
11 изменённых файлов: 763 добавлений и 18 удалений

Просмотреть файл

@@ -603,6 +603,10 @@ func (a *App) CreateCommand(cmd *model.Command) (*model.Command, *model.AppError
return nil, model.NewAppError("CreateCommand", "api.command.disabled.app_error", nil, "", http.StatusNotImplemented)
}
return a.createCommand(cmd)
}
func (a *App) createCommand(cmd *model.Command) (*model.Command, *model.AppError) {
cmd.Trigger = strings.ToLower(cmd.Trigger)
teamCmds, err := a.Srv().Store.Command().GetByTeam(cmd.TeamId)
@@ -667,6 +671,7 @@ func (a *App) UpdateCommand(oldCmd, updatedCmd *model.Command) (*model.Command,
updatedCmd.UpdateAt = model.GetMillis()
updatedCmd.DeleteAt = oldCmd.DeleteAt
updatedCmd.CreatorId = oldCmd.CreatorId
updatedCmd.PluginId = oldCmd.PluginId
updatedCmd.TeamId = oldCmd.TeamId
command, err := a.Srv().Store.Command().Update(updatedCmd)

Просмотреть файл

@@ -929,3 +929,110 @@ func (api *PluginAPI) PluginHTTP(request *http.Request) *http.Response {
api.app.ServeInterPluginRequest(responseTransfer, request, api.id, destinationPluginId)
return responseTransfer.GenerateResponse()
}
func (api *PluginAPI) CreateCommand(cmd *model.Command) (*model.Command, error) {
cmd.CreatorId = ""
cmd.PluginId = api.id
cmd, appErr := api.app.createCommand(cmd)
if appErr != nil {
return cmd, appErr
}
return cmd, nil
}
func (api *PluginAPI) ListCommands(teamID string) ([]*model.Command, error) {
ret := make([]*model.Command, 0)
cmds, err := api.ListPluginCommands(teamID)
if err != nil {
return nil, err
}
ret = append(ret, cmds...)
cmds, err = api.ListBuiltInCommands()
if err != nil {
return nil, err
}
ret = append(ret, cmds...)
cmds, err = api.ListCustomCommands(teamID)
if err != nil {
return nil, err
}
ret = append(ret, cmds...)
return ret, nil
}
func (api *PluginAPI) ListCustomCommands(teamID string) ([]*model.Command, error) {
// Plugins are allowed to bypass the a.Config().ServiceSettings.EnableCommands setting.
return api.app.Srv().Store.Command().GetByTeam(teamID)
}
func (api *PluginAPI) ListPluginCommands(teamID string) ([]*model.Command, error) {
commands := make([]*model.Command, 0)
seen := make(map[string]bool)
for _, cmd := range api.app.PluginCommandsForTeam(teamID) {
if !seen[cmd.Trigger] {
seen[cmd.Trigger] = true
commands = append(commands, cmd)
}
}
return commands, nil
}
func (api *PluginAPI) ListBuiltInCommands() ([]*model.Command, error) {
commands := make([]*model.Command, 0)
seen := make(map[string]bool)
for _, value := range commandProviders {
if cmd := value.GetCommand(api.app, utils.T); cmd != nil {
cpy := *cmd
if cpy.AutoComplete && !seen[cpy.Trigger] {
cpy.Sanitize()
seen[cpy.Trigger] = true
commands = append(commands, &cpy)
}
}
}
return commands, nil
}
func (api *PluginAPI) GetCommand(commandID string) (*model.Command, error) {
return api.app.Srv().Store.Command().Get(commandID)
}
func (api *PluginAPI) UpdateCommand(commandID string, updatedCmd *model.Command) (*model.Command, error) {
oldCmd, err := api.GetCommand(commandID)
if err != nil {
return nil, err
}
updatedCmd.Trigger = strings.ToLower(updatedCmd.Trigger)
updatedCmd.Id = oldCmd.Id
updatedCmd.Token = oldCmd.Token
updatedCmd.CreateAt = oldCmd.CreateAt
updatedCmd.UpdateAt = model.GetMillis()
updatedCmd.DeleteAt = oldCmd.DeleteAt
updatedCmd.PluginId = api.id
if updatedCmd.TeamId == "" {
updatedCmd.TeamId = oldCmd.TeamId
}
return api.app.Srv().Store.Command().Update(updatedCmd)
}
func (api *PluginAPI) DeleteCommand(commandID string) error {
err := api.app.Srv().Store.Command().Delete(commandID, model.GetMillis())
if err != nil {
return err
}
return nil
}

Просмотреть файл

@@ -1730,3 +1730,88 @@ func TestPluginAPISearchPostsInTeamByUser(t *testing.T) {
})
}
}
func TestPluginAPICreateCommandAndListCommands(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
api := th.SetupPluginAPI()
foundCommand := func(listXCommand func(teamId string) ([]*model.Command, error)) bool {
cmds, appErr := listXCommand(th.BasicTeam.Id)
require.Nil(t, appErr)
for _, cmd := range cmds {
if cmd.Trigger == "testcmd" {
return true
}
}
return false
}
require.False(t, foundCommand(api.ListCommands))
cmd := &model.Command{
TeamId: th.BasicTeam.Id,
Trigger: "testcmd",
Method: "G",
URL: "http://test.com/testcmd",
}
cmd, appErr := api.CreateCommand(cmd)
require.Nil(t, appErr)
newCmd, appErr := api.GetCommand(cmd.Id)
require.Nil(t, appErr)
require.Equal(t, "pluginid", newCmd.PluginId)
require.Equal(t, "", newCmd.CreatorId)
require.True(t, foundCommand(api.ListCommands))
require.True(t, foundCommand(api.ListCustomCommands))
require.False(t, foundCommand(api.ListPluginCommands))
}
func TestPluginAPIUpdateCommand(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
api := th.SetupPluginAPI()
cmd := &model.Command{
TeamId: th.BasicTeam.Id,
Trigger: "testcmd",
Method: "G",
URL: "http://test.com/testcmd",
}
cmd, appErr := api.CreateCommand(cmd)
require.Nil(t, appErr)
newCmd, appErr := api.GetCommand(cmd.Id)
require.Nil(t, appErr)
require.Equal(t, "pluginid", newCmd.PluginId)
require.Equal(t, "", newCmd.CreatorId)
newCmd.Trigger = "NewTrigger"
newCmd.PluginId = "CannotChangeMe"
newCmd2, appErr := api.UpdateCommand(newCmd.Id, newCmd)
require.Nil(t, appErr)
require.Equal(t, "pluginid", newCmd2.PluginId)
require.Equal(t, "newtrigger", newCmd2.Trigger)
team1 := th.CreateTeam()
newCmd2.PluginId = "CannotChangeMe"
newCmd2.Trigger = "anotherNewTrigger"
newCmd2.TeamId = team1.Id
newCmd3, appErr := api.UpdateCommand(newCmd2.Id, newCmd2)
require.Nil(t, appErr)
require.Equal(t, "pluginid", newCmd3.PluginId)
require.Equal(t, "anothernewtrigger", newCmd3.Trigger)
require.Equal(t, team1.Id, newCmd3.TeamId)
newCmd3.Trigger = "anotherNewTriggerAgain"
newCmd3.TeamId = ""
newCmd4, appErr := api.UpdateCommand(newCmd2.Id, newCmd2)
require.Nil(t, appErr)
require.Equal(t, "anothernewtriggeragain", newCmd4.Trigger)
require.Equal(t, team1.Id, newCmd4.TeamId)
}