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

Detect cases where remaining work is identical but changed by user nonetheless

Christophe Bliardf80ed8c94ee8
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 7ee4ef34457..6f1a4460a59 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -332,6 +332,7 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
end
def update_remaining_hours_from_percent_complete
+ return if work_package.remaining_hours_came_from_user?
return if work_package.estimated_hours&.negative?
work_package.remaining_hours = remaining_hours_from_done_ratio_and_estimated_hours
@@ -386,8 +387,7 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
update_remaining_hours_from_percent_complete
elsif WorkPackage.use_field_for_done_ratio? &&
work_package.estimated_hours_changed?
- # Here's the issue | Ask Christophe
- return if work_package.remaining_hours_changed?
+ return if work_package.remaining_hours_came_from_user?
return if work_package.estimated_hours&.negative?
return if work_was_unset_and_remaining_work_is_set? # remaining work is kept and % complete will be set
@@ -399,7 +399,6 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
end
end
end
-
# rubocop:enable Metrics/AbcSize,Metrics/PerceivedComplexity
def estimated_hours_from_done_ratio_and_remaining_hours
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index c7b4279c2f8..b776c6014c3 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -199,6 +199,13 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call",
description: "recomputes remaining work according to the % complete value of the new status"
end
+
+ context "when floating point operations are inaccurate (2.4000000000000004h)" do
+ let(:call_attributes) { { estimated_hours: 8.0, status: status_70_pct_complete } }
+ let(:expected_attributes) { { remaining_hours: 2.4 } } # would be 2.4000000000000004 without rounding
+
+ it_behaves_like "service call", description: "remaining work is computed and rounded (2.4)"
+ end
end
context "given a work package with work and remaining work unset, and a status with 0% complete" do
@@ -378,10 +385,21 @@ RSpec.describe WorkPackages::SetAttributesService,
context "when work is changed and remaining work is unset" do
let(:call_attributes) { { estimated_hours: 8.0, remaining_hours: nil } }
- let(:expected_attributes) { { remaining_hours: 2.4 } } # would be 2.4000000000000004 without rounding
+ let(:expected_attributes) { call_attributes.dup }
let(:expected_kept_attributes) { %w[done_ratio] }
- it_behaves_like "service call", description: "% complete is kept and remaining work is recomputed (and rounded)"
+ it_behaves_like "service call",
+ description: "% complete is kept and remaining work is kept unset and not recomputed" \
+ "(error state to be detected by contract)"
+ end
+
+ context "when work is increased and remaining work is set to its current value (to prevent it from being increased)" do
+ # work changed by +10h
+ let(:call_attributes) { { estimated_hours: 10.0 + 10.0, remaining_hours: 3 } }
+ let(:expected_attributes) { { remaining_hours: 3.0, done_ratio: 85 } }
+
+ it_behaves_like "service call",
+ description: "remaining work is kept (not increased), and % complete is updated accordingly"
end
end
@@ -459,6 +477,16 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call", description: "remaining work is set to the same value and % complete is set to 0%"
end
+ context "when work is changed and remaining work is unset" do
+ let(:call_attributes) { { estimated_hours: 10.0, remaining_hours: nil } }
+ let(:expected_attributes) { call_attributes.dup }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call",
+ description: "% complete is kept and remaining work is kept unset and not recomputed" \
+ "(error state to be detected by contract)"
+ end
+
context "when work is changed to a negative value" do
let(:call_attributes) { { estimated_hours: -1.0 } }
let(:expected_kept_attributes) { %w[remaining_hours done_ratio] }
@@ -536,6 +564,16 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call", description: "% complete is updated accordingly"
end
+ context "when work is set and remaining work is unset" do
+ let(:call_attributes) { { estimated_hours: 10.0, remaining_hours: nil } }
+ let(:expected_attributes) { call_attributes.dup }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ it_behaves_like "service call",
+ description: "% complete is kept and remaining work is kept unset and not recomputed" \
+ "(error state to be detected by contract)"
+ end
+
context "when work and remaining work are both set to negative values" do
let(:call_attributes) { { estimated_hours: -10, remaining_hours: -5 } }
let(:expected_kept_attributes) { %w[done_ratio] }