diff options
Diffstat (limited to 'lib/gitlab/quick_actions/work_item_actions.rb')
-rw-r--r-- | lib/gitlab/quick_actions/work_item_actions.rb | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/lib/gitlab/quick_actions/work_item_actions.rb b/lib/gitlab/quick_actions/work_item_actions.rb index 0a96d502862..2adee0f9a9a 100644 --- a/lib/gitlab/quick_actions/work_item_actions.rb +++ b/lib/gitlab/quick_actions/work_item_actions.rb @@ -27,6 +27,30 @@ module Gitlab command :promote_to do |type_name| @execution_message[:promote_to] = update_type(type_name, :promote_to) end + + desc { _('Change work item parent') } + explanation do |parent_param| + format(_("Change work item's parent to %{parent_ref}."), parent_ref: parent_param) + end + types WorkItem + params 'Parent #iid, reference or URL' + condition { supports_parent? && can_admin_link? } + command :set_parent do |parent_param| + @updates[:set_parent] = extract_work_items(parent_param).first + @execution_message[:set_parent] = success_msg[:set_parent] + end + + desc { _('Add children to work item') } + explanation do |child_param| + format(_("Add %{child_ref} to this work item as child(ren)."), child_ref: child_param) + end + types WorkItem + params 'Children #iids, references or URLs' + condition { supports_children? && can_admin_link? } + command :add_child do |child_param| + @updates[:add_child] = extract_work_items(child_param) + @execution_message[:add_child] = success_msg[:add_child] + end end private @@ -52,6 +76,18 @@ module Gitlab nil end + # rubocop: disable CodeReuse/ActiveRecord + def extract_work_items(params) + return if params.nil? + + issuable_type = params.include?('work_items') ? :work_item : :issue + issuables = extract_references(params, issuable_type) + return unless issuables + + WorkItem.find(issuables.pluck(:id)) + end + # rubocop: enable CodeReuse/ActiveRecord + def validate_promote_to(type) return error_msg(:not_found, action: 'promote') unless type && supports_promote_to?(type.name) return if current_user.can?(:"create_#{type.base_type}", quick_action_target) @@ -78,8 +114,8 @@ module Gitlab def error_msg(reason, action: 'convert') message = { not_found: 'Provided type is not supported', - same_type: 'Types are the same', - forbidden: 'You have insufficient permissions' + forbidden: 'You have insufficient permissions', + same_type: 'Types are the same' }.freeze format(_("Failed to %{action} this work item: %{reason}."), { action: action, reason: message[reason] }) @@ -88,9 +124,23 @@ module Gitlab def success_msg { type: _('Type changed successfully.'), - promote_to: _("Work item promoted successfully.") + promote_to: _("Work item promoted successfully."), + set_parent: _('Work item parent set successfully'), + add_child: _('Child work item(s) added successfully') } end + + def supports_parent? + ::WorkItems::HierarchyRestriction.find_by_child_type_id(quick_action_target.work_item_type_id).present? + end + + def supports_children? + ::WorkItems::HierarchyRestriction.find_by_parent_type_id(quick_action_target.work_item_type_id).present? + end + + def can_admin_link? + current_user.can?(:admin_issue_link, quick_action_target) + end end end end |