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

Set total values to 0h when all children values are 0h

Christophe Bliard0a1f3038fbf6
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rb
index 6307969b1c7..0e058e285e5 100644
--- a/app/services/work_packages/update_ancestors_service.rb
+++ b/app/services/work_packages/update_ancestors_service.rb
@@ -119,12 +119,12 @@ class WorkPackages::UpdateAncestorsService
ancestor.derived_done_ratio = compute_derived_done_ratio(ancestor, loader)
end
- def compute_derived_done_ratio(work_package, loader)
+ def compute_derived_done_ratio(work_package, _loader)
return if work_package.derived_estimated_hours.nil? || work_package.derived_remaining_hours.nil?
- leaves = loader.leaves_of(work_package)
-
- if leaves.size.positive?
+ if work_package.derived_estimated_hours.zero?
+ nil
+ else
work_done = (work_package.derived_estimated_hours - work_package.derived_remaining_hours)
progress = (work_done.to_f / work_package.derived_estimated_hours) * 100
progress.round
@@ -151,22 +151,20 @@ class WorkPackages::UpdateAncestorsService
def derive_total_estimated_and_remaining_hours(work_package, loader)
descendants = loader.descendants_of(work_package)
- work_package.derived_estimated_hours = not_zero(all_estimated_hours([work_package] + descendants).sum.to_f)
- work_package.derived_remaining_hours = not_zero(all_remaining_hours([work_package] + descendants).sum.to_f)
+ work_package.derived_estimated_hours = total(all_estimated_hours([work_package] + descendants))
+ work_package.derived_remaining_hours = total(all_remaining_hours([work_package] + descendants))
end
- def not_zero(value)
- value unless value.zero?
+ def total(hours)
+ hours.empty? ? nil : hours.sum.to_f
end
def all_estimated_hours(work_packages)
- work_packages
- .map(&:estimated_hours)
- .reject { |hours| hours.to_f.zero? }
+ work_packages.filter_map(&:estimated_hours)
end
def all_remaining_hours(work_packages)
- work_packages.map(&:remaining_hours).reject { |hours| hours.to_f.zero? }
+ work_packages.filter_map(&:remaining_hours)
end
def modified_attributes_justify_derivation?(attributes)
diff --git a/spec/migrations/update_progress_calculation_spec.rb b/spec/migrations/update_progress_calculation_spec.rb
index 1895179a257..799241e1840 100644
--- a/spec/migrations/update_progress_calculation_spec.rb
+++ b/spec/migrations/update_progress_calculation_spec.rb
@@ -635,6 +635,26 @@ RSpec.describe UpdateProgressCalculation, type: :model do
)
end
end
+
+ context "when parent is open without any work or remaining work set, " \
+ "and children have a 100% complete status" do
+ it "sets parent total % complete to 100%" do
+ expect_migrates(
+ from: <<~TABLE,
+ hierarchy | status | work | remaining work | % complete
+ parent | To do (0%) | | | 0%
+ child1 | Done (100%) | 10h | 0h | 100%
+ child2 | Done (100%) | 10h | 0h | 100%
+ TABLE
+ to: <<~TABLE
+ hierarchy | status | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
+ parent | To do (0%) | | | 0% | 20h | 0h | 100%
+ child1 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%
+ child2 | Done (100%) | 10h | 0h | 100% | 10h | 0h | 100%
+ TABLE
+ )
+ end
+ end
end
describe "totals computation" do
@@ -689,6 +709,87 @@ RSpec.describe UpdateProgressCalculation, type: :model do
)
end
end
+
+ context "when parent does not have any work or remaining work set, " \
+ "and children have a 100% complete status" do
+ it "sets parent total % complete to 100%" do
+ expect_migrates(
+ from: <<~TABLE,
+ hierarchy | work | remaining work | % complete
+ parent | | |
+ child1 | 10h | 0h | 100%
+ child2 | 10h | 0h | 100%
+ TABLE
+ to: <<~TABLE
+ hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
+ parent | | | | 20h | 0h | 100%
+ child1 | 10h | 0h | 100% | 10h | 0h | 100%
+ child2 | 10h | 0h | 100% | 10h | 0h | 100%
+ TABLE
+ )
+ end
+ end
+
+ context "when work and remaining work are unset" do
+ it "does not set total work, total remaining work, and total % complete" do
+ expect_migrates(
+ from: <<~TABLE,
+ subject | work | remaining work | % complete
+ wp all unset | | |
+ wp 0% | | | 0%
+ wp 30% | | | 30%
+ wp 100% | | | 100%
+ TABLE
+ to: <<~TABLE
+ subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
+ wp all unset | | | | | |
+ wp 0% | | | 0% | | |
+ wp 30% | | | 30% | | |
+ wp 100% | | | 100% | | |
+ TABLE
+ )
+ end
+ end
+
+ context "when work is set to 0h and remaining work is unset" do
+ it "sets remaining work, total work, and total remaining work to 0h, unsets % complete, and keeps total % complete unset" do
+ expect_migrates(
+ from: <<~TABLE,
+ subject | work | remaining work | % complete
+ wp all unset | 0h | |
+ wp 0% | 0h | | 0%
+ wp 30% | 0h | | 30%
+ wp 100% | 0h | | 100%
+ TABLE
+ to: <<~TABLE
+ subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
+ wp all unset | 0h | 0h | | 0h | 0h |
+ wp 0% | 0h | 0h | | 0h | 0h |
+ wp 30% | 0h | 0h | | 0h | 0h |
+ wp 100% | 0h | 0h | | 0h | 0h |
+ TABLE
+ )
+ end
+ end
+
+ context "when work and remaining work are set and not 0h" do
+ it "sets total work, total remaining work, and total % complete accordingly same as work, remaining work and % complete" do
+ expect_migrates(
+ from: <<~TABLE,
+ subject | work | remaining work |
+ wp w set | 10h | |
+ wp rw set | | 5h |
+ wp w rw set | 10h | 5h |
+ TABLE
+ to: <<~TABLE
+ subject | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete
+ wp w set | 10h | 10h | 0% | 10h | 10h | 0%
+ wp rw set | 5h | 5h | 0% | 5h | 5h | 0%
+ wp w rw set | 10h | 5h | 50% | 10h | 5h | 50%
+ TABLE
+ )
+ end
+ end
end
describe "error during job execution" do
diff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rb
index 05c495d178f..ce44ef03c83 100644
--- a/spec/services/work_packages/update_ancestors_service_spec.rb
+++ b/spec/services/work_packages/update_ancestors_service_spec.rb
@@ -32,8 +32,8 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
shared_association_default(:author, factory_name: :user) { create(:user) }
shared_association_default(:project_with_types) { create(:project_with_types) }
shared_association_default(:priority) { create(:priority) }
- shared_association_default(:open_status, factory_name: :status) { create(:status) }
- shared_let(:closed_status) { create(:closed_status) }
+ shared_association_default(:open_status, factory_name: :status) { create(:status, name: "Open", default_done_ratio: 0) }
+ shared_let(:closed_status) { create(:closed_status, name: "Closed", default_done_ratio: 100) }
shared_let(:user) { create(:user) }
let(:estimated_hours) { [nil, nil, nil] }
@@ -56,11 +56,14 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
describe "done_ratio/estimated_hours/remaining_hours propagation" do
context "when setting the status of a work package" do
- shared_let(:open_status) { create(:status, name: "open", default_done_ratio: 0) }
- shared_let(:complete_status_with_100p_done_ratio) { create(:status, name: "complete", default_done_ratio: 100) }
-
context 'when using the "status-based" % complete mode',
with_settings: { work_package_done_ratio: "status" } do
+ def call_update_ancestors_service(work_package)
+ changed_attributes = work_package.changes.keys.map(&:to_sym)
+ described_class.new(user:, work_package:)
+ .call(changed_attributes)
+ end
+
context "with both parent and children having estimated hours set" do
shared_let(:parent) do
create(:work_package,
@@ -80,19 +83,14 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
status: open_status)
end
- def call_update_ancestors_service(work_package)
- changed_attributes = work_package.changes.keys.map(&:to_sym)
- described_class.new(user:, work_package:)
- .call(changed_attributes)
- end
context "when changing child status to a status with a default done ratio" do
%i[status status_id].each do |field|
context "with the #{field} field" do
it "recomputes child remaining work and update ancestors total % complete accordingly" do
value =
case field
- when :status then complete_status_with_100p_done_ratio
- when :status_id then complete_status_with_100p_done_ratio.id
+ when :status then closed_status
+ when :status_id then closed_status.id
end
set_attributes_on(child, field => value)
call_update_ancestors_service(child)
@@ -100,13 +98,40 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
expect_work_packages([parent, child], <<~TABLE)
| subject | work | total work | remaining work | total remaining work | % complete | total % complete |
| parent | 10h | 15h | 10h | 10h | 0% | 33% |
- | child | 5h | 5h | 0h | | 100% | |
+ | child | 5h | 5h | 0h | 0h | 100% | 100% |
TABLE
end
end
end
end
end
+
+ context "with parent having nothing set, and 2 children having values set (bug #54179)" do
+ let_work_packages(<<~TABLE)
+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete
+ parent | Open | | 15h | | 10h | 0% | 33%
+ child1 | Open | 10h | | 10h | | 0% |
+ child2 | Closed | 5h | | 0h | | 100% |
+ TABLE
+
+ context "when changing children to all have 100% complete" do
+ before do
+ set_attributes_on(child1, status: closed_status)
+ call_update_ancestors_service(child1)
+ end
+
+ it "sets parent total % complete to 100% and its total remaining work to 0h, " \
+ "and computes totals for the updated children too" do
+ table_work_packages.map(&:reload)
+ expect_work_packages(table_work_packages, <<~TABLE)
+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete
+ parent | Open | | 15h | | 0h | 0% | 100%
+ child1 | Closed | 10h | 10h | 0h | 0h | 100% | 100%
+ child2 | Closed | 5h | | 0h | | 100% |
+ TABLE
+ end
+ end
+ end
end
end
@@ -224,7 +249,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
end
end
- context "with all tasks having estimated hours and no tasks having any progress done yet" do
+ context "with all tasks having estimated hours and no tasks having any remaining hours" do
it_behaves_like "attributes of parent having children" do
let(:estimated_hours) do
[10.0, 2.0, 3.0]
@@ -237,10 +262,10 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
15.0
end
let(:aggregate_remaining_hours) do
- nil # zero-values aren't accounted for
+ 0.0
end
let(:aggregate_done_ratio) do
- nil
+ 100
end
end
end
@@ -431,7 +456,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
end
end
- context "with all tasks having estimated hours and no tasks having any progress done yet" do
+ context "with all tasks having estimated hours and no tasks having remaining hours" do
it_behaves_like "attributes of parent having children" do
let(:estimated_hours) do
[10.0, 2.0, 3.0]
@@ -444,10 +469,10 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
15.0
end
let(:aggregate_remaining_hours) do
- nil # zero-values aren't accounted for
+ 0.0
end
let(:aggregate_done_ratio) do
- nil
+ 100
end
end
end