Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

prevent parent relating to a child of a related to work package

ulfertsa7c3914b77a7
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index dce3416b4e3..bd0ce2d0f80 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -126,7 +126,8 @@ class WorkPackage < ApplicationRecord
:include_spent_time,
:involving_user,
:left_join_self_and_descendants,
- :relatable
+ :relatable,
+ :directly_related
acts_as_watchable
diff --git a/app/models/work_packages/scopes/directly_related.rb b/app/models/work_packages/scopes/directly_related.rb
new file mode 100644
index 00000000000..e8c2228c277
--- /dev/null
+++ b/app/models/work_packages/scopes/directly_related.rb
@@ -0,0 +1,40 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+module WorkPackages::Scopes
+ module DirectlyRelated
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def directly_related(work_package)
+ where(id: Relation.where(from_id: work_package).select(:to_id))
+ .or(where(id: Relation.where(to_id: work_package).select(:from_id)))
+ end
+ end
+ end
+end
diff --git a/app/models/work_packages/scopes/relatable.rb b/app/models/work_packages/scopes/relatable.rb
index be9aa63eb4b..2cf44b9d6cc 100644
--- a/app/models/work_packages/scopes/relatable.rb
+++ b/app/models/work_packages/scopes/relatable.rb
@@ -95,10 +95,16 @@ module WorkPackages::Scopes
# away as it would create a circle.
#
# The implementation focuses on excluding candidates. It does so in two parts:
- # * Excluding all work packages with which a direct relation already exist.
+ # * Excluding all work packages with which a direct relation already exist (with additions for PARENT relations).
# * Excluding work packages that are related transitively (following a path of direct relationships).
#
- # The first is straightforward. The second is more complicated and also depends on the type of relation that is
+ # The first is straightforward for all relation types except for PARENT relations. For that majority, whenever there is
+ # a relation of any type except PARENT either to or from the work package queried for, it is excluded. For PARENT relations,
+ # both the descendants of the queried for work package as well as the decendants of any directly related work packages are
+ # excluded as well since creating a PARENT relationship to one such work package would result in a relation up or down the
+ # hierarchy which violates the ancestor/descendant rule.
+ #
+ # The second exclusion of candidates is more complicated and also depends on the type of relation that is
# queried for. It uses a CTE to recursively find all work packages with which a transitive relationship of interest based
# on the rules outlined above exist.
#
@@ -161,18 +167,16 @@ module WorkPackages::Scopes
def relatable(work_package, relation_type)
return all if work_package.new_record?
- scope = not_having_direct_relation(work_package, relation_type)
+ scope = if relation_type == Relation::TYPE_PARENT
+ not_having_potential_tree_relation(work_package)
+ else
+ where.not(id: directly_related(work_package))
+ end
+
+ scope = scope
.not_having_transitive_relation(work_package, relation_type)
.where.not(id: work_package.id)
- # On a parent relationship, explicitly remove the former parent (which might be the current one as well)
- # from the list of work packages one can relate to. This is not strictly necessary since it would not
- # cause faulty relationships but doing it removes the parent from places where it should not show up,
- # e.g. in an auto completer.
- if relation_type == Relation::TYPE_PARENT && work_package.parent_id_was
- scope = scope.where.not(id: work_package.parent_id_was)
- end
-
if Setting.cross_project_work_package_relations
scope
else
@@ -180,19 +184,6 @@ module WorkPackages::Scopes
end
end
- def not_having_direct_relation(work_package, relation_type)
- origin = if relation_type == Relation::TYPE_PARENT
- WorkPackageHierarchy
- .where(ancestor_id: work_package.id)
- .select(:descendant_id)
- else
- work_package.id
- end
-
- where.not(id: Relation.where(from_id: origin).select(:to_id))
- .where.not(id: Relation.where(to_id: origin).select(:from_id))
- end
-
def not_having_transitive_relation(work_package, relation_type)
sql = <<~SQL.squish
WITH
@@ -209,6 +200,22 @@ module WorkPackages::Scopes
private
+ def not_having_potential_tree_relation(work_package)
+ # On a parent relationship, explicitly remove the former parent (which might be the current one as well)
+ # from the list of work packages one can relate to. This is not strictly necessary since it would not
+ # cause faulty relationships but doing it removes the parent from places where it should not show up,
+ # e.g. in an auto completer.
+ scope = if work_package.parent_id_was
+ where.not(id: work_package.parent_id_was)
+ else
+ all
+ end
+
+ scope
+ .where.not(id: directly_related(descendant_or_self_ids_of(work_package)))
+ .where.not(id: descendant_or_self_ids_of(directly_related(descendant_or_self_ids_of(work_package))))
+ end
+
def non_relatable_paths_sql(work_package, relation_type)
<<~SQL.squish
related (id,
@@ -365,6 +372,12 @@ module WorkPackages::Scopes
'1 = 1'
end
end
+
+ def descendant_or_self_ids_of(work_packages)
+ WorkPackageHierarchy
+ .where(ancestor_id: work_packages)
+ .select(:descendant_id)
+ end
end
end
end
diff --git a/spec/models/work_packages/scopes/directly_related_spec.rb b/spec/models/work_packages/scopes/directly_related_spec.rb
new file mode 100644
index 00000000000..50ba9c9a6e7
--- /dev/null
+++ b/spec/models/work_packages/scopes/directly_related_spec.rb
@@ -0,0 +1,85 @@
+# --copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+require 'spec_helper'
+
+describe WorkPackages::Scopes::DirectlyRelated, '.directly_related scope' do
+ create_shared_association_defaults_for_work_package_factory
+
+ shared_let(:origin) { create(:work_package) }
+ shared_let(:related_work_package_to) { create(:work_package) }
+ shared_let(:transitively_related_work_package_to) { create(:work_package) }
+ shared_let(:related_work_package_from) { create(:work_package) }
+ shared_let(:transitively_related_work_package_from) { create(:work_package) }
+ shared_let(:unrelated_work_package) { create(:work_package) }
+
+ let(:relation_from) do
+ create(:relation,
+ relation_type:,
+ from: origin,
+ to: related_work_package_from)
+ end
+ let(:relation_to) do
+ create(:relation,
+ relation_type:,
+ to: origin,
+ from: related_work_package_to)
+ end
+ let(:transitive_relation_from) do
+ create(:relation,
+ relation_type:,
+ from: related_work_package_from,
+ to: transitively_related_work_package_from)
+ end
+ let(:transitive_relation_to) do
+ create(:relation,
+ relation_type:,
+ to: related_work_package_to,
+ from: transitively_related_work_package_to)
+ end
+
+ let!(:existing_relations) { [relation_to, transitive_relation_to, relation_from, transitive_relation_from] }
+
+ subject(:directly_related) { WorkPackage.directly_related(origin) }
+
+ it 'is an AR scope' do
+ expect(directly_related)
+ .to be_a ActiveRecord::Relation
+ end
+
+ Relation::TYPES.each_key do |current_type|
+ let(:relation_type) { current_type }
+
+ context "with existing relations of type '#{current_type}'" do
+ it 'contains the directly related work packages in both directions' do
+ expect(directly_related)
+ .to match_array([related_work_package_to, related_work_package_from])
+ end
+ end
+ end
+end
diff --git a/spec/models/work_packages/scopes/relatable_spec.rb b/spec/models/work_packages/scopes/relatable_spec.rb
index 863fa4d808a..1105d62dd1b 100644
--- a/spec/models/work_packages/scopes/relatable_spec.rb
+++ b/spec/models/work_packages/scopes/relatable_spec.rb
@@ -1096,5 +1096,56 @@ describe WorkPackages::Scopes::Relatable, '.relatable scope' do
.to be_empty
end
end
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the parent" do
+ expect(relatable)
+ .to match_array [blocks_parent]
+ end
+ end
+ end
+
+ context 'with a blocked work package that has a child and a parent' do
+ let(:blocked_child) do
+ create(:work_package, parent: blocked)
+ end
+ let(:blocked) do
+ create(:work_package, parent: blocked_parent).tap do |bl|
+ create(:relation, from: origin, to: bl, relation_type: Relation::TYPE_BLOCKED)
+ end
+ end
+ let(:blocked_parent) do
+ create(:work_package)
+ end
+ let!(:existing_work_packages) { [origin, blocked, blocked_parent, blocked_child] }
+
+ context "for a 'blocks' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKS }
+
+ it "is empty" do
+ expect(relatable)
+ .to be_empty
+ end
+ end
+
+ context "for a 'blocked' relation" do
+ let(:relation_type) { Relation::TYPE_BLOCKED }
+
+ it "contains the parent and the child" do
+ expect(relatable)
+ .to match_array [blocked_parent, blocked_child]
+ end
+ end
+
+ context "for a 'parent' relation" do
+ let(:relation_type) { Relation::TYPE_PARENT }
+
+ it "contains the parent" do
+ expect(relatable)
+ .to match_array [blocked_parent]
+ end
+ end
end
end