From 132cd0092d6aea87359a9a0627ad2c53c4a91837 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Wed, 14 Jun 2017 22:08:24 +0200 Subject: [PATCH] Backport issuable for assignee slash commands from EE Avoid conflicts when merge CE to EE by backporting code from EE. Instead of checking in `SlashCommands::InterpretService` what the issuable the type of the issuable is, ask the issuable if it is capable to do those thing and implement it in the issuable itself. The issuable will check if it's possible and if the licensed feature is available. This should also make it easier to ever add multiple assignees to MergeRequests. --- app/models/concerns/issuable.rb | 12 ++++++ .../quick_actions/interpret_service.rb | 38 +++++++++++++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index 8e367576c9d..0a476efdaa9 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -102,6 +102,18 @@ module Issuable def locking_enabled? title_changed? || description_changed? end + + def allows_multiple_assignees? + false + end + + def has_multiple_assignees? + supports_multiple_assignees? && assignees.count > 1 + end + + def supports_multiple_assignees? + respond_to?(:assignee_ids) + end end module ClassMethods diff --git a/app/services/quick_actions/interpret_service.rb b/app/services/quick_actions/interpret_service.rb index 6816b137361..8adfd939c2e 100644 --- a/app/services/quick_actions/interpret_service.rb +++ b/app/services/quick_actions/interpret_service.rb @@ -92,9 +92,12 @@ module QuickActions desc 'Assign' explanation do |users| - "Assigns #{users.first.to_reference}." if users.any? + users = issuable.allows_multiple_assignees? ? users : users.take(1) + "Assigns #{users.map(&:to_reference).to_sentence}." + end + params do + issuable.allows_multiple_assignees? ? '@user1 @user2' : '@user' end - params '@user' condition do current_user.can?(:"admin_#{issuable.to_ability_name}", project) end @@ -104,25 +107,44 @@ module QuickActions command :assign do |users| next if users.empty? - if issuable.is_a?(Issue) + if issuable.allows_multiple_assignees? + @updates[:assignee_ids] = issuable.assignees.pluck(:id) + users.map(&:id) + elsif issuable.supports_multiple_assignees? @updates[:assignee_ids] = [users.last.id] else @updates[:assignee_id] = users.last.id end end - desc 'Remove assignee' + desc do + if issuable.allows_multiple_assignees? + 'Remove all or specific assignee(s)' + else + 'Remove assignee' + end + end explanation do - "Removes assignee #{issuable.assignees.first.to_reference}." + "Removes #{'assignee'.pluralize(issuable.assignees.size)} #{issuable.assignees.map(&:to_reference).to_sentence}." + end + params do + issuable.allows_multiple_assignees? ? '@user1 @user2' : '' end condition do issuable.persisted? && issuable.assignees.any? && current_user.can?(:"admin_#{issuable.to_ability_name}", project) end - command :unassign do - if issuable.is_a?(Issue) - @updates[:assignee_ids] = [] + command :unassign do |unassign_param = nil| + # When multiple users are assigned, all will be unassigned if multiple assignees are no longer allowed + users = extract_users(unassign_param) if issuable.allows_multiple_assignees? + + if issuable.supports_multiple_assignees? + @updates[:assignee_ids] = + if users&.any? + issuable.assignees.pluck(:id) - users.map(&:id) + else + [] + end else @updates[:assignee_id] = nil end