MM-12356 Add cli command "command delete" (#9553)
* Add cli command for deleting commands * Add code/test for delete command * Fix test * Add confirm flag * Update as per comments * Uncomment test * Fix test
Этот коммит содержится в:
коммит произвёл
George Goldberg
родитель
6232ff3738
Коммит
c317d6f265
@@ -423,10 +423,9 @@ func (a *App) DeleteCommand(commandId string) *model.AppError {
|
||||
if !*a.Config().ServiceSettings.EnableCommands {
|
||||
return model.NewAppError("DeleteCommand", "api.command.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
if err := (<-a.Srv.Store.Command().Delete(commandId, model.GetMillis())).Err; err != nil {
|
||||
return err
|
||||
result := <-a.Srv.Store.Command().Delete(commandId, model.GetMillis())
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -44,6 +44,15 @@ var CommandListCmd = &cobra.Command{
|
||||
RunE: listCommandCmdF,
|
||||
}
|
||||
|
||||
var CommandDeleteCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Short: "Delete a slash command",
|
||||
Long: `Delete a slash command. Commands can be specified by command ID.`,
|
||||
Example: ` command delete commandID`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: deleteCommandCmdF,
|
||||
}
|
||||
|
||||
func init() {
|
||||
CommandCreateCmd.Flags().String("title", "", "Command Title")
|
||||
CommandCreateCmd.Flags().String("description", "", "Command Description")
|
||||
@@ -64,6 +73,7 @@ func init() {
|
||||
CommandCreateCmd,
|
||||
CommandMoveCmd,
|
||||
CommandListCmd,
|
||||
CommandDeleteCmd,
|
||||
)
|
||||
RootCmd.AddCommand(CommandCmd)
|
||||
}
|
||||
@@ -204,3 +214,21 @@ func listCommandCmdF(command *cobra.Command, args []string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteCommandCmdF(command *cobra.Command, args []string) error {
|
||||
a, err := InitDBCommandContextCobra(command)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer a.Shutdown()
|
||||
|
||||
commandID := args[0]
|
||||
|
||||
deleteErr := a.DeleteCommand(commandID)
|
||||
if deleteErr != nil {
|
||||
CommandPrintErrorln("Unable to delete command '" + commandID + "' error: " + deleteErr.Error())
|
||||
return deleteErr
|
||||
}
|
||||
CommandPrettyPrintln("Deleted command '" + commandID + "'")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/api4"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -126,3 +127,34 @@ func TestCreateCommand(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteCommand(t *testing.T) {
|
||||
th := api4.Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
url := "http://localhost:8000/test-command"
|
||||
team := th.BasicTeam
|
||||
user := th.BasicUser
|
||||
th.LinkUserToTeam(user, team)
|
||||
|
||||
// Check the appropriate permissions are enforced.
|
||||
defaultRolePermissions := th.SaveDefaultRolePermissions()
|
||||
defer func() {
|
||||
th.RestoreDefaultRolePermissions(defaultRolePermissions)
|
||||
}()
|
||||
id := model.NewId()
|
||||
c := &model.Command{
|
||||
DisplayName: "dn_" + id,
|
||||
Method: "G",
|
||||
TeamId: team.Id,
|
||||
Username: user.Username,
|
||||
URL: url,
|
||||
Trigger: "test",
|
||||
}
|
||||
th.AddPermissionToRole(model.PERMISSION_MANAGE_SLASH_COMMANDS.Id, model.TEAM_USER_ROLE_ID)
|
||||
command, _ := th.Client.CreateCommand(c)
|
||||
commands, _ := th.Client.ListCommands(team.Id, true)
|
||||
assert.Equal(t, len(commands), 1)
|
||||
CheckCommand(t, "command", "delete", command.Id)
|
||||
commands, _ = th.Client.ListCommands(team.Id, true)
|
||||
assert.Equal(t, len(commands), 0)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user