diff --git a/app/models/work_items/system_defined/hierarchy_restriction.rb b/app/models/work_items/system_defined/hierarchy_restriction.rb index f46161bf047b6b6f6ca1b0988ec3392c0f99218c..28f1afc37482fe8f3ba6b5551db3db2d70247004 100644 --- a/app/models/work_items/system_defined/hierarchy_restriction.rb +++ b/app/models/work_items/system_defined/hierarchy_restriction.rb @@ -17,6 +17,16 @@ class HierarchyRestriction INCIDENT_ID = ::WorkItems::Type::BASE_TYPES[:incident][:id] TICKET_ID = ::WorkItems::Type::BASE_TYPES[:ticket][:id] + class << self + def with_parent_type_id(parent_type_id) + where(parent_type_id: parent_type_id) + end + + def hierarchy_relationship_allowed?(parent_type_id:, child_type_id:) + where(parent_type_id: parent_type_id, child_type_id: child_type_id).present? + end + end + ITEMS = [ { id: 1, diff --git a/lib/gitlab/quick_actions/work_item_actions.rb b/lib/gitlab/quick_actions/work_item_actions.rb index aed024eccb5da821b2cad82db13f1a003cc07dbc..dc5f89df35cf8a6f470a9cce53dc1a45fb83f026 100644 --- a/lib/gitlab/quick_actions/work_item_actions.rb +++ b/lib/gitlab/quick_actions/work_item_actions.rb @@ -176,7 +176,9 @@ def work_item_parent end def supports_children? - ::WorkItems::HierarchyRestriction.find_by_parent_type_id(quick_action_target.work_item_type_id).present? + ::WorkItems::SystemDefined::HierarchyRestriction + .with_parent_type_id(quick_action_target.work_item_type_id) + .present? end def has_children? @@ -202,7 +204,7 @@ def apply_type_commands(new_type, command) # rubocop:enable Gitlab/ModuleWithInstanceVariables # overridden in EE - def handle_set_epic(parent_param); end + def handle_set_epic(_); end # rubocop:disable Gitlab/ModuleWithInstanceVariables -- @updates is already defined and part of # Gitlab::QuickActions::Dsl implementation @@ -265,10 +267,10 @@ def fetch_child_work_item(child) end def hierarchy_relationship_allowed?(parent, child_work_item) - ::WorkItems::HierarchyRestriction.find_by_parent_type_id_and_child_type_id( - parent.work_item_type_id, - child_work_item.work_item_type_id - ).present? + ::WorkItems::SystemDefined::HierarchyRestriction.hierarchy_relationship_allowed?( + parent_type_id: parent.work_item_type_id, + child_type_id: child_work_item.work_item_type_id + ) end end end diff --git a/spec/models/work_items/system_defined/hierarchy_restriction_spec.rb b/spec/models/work_items/system_defined/hierarchy_restriction_spec.rb index 6d4f443e6e72cb1be58d13bae99a64ba99c46de0..1f14070380eb25c87fe6cc5de65cfd0f09cd356f 100644 --- a/spec/models/work_items/system_defined/hierarchy_restriction_spec.rb +++ b/spec/models/work_items/system_defined/hierarchy_restriction_spec.rb @@ -88,4 +88,31 @@ expect(restriction.maximum_depth).to eq(1) end end + + describe '.with_parent_type_id' do + it 'returns hierarchy restrictions with the specified parent type' do + restrictions = described_class.with_parent_type_id(described_class::EPIC_ID) + expect(restrictions.map(&:parent_type_id).uniq).to match_array([described_class::EPIC_ID]) + end + end + + describe '.hierarchy_relationship_allowed?' do + it 'returns true if a hierarchical relationship is allowed' do + result = described_class.hierarchy_relationship_allowed?( + parent_type_id: described_class::EPIC_ID, + child_type_id: described_class::ISSUE_ID + ) + + expect(result).to be true + end + + it 'returns false if a hierarchical relationship is not allowed' do + result = described_class.hierarchy_relationship_allowed?( + parent_type_id: described_class::EPIC_ID, + child_type_id: described_class::TASK_ID + ) + + expect(result).to be false + end + end end