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
[50960] Derive estimated time from summing self and descendants
Christophe Bliarda91a20ba5ba8
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rbindex f95842a2b20..a7fb623394d 100644--- a/app/services/work_packages/update_ancestors_service.rb+++ b/app/services/work_packages/update_ancestors_service.rb@@ -36,20 +36,14 @@ class WorkPackages::UpdateAncestorsServiceenddef call(attributes)- modified = update_current_and_former_ancestors(attributes)+ updated_work_packages = update_current_and_former_ancestors(attributes)- modified_ancestors = modified.reject { |wp| initiator?(wp) }-- set_journal_note(modified_ancestors)-- # Do not send notification for parent updates- success = Journal::NotificationConfiguration.with(false) do- modified.all? { |wp| wp.save(validate: false) }- end+ set_journal_note(ancestors(updated_work_packages))+ success = save_updated_work_packages(updated_work_packages)result = ServiceResult.new(success:, result: initiator_work_package)- modified_ancestors.each do |wp|+ ancestors(updated_work_packages).each do |wp|result.add_dependent!(ServiceResult.new(success: !wp.changed?, result: wp))end@@ -62,6 +56,10 @@ class WorkPackages::UpdateAncestorsServicework_package == initiator_work_packageend+ def ancestors(work_packages)+ work_packages.reject { initiator?(_1) }+ end+def update_current_and_former_ancestors(attributes)include_former_ancestors = attributes.intersect?(%i[parent_id parent])WorkPackages::UpdateAncestors::Loader@@ -73,6 +71,18 @@ class WorkPackages::UpdateAncestorsServiceendend+ def save_updated_work_packages(updated_work_packages)+ updated_initiators, updated_ancestors = updated_work_packages.partition { initiator?(_1) }++ # Send notifications for initiator updates+ success = updated_initiators.all? { |wp| wp.save(validate: false) }+ # Do not send notifications for parent updates+ success &&= Journal::NotificationConfiguration.with(false) do+ updated_ancestors.all? { |wp| wp.save(validate: false) }+ end+ success+ end+def derive_attributes(work_package, loader, attributes)return unless modified_attributes_justify_derivation?(attributes)@@ -178,7 +188,7 @@ class WorkPackages::UpdateAncestorsServicedef derive_estimated_hours(work_package, loader)descendants = loader.descendants_of(work_package)- work_package.derived_estimated_hours = not_zero(all_estimated_hours(descendants).sum.to_f)+ work_package.derived_estimated_hours = not_zero(all_estimated_hours([work_package] + descendants).sum.to_f)enddef derive_remaining_hours(work_package, loader)diff --git a/spec/services/work_packages/delete_service_integration_spec.rb b/spec/services/work_packages/delete_service_integration_spec.rbindex de8248cdb98..bf4e216ecd2 100644--- a/spec/services/work_packages/delete_service_integration_spec.rb+++ b/spec/services/work_packages/delete_service_integration_spec.rb@@ -39,11 +39,12 @@ RSpec.describe WorkPackages::DeleteService, 'integration', type: :model doenddescribe 'deleting a child with estimated_hours set' do- let(:parent) { create(:work_package, project:) }+ let(:parent) { create(:work_package, project:, subject: 'parent') }let(:child) docreate(:work_package,project:,parent:,+ subject: 'child',estimated_hours: 123)end@@ -65,7 +66,8 @@ RSpec.describe WorkPackages::DeleteService, 'integration', type: :model doexpect(parent.derived_estimated_hours).to eq 123expect(parent.estimated_hours).to be_nil- expect(subject).to be_success+ expect(subject).to be_success, "Expected service call to be successful, but failed\n" \+ "service call errors: #{subject.errors.full_messages.inspect}"parent.reloaddiff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rbindex 18caa8b27d3..e91f3f18f7a 100644--- a/spec/services/work_packages/update_ancestors_service_spec.rb+++ b/spec/services/work_packages/update_ancestors_service_spec.rb@@ -43,6 +43,26 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model dolet(:ignore_non_working_days) { [false, false, false] }describe 'done_ratio/estimated_hours/remaining_hours propagation' do+ context 'when setting estimated hours of a work package' do+ shared_let(:work_package) { create(:work_package, status: open_status) }++ before do+ work_package.estimated_hours = 2.0+ end++ subject(:call_result) do+ described_class.new(user:, work_package:)+ .call(%i(estimated_hours))+ end++ it 'sets its derived value to the same value' do+ expect { call_result }+ .to change(work_package, :derived_estimated_hours).from(nil).to(2.0)+ expect(call_result).to be_success+ expect(call_result.dependent_results).to be_empty+ end+ end+context 'for the new ancestor chain' doshared_examples 'attributes of parent having children' dobefore do@@ -143,6 +163,26 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doendendend++ context 'with parent having estimated hours' do+ before do+ parent.update(estimated_hours: 5.0)+ end++ it_behaves_like 'attributes of parent having children' do+ let(:estimated_hours) do+ [nil, 2.0, 0.0]+ end++ # 66.67 rounded - previous wrong result: 100+ let(:aggregate_done_ratio) do+ 67+ end+ let(:aggregate_estimated_hours) do+ 7.0+ end+ end+ endendcontext 'with some values same for done ratio' do@@ -219,6 +259,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doshared_let(:sibling_done_ratio) { 50 }shared_let(:sibling_estimated_hours) { 7.0 }shared_let(:sibling_remaining_hours) { 3.5 }+ shared_let(:parent_estimated_hours) { 3.0 }shared_let(:grandparent) docreate(:work_package,@@ -227,6 +268,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doshared_let(:parent) docreate(:work_package,subject: 'parent',+ estimated_hours: parent_estimated_hours,parent: grandparent)endshared_let(:sibling) do@@ -271,7 +313,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doparent.reloadend.to change(parent, :done_ratio).to(sibling_done_ratio)- .and change(parent, :derived_estimated_hours).to(sibling_estimated_hours)+ .and change(parent, :derived_estimated_hours).to(parent_estimated_hours + sibling_estimated_hours).and change(parent, :derived_remaining_hours).to(sibling_remaining_hours)end@@ -281,7 +323,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model dograndparent.reloadend.to change(grandparent, :done_ratio).to(sibling_done_ratio)- .and change(grandparent, :derived_estimated_hours).to(sibling_estimated_hours)+ .and change(grandparent, :derived_estimated_hours).to(parent_estimated_hours + sibling_estimated_hours).and change(grandparent, :derived_remaining_hours).to(sibling_remaining_hours)endend@@ -291,6 +333,8 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doshared_let(:done_ratio) { 50 }shared_let(:estimated_hours) { 7.0 }shared_let(:remaining_hours) { 3.5 }+ shared_let(:parent_estimated_hours) { 3.0 }+ shared_let(:parent_remaining_hours) { 1.5 }shared_let(:grandparent) docreate(:work_package,@@ -299,6 +343,8 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doshared_let(:parent) docreate(:work_package,subject: 'parent',+ estimated_hours: parent_estimated_hours,+ remaining_hours: parent_remaining_hours,parent: grandparent)endshared_let(:work_package) do@@ -327,8 +373,8 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doparent.reloadend.to change(parent, :done_ratio).to(done_ratio)- .and change(parent, :derived_estimated_hours).to(estimated_hours)- .and change(parent, :derived_remaining_hours).to(remaining_hours)+ .and change(parent, :derived_estimated_hours).to(parent_estimated_hours + estimated_hours)+ .and change(parent, :derived_remaining_hours).to(parent_remaining_hours + remaining_hours)endit 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the new grandparent' do@@ -337,8 +383,8 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model dograndparent.reloadend.to change(grandparent, :done_ratio).to(done_ratio)- .and change(grandparent, :derived_estimated_hours).to(estimated_hours)- .and change(grandparent, :derived_remaining_hours).to(remaining_hours)+ .and change(grandparent, :derived_estimated_hours).to(parent_estimated_hours + estimated_hours)+ .and change(grandparent, :derived_remaining_hours).to(parent_remaining_hours + remaining_hours)endend