From 2703330a19e813351e9c33241a59d6b7f54741df Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Thu, 18 Aug 2016 14:21:52 -0500 Subject: Fix behavior around commands with optional arguments --- lib/gitlab/slash_commands/command_definition.rb | 11 ++++++----- lib/gitlab/slash_commands/extractor.rb | 13 +++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/slash_commands/command_definition.rb b/lib/gitlab/slash_commands/command_definition.rb index 2ff8f4eddf0..60d35be2599 100644 --- a/lib/gitlab/slash_commands/command_definition.rb +++ b/lib/gitlab/slash_commands/command_definition.rb @@ -28,13 +28,14 @@ module Gitlab context.instance_exec(&condition_block) end - def execute(context, opts, args) + def execute(context, opts, arg) return if noop? || !available?(opts) - block_arity = action_block.arity - return unless (args.present? && block_arity == 1) || (args.blank? && block_arity <= 0) - - context.instance_exec(args, &action_block) + if arg.present? + context.instance_exec(arg, &action_block) + elsif action_block.arity == 0 + context.instance_exec(&action_block) + end end def to_h(opts) diff --git a/lib/gitlab/slash_commands/extractor.rb b/lib/gitlab/slash_commands/extractor.rb index c790b825347..a672e5e4855 100644 --- a/lib/gitlab/slash_commands/extractor.rb +++ b/lib/gitlab/slash_commands/extractor.rb @@ -39,7 +39,7 @@ module Gitlab content.delete!("\r") content.gsub!(commands_regex(opts)) do if $~[:cmd] - commands << [$~[:cmd], $~[:args]].reject(&:blank?) + commands << [$~[:cmd], $~[:arg]].reject(&:blank?) '' else $~[0] @@ -50,13 +50,14 @@ module Gitlab end private + # Builds a regular expression to match known commands. # First match group captures the command name and # second match group captures its arguments. # # It looks something like: # - # /^\/(?close|reopen|...)(?:( |$))(?[^\/\n]*)(?:\n|$)/ + # /^\/(?close|reopen|...)(?:( |$))(?[^\/\n]*)(?:\n|$)/ def commands_regex(opts) names = command_names(opts).map(&:to_s) @@ -64,7 +65,7 @@ module Gitlab (? # Code blocks: # ``` - # Anything, including `/cmd args` which are ignored by this filter + # Anything, including `/cmd arg` which are ignored by this filter # ``` ^``` @@ -75,7 +76,7 @@ module Gitlab (? # HTML block: # - # Anything, including `/cmd args` which are ignored by this filter + # Anything, including `/cmd arg` which are ignored by this filter # ^<[^>]+?>\n @@ -86,7 +87,7 @@ module Gitlab (? # Quote block: # >>> - # Anything, including `/cmd args` which are ignored by this filter + # Anything, including `/cmd arg` which are ignored by this filter # >>> ^>>> @@ -102,7 +103,7 @@ module Gitlab (?#{Regexp.union(names)}) (?: [ ] - (?[^\/\n]*) + (?[^\/\n]*) )? (?:\n|$) ) -- cgit v1.2.1