summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZ.J. van de Weg <git@zjvandeweg.nl>2017-01-11 09:04:49 -0500
committerZ.J. van de Weg <git@zjvandeweg.nl>2017-01-30 09:25:15 +0100
commit53846da2c7fe3879b4f26383d6367b0bb69e5dc8 (patch)
treef6c3d92ef6b46294ba7f882debc794604c73a493
parent746f47208dc52cd6ca68c0893de5513c250f524b (diff)
downloadgitlab-ce-53846da2c7fe3879b4f26383d6367b0bb69e5dc8.tar.gz
Add help command
-rw-r--r--lib/gitlab/chat_commands/command.rb13
-rw-r--r--lib/gitlab/chat_commands/help.rb28
-rw-r--r--lib/gitlab/chat_commands/presenters/help.rb20
3 files changed, 53 insertions, 8 deletions
diff --git a/lib/gitlab/chat_commands/command.rb b/lib/gitlab/chat_commands/command.rb
index ac7ee868402..4e5031a8a26 100644
--- a/lib/gitlab/chat_commands/command.rb
+++ b/lib/gitlab/chat_commands/command.rb
@@ -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)
diff --git a/lib/gitlab/chat_commands/help.rb b/lib/gitlab/chat_commands/help.rb
new file mode 100644
index 00000000000..e76733f5445
--- /dev/null
+++ b/lib/gitlab/chat_commands/help.rb
@@ -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
diff --git a/lib/gitlab/chat_commands/presenters/help.rb b/lib/gitlab/chat_commands/presenters/help.rb
new file mode 100644
index 00000000000..133b707231f
--- /dev/null
+++ b/lib/gitlab/chat_commands/presenters/help.rb
@@ -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