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

Handle negative work and/or remaining work values

Christophe Bliarde838f2ccfe79
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index acff442134c..5ca5ba983ff 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -331,6 +331,8 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
end
def update_remaining_hours_from_percent_complete
+ return if work_package.estimated_hours&.negative?
+
work_package.remaining_hours = remaining_hours_from_done_ratio_and_estimated_hours
end
@@ -363,12 +365,14 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
def update_estimated_hours
return unless WorkPackage.use_field_for_done_ratio?
return if work_package.estimated_hours_changed?
- return if work_package.estimated_hours.present?
return unless work_package.remaining_hours_changed?
- if work_package.remaining_hours.present?
- work_package.estimated_hours = estimated_hours_from_done_ratio_and_remaining_hours
- end
+ work = work_package.estimated_hours
+ remaining_work = work_package.remaining_hours
+ return if work.present?
+ return if remaining_work.nil? || remaining_work.negative?
+
+ work_package.estimated_hours = estimated_hours_from_done_ratio_and_remaining_hours
end
# When in "Status-based" mode for % Complete, remaining hours are based
@@ -378,10 +382,11 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
# rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity
def update_remaining_hours
if WorkPackage.use_status_for_done_ratio?
- work_package.remaining_hours = remaining_hours_from_done_ratio_and_estimated_hours
+ update_remaining_hours_from_percent_complete
elsif WorkPackage.use_field_for_done_ratio? &&
work_package.estimated_hours_changed?
return if work_package.remaining_hours_changed?
+ 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
if work_package.estimated_hours.nil? || work_package.remaining_hours.nil?
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 08748126414..c7b4279c2f8 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -177,13 +177,21 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call", description: "unsets remaining work"
end
- context "when work is modified" do
+ context "when work is changed" do
let(:call_attributes) { { estimated_hours: 5.0 } }
let(:expected_attributes) { { remaining_hours: 2.5 } }
it_behaves_like "service call", description: "recomputes remaining work accordingly"
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] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), and remaining work is kept"
+ end
+
context "when another status is set" do
let(:call_attributes) { { status: status_70_pct_complete } }
let(:expected_attributes) { { remaining_hours: 3.0 } }
@@ -217,6 +225,14 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call",
description: "remaining work is updated accordingly from work and % complete value of the status"
end
+
+ context "when work is set to a negative value" do
+ let(:call_attributes) { { estimated_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), and remaining work is kept"
+ end
end
end
end
@@ -315,6 +331,15 @@ RSpec.describe WorkPackages::SetAttributesService,
description: "remaining work becomes 0h, and % complete becomes 100%"
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] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and remaining work are kept"
+ end
+
context "when remaining work is changed" do
let(:call_attributes) { { remaining_hours: 2 } }
let(:expected_attributes) { { done_ratio: 80 } }
@@ -376,6 +401,15 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call", description: "% complete is kept and remaining work is updated accordingly"
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] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and remaining work are kept"
+ end
+
context "when remaining work is set" do
let(:call_attributes) { { remaining_hours: 1.0 } }
let(:expected_attributes) { call_attributes.merge(done_ratio: 90.0) }
@@ -424,6 +458,15 @@ 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 to a negative value" do
+ let(:call_attributes) { { estimated_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and remaining work are kept"
+ end
end
context "given a work package with remaining work being set, and work and % complete being unset" do
@@ -442,6 +485,32 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call",
description: "remaining work is kept to the same value and % complete is updated accordingly"
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] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and remaining work are kept"
+ end
+
+ context "when remaining work is changed" do
+ let(:call_attributes) { { remaining_hours: 12.0 } }
+ let(:expected_attributes) { { estimated_hours: 12.0, done_ratio: 0 } }
+
+ it_behaves_like "service call",
+ description: "work is set to the same value and % complete is set to 0%"
+ end
+
+ context "when remaining work is changed to a negative value" do
+ let(:call_attributes) { { remaining_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and work are kept"
+ end
end
context "given a work package with work and remaining work unset, and % complete being set" do
@@ -466,6 +535,13 @@ RSpec.describe WorkPackages::SetAttributesService,
it_behaves_like "service call", description: "% complete is updated accordingly"
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] }
+
+ it_behaves_like "service call", description: "is an error state (to be detected by contract), and % Complete is kept"
+ end
end
context "given a work package with work, remaining work, and % complete being unset" do
@@ -485,12 +561,30 @@ 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 set to a negative value" do
+ let(:call_attributes) { { estimated_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and remaining work are kept"
+ end
+
context "when remaining work is set" do
let(:call_attributes) { { remaining_hours: 10.0 } }
let(:expected_attributes) { { estimated_hours: 10.0, done_ratio: 0 } }
it_behaves_like "service call", description: "work is set to the same value and % complete is set to 0%"
end
+
+ context "when remaining work is set to a negative value" do
+ let(:call_attributes) { { remaining_hours: -1.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours done_ratio] }
+
+ it_behaves_like "service call",
+ description: "is an error state (to be detected by contract), " \
+ "and % complete and work are kept"
+ end
end
end
end