From 01b953e3cc8d70aab954f8e0409be0815c07dc97 Mon Sep 17 00:00:00 2001 From: Matt D'Angelo Date: Fri, 5 Sep 2025 16:16:28 +0930 Subject: [PATCH 1/2] Add model scopes and use them for quick actions --- .../work_items/system_defined/hierarchy_restriction.rb | 10 ++++++++++ lib/gitlab/quick_actions/work_item_actions.rb | 10 ++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/models/work_items/system_defined/hierarchy_restriction.rb b/app/models/work_items/system_defined/hierarchy_restriction.rb index f46161bf047b6b..f65456742d6761 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 find_by_parent_type_id(parent_type_id) + find_by(parent_type_id: parent_type_id) + end + + def find_by_parent_type_id_and_child_type_id(parent_type_id:, child_type_id:) + find_by(parent_type_id: parent_type_id, child_type_id: child_type_id) + 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 aed024eccb5da8..06f5ccf70fabab 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 + .find_by_parent_type_id(quick_action_target.work_item_type_id) + .present? end def has_children? @@ -265,9 +267,9 @@ 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 + ::WorkItems::SystemDefined::HierarchyRestriction.find_by_parent_type_id_and_child_type_id( + parent_type_id: parent.work_item_type_id, + child_type_id: child_work_item.work_item_type_id ).present? end end -- GitLab From 2cf89654f62d56162ae55cf87fe5c6596640cc48 Mon Sep 17 00:00:00 2001 From: Matt D'Angelo Date: Mon, 8 Sep 2025 10:16:01 +0930 Subject: [PATCH 2/2] Add class method specs --- .../system_defined/hierarchy_restriction.rb | 8 +++--- lib/gitlab/quick_actions/work_item_actions.rb | 8 +++--- .../hierarchy_restriction_spec.rb | 27 +++++++++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/app/models/work_items/system_defined/hierarchy_restriction.rb b/app/models/work_items/system_defined/hierarchy_restriction.rb index f65456742d6761..28f1afc37482fe 100644 --- a/app/models/work_items/system_defined/hierarchy_restriction.rb +++ b/app/models/work_items/system_defined/hierarchy_restriction.rb @@ -18,12 +18,12 @@ class HierarchyRestriction TICKET_ID = ::WorkItems::Type::BASE_TYPES[:ticket][:id] class << self - def find_by_parent_type_id(parent_type_id) - find_by(parent_type_id: parent_type_id) + def with_parent_type_id(parent_type_id) + where(parent_type_id: parent_type_id) end - def find_by_parent_type_id_and_child_type_id(parent_type_id:, child_type_id:) - find_by(parent_type_id: parent_type_id, child_type_id: child_type_id) + 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 diff --git a/lib/gitlab/quick_actions/work_item_actions.rb b/lib/gitlab/quick_actions/work_item_actions.rb index 06f5ccf70fabab..dc5f89df35cf8a 100644 --- a/lib/gitlab/quick_actions/work_item_actions.rb +++ b/lib/gitlab/quick_actions/work_item_actions.rb @@ -177,7 +177,7 @@ def work_item_parent def supports_children? ::WorkItems::SystemDefined::HierarchyRestriction - .find_by_parent_type_id(quick_action_target.work_item_type_id) + .with_parent_type_id(quick_action_target.work_item_type_id) .present? end @@ -204,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 @@ -267,10 +267,10 @@ def fetch_child_work_item(child) end def hierarchy_relationship_allowed?(parent, child_work_item) - ::WorkItems::SystemDefined::HierarchyRestriction.find_by_parent_type_id_and_child_type_id( + ::WorkItems::SystemDefined::HierarchyRestriction.hierarchy_relationship_allowed?( parent_type_id: parent.work_item_type_id, child_type_id: child_work_item.work_item_type_id - ).present? + ) 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 6d4f443e6e72cb..1f14070380eb25 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 -- GitLab