diff --git a/api4/command.go b/api4/command.go index 74b1c8a592..32d9fc28b9 100644 --- a/api4/command.go +++ b/api4/command.go @@ -382,7 +382,8 @@ func listCommandAutocompleteSuggestions(c *Context, w http.ResponseWriter, r *ht roleId = model.SYSTEM_ADMIN_ROLE_ID } - userInput := r.URL.Query().Get("user_input") + query := r.URL.Query() + userInput := query.Get("user_input") if userInput == "" { c.SetInvalidParam("userInput") return @@ -395,7 +396,19 @@ func listCommandAutocompleteSuggestions(c *Context, w http.ResponseWriter, r *ht return } - suggestions := c.App.GetSuggestions(commands, userInput, roleId) + commandArgs := &model.CommandArgs{ + ChannelId: query.Get("channel_id"), + TeamId: c.Params.TeamId, + RootId: query.Get("root_id"), + ParentId: query.Get("parent_id"), + UserId: c.App.Session().UserId, + T: c.App.T, + Session: *c.App.Session(), + SiteURL: c.GetSiteURLHeader(), + Command: userInput, + } + + suggestions := c.App.GetSuggestions(commandArgs, commands, roleId) w.Write(model.AutocompleteSuggestionsToJSON(suggestions)) } diff --git a/app/app_iface.go b/app/app_iface.go index 03214e602a..14b293501e 100644 --- a/app/app_iface.go +++ b/app/app_iface.go @@ -190,7 +190,7 @@ type AppIface interface { // based on the type of session (Mobile, SSO, Web/LDAP). GetSessionLengthInMillis(session *model.Session) int64 // GetSuggestions returns suggestions for user input. - GetSuggestions(commands []*model.Command, userInput, roleID string) []model.AutocompleteSuggestion + GetSuggestions(commandArgs *model.CommandArgs, commands []*model.Command, roleID string) []model.AutocompleteSuggestion // GetTeamGroupUsers returns the users who are associated to the team via GroupTeams and GroupMembers. GetTeamGroupUsers(teamID string) ([]*model.User, *model.AppError) // GetTeamSchemeChannelRoles Checks if a team has an override scheme and returns the scheme channel role names or default channel role names. diff --git a/app/command_autocomplete.go b/app/command_autocomplete.go index 3b480e8a79..80adbbef15 100644 --- a/app/command_autocomplete.go +++ b/app/command_autocomplete.go @@ -13,7 +13,7 @@ import ( ) // GetSuggestions returns suggestions for user input. -func (a *App) GetSuggestions(commands []*model.Command, userInput, roleID string) []model.AutocompleteSuggestion { +func (a *App) GetSuggestions(commandArgs *model.CommandArgs, commands []*model.Command, roleID string) []model.AutocompleteSuggestion { sort.Slice(commands, func(i, j int) bool { return strings.Compare(strings.ToLower(commands[i].Trigger), strings.ToLower(commands[j].Trigger)) < 0 }) @@ -26,7 +26,8 @@ func (a *App) GetSuggestions(commands []*model.Command, userInput, roleID string autocompleteData = append(autocompleteData, command.AutocompleteData) } - suggestions := a.getSuggestions(autocompleteData, "", userInput, roleID) + userInput := commandArgs.Command + suggestions := a.getSuggestions(commandArgs, autocompleteData, "", userInput, roleID) for i, suggestion := range suggestions { for _, command := range commands { if strings.HasPrefix(suggestion.Complete, command.Trigger) { @@ -35,12 +36,14 @@ func (a *App) GetSuggestions(commands []*model.Command, userInput, roleID string } } } + return suggestions } -func (a *App) getSuggestions(commands []*model.AutocompleteData, inputParsed, inputToBeParsed, roleID string) []model.AutocompleteSuggestion { +func (a *App) getSuggestions(commandArgs *model.CommandArgs, commands []*model.AutocompleteData, inputParsed, inputToBeParsed, roleID string) []model.AutocompleteSuggestion { suggestions := []model.AutocompleteSuggestion{} index := strings.Index(inputToBeParsed, " ") + if index == -1 { // no space in input for _, command := range commands { if strings.HasPrefix(command.Trigger, strings.ToLower(inputToBeParsed)) && (command.RoleID == roleID || roleID == model.SYSTEM_ADMIN_ROLE_ID || roleID == "") { @@ -55,6 +58,7 @@ func (a *App) getSuggestions(commands []*model.AutocompleteData, inputParsed, in } return suggestions } + for _, command := range commands { if command.Trigger != strings.ToLower(inputToBeParsed[:index]) { continue @@ -64,39 +68,44 @@ func (a *App) getSuggestions(commands []*model.AutocompleteData, inputParsed, in } toBeParsed := inputToBeParsed[index+1:] parsed := inputParsed + inputToBeParsed[:index+1] + if len(command.Arguments) == 0 { // Seek recursively in subcommands - subSuggestions := a.getSuggestions(command.SubCommands, parsed, toBeParsed, roleID) + subSuggestions := a.getSuggestions(commandArgs, command.SubCommands, parsed, toBeParsed, roleID) suggestions = append(suggestions, subSuggestions...) continue } - found, _, _, suggestion := a.parseArguments(command.Arguments, parsed, toBeParsed) + + found, _, _, suggestion := a.parseArguments(commandArgs, command.Arguments, parsed, toBeParsed) if found { suggestions = append(suggestions, suggestion...) } } + return suggestions } -func (a *App) parseArguments(args []*model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) { +func (a *App) parseArguments(commandArgs *model.CommandArgs, args []*model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) { if len(args) == 0 { return false, parsed, toBeParsed, suggestions } + if args[0].Required { - found, changedParsed, changedToBeParsed, suggestion := a.parseArgument(args[0], parsed, toBeParsed) + found, changedParsed, changedToBeParsed, suggestion := a.parseArgument(commandArgs, args[0], parsed, toBeParsed) if found { suggestions = append(suggestions, suggestion...) return true, changedParsed, changedToBeParsed, suggestions } - return a.parseArguments(args[1:], changedParsed, changedToBeParsed) + return a.parseArguments(commandArgs, args[1:], changedParsed, changedToBeParsed) } + // Handling optional arguments. Optional argument can be inputted or not, // so we have to pase both cases recursively and output combined suggestions. - foundWithOptional, changedParsedWithOptional, changedToBeParsedWithOptional, suggestionsWithOptional := a.parseArgument(args[0], parsed, toBeParsed) + foundWithOptional, changedParsedWithOptional, changedToBeParsedWithOptional, suggestionsWithOptional := a.parseArgument(commandArgs, args[0], parsed, toBeParsed) if foundWithOptional { suggestions = append(suggestions, suggestionsWithOptional...) } else { - foundWithOptionalRest, changedParsedWithOptionalRest, changedToBeParsedWithOptionalRest, suggestionsWithOptionalRest := a.parseArguments(args[1:], changedParsedWithOptional, changedToBeParsedWithOptional) + foundWithOptionalRest, changedParsedWithOptionalRest, changedToBeParsedWithOptionalRest, suggestionsWithOptionalRest := a.parseArguments(commandArgs, args[1:], changedParsedWithOptional, changedToBeParsedWithOptional) if foundWithOptionalRest { suggestions = append(suggestions, suggestionsWithOptionalRest...) } @@ -105,7 +114,7 @@ func (a *App) parseArguments(args []*model.AutocompleteArg, parsed, toBeParsed s changedToBeParsedWithOptional = changedToBeParsedWithOptionalRest } - foundWithoutOptional, changedParsedWithoutOptional, changedToBeParsedWithoutOptional, suggestionsWithoutOptional := a.parseArguments(args[1:], parsed, toBeParsed) + foundWithoutOptional, changedParsedWithoutOptional, changedToBeParsedWithoutOptional, suggestionsWithoutOptional := a.parseArguments(commandArgs, args[1:], parsed, toBeParsed) if foundWithoutOptional { suggestions = append(suggestions, suggestionsWithoutOptional...) } @@ -114,15 +123,17 @@ func (a *App) parseArguments(args []*model.AutocompleteArg, parsed, toBeParsed s if foundWithOptional || foundWithoutOptional { return true, parsed + toBeParsed, "", suggestions } + // no suggestions found yet, check if optional argument was inputted if changedParsedWithOptional != parsed && changedToBeParsedWithOptional != toBeParsed { return false, changedParsedWithOptional, changedToBeParsedWithOptional, suggestions } + // no suggestions and optional argument was not inputted return foundWithoutOptional, changedParsedWithoutOptional, changedToBeParsedWithoutOptional, suggestions } -func (a *App) parseArgument(arg *model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) { +func (a *App) parseArgument(commandArgs *model.CommandArgs, arg *model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) { if arg.Name != "" { //Parse the --name first found, changedParsed, changedToBeParsed, suggestion := parseNamedArgument(arg, parsed, toBeParsed) if found { @@ -138,6 +149,7 @@ func (a *App) parseArgument(arg *model.AutocompleteArg, parsed, toBeParsed strin parsed = changedParsed toBeParsed = changedToBeParsed } + if arg.Type == model.AutocompleteArgTypeText { found, changedParsed, changedToBeParsed, suggestion := parseInputTextArgument(arg, parsed, toBeParsed) if found { @@ -147,22 +159,23 @@ func (a *App) parseArgument(arg *model.AutocompleteArg, parsed, toBeParsed strin parsed = changedParsed toBeParsed = changedToBeParsed } else if arg.Type == model.AutocompleteArgTypeStaticList { - found, changedParsed, changedToBeParsed, staticListsuggestions := parseStaticListArgument(arg, parsed, toBeParsed) + found, changedParsed, changedToBeParsed, staticListSuggestions := parseStaticListArgument(arg, parsed, toBeParsed) if found { - suggestions = append(suggestions, staticListsuggestions...) + suggestions = append(suggestions, staticListSuggestions...) return true, changedParsed, changedToBeParsed, suggestions } parsed = changedParsed toBeParsed = changedToBeParsed } else if arg.Type == model.AutocompleteArgTypeDynamicList { - found, changedParsed, changedToBeParsed, dynamicListsuggestions := a.getDynamicListArgument(arg, parsed, toBeParsed) + found, changedParsed, changedToBeParsed, dynamicListSuggestions := a.getDynamicListArgument(commandArgs, arg, parsed, toBeParsed) if found { - suggestions = append(suggestions, dynamicListsuggestions...) + suggestions = append(suggestions, dynamicListSuggestions...) return true, changedParsed, changedToBeParsed, suggestions } parsed = changedParsed toBeParsed = changedToBeParsed } + return false, parsed, toBeParsed, suggestions } @@ -217,17 +230,39 @@ func parseStaticListArgument(arg *model.AutocompleteArg, parsed, toBeParsed stri return parseListItems(a.PossibleArguments, parsed, toBeParsed) } -func (a *App) getDynamicListArgument(arg *model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) { +func (a *App) getDynamicListArgument(commandArgs *model.CommandArgs, arg *model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) { dynamicArg := arg.Data.(*model.AutocompleteDynamicListArg) + params := url.Values{} params.Add("user_input", parsed+toBeParsed) params.Add("parsed", parsed) + + // Encode the information normally provided to a plugin slash command handler into the request parameters + // Encode PluginContext: + pluginContext := a.PluginContext() + params.Add("request_id", pluginContext.RequestId) + params.Add("session_id", pluginContext.SessionId) + params.Add("ip_address", pluginContext.IpAddress) + params.Add("accept_language", pluginContext.AcceptLanguage) + params.Add("user_agent", pluginContext.UserAgent) + + // Encode CommandArgs: + params.Add("channel_id", commandArgs.ChannelId) + params.Add("team_id", commandArgs.TeamId) + params.Add("root_id", commandArgs.RootId) + params.Add("parent_id", commandArgs.ParentId) + params.Add("user_id", commandArgs.UserId) + params.Add("site_url", commandArgs.SiteURL) + resp, err := a.doPluginRequest("GET", dynamicArg.FetchURL, params, nil) + if err != nil { a.Log().Error("Can't fetch dynamic list arguments for", mlog.String("url", dynamicArg.FetchURL), mlog.Err(err)) return false, parsed, toBeParsed, []model.AutocompleteSuggestion{} } + listItems := model.AutocompleteStaticListItemsFromJSON(resp.Body) + return parseListItems(listItems, parsed, toBeParsed) } diff --git a/app/command_autocomplete_test.go b/app/command_autocomplete_test.go index 81a97ede26..36e353732b 100644 --- a/app/command_autocomplete_test.go +++ b/app/command_autocomplete_test.go @@ -202,22 +202,23 @@ func TestSuggestions(t *testing.T) { defer th.TearDown() jira := createJiraAutocompleteData() + emptyCmdArgs := &model.CommandArgs{} - suggestions := th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "ji", model.SYSTEM_ADMIN_ROLE_ID) + suggestions := th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "ji", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, jira.Trigger, suggestions[0].Complete) assert.Equal(t, jira.Trigger, suggestions[0].Suggestion) assert.Equal(t, "[command]", suggestions[0].Hint) assert.Equal(t, jira.HelpText, suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira crea", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira crea", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "jira create", suggestions[0].Complete) assert.Equal(t, "create", suggestions[0].Suggestion) assert.Equal(t, "[issue text]", suggestions[0].Hint) assert.Equal(t, "Create a new Issue", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira c", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira c", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 2) assert.Equal(t, "jira create", suggestions[1].Complete) assert.Equal(t, "create", suggestions[1].Suggestion) @@ -228,27 +229,27 @@ func TestSuggestions(t *testing.T) { assert.Equal(t, "[url]", suggestions[0].Hint) assert.Equal(t, "Connect your Mattermost account to your Jira account", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira create ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira create ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "jira create ", suggestions[0].Complete) assert.Equal(t, "", suggestions[0].Suggestion) assert.Equal(t, "[text]", suggestions[0].Hint) assert.Equal(t, "This text is optional, will be inserted into the description field", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira create some", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira create some", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "jira create some", suggestions[0].Complete) assert.Equal(t, "", suggestions[0].Suggestion) assert.Equal(t, "[text]", suggestions[0].Hint) assert.Equal(t, "This text is optional, will be inserted into the description field", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira create some text ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira create some text ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 0) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "invalid command", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "invalid command", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 0) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira settings notifications o", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira settings notifications o", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 2) assert.Equal(t, "jira settings notifications On", suggestions[0].Complete) assert.Equal(t, "On", suggestions[0].Suggestion) @@ -259,48 +260,48 @@ func TestSuggestions(t *testing.T) { assert.Equal(t, "Turn notifications off", suggestions[1].Hint) assert.Equal(t, "", suggestions[1].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 11) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira ", model.SYSTEM_USER_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira ", model.SYSTEM_USER_ROLE_ID) assert.Len(t, suggestions, 9) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira create \"some issue text", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira create \"some issue text", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "jira create \"some issue text", suggestions[0].Complete) assert.Equal(t, "", suggestions[0].Suggestion) assert.Equal(t, "[text]", suggestions[0].Hint) assert.Equal(t, "This text is optional, will be inserted into the description field", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira timezone ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira timezone ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "jira timezone --zone ", suggestions[0].Complete) assert.Equal(t, "--zone", suggestions[0].Suggestion) assert.Equal(t, "", suggestions[0].Hint) assert.Equal(t, "Set timezone", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira timezone --", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira timezone --", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "jira timezone --zone ", suggestions[0].Complete) assert.Equal(t, "--zone", suggestions[0].Suggestion) assert.Equal(t, "", suggestions[0].Hint) assert.Equal(t, "Set timezone", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira timezone --zone ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira timezone --zone ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "jira timezone --zone ", suggestions[0].Complete) assert.Equal(t, "", suggestions[0].Suggestion) assert.Equal(t, "[UTC+07:00]", suggestions[0].Hint) assert.Equal(t, "Set timezone", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira timezone --zone bla", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira timezone --zone bla", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "jira timezone --zone bla", suggestions[0].Complete) assert.Equal(t, "", suggestions[0].Suggestion) assert.Equal(t, "[UTC+07:00]", suggestions[0].Hint) assert.Equal(t, "Set timezone", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{jira}, "", "jira timezone bla", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{jira}, "", "jira timezone bla", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 0) commandA := &model.Command{ @@ -315,7 +316,7 @@ func TestSuggestions(t *testing.T) { Trigger: "charles", AutocompleteData: model.NewAutocompleteData("charles", "", ""), } - suggestions = th.App.GetSuggestions([]*model.Command{commandB, commandC, commandA}, "", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.GetSuggestions(emptyCmdArgs, []*model.Command{commandB, commandC, commandA}, model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 3) assert.Equal(t, "alice", suggestions[0].Complete) assert.Equal(t, "bob", suggestions[1].Complete) @@ -327,15 +328,16 @@ func TestCommandWithOptionalArgs(t *testing.T) { defer th.TearDown() command := createCommandWithOptionalArgs() + emptyCmdArgs := &model.CommandArgs{} - suggestions := th.App.getSuggestions([]*model.AutocompleteData{command}, "", "comm", model.SYSTEM_ADMIN_ROLE_ID) + suggestions := th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "comm", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, command.Trigger, suggestions[0].Complete) assert.Equal(t, command.Trigger, suggestions[0].Suggestion) assert.Equal(t, "", suggestions[0].Hint) assert.Equal(t, command.HelpText, suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 4) assert.Equal(t, "command subcommand1", suggestions[0].Complete) assert.Equal(t, "subcommand1", suggestions[0].Suggestion) @@ -350,7 +352,7 @@ func TestCommandWithOptionalArgs(t *testing.T) { assert.Equal(t, "", suggestions[2].Hint) assert.Equal(t, "", suggestions[2].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand1 ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand1 ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 2) assert.Equal(t, "command subcommand1 item1", suggestions[0].Complete) assert.Equal(t, "item1", suggestions[0].Suggestion) @@ -361,21 +363,21 @@ func TestCommandWithOptionalArgs(t *testing.T) { assert.Equal(t, "", suggestions[1].Hint) assert.Equal(t, "", suggestions[1].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand1 item1 ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand1 item1 ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "command subcommand1 item1 --name2 ", suggestions[0].Complete) assert.Equal(t, "--name2", suggestions[0].Suggestion) assert.Equal(t, "", suggestions[0].Hint) assert.Equal(t, "arg2", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand1 item1 --name2 bla", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand1 item1 --name2 bla", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "command subcommand1 item1 --name2 bla", suggestions[0].Complete) assert.Equal(t, "", suggestions[0].Suggestion) assert.Equal(t, "", suggestions[0].Hint) assert.Equal(t, "arg2", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand2 ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand2 ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 2) assert.Equal(t, "command subcommand2 --name1 ", suggestions[0].Complete) assert.Equal(t, "--name1", suggestions[0].Suggestion) @@ -386,7 +388,7 @@ func TestCommandWithOptionalArgs(t *testing.T) { assert.Equal(t, "", suggestions[1].Hint) assert.Equal(t, "arg2", suggestions[1].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand2 -", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand2 -", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 2) assert.Equal(t, "command subcommand2 --name1 ", suggestions[0].Complete) assert.Equal(t, "--name1", suggestions[0].Suggestion) @@ -397,7 +399,7 @@ func TestCommandWithOptionalArgs(t *testing.T) { assert.Equal(t, "", suggestions[1].Hint) assert.Equal(t, "arg2", suggestions[1].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand2 --name1 ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand2 --name1 ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 3) assert.Equal(t, "command subcommand2 --name1 item1", suggestions[0].Complete) assert.Equal(t, "item1", suggestions[0].Suggestion) @@ -412,7 +414,7 @@ func TestCommandWithOptionalArgs(t *testing.T) { assert.Equal(t, "", suggestions[2].Hint) assert.Equal(t, "arg3", suggestions[2].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand2 --name1 item", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand2 --name1 item", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 3) assert.Equal(t, "command subcommand2 --name1 item1", suggestions[0].Complete) assert.Equal(t, "item1", suggestions[0].Suggestion) @@ -427,24 +429,24 @@ func TestCommandWithOptionalArgs(t *testing.T) { assert.Equal(t, "", suggestions[2].Hint) assert.Equal(t, "arg3", suggestions[2].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand2 --name1 item1 ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand2 --name1 item1 ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "command subcommand2 --name1 item1 ", suggestions[0].Complete) assert.Equal(t, "", suggestions[0].Suggestion) assert.Equal(t, "", suggestions[0].Hint) assert.Equal(t, "arg2", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand2 --name1 item1 bla ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand2 --name1 item1 bla ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "command subcommand2 --name1 item1 bla ", suggestions[0].Complete) assert.Equal(t, "", suggestions[0].Suggestion) assert.Equal(t, "", suggestions[0].Hint) assert.Equal(t, "arg3", suggestions[0].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand2 --name1 item1 bla bla ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand2 --name1 item1 bla bla ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 0) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand3 ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand3 ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 3) assert.Equal(t, "command subcommand3 --name1 ", suggestions[0].Complete) assert.Equal(t, "--name1", suggestions[0].Suggestion) @@ -459,7 +461,7 @@ func TestCommandWithOptionalArgs(t *testing.T) { assert.Equal(t, "", suggestions[2].Hint) assert.Equal(t, "arg3", suggestions[2].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand3 --name", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand3 --name", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 3) assert.Equal(t, "command subcommand3 --name1 ", suggestions[0].Complete) assert.Equal(t, "--name1", suggestions[0].Suggestion) @@ -474,7 +476,7 @@ func TestCommandWithOptionalArgs(t *testing.T) { assert.Equal(t, "", suggestions[2].Hint) assert.Equal(t, "arg3", suggestions[2].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand3 --name1 ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand3 --name1 ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 2) assert.Equal(t, "command subcommand3 --name1 item1", suggestions[0].Complete) assert.Equal(t, "item1", suggestions[0].Suggestion) @@ -485,7 +487,7 @@ func TestCommandWithOptionalArgs(t *testing.T) { assert.Equal(t, "", suggestions[1].Hint) assert.Equal(t, "", suggestions[1].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand4 ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand4 ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 2) assert.Equal(t, "command subcommand4 item1", suggestions[0].Complete) assert.Equal(t, "item1", suggestions[0].Suggestion) @@ -496,7 +498,7 @@ func TestCommandWithOptionalArgs(t *testing.T) { assert.Equal(t, "message", suggestions[1].Hint) assert.Equal(t, "help4", suggestions[1].Description) - suggestions = th.App.getSuggestions([]*model.AutocompleteData{command}, "", "command subcommand4 item1 ", model.SYSTEM_ADMIN_ROLE_ID) + suggestions = th.App.getSuggestions(emptyCmdArgs, []*model.AutocompleteData{command}, "", "command subcommand4 item1 ", model.SYSTEM_ADMIN_ROLE_ID) assert.Len(t, suggestions, 1) assert.Equal(t, "command subcommand4 item1 ", suggestions[0].Complete) assert.Equal(t, "", suggestions[0].Suggestion) diff --git a/app/opentracing_layer.go b/app/opentracing_layer.go index 5c53dc904d..f63c908d69 100644 --- a/app/opentracing_layer.go +++ b/app/opentracing_layer.go @@ -7617,7 +7617,7 @@ func (a *OpenTracingAppLayer) GetStatusesByIds(userIds []string) (map[string]int return resultVar0, resultVar1 } -func (a *OpenTracingAppLayer) GetSuggestions(commands []*model.Command, userInput string, roleID string) []model.AutocompleteSuggestion { +func (a *OpenTracingAppLayer) GetSuggestions(commandArgs *model.CommandArgs, commands []*model.Command, roleID string) []model.AutocompleteSuggestion { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetSuggestions") @@ -7629,7 +7629,7 @@ func (a *OpenTracingAppLayer) GetSuggestions(commands []*model.Command, userInpu }() defer span.Finish() - resultVar0 := a.app.GetSuggestions(commands, userInput, roleID) + resultVar0 := a.app.GetSuggestions(commandArgs, commands, roleID) return resultVar0 }