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

Make ScheduleService also reschedule the work packages being passed in

Christophe Bliard0d7104ccc377
diff --git a/app/services/work_packages/get_rescheduled_children_dates_service.rb b/app/services/work_packages/get_rescheduled_children_dates_service.rb
deleted file mode 100644
index 2aee61808ed..00000000000
--- a/app/services/work_packages/get_rescheduled_children_dates_service.rb
+++ /dev/null
@@ -1,156 +0,0 @@
-# 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.
-#++
-
-# Service to get the new dates of an automatically scheduled parent from its
-# children once the children have been rescheduled.
-#
-# It simulates a rescheduling and then returns the dates the parent should have:
-# the earliest start date and the latest due date of the children.
-class WorkPackages::GetRescheduledChildrenDatesService
- attr_accessor :work_package
-
- ParentDates = Data.define(:start_date, :due_date)
-
- def initialize(work_package:)
- self.work_package = Array(work_package).first
- end
-
- def call(changed_attributes = %i(start_date due_date))
- if %i(start_date due_date parent parent_id).intersect?(changed_attributes)
- schedule_following
- end
-
- children = @dependencies.children_by_parent_id(work_package.id)
- dates = children.pluck(:start_date, :due_date).flatten.compact
- start_date, due_date = dates.minmax
-
- ServiceResult.success(result: ParentDates.new(start_date:, due_date:))
- end
-
- private
-
- # Finds all work packages that need to be rescheduled because of a
- # rescheduling of the service's work package and reschedules them.
- #
- # The order of the rescheduling is important as successors' dates are
- # calculated based on their predecessors' dates and ancestors' dates based on
- # their children's dates.
- #
- # Thus, the work packages following (having a follows relation, direct or
- # transitively) the service's work package are first all loaded, and then
- # sorted by their need to be scheduled before one another:
- #
- # - predecessors are scheduled before their successors
- # - children/descendants are scheduled before their parents/ancestors
- #
- # Manually scheduled work packages are not encountered at this point as they
- # are filtered out when fetching the work packages eligible for rescheduling.
- def schedule_following
- work_packages = [work_package]
- work_packages << work_package.parent if work_package.parent && work_package.parent_id_changed?
- @dependencies = WorkPackages::ScheduleDependency.new(work_packages, switching_to_automatic_mode: [work_package])
- @dependencies.in_schedule_order do |scheduled, dependency|
- reschedule(scheduled, dependency)
- end
- end
-
- # Schedules work packages based on either
- # - their descendants if they are parents
- # - their predecessors (or predecessors of their ancestors) if they are
- # leaves
- def reschedule(scheduled, dependency)
- if dependency.has_descendants?
- reschedule_by_descendants(scheduled, dependency)
- else
- reschedule_by_predecessors(scheduled, dependency)
- end
- end
-
- # Inherits the start/due_date from the descendants of this work package.
- #
- # Only parent work packages are scheduled like this. start_date receives the
- # minimum of the dates (start_date and due_date) of the descendants due_date
- # receives the maximum of the dates (start_date and due_date) of the
- # descendants
- def reschedule_by_descendants(scheduled, dependency)
- set_dates(scheduled, dependency.start_date, dependency.due_date)
- end
-
- # Calculates the dates of a work package based on its follows relations.
- #
- # The start date of a work package is constrained by its direct and indirect
- # predecessors, as it must start strictly after all predecessors finish.
- #
- # The follows relations of ancestors are considered to be equal to own follows
- # relations as they inhibit moving a work package just the same. Only leaf
- # work packages are calculated like this.
- #
- # work package is moved to a later date:
- # - following work packages are moved forward only to ensure they start
- # after their predecessor's finish date. They may not need to move at all
- # when there a time buffer between a follower and its predecessors
- # (predecessors can also be acquired transitively by ancestors)
- #
- # work package moved to an earlier date:
- # - following work packages do not move at all.
- def reschedule_by_predecessors(scheduled, dependency)
- return unless dependency.soonest_start_date
-
- new_start_date = dependency.soonest_start_date
- new_due_date = determine_due_date(scheduled, new_start_date)
- set_dates(scheduled, new_start_date, new_due_date)
- end
-
- def determine_due_date(work_package, start_date)
- # due date is set only if the moving work package already has one or has a
- # duration. If not, due date is nil (and duration will be nil too).
- return unless work_package.due_date || work_package.duration
-
- due_date =
- if work_package.duration
- days(work_package).due_date(start_date, work_package.duration)
- else
- work_package.due_date
- end
-
- # if due date is before start date, then start is used as due date.
- [start_date, due_date].max
- end
-
- def set_dates(work_package, start_date, due_date)
- work_package.start_date = start_date
- work_package.due_date = due_date
- work_package.duration = days(work_package).duration(start_date, due_date)
- end
-
- def days(work_package)
- WorkPackages::Shared::Days.for(work_package)
- end
-end
diff --git a/app/services/work_packages/schedule_dependency.rb b/app/services/work_packages/schedule_dependency.rb
index a87980fd37f..22d7cf6f6d9 100644
--- a/app/services/work_packages/schedule_dependency.rb
+++ b/app/services/work_packages/schedule_dependency.rb
@@ -132,7 +132,13 @@ class WorkPackages::ScheduleDependency
end
def create_dependencies
- moving_work_packages.index_with { |work_package| Dependency.new(work_package, self) }
+ (moved_work_packages + moving_work_packages)
+ .filter { |work_package| automatically_scheduled?(work_package) }
+ .index_with { |work_package| Dependency.new(work_package, self) }
+ end
+
+ def automatically_scheduled?(work_package)
+ work_package.schedule_automatically? || switching_to_automatic_mode.include?(work_package)
end
def moving_work_packages
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 18f007234d5..4eb0b0dec57 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -286,11 +286,9 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
def update_dates_from_rescheduled_children
return if work_package.schedule_manually?
- # do a schedule call to get the children rescheduled before getting their dates
- service = WorkPackages::GetRescheduledChildrenDatesService.new(work_package:)
- parent_dates = service.call.result
- work_package.start_date = parent_dates.start_date
- work_package.due_date = parent_dates.due_date
+ # do a reschedule call to get the work package dates from the rescheduled children
+ service = WorkPackages::SetScheduleService.new(user: User.current, work_package:, switching_to_automatic_mode: [work_package])
+ service.call(work_package.changes.keys.map(&:to_sym)).result
end
def update_dates_from_self
diff --git a/app/services/work_packages/set_schedule_service.rb b/app/services/work_packages/set_schedule_service.rb
index 601df69e509..6c525eee9c4 100644
--- a/app/services/work_packages/set_schedule_service.rb
+++ b/app/services/work_packages/set_schedule_service.rb
@@ -39,7 +39,7 @@ class WorkPackages::SetScheduleService
def call(changed_attributes = %i(start_date due_date))
altered = []
- if %i(start_date due_date parent parent_id).intersect?(changed_attributes)
+ if %i(start_date due_date parent parent_id schedule_manually).intersect?(changed_attributes)
altered += schedule_following
end
@@ -73,10 +73,21 @@ class WorkPackages::SetScheduleService
def schedule_following
altered = []
- WorkPackages::ScheduleDependency.new(work_packages, switching_to_automatic_mode:).in_schedule_order do |scheduled, dependency|
+ moved_work_packages = work_packages.flat_map do |work_package|
+ if work_package.parent && work_package.parent_id_changed?
+ [work_package, work_package.parent]
+ else
+ [work_package]
+ end
+ end
+
+ WorkPackages::ScheduleDependency.new(moved_work_packages, switching_to_automatic_mode:)
+ .in_schedule_order do |scheduled, dependency|
+ changes_before = scheduled.changes
reschedule(scheduled, dependency)
+ changes_after = scheduled.changes
- altered << scheduled if scheduled.changed?
+ altered << scheduled if changes_before != changes_after
end
altered
diff --git a/spec/services/work_packages/update_service_integration_spec.rb b/spec/services/work_packages/update_service_integration_spec.rb
index 77f18bead0d..cf1f2229f36 100644
--- a/spec/services/work_packages/update_service_integration_spec.rb
+++ b/spec/services/work_packages/update_service_integration_spec.rb
@@ -820,8 +820,8 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "schedules parent to start and end at soonest working start date and the child to start at the parent start" do
expect_work_packages(subject.all_results, <<~TABLE)
subject | MTWTFSS |
- work_package | [ |
new_parent | X |
+ work_package | [ |
TABLE
end
end
@@ -838,8 +838,8 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
"and schedules the parent dates to match the child dates" do
expect_work_packages(subject.all_results, <<~TABLE)
subject | MTWTFSS |
- work_package | XXXX |
new_parent | XXXX |
+ work_package | XXXX |
TABLE
end
end
@@ -855,8 +855,8 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "schedules the moved work package to start and end at the parent soonest working start date" do
expect_work_packages(subject.all_results, <<~TABLE)
subject | MTWTFSS |
- work_package | X |
new_parent | X |
+ work_package | X |
TABLE
end
end
@@ -872,8 +872,8 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "schedules the moved work package to start at the parent soonest working start date and keep the due date" do
expect_work_packages(subject.all_results, <<~TABLE)
subject | MTWTFSS |
- work_package | X..XX |
new_parent | X..XX |
+ work_package | X..XX |
TABLE
end
end
@@ -888,9 +888,9 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
it "does not reschedule the moved work package, and sets new parent dates to child dates" do
expect_work_packages(subject.all_results, <<~TABLE)
- subject | MTWTFSS |
- work_package | XX |
- new_parent | XX |
+ subject | MTWTFSS | scheduling mode
+ new_parent | XX | automatic
+ work_package | XX | manual
TABLE
end
end