diff options
author | Rémy Coutable <remy@rymai.me> | 2016-08-10 17:51:01 +0200 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2016-08-13 00:06:12 +0200 |
commit | 65349c22129fcdf2ae0c7103094bbf50ae73db61 (patch) | |
tree | 5e2f6c4926a979d2a20665e4ce3e3453ee82ccb0 /lib/gitlab/slash_commands | |
parent | 23db6449542498636c145e83c71a4a466eb62746 (diff) | |
download | gitlab-ce-65349c22129fcdf2ae0c7103094bbf50ae73db61.tar.gz |
Make slash commands contextual
- Return only slash commands that make sense for the current noteable
- Allow slash commands decription to be dynamic
Other improvements:
- Add permission checks in slash commands definition
- Use IssuesFinder and MergeRequestsFinder
- Use next if instead of a unless block, and use splat operator instead of flatten
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'lib/gitlab/slash_commands')
-rw-r--r-- | lib/gitlab/slash_commands/dsl.rb | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/lib/gitlab/slash_commands/dsl.rb b/lib/gitlab/slash_commands/dsl.rb index edfe8405876..20e1d071d06 100644 --- a/lib/gitlab/slash_commands/dsl.rb +++ b/lib/gitlab/slash_commands/dsl.rb @@ -8,15 +8,25 @@ module Gitlab end module ClassMethods - def command_definitions - @command_definitions - end + def command_definitions(opts = {}) + @command_definitions.map do |cmd_def| + next if cmd_def[:cond_lambda] && !cmd_def[:cond_lambda].call(opts) + + cmd_def = cmd_def.dup - def command_names - command_definitions.flat_map do |command_definition| - unless command_definition[:noop] - [command_definition[:name], command_definition[:aliases]].flatten + if cmd_def[:description].present? && cmd_def[:description].respond_to?(:call) + cmd_def[:description] = cmd_def[:description].call(opts) rescue '' end + + cmd_def + end.compact + end + + def command_names(opts = {}) + command_definitions(opts).flat_map do |command_definition| + next if command_definition[:noop] + + [command_definition[:name], *command_definition[:aliases]] end.compact end @@ -35,6 +45,11 @@ module Gitlab @noop = noop end + # Allows to define if a lambda to conditionally return an action + def condition(cond_lambda) + @cond_lambda = cond_lambda + end + # Registers a new command which is recognizeable # from body of email or comment. # Example: @@ -53,6 +68,10 @@ module Gitlab define_method(proxy_method_name, &block) define_method(command_name) do |*args| + unless @cond_lambda.nil? || @cond_lambda.call(project: project, current_user: current_user, noteable: noteable) + return + end + proxy_method = method(proxy_method_name) if proxy_method.arity == -1 || proxy_method.arity == args.size @@ -70,13 +89,16 @@ module Gitlab name: command_name, aliases: aliases, description: @description || '', - params: @params || [], - noop: @noop || false + params: @params || [] } + command_definition[:noop] = @noop unless @noop.nil? + command_definition[:cond_lambda] = @cond_lambda unless @cond_lambda.nil? @command_definitions << command_definition @description = nil @params = nil + @noop = nil + @cond_lambda = nil end end end |