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

[59539] Reschedule successor when deleting a follows relation

Christophe Bliard311ce0fea41a
diff --git a/app/services/relations/base_service.rb b/app/services/relations/base_service.rb
index 1fe76bf3e7d..b30d5e26b90 100644
--- a/app/services/relations/base_service.rb
+++ b/app/services/relations/base_service.rb
@@ -29,6 +29,7 @@
class Relations::BaseService < BaseServices::BaseCallable
include Contracted
include Shared::ServiceContext
+ include Relations::Concerns::Rescheduling
attr_accessor :user
@@ -61,50 +62,4 @@ class Relations::BaseService < BaseServices::BaseCallable
model.lag = nil
end
end
-
- def reschedule(relation)
- schedule_result = WorkPackages::SetScheduleService
- .new(user:,
- work_package: relation.predecessor,
- switching_to_automatic_mode: switching_to_automatic_mode(relation))
- .call
-
- # The predecessor work package will not be altered by the schedule service so
- # we do not have to save the result of the service, only the dependent results.
- save_result = if schedule_result.success?
- schedule_result.dependent_results.all? { |dr| !dr.result.changed? || dr.result.save(validate: false) }
- end || false
-
- schedule_result.success = save_result
-
- schedule_result
- end
-
- def switching_to_automatic_mode(relation)
- if should_switch_successor_to_automatic_mode?(relation)
- [relation.successor]
- else
- []
- end
- end
-
- def should_switch_successor_to_automatic_mode?(relation)
- relation.follows? \
- && creating? \
- && last_successor_relation?(relation) \
- && has_no_children?(relation.successor)
- end
-
- def creating?
- self.class.name.include?("Create")
- end
-
- def last_successor_relation?(relation)
- Relation.follows.of_successor(relation.successor)
- .not_of_predecessor(relation.predecessor).none?
- end
-
- def has_no_children?(work_package)
- !WorkPackage.exists?(parent: work_package)
- end
end
diff --git a/app/services/relations/concerns/rescheduling.rb b/app/services/relations/concerns/rescheduling.rb
new file mode 100644
index 00000000000..6707a35c40c
--- /dev/null
+++ b/app/services/relations/concerns/rescheduling.rb
@@ -0,0 +1,79 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 Relations::Concerns
+ module Rescheduling
+ def reschedule(relation)
+ schedule_result = WorkPackages::SetScheduleService
+ .new(user:,
+ work_package: relation.predecessor,
+ switching_to_automatic_mode: switching_to_automatic_mode(relation))
+ .call
+
+ # The predecessor work package will not be altered by the schedule service so
+ # we do not have to save the result of the service, only the dependent results.
+ save_result = if schedule_result.success?
+ schedule_result.dependent_results.all? { |dr| !dr.result.changed? || dr.result.save(validate: false) }
+ end || false
+
+ schedule_result.success = save_result
+
+ schedule_result
+ end
+
+ def switching_to_automatic_mode(relation)
+ if should_switch_successor_to_automatic_mode?(relation)
+ [relation.successor]
+ else
+ []
+ end
+ end
+
+ def should_switch_successor_to_automatic_mode?(relation)
+ relation.follows? \
+ && creating? \
+ && first_successor_relation?(relation) \
+ && has_no_children?(relation.successor)
+ end
+
+ def creating?
+ self.class.name.include?("Create")
+ end
+
+ def first_successor_relation?(relation)
+ Relation.follows.of_successor(relation.successor)
+ .not_of_predecessor(relation.predecessor).none?
+ end
+
+ def has_no_children?(work_package)
+ !WorkPackage.exists?(parent: work_package)
+ end
+ end
+end
diff --git a/app/services/relations/delete_service.rb b/app/services/relations/delete_service.rb
index 6d89133a4ab..0b65b0ec721 100644
--- a/app/services/relations/delete_service.rb
+++ b/app/services/relations/delete_service.rb
@@ -27,16 +27,29 @@
#++
class Relations::DeleteService < BaseServices::Delete
+ include Relations::Concerns::Rescheduling
+
def after_perform(_result)
result = super
- if result.success? && successor_must_switch_to_manual_mode?
- deleted_relation.successor.update(schedule_manually: true)
+ if result.success?
+ update_related_result = update_related
+ if update_related_result
+ result.merge!(update_related_result)
+ end
end
result
end
private
+ def update_related
+ if successor_must_switch_to_manual_mode?
+ switch_successor_to_manual_scheduling
+ elsif successor_must_be_rescheduled?
+ reschedule_successor
+ end
+ end
+
def deleted_relation
model
end
@@ -47,6 +60,21 @@ class Relations::DeleteService < BaseServices::Delete
&& was_last_relation_to_the_successor?
end
+ def switch_successor_to_manual_scheduling
+ deleted_relation.successor.update(schedule_manually: true)
+ ServiceResult.success(dependent_results: [ServiceResult.success(result: deleted_relation.successor)])
+ end
+
+ def successor_must_be_rescheduled?
+ deleted_relation.follows? \
+ && !was_last_relation_to_the_successor?
+ end
+
+ def reschedule_successor
+ some_sibling_relation = Relation.follows.of_successor(deleted_relation.successor).first
+ reschedule(some_sibling_relation)
+ end
+
def successor_has_dates?
deleted_relation.successor.start_date.present? || deleted_relation.successor.due_date.present?
end
diff --git a/spec/factories/type_factory.rb b/spec/factories/type_factory.rb
index 27c3beb06ce..878755a291f 100644
--- a/spec/factories/type_factory.rb
+++ b/spec/factories/type_factory.rb
@@ -58,6 +58,10 @@ FactoryBot.define do
end
end
+ trait :default do
+ is_default { true }
+ end
+
factory :type_standard, class: "::Type" do
name { "None" }
is_standard { true }
diff --git a/spec/services/relations/delete_service_spec.rb b/spec/services/relations/delete_service_spec.rb
new file mode 100644
index 00000000000..c7ca086233f
--- /dev/null
+++ b/spec/services/relations/delete_service_spec.rb
@@ -0,0 +1,150 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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"
+
+RSpec.describe Relations::DeleteService do
+ # this mandatory custom field is used to make work packages invalid in tests
+ shared_let(:mandatory_custom_field) do
+ create(:integer_wp_custom_field, is_required: true, is_for_all: true, default_value: nil)
+ end
+ shared_let(:priority) { create(:priority) }
+ shared_let(:type_task) { create(:type_task, :default) }
+ shared_let(:type_with_mandatory_cf) do
+ create(:type,
+ name: "Type with mandatory custom field",
+ custom_fields: [mandatory_custom_field])
+ end
+ shared_let(:project) do
+ create(:project,
+ types: [type_task, type_with_mandatory_cf],
+ work_package_custom_fields: [mandatory_custom_field])
+ end
+ shared_let(:status) { create(:status) }
+ shared_let(:user) { create(:user) }
+ shared_let(:admin) { create(:admin) }
+
+ before_all do
+ set_factory_default(:priority, priority)
+ set_factory_default(:project_with_types, project)
+ set_factory_default(:status, status)
+ set_factory_default(:type, type_task)
+ set_factory_default(:user, user)
+ end
+
+ shared_let(:admin) { create(:admin) }
+
+ subject(:delete_service_result) do
+ described_class.new(user: admin, model: relation_to_delete).call
+ end
+
+ context "for predecessors/successors relations" do
+ context "when the successor no longer has any predecessors" do
+ let_work_packages(<<~TABLE)
+ subject | MTWTFSS | scheduling mode | predecessors
+ predecessor | XX | manual |
+ work_package | X | automatic | predecessor
+ TABLE
+
+ let(:relation_to_delete) { _table.relation(predecessor: predecessor) }
+
+ it "removes the relation and switches the work package to manual scheduling mode" do
+ expect(delete_service_result).to be_success
+ expect(subject.all_results).to contain_exactly(relation_to_delete, work_package)
+
+ expect(work_package.relations.count).to eq 0
+ expect_work_packages(WorkPackage.all, <<~TABLE)
+ subject | MTWTFSS | scheduling mode
+ predecessor | XX | manual
+ work_package | X | manual
+ TABLE
+ end
+
+ context "when the successor is invalid (missing required custom field for instance)" do
+ before do
+ work_package.update_attribute(:type, type_with_mandatory_cf)
+ end
+
+ it "still updates correctly" do
+ # ensure the work package is invalid as intended
+ expect(work_package).not_to be_valid
+
+ expect(delete_service_result).to be_success
+
+ # work package has been changed to manual scheduling though still invalid
+ expect { work_package.reload }.to change(work_package, :schedule_manually)
+ expect(work_package).not_to be_valid
+ end
+ end
+ end
+
+ context "when a successor has two predecessors and the closest relation is deleted" do
+ let_work_packages(<<~TABLE)
+ subject | MTWTFSS | scheduling mode | predecessors
+ predecessor1 | XX | manual |
+ predecessor2 | X | manual |
+ work_package | X | automatic | predecessor1, predecessor2
+ TABLE
+
+ let(:relation_to_delete) { _table.relation(predecessor: predecessor2) }
+
+ it "removes the relation and reschedules the successor" do
+ expect(delete_service_result).to be_success
+ expect(subject.all_results).to contain_exactly(relation_to_delete, work_package)
+
+ expect(work_package.relations.count).to eq 1
+ expect_work_packages(WorkPackage.all, <<~TABLE)
+ subject | MTWTFSS | scheduling mode
+ predecessor1 | XX | manual
+ predecessor2 | X | manual
+ # successor has been rescheduled to start right after predecessor1
+ work_package | X | automatic
+ TABLE
+ end
+
+ context "when the successor is invalid (missing required custom field for instance)" do
+ before do
+ work_package.update_attribute(:type, type_with_mandatory_cf)
+ end
+
+ it "still updates correctly" do
+ # ensure the work package is invalid as intended
+ expect(work_package).not_to be_valid
+
+ expect(delete_service_result).to be_success
+
+ # work package has been rescheduled though still invalid
+ expect { work_package.reload }.to change(work_package, :start_date)
+ expect(work_package).not_to be_valid
+ end
+ end
+ end
+ end
+end
diff --git a/spec/support/table_helpers/table.rb b/spec/support/table_helpers/table.rb
index aab27841cd6..8f5e22cf476 100644
--- a/spec/support/table_helpers/table.rb
+++ b/spec/support/table_helpers/table.rb
@@ -56,8 +56,8 @@ module TableHelpers
def relation(predecessor: nil, successor: nil)
@relations.find do |relation|
relation.follows? \
- && (predecessor.nil? || relation.predecessor.subject == predecessor) \
- && (successor.nil? || relation.successor.subject == successor)
+ && (predecessor.nil? || relation.predecessor.subject == subject_of(predecessor)) \
+ && (successor.nil? || relation.successor.subject == subject_of(successor))
end
end
@@ -67,6 +67,19 @@ module TableHelpers
private
+ def subject_of(object)
+ case object
+ when nil
+ nil
+ when String
+ object
+ when WorkPackage
+ object.subject
+ else
+ raise "Cannot find subject for #{object.inspect}"
+ end
+ end
+
def normalize_name(name)
symbolic_name = name.to_sym
return symbolic_name if @work_packages_by_identifier.has_key?(symbolic_name)