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

Setting remaining work sets work if unset

Christophe Bliard22c92f98c3eb
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 68d8d8d4bfd..60adf9cbd93 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -360,18 +360,16 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
work && remaining_work && remaining_work > work
end
- # rubocop:disable Metrics/AbcSize
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.done_ratio.present?
+ if work_package.remaining_hours.present?
work_package.estimated_hours = estimated_hours_from_done_ratio_and_remaining_hours
end
end
- # rubocop:enable Metrics/AbcSize
# When in "Status-based" mode for % Complete, remaining hours are based
# on the computation of it derived from the status's default done ratio
@@ -397,7 +395,7 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
# rubocop:enable Metrics/AbcSize,Metrics/PerceivedComplexity
def estimated_hours_from_done_ratio_and_remaining_hours
- remaining_ratio = 1.0 - (work_package.done_ratio / 100.0)
+ remaining_ratio = 1.0 - ((work_package.done_ratio || 0) / 100.0)
work_package.remaining_hours / remaining_ratio
end
diff --git a/modules/backlogs/spec/api/work_package_resource_spec.rb b/modules/backlogs/spec/api/work_package_resource_spec.rb
index 8954e078c88..d2cc6590f82 100644
--- a/modules/backlogs/spec/api/work_package_resource_spec.rb
+++ b/modules/backlogs/spec/api/work_package_resource_spec.rb
@@ -39,6 +39,7 @@ RSpec.describe "API v3 Work package resource" do
create(:work_package,
project:,
story_points: 8,
+ estimated_hours: 5,
remaining_hours: 5)
end
let(:wp_path) { "/api/v3/work_packages/#{work_package.id}" }
diff --git a/modules/backlogs/spec/services/stories/create_service_spec.rb b/modules/backlogs/spec/services/stories/create_service_spec.rb
index 4f0caf21280..c1ca11146b6 100644
--- a/modules/backlogs/spec/services/stories/create_service_spec.rb
+++ b/modules/backlogs/spec/services/stories/create_service_spec.rb
@@ -30,22 +30,12 @@ require "spec_helper"
RSpec.describe Stories::CreateService, type: :model do
let(:priority) { create(:priority) }
- let(:project) do
- project = create(:project, types: [type_feature])
-
- create(:member,
- principal: user,
- project:,
- roles: [role])
- project
- end
- let(:role) { create(:project_role, permissions:) }
- let(:permissions) { %i(add_work_packages manage_subtasks assign_versions) }
+ let(:project) { create(:project, types: [type_feature]) }
let(:status) { create(:status) }
let(:type_feature) { create(:type_feature) }
let(:user) do
- create(:user)
+ create(:user, member_with_permissions: { project => %i(add_work_packages manage_subtasks assign_versions) })
end
let(:instance) do
@@ -67,8 +57,6 @@ RSpec.describe Stories::CreateService, type: :model do
let(:version) { create(:version, project:) }
let(:story) do
- project.enabled_module_names += ["backlogs"]
-
create(:story,
version:,
project:,
@@ -83,24 +71,30 @@ RSpec.describe Stories::CreateService, type: :model do
subject { instance.call(attributes:) }
- describe "remaining_hours" do
+ describe "creating a feature in a story" do
before do
subject
end
- context "with the story having remaining_hours" do
+ context "with the feature being created with some remaining work (remaining hours)" do
let(:remaining_hours) { 15.0 }
- it "does update the parents remaining hours" do
- expect(story.reload.derived_remaining_hours).to eq(15)
+ it "updates the story total work, total remaining work, and total % complete" do
+ expect(subject).to be_success
+ expect(story.reload).to have_attributes(derived_estimated_hours: 15,
+ derived_remaining_hours: 15,
+ derived_done_ratio: 0)
end
end
- context "with the subtask not having remaining_hours" do
+ context "with the feature being created without any remaining work (remaining hours)" do
let(:remaining_hours) { nil }
- it "does not note remaining hours to be changed" do
- expect(story.reload.remaining_hours).to be_nil
+ it "does not change the story total work, total remaining work, and total % complete" do
+ expect(subject).to be_success
+ expect(story.reload).to have_attributes(derived_estimated_hours: nil,
+ derived_remaining_hours: nil,
+ derived_done_ratio: nil)
end
end
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 7ce6c30b859..eff8902fed8 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -466,6 +466,13 @@ 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 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
end
end
end