From e96ddef3deaac499bcdd67800839e59f46ae67b5 Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Wed, 11 Jan 2017 09:04:49 -0500 Subject: Add help command --- lib/gitlab/chat_commands/command.rb | 13 +++++-------- lib/gitlab/chat_commands/help.rb | 28 ++++++++++++++++++++++++++++ lib/gitlab/chat_commands/presenters/help.rb | 20 ++++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 lib/gitlab/chat_commands/help.rb create mode 100644 lib/gitlab/chat_commands/presenters/help.rb 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 -- cgit v1.2.1