From 223b87d7b1eccfc844631118e454c69a6312004d Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Tue, 6 Jun 2017 11:49:36 +0300 Subject: [PATCH] Backport Fix '/unassign' slash command --- .../slash_commands/interpret_service.rb | 24 ++++++++++++------- .../slash_commands/command_definition.rb | 10 ++++++-- lib/gitlab/slash_commands/dsl.rb | 4 ++-- .../issuable/bulk_update_service_spec.rb | 2 +- .../slash_commands/interpret_service_spec.rb | 2 +- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/services/slash_commands/interpret_service.rb b/app/services/slash_commands/interpret_service.rb index a7e13648b54..b6b411d2185 100644 --- a/app/services/slash_commands/interpret_service.rb +++ b/app/services/slash_commands/interpret_service.rb @@ -92,26 +92,20 @@ module SlashCommands desc 'Assign' explanation do |users| - "Assigns #{users.map(&:to_reference).to_sentence}." if users.any? + "Assigns #{users.first.to_reference}." if users.any? end params '@user' condition do current_user.can?(:"admin_#{issuable.to_ability_name}", project) end parse_params do |assignee_param| - users = extract_references(assignee_param, :user) - - if users.empty? - users = User.where(username: assignee_param.split(' ').map(&:strip)) - end - - users + extract_users(assignee_param) end command :assign do |users| next if users.empty? if issuable.is_a?(Issue) - @updates[:assignee_ids] = users.map(&:id) + @updates[:assignee_ids] = [users.last.id] else @updates[:assignee_id] = users.last.id end @@ -459,6 +453,18 @@ module SlashCommands end end + def extract_users(params) + return [] if params.nil? + + users = extract_references(params, :user) + + if users.empty? + users = User.where(username: params.split(' ').map(&:strip)) + end + + users + end + def find_labels(labels_param) extract_references(labels_param, :label) | LabelsFinder.new(current_user, project_id: project.id, name: labels_param.split).execute diff --git a/lib/gitlab/slash_commands/command_definition.rb b/lib/gitlab/slash_commands/command_definition.rb index 12a385f90fd..caab8856014 100644 --- a/lib/gitlab/slash_commands/command_definition.rb +++ b/lib/gitlab/slash_commands/command_definition.rb @@ -48,17 +48,23 @@ module Gitlab end def to_h(opts) + context = OpenStruct.new(opts) + desc = description if desc.respond_to?(:call) - context = OpenStruct.new(opts) desc = context.instance_exec(&desc) rescue '' end + prms = params + if prms.respond_to?(:call) + prms = Array(context.instance_exec(&prms)) rescue params + end + { name: name, aliases: aliases, description: desc, - params: params + params: prms } end diff --git a/lib/gitlab/slash_commands/dsl.rb b/lib/gitlab/slash_commands/dsl.rb index 614bafbe1b2..1b5b4566d81 100644 --- a/lib/gitlab/slash_commands/dsl.rb +++ b/lib/gitlab/slash_commands/dsl.rb @@ -40,8 +40,8 @@ module Gitlab # command :command_key do |arguments| # # Awesome code block # end - def params(*params) - @params = params + def params(*params, &block) + @params = block_given? ? block : params end # Allows to give an explanation of what the command will do when diff --git a/spec/services/issuable/bulk_update_service_spec.rb b/spec/services/issuable/bulk_update_service_spec.rb index 6437d00e451..eb9b1670c71 100644 --- a/spec/services/issuable/bulk_update_service_spec.rb +++ b/spec/services/issuable/bulk_update_service_spec.rb @@ -72,7 +72,7 @@ describe Issuable::BulkUpdateService, services: true do end context "when the new assignee ID is #{IssuableFinder::NONE}" do - it "unassigns the issues" do + it 'unassigns the issues' do expect { bulk_update(merge_request, assignee_id: IssuableFinder::NONE) } .to change { merge_request.reload.assignee }.to(nil) end diff --git a/spec/services/slash_commands/interpret_service_spec.rb b/spec/services/slash_commands/interpret_service_spec.rb index e5e400ee281..4db491fd5f3 100644 --- a/spec/services/slash_commands/interpret_service_spec.rb +++ b/spec/services/slash_commands/interpret_service_spec.rb @@ -384,7 +384,7 @@ describe SlashCommands::InterpretService, services: true do it 'fetches assignee and populates assignee_id if content contains /assign' do _, updates = service.execute(content, issue) - expect(updates[:assignee_ids]).to match_array([developer.id, developer2.id]) + expect(updates[:assignee_ids]).to match_array([developer.id]) end end