коммит произвёл
GitHub
родитель
8ef7f78d8d
Коммит
532f2882d1
@@ -671,22 +671,8 @@ func (a *App) CreateCommand(cmd *model.Command) (*model.Command, *model.AppError
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("CreateCommand", "app.command.createcommand.internal_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
|
||||
for _, existingCommand := range teamCmds {
|
||||
if cmd.Trigger == existingCommand.Trigger {
|
||||
return nil, model.NewAppError("CreateCommand", "api.command.duplicate_trigger.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
for _, builtInProvider := range commandProviders {
|
||||
builtInCommand := builtInProvider.GetCommand(a, i18n.T)
|
||||
if builtInCommand != nil && cmd.Trigger == builtInCommand.Trigger {
|
||||
return nil, model.NewAppError("CreateCommand", "api.command.duplicate_trigger.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
if appErr := a.validateCommandTriggerUniqueness(cmd.TeamId, cmd.Trigger, ""); appErr != nil {
|
||||
return nil, appErr
|
||||
}
|
||||
|
||||
command, nErr := a.Srv().Store().Command().Save(cmd)
|
||||
@@ -703,6 +689,30 @@ func (a *App) createCommand(cmd *model.Command) (*model.Command, *model.AppError
|
||||
return command, nil
|
||||
}
|
||||
|
||||
func (a *App) validateCommandTriggerUniqueness(teamID, trigger, excludeCommandID string) *model.AppError {
|
||||
trigger = strings.ToLower(trigger)
|
||||
|
||||
for _, builtInProvider := range commandProviders {
|
||||
builtInCommand := builtInProvider.GetCommand(a, i18n.T)
|
||||
if builtInCommand != nil && trigger == strings.ToLower(builtInCommand.Trigger) {
|
||||
return model.NewAppError("validateCommandTriggerUniqueness", "api.command.duplicate_trigger.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
teamCmds, err := a.Srv().Store().Command().GetByTeam(teamID)
|
||||
if err != nil {
|
||||
return model.NewAppError("validateCommandTriggerUniqueness", "app.command.validatecommandtriggeruniqueness.internal_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
|
||||
for _, existingCommand := range teamCmds {
|
||||
if existingCommand.Id != excludeCommandID && trigger == strings.ToLower(existingCommand.Trigger) {
|
||||
return model.NewAppError("validateCommandTriggerUniqueness", "api.command.duplicate_trigger.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) GetCommand(commandID string) (*model.Command, *model.AppError) {
|
||||
if !*a.Config().ServiceSettings.EnableCommands {
|
||||
return nil, model.NewAppError("GetCommand", "api.command.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||
@@ -736,6 +746,10 @@ func (a *App) UpdateCommand(oldCmd, updatedCmd *model.Command) (*model.Command,
|
||||
updatedCmd.PluginId = oldCmd.PluginId
|
||||
updatedCmd.TeamId = oldCmd.TeamId
|
||||
|
||||
if appErr := a.validateCommandTriggerUniqueness(updatedCmd.TeamId, updatedCmd.Trigger, updatedCmd.Id); appErr != nil {
|
||||
return nil, appErr
|
||||
}
|
||||
|
||||
command, err := a.Srv().Store().Command().Update(updatedCmd)
|
||||
if err != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
@@ -754,6 +768,10 @@ func (a *App) UpdateCommand(oldCmd, updatedCmd *model.Command) (*model.Command,
|
||||
}
|
||||
|
||||
func (a *App) MoveCommand(team *model.Team, command *model.Command) *model.AppError {
|
||||
if appErr := a.validateCommandTriggerUniqueness(team.Id, command.Trigger, command.Id); appErr != nil {
|
||||
return appErr
|
||||
}
|
||||
|
||||
command.TeamId = team.Id
|
||||
|
||||
_, err := a.Srv().Store().Command().Update(command)
|
||||
|
||||
@@ -1305,6 +1305,10 @@ func (api *PluginAPI) UpdateCommand(commandID string, updatedCmd *model.Command)
|
||||
updatedCmd.TeamId = oldCmd.TeamId
|
||||
}
|
||||
|
||||
if appErr := api.app.validateCommandTriggerUniqueness(updatedCmd.TeamId, updatedCmd.Trigger, updatedCmd.Id); appErr != nil {
|
||||
return nil, appErr
|
||||
}
|
||||
|
||||
return api.app.Srv().Store().Command().Update(updatedCmd)
|
||||
}
|
||||
|
||||
|
||||
@@ -2247,6 +2247,23 @@ func TestPluginAPIUpdateCommand(t *testing.T) {
|
||||
require.NoError(t, appErr)
|
||||
require.Equal(t, "anothernewtriggeragain", newCmd4.Trigger)
|
||||
require.Equal(t, team1.Id, newCmd4.TeamId)
|
||||
|
||||
// Updating a command's trigger to one that already exists should fail.
|
||||
cmd2 := &model.Command{
|
||||
TeamId: team1.Id,
|
||||
Trigger: "uniquetrigger",
|
||||
Method: "G",
|
||||
URL: "http://test.com/uniquetrigger",
|
||||
}
|
||||
cmd2, appErr = api.CreateCommand(cmd2)
|
||||
require.NoError(t, appErr)
|
||||
|
||||
cmd2.Trigger = "anotherNewTriggerAgain"
|
||||
_, appErr = api.UpdateCommand(cmd2.Id, cmd2)
|
||||
require.Error(t, appErr)
|
||||
var appError *model.AppError
|
||||
require.ErrorAs(t, appErr, &appError)
|
||||
require.Equal(t, "api.command.duplicate_trigger.app_error", appError.Id)
|
||||
}
|
||||
|
||||
func TestPluginAPIIsEnterpriseReady(t *testing.T) {
|
||||
|
||||
@@ -64,6 +64,21 @@ func TestMoveCommand(t *testing.T) {
|
||||
retrievedCommand, err = th.App.GetCommand(command.Id)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualValues(t, targetTeam.Id, retrievedCommand.TeamId)
|
||||
|
||||
// Move a command to a team where the trigger already exists should fail.
|
||||
command2 := &model.Command{}
|
||||
command2.CreatorId = model.NewId()
|
||||
command2.Method = model.CommandMethodPost
|
||||
command2.TeamId = sourceTeam.Id
|
||||
command2.URL = "http://nowhere.com/"
|
||||
command2.Trigger = "trigger1"
|
||||
|
||||
command2, err = th.App.CreateCommand(command2)
|
||||
assert.Nil(t, err)
|
||||
|
||||
moveErr := th.App.MoveCommand(targetTeam, command2)
|
||||
assert.NotNil(t, moveErr)
|
||||
assert.Equal(t, "api.command.duplicate_trigger.app_error", moveErr.Id)
|
||||
}
|
||||
|
||||
func TestCreateCommandPost(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user