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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex 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::UpdateAncestorsServiceancestor.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+ elsework_done = (work_package.derived_estimated_hours - work_package.derived_remaining_hours)progress = (work_done.to_f / work_package.derived_estimated_hours) * 100progress.round@@ -151,22 +151,20 @@ class WorkPackages::UpdateAncestorsServicedef 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_fenddef all_estimated_hours(work_packages)- work_packages- .map(&:estimated_hours)- .reject { |hours| hours.to_f.zero? }+ work_packages.filter_map(&:estimated_hours)enddef all_remaining_hours(work_packages)- work_packages.map(&:remaining_hours).reject { |hours| hours.to_f.zero? }+ work_packages.filter_map(&:remaining_hours)enddef modified_attributes_justify_derivation?(attributes)diff --git a/spec/migrations/update_progress_calculation_spec.rb b/spec/migrations/update_progress_calculation_spec.rbindex 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)endend++ 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+ endenddescribe "totals computation" do@@ -689,6 +709,87 @@ RSpec.describe UpdateProgressCalculation, type: :model do)endend++ 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+ endenddescribe "error during job execution" dodiff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rbindex 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 doshared_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 dodescribe "done_ratio/estimated_hours/remaining_hours propagation" docontext "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" doshared_let(:parent) docreate(:work_package,@@ -80,19 +83,14 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model dostatus: 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)- endcontext "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" doit "recomputes child remaining work and update ancestors total % complete accordingly" dovalue =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.idendset_attributes_on(child, field => value)call_update_ancestors_service(child)@@ -100,13 +98,40 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doexpect_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% |TABLEendendendendend++ 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+ endendend@@ -224,7 +249,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doendend- 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" doit_behaves_like "attributes of parent having children" dolet(:estimated_hours) do[10.0, 2.0, 3.0]@@ -237,10 +262,10 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do15.0endlet(:aggregate_remaining_hours) do- nil # zero-values aren't accounted for+ 0.0endlet(:aggregate_done_ratio) do- nil+ 100endendend@@ -431,7 +456,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doendend- 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" doit_behaves_like "attributes of parent having children" dolet(:estimated_hours) do[10.0, 2.0, 3.0]@@ -444,10 +469,10 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do15.0endlet(:aggregate_remaining_hours) do- nil # zero-values aren't accounted for+ 0.0endlet(:aggregate_done_ratio) do- nil+ 100endendend