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

Ensure remaining_hours isn't writable on "status-based" mode

Aaron Contreras40c4397e7b22
diff --git a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb
index 69f6c1ed92f..e17074c1d76 100644
--- a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb
+++ b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb
@@ -168,12 +168,14 @@ module API
schema :remaining_time,
name_source: :remaining_hours,
type: 'Duration',
- required: false
+ required: false,
+ writable: ->(*) { !WorkPackage.use_status_for_done_ratio? }
schema :derived_remaining_time,
name_source: :derived_remaining_hours,
type: 'Duration',
- required: false
+ required: false,
+ writable: false
schema :spent_time,
type: 'Duration',
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index d4e23d11d87..face30dda14 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -409,6 +409,7 @@ module API
datetime_formatter.format_duration_from_hours(represented.remaining_hours,
allow_nil: true)
end,
+ writable: ->(*) { !WorkPackage.use_status_for_done_ratio? },
render_nil: true
property :derived_remaining_time,
@@ -417,6 +418,7 @@ module API
datetime_formatter.format_duration_from_hours(represented.derived_remaining_hours,
allow_nil: true)
end,
+ writable: ->(*) { !WorkPackage.use_status_for_done_ratio? },
render_nil: true
property :duration,
diff --git a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb
index cc573c5e4cd..6210b75d02a 100644
--- a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb
@@ -611,6 +611,17 @@ RSpec.describe API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:required) { false }
let(:writable) { true }
end
+
+ context 'when on the "status-based" % complete mode',
+ with_settings: { work_package_done_ratio: 'status' } do
+ it_behaves_like 'has basic schema properties' do
+ let(:path) { 'remainingTime' }
+ let(:type) { 'Duration' }
+ let(:name) { I18n.t('activerecord.attributes.work_package.remaining_hours') }
+ let(:required) { false }
+ let(:writable) { false }
+ end
+ end
end
describe 'derivedRemainingTime' do
diff --git a/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb
index ed71c552992..4a7e2827a05 100644
--- a/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb
@@ -126,6 +126,25 @@ RSpec.describe API::V3::WorkPackages::WorkPackagePayloadRepresenter do
end
end
+ describe 'remaining hours' do
+ it { is_expected.to have_json_path('remainingTime') }
+
+ it do
+ expect(subject).to be_json_eql(work_package.estimated_hours.to_json)
+ .at_path('estimatedTime')
+ end
+
+ context 'when not set' do
+ it { is_expected.to have_json_type(NilClass).at_path('estimatedTime') }
+ end
+
+ context 'when set' do
+ let(:work_package) { build(:work_package, estimated_hours: 7, remaining_hours: 5) }
+
+ it { is_expected.to have_json_type(String).at_path('estimatedTime') }
+ end
+ end
+
describe 'startDate' do
before do
allow(work_package)