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

Add duration setter for WorkPackageRepresenter

Dombi Attila0bb487ab269a
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 20b7953f534..783f909fd8a 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -619,6 +619,7 @@ en:
confirmation: "doesn't match %{attribute}."
could_not_be_copied: "%{dependency} could not be (fully) copied."
does_not_exist: "does not exist."
+ duration_for_milestones_not_writable: "Milestone typed work packages don't have a duration."
error_enterprise_only: "is only available in the OpenProject Enterprise Edition"
error_unauthorized: "may not be accessed."
error_readonly: "was attempted to be written but is not writable."
diff --git a/lib/api/v3/utilities/date_time_formatter.rb b/lib/api/v3/utilities/date_time_formatter.rb
index 1c32a2302ab..b9442a180ac 100644
--- a/lib/api/v3/utilities/date_time_formatter.rb
+++ b/lib/api/v3/utilities/date_time_formatter.rb
@@ -98,6 +98,12 @@ module API
duration)
end
end
+
+ def self.parse_duration_to_days(duration, property_name, allow_nil: false)
+ return nil if duration.nil? && allow_nil
+
+ parse_duration_to_hours(duration, property_name).to_i / 24
+ end
end
end
end
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index d73da740f72..9276117bf98 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -593,6 +593,16 @@ module API
# noop
end
+ def duration=(value)
+ if represented.milestone?
+ raise NoMethodError, I18n.t('activerecord.errors.messages.duration_for_milestones_not_writable')
+ end
+
+ represented.duration = datetime_formatter.parse_duration_to_days(value,
+ 'duration',
+ allow_nil: true)
+ end
+
def ordered_custom_actions
# As the custom actions are sometimes set as an array
represented.custom_actions(current_user).to_a.sort_by(&:position)
diff --git a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb b/spec/lib/api/v3/utilities/date_time_formatter_spec.rb
index ce2a18b7111..2280fa979a7 100644
--- a/spec/lib/api/v3/utilities/date_time_formatter_spec.rb
+++ b/spec/lib/api/v3/utilities/date_time_formatter_spec.rb
@@ -56,6 +56,26 @@ describe ::API::V3::Utilities::DateTimeFormatter do
end
end
+ shared_examples_for 'rejects durations with parsing errors' do
+ it 'rejects parsing non sense' do
+ expect do
+ subject.send(method, 'foo', 'prop')
+ end.to raise_error(API::Errors::PropertyFormatError)
+ end
+
+ it 'rejects parsing pure number strings' do
+ expect do
+ subject.send(method, '5', 'prop')
+ end.to raise_error(API::Errors::PropertyFormatError)
+ end
+
+ it 'rejects parsing pure numbers' do
+ expect do
+ subject.send(method, 5, 'prop')
+ end.to raise_error(API::Errors::PropertyFormatError)
+ end
+ end
+
describe 'format_date' do
it 'formats dates' do
expect(subject.format_date(date)).to eq(date.iso8601)
@@ -185,26 +205,35 @@ describe ::API::V3::Utilities::DateTimeFormatter do
expect(subject.parse_duration_to_hours('P1D', 'prop')).to eq(24.0)
end
- it 'rejects parsing non sense' do
- expect do
- subject.parse_duration_to_hours('foo', 'prop')
- end.to raise_error(API::Errors::PropertyFormatError)
+ it_behaves_like 'rejects durations with parsing errors' do
+ let(:method) { :parse_duration_to_hours }
end
- it 'rejects parsing pure number strings' do
- expect do
- subject.parse_duration_to_hours('5', 'prop')
- end.to raise_error(API::Errors::PropertyFormatError)
+ it_behaves_like 'can parse nil' do
+ let(:method) { :parse_duration_to_hours }
+ let(:input) { 'PT5H' }
end
+ end
- it 'rejects parsing pure numbers' do
- expect do
- subject.parse_duration_to_hours(5, 'prop')
- end.to raise_error(API::Errors::PropertyFormatError)
+ describe 'parse_duration_to_days' do
+ it 'parses ISO 8601 durations of full days' do
+ expect(subject.parse_duration_to_days('P5D', 'prop')).to eq(5)
+ end
+
+ it 'parses ISO 8601 durations of fractional days as whole' do
+ expect(subject.parse_duration_to_days('P5DT18H30M', 'prop')).to eq(5)
+ end
+
+ it 'parses ISO 8601 durations of hours as 0 days' do
+ expect(subject.parse_duration_to_days('PT1H30M', 'prop')).to eq(0)
+ end
+
+ it_behaves_like 'rejects durations with parsing errors' do
+ let(:method) { :parse_duration_to_days }
end
it_behaves_like 'can parse nil' do
- let(:method) { :parse_duration_to_hours }
+ let(:method) { :parse_duration_to_days }
let(:input) { 'PT5H' }
end
end
diff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
index 02deeccd36b..a5632a3ddc0 100644
--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
@@ -1377,5 +1377,25 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
end
end
end
+
+ describe 'parsing' do
+ describe 'duration', with_flag: { work_packages_duration_field_active: true } do
+ subject { representer }
+
+ it 'parses form iso8601 format' do
+ subject.duration = 'P6D'
+ expect(subject.represented.duration).to eq(6)
+ end
+
+ context 'when the work_package is a milestone' do
+ let(:type_milestone) { true }
+
+ it 'raises an error' do
+ expect { subject.duration = 'P6D' }
+ .to raise_error NoMethodError, /Milestone typed work packages don't have a duration/
+ end
+ end
+ end
+ end
end
# rubocop:enable RSpec:MultipleMemoizedHelpers