Add help command

This commit is contained in:
Z.J. van de Weg 2017-01-11 09:04:49 -05:00
parent 746f47208d
commit 53846da2c7
3 changed files with 53 additions and 8 deletions

View File

@ -18,25 +18,22 @@ module Gitlab
Gitlab::ChatCommands::Presenters::Access.new.access_denied
end
else
help(help_messages)
Gitlab::ChatCommands::Help.new(project, current_user, params).execute(available_commands)
end
end
def match_command
match = nil
service = available_commands.find do |klass|
match = klass.match(params[:text])
end
service =
available_commands.find do |klass|
match = klass.match(params[:text])
end
[service, match]
end
private
def help_messages
available_commands.map(&:help_message)
end
def available_commands
COMMANDS.select do |klass|
klass.available?(project)

View File

@ -0,0 +1,28 @@
module Gitlab
module ChatCommands
class Help < BaseCommand
# This class has to be used last, as it always matches. It has to match
# because other commands were not triggered and we want to show the help
# command
def self.match(_text)
true
end
def self.help_message
'help'
end
def self.allowed?(_project, _user)
true
end
def execute(commands)
Gitlab::ChatCommands::Presenters::Help.new(commands).present(trigger)
end
def trigger
params[:command]
end
end
end
end

View File

@ -0,0 +1,20 @@
module Gitlab::ChatCommands::Presenters
class Help < Gitlab::ChatCommands::Presenters::Base
def present(trigger)
message =
if @resource.none?
"No commands available :thinking_face:"
else
header_with_list("Available commands", full_commands(trigger))
end
ephemeral_response(text: message)
end
private
def full_commands(trigger)
@resource.map { |command| "#{trigger} #{command.help_message}" }
end
end
end