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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Update the work package query timestamps to allow the date bare words
Dombi Attila6b4bf96663fd
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rbindex 746706c3dcd..acbfadae739 100644--- a/app/models/timestamp.rb+++ b/app/models/timestamp.rb@@ -32,6 +32,9 @@ class Timestampclass Exception < StandardError; endclass TimestampParser+ DATE_KEYWORD_REGEX =+ /^(?:yesterday|lastWorkingDay|lastWeek|lastMonth)@(?:([0-1]?[0-9]|2[0-3]):[0-5]?[0-9])$/+def initialize(string)@original_string = stringend@@ -42,8 +45,8 @@ class Timestampcase @timestamp_stringwhen /[+-]?P/ # ISO8601 "Period"ActiveSupport::Duration.parse(@timestamp_string).iso8601- when /@/ # Built in date bare words- parse_relative_date(@timestamp_string).iso8601+ when DATE_KEYWORD_REGEX # Built in date keywords+ @timestamp_stringelseTime.zone.iso8601(@timestamp_string).iso8601end@@ -51,26 +54,6 @@ class Timestampraise e.class, "The string \"#{@original_string}\" cannot be parsed to a Timestamp."end- def parse_relative_date(string)- date = case string- when /yesterday@/- 1.day.ago- when /^lastWorkingDay@/- Day.last_working.date || 1.day.ago- when /^lastWeek@/- 1.week.ago- when /^lastMonth@/- 1.month.ago- end-- hours = string[/@(([0-1]?[0-9]|2[0-3]):[0-5][0-9])$/, 1].presence- unless date && hours- raise ArgumentError, "The string \"#{string}\" cannot be parsed to a Timestamp."- end-- Time.zone.parse(hours, date)- end-class << self# rubocop:disable Metrics/AbcSize# rubocop:disable Metrics/PerceivedComplexity@@ -135,14 +118,22 @@ class Timestampelseraise Timestamp::Exception,"Argument type not supported. " \- "Please provide an ISO-8601 String or anything that responds to :iso8601, e.g. a Time."+ "Please provide an ISO-8601 or a relative date keyword String, or anything that responds to :iso8601, e.g. a Time."endenddef relative?+ duration? || relative_date_keyword?+ end++ def duration?to_s.first == "P" # ISO8601 "Period"end+ def relative_date_keyword?+ TimestampParser::DATE_KEYWORD_REGEX.match?(to_s)+ end+def to_siso8601end@@ -168,19 +159,38 @@ class Timestampenddef to_time- if relative?+ if duration?Time.zone.now - to_duration.abs+ elsif relative_date_keyword?+ relative_date_keyword_to_timeelseTime.zone.parse(self)endenddef to_duration- if relative?+ if duration?ActiveSupport::Duration.parse(self)else- raise Timestamp::Exception, "This timestamp is absolute and cannot be represented as ActiveSupport::Duration."+ raise Timestamp::Exception, "This timestamp does not contain a duration cannot be represented as ActiveSupport::Duration."+ end+ end++ def relative_date_keyword_to_time+ unless relative_date_keyword?+ raise ArgumentError, "This timestamp does not contain a relative date keyword and cannot be represented as Time."end++ relative_date_keyword, time_part = @timestamp_string.split('@')++ date = case relative_date_keyword+ when 'yesterday' then 1.day.ago+ when 'lastWorkingDay' then Day.last_working.date || 1.day.ago+ when 'lastWeek' then 1.week.ago+ when 'lastMonth' then 1.month.ago+ end++ Time.zone.parse(time_part, date)enddef as_json(*_args)diff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rbindex 4d740eecc68..8d20aad3f9b 100644--- a/spec/models/timestamp_spec.rb+++ b/spec/models/timestamp_spec.rb@@ -57,7 +57,7 @@ describe Timestamp doit "returns an absolute described_class representing that time (up to full seconds)" doexpect(subject).to be_a described_class- expect(subject.relative?).to be false+ expect(subject).not_to be_relativeexpect(subject.to_time).to eq Time.zone.at(time.to_i)endend@@ -79,7 +79,7 @@ describe Timestamp doendit "returns a relative timestamp" do- expect(subject.relative?).to be true+ expect(subject).to be_relativeendit "corresponds to a duration of 0 seconds (ago)" do@@ -95,7 +95,7 @@ describe Timestamp doexpect(subject).to be_a described_classexpect(subject.to_s).to eq "PT10S"expect(subject.to_duration).to eq ActiveSupport::Duration.build(10)- expect(subject.relative?).to be true+ expect(subject).to be_relativeend{@@ -122,7 +122,7 @@ describe Timestamp doexpect(subject).to be_a described_classexpect(subject.to_s).to eq "2022-10-29T21:55:58Z"expect(subject.to_time).to eq Time.zone.parse("2022-10-29T21:55:58Z")- expect(subject.relative?).to be false+ expect(subject).not_to be_relativeendend@@ -133,7 +133,7 @@ describe Timestamp doit "returns a Timestamp representing the current time" doexpect(subject).to be_a described_classexpect(subject.to_iso8601).to eq "PT0S"- expect(subject.relative?).to be true+ expect(subject).to be_relativeendend@@ -144,7 +144,7 @@ describe Timestamp doexpect(subject).to be_a described_classexpect(subject.to_iso8601).to eq "P-1Y"expect(subject.to_duration).to eq ActiveSupport::Duration.build(-1.year)- expect(subject.relative?).to be true+ expect(subject).to be_relativeendend@@ -155,7 +155,7 @@ describe Timestamp doexpect(subject).to be_a described_classexpect(subject.to_iso8601).to eq "P-1Y-2M"expect(subject.to_duration).to eq ActiveSupport::Duration.build(-1.year - 2.months)- expect(subject.relative?).to be true+ expect(subject).to be_relativeendend@@ -166,18 +166,19 @@ describe Timestamp doexpect(subject).to be_a described_classexpect(subject.to_iso8601).to eq "2022-01-01T00:00:00Z"expect(subject.to_time).to eq Time.zone.parse("2022-01-01T00:00:00Z")- expect(subject.relative?).to be false+ expect(subject).not_to be_relativeendendend- describe "when providing bare words for dates" do+ describe "when providing relative date keywords" dodescribe "yesterday@12:00" dosubject { described_class.parse("yesterday@12:00") }it "returns a Timestamp representing the yesterday at 12:00 pm" doexpect(subject).to be_a described_class- expect(subject.relative?).to be false+ expect(subject).to be_relative+ expect(subject).to be_relative_date_keywordexpect(subject.to_time).to eq 1.day.ago.change(hour: 12)endend@@ -192,7 +193,8 @@ describe Timestamp doit "returns a Timestamp representing the last working day at 12:00 pm" doexpect(subject).to be_a described_class- expect(subject.relative?).to be false+ expect(subject).to be_relative+ expect(subject).to be_relative_date_keywordexpect(subject.to_time).to eq 2.days.ago.change(hour: 12)endend@@ -202,7 +204,8 @@ describe Timestamp doit "returns a Timestamp representing the last week at 12:00 pm" doexpect(subject).to be_a described_class- expect(subject.relative?).to be false+ expect(subject).to be_relative+ expect(subject).to be_relative_date_keywordexpect(subject.to_time).to eq 1.week.ago.change(hour: 12)endend@@ -212,7 +215,8 @@ describe Timestamp doit "returns a Timestamp representing the last month at 12:00 pm" doexpect(subject).to be_a described_class- expect(subject.relative?).to be false+ expect(subject).to be_relative+ expect(subject).to be_relative_date_keywordexpect(subject.to_time).to eq 1.month.ago.change(hour: 12)endend@@ -234,7 +238,7 @@ describe Timestamp doendend- describe "when providing something invalid with date bare words" do+ describe "when providing something invalid with relative date keywords" dosubject { described_class.parse("yesterday@") }it "raises an error" do@@ -242,7 +246,7 @@ describe Timestamp doendend- describe "when providing something invalid with date bare words#2" do+ describe "when providing something invalid with relative date keywords#2" dosubject { described_class.parse("yesterday@11:22:asd") }it "raises an error" do@@ -330,6 +334,14 @@ describe Timestamp doexpect(subject).to be trueendend++ describe "for a timestamp as a date keyword representing a point in time relative to now" do+ let(:timestamp) { described_class.new("lastWeek@12:00") }++ it "returns true" do+ expect(subject).to be true+ end+ endenddescribe "#iso8601" do@@ -351,6 +363,14 @@ describe Timestamp doexpect(subject).to eq ActiveSupport::Duration.build(10).iso8601endend++ describe "for a timestamp as a date keyword representing a point in time relative to now" do+ let(:timestamp) { described_class.new("yesterday@12:00") }++ it "returns an ISO8601 String representing the duration between that time and now" do+ expect(subject).to eq "yesterday@12:00"+ end+ endenddescribe "#to_s" dodiff --git a/spec/requests/api/v3/work_packages/index_resource_spec.rb b/spec/requests/api/v3/work_packages/index_resource_spec.rbindex c977be15f1c..2964be80544 100644--- a/spec/requests/api/v3/work_packages/index_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/index_resource_spec.rb@@ -773,14 +773,118 @@ describe 'API v3 Work package resource',endend+ context 'when the timestamps are relative date keywords' do+ let(:timestamps) { [Timestamp.parse('lastWeek@11:00'), Timestamp.parse('lastWorkingDay@12:00')] }++ it 'has an embedded link to the baseline work package' do+ expect(subject.body)+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.first).to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_links/self/href')+ end++ it 'has the absolute timestamps within the self links of the elements' do+ Timecop.freeze do+ expect(subject.body)+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.map(&:absolute)).to_json)+ .at_path('_embedded/elements/0/_links/self/href')+ end+ end++ it 'has the absolute timestamps within the collection self link' do+ Timecop.freeze do+ expected_self_href = { timestamps: api_v3_paths.timestamps_to_param_value(timestamps.map(&:absolute)) }.to_query+ expect(subject.body)+ .to include_json(expected_self_href.to_json)+ .at_path('_links/self/href')+ end+ end++ it 'has the relative timestamps within the _meta timestamps' do+ expect(subject.body)+ .to be_json_eql('lastWeek@11:00'.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/timestamp')+ expect(subject.body)+ .to be_json_eql('lastWorkingDay@12:00'.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/timestamp')+ expect(subject.body)+ .to be_json_eql('lastWorkingDay@12:00'.to_json)+ .at_path('_embedded/elements/0/_meta/timestamp')+ end++ describe "when the work package has not been present at the baseline time" do+ let(:created_at) { 10.days.ago }++ describe "attributesByTimestamp" do+ describe "0 (baseline attributes)" do+ describe "_meta" do+ describe "timestamp" do+ it 'has the baseline timestamp, which is the first timestmap' do+ expect(subject.body)+ .to be_json_eql('lastWeek@11:00'.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/timestamp')+ end+ end+ end+ end++ describe "1 (current attributes)" do+ describe "_meta" do+ describe "timestamp" do+ it 'has the current timestamp, which is the second timestamp' do+ expect(subject.body)+ .to be_json_eql('lastWorkingDay@12:00'.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/timestamp')+ end+ end+ end+ end+ end+ end++ describe "when the work package has not changed at all between the baseline and today" do+ let(:timestamps) { [Timestamp.parse('lastWorkingDay@12:00'), Timestamp.now] }++ describe "_meta" do+ describe "timestamp" do+ it 'has the current timestamp, which is the second timestamp, ' \+ 'in the same format as given in the request parameter' do+ expect(subject.body)+ .to be_json_eql("PT0S".to_json)+ .at_path('_embedded/elements/0/_meta/timestamp')+ end+ end+ end++ describe "attributesByTimestamp" do+ describe "_meta" do+ describe "timestamp" do+ it 'has the current timestamp, which is the second timestamp, ' \+ 'in the same format as given in the request parameter' do+ expect(subject.body)+ .to be_json_eql("PT0S".to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/timestamp')+ end++ it 'has the baseline timestamp, which is the first timestamp, ' \+ 'in the same format as given in the request parameter' do+ expect(subject.body)+ .to be_json_eql('lastWorkingDay@12:00'.to_json)+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/timestamp')+ end+ end+ end+ end+ end+ end+context "with caching" docontext "with relative timestamps" do- let(:timestamps) { [Timestamp.parse("P-2D"), Timestamp.now] }+ let(:timestamps) { [Timestamp.parse('lastWeek@12:00'), Timestamp.now] }let(:created_at) { '2015-01-01' }describe "when the filter becomes outdated" do# The work package has been updated 1 day ago, which is after the baseline- # date (2 days ago). When time progresses, the date of the update will be+ # date (last week). When time progresses, the date of the update will be# before the baseline date, because the baseline date is relative to the# current date. This means that the filter will become outdated and we cannot# use a cached result in this case.@@ -799,10 +903,10 @@ describe 'API v3 Work package resource',let(:search_term) { 'original' }it 'has the relative timestamps within the _meta timestamps' do- expect(timestamps.first.to_s).to eq('P-2D')+ expect(timestamps.first.to_s).to eq('lastWeek@12:00')expect(timestamps.first).to be_relativeexpect(subject.body)- .to be_json_eql('P-2D'.to_json)+ .to be_json_eql('lastWeek@12:00'.to_json).at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/timestamp')expect(subject.body).to be_json_eql('PT0S'.to_json)@@ -815,7 +919,7 @@ describe 'API v3 Work package resource',it "does not use an outdated cache" doget pathexpect do- Timecop.travel 5.days do+ Timecop.travel 1.week doget pathendend.to change {diff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rbindex f35c24b82d6..5cb859beb43 100644--- a/spec/requests/api/v3/work_packages/show_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/show_resource_spec.rb@@ -418,6 +418,93 @@ describe 'API v3 Work package resource',endendend++ context 'when the timestamps are relative date keywords' do+ let(:timestamps) { [Timestamp.new('lastWeek@12:00'), Timestamp.now] }++ it 'has an embedded link to the baseline work package' do+ expect(subject)+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.first).to_json)+ .at_path('_embedded/attributesByTimestamp/0/_links/self/href')+ end++ it 'has the absolute timestamps within the self link' do+ Timecop.freeze do+ expect(subject)+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.map(&:absolute)).to_json)+ .at_path('_links/self/href')+ end+ end++ describe "attributesByTimestamp" do+ describe '_meta' do+ describe 'timestamp' do+ it 'has the relative timestamps' do+ expect(subject)+ .to be_json_eql('lastWeek@12:00'.to_json)+ .at_path('_embedded/attributesByTimestamp/0/_meta/timestamp')+ expect(subject)+ .to be_json_eql('PT0S'.to_json)+ .at_path('_embedded/attributesByTimestamp/1/_meta/timestamp')+ end+ end+ end+ end++ context "with caching" do+ before { login_as current_user }++ context "with relative timestamps" do+ let(:timestamps) { [Timestamp.parse("lastWeek@12:00"), Timestamp.now] }+ let(:created_at) { '2015-01-01' }++ describe "attributesByTimestamp" do+ it "does not cache the self link" do+ get get_path+ expect do+ Timecop.travel 1.day do+ get get_path+ end+ end.to change {+ JSON.parse(last_response.body)+ .dig("_embedded", "attributesByTimestamp", 0, "_links", "self", "href")+ }+ end++ it "does not cache the attributes" do+ get get_path+ expect do+ Timecop.travel 1.week do+ get get_path+ end+ end.to change {+ JSON.parse(last_response.body)+ .dig("_embedded", "attributesByTimestamp", 0, "subject")+ }+ end+ end++ describe "_meta" do+ describe "exists" do+ let(:timestamps) { [Timestamp.parse("lastWorkingDay@12:00")] }+ let(:created_at) { 25.hours.ago }++ it "is not cached" do+ get get_path+ expect do+ Timecop.travel 2.days do+ get get_path+ end+ end.to change {+ JSON.parse(last_response.body)+ .dig("_meta", "exists")+ }+ end+ end+ end+ end+ end+ endendendenddiff --git a/spec/services/api/v3/parse_query_params_service_spec.rb b/spec/services/api/v3/parse_query_params_service_spec.rbindex 0a9583c2963..8070ec75e0a 100644--- a/spec/services/api/v3/parse_query_params_service_spec.rb+++ b/spec/services/api/v3/parse_query_params_service_spec.rb@@ -341,6 +341,16 @@ describe API::V3::ParseQueryParamsService,let(:expected) { { timestamps: [Timestamp.new("P-1Y"), Timestamp.parse("P-0Y")] } }end+ it_behaves_like 'transforms' do+ let(:params) { { timestamps: "lastMonth@11:00, now" } }+ let(:expected) { { timestamps: [Timestamp.parse("lastMonth@11:00"), Timestamp.parse("P-0Y")] } }+ end++ it_behaves_like 'transforms' do+ let(:params) { { timestamps: "lastMonth@11:00, lastWeek@12:00" } }+ let(:expected) { { timestamps: [Timestamp.parse("lastMonth@11:00"), Timestamp.parse("lastWeek@12:00")] } }+ end+describe "for invalid parameters" dolet(:params) { { timestamps: "foo,bar" } }diff --git a/spec/services/api/v3/work_package_collection_from_query_service_spec.rb b/spec/services/api/v3/work_package_collection_from_query_service_spec.rbindex 1f09faf04f8..8abd227a648 100644--- a/spec/services/api/v3/work_package_collection_from_query_service_spec.rb+++ b/spec/services/api/v3/work_package_collection_from_query_service_spec.rb@@ -236,7 +236,7 @@ describe API::V3::WorkPackageCollectionFromQueryService,endcontext 'when timestamps are given' do- let(:timestamps) { [Timestamp.parse("P-1Y"), Timestamp.now] }+ let(:timestamps) { [Timestamp.parse("P-1Y"), Timestamp.parse("lastWeek@12:00"), Timestamp.now] }let(:query) { build_stubbed(:query, timestamps:) }it 'has the query timestamps' dodiff --git a/spec/services/update_query_from_params_service_spec.rb b/spec/services/update_query_from_params_service_spec.rbindex 45b842472b2..03a77cbc558 100644--- a/spec/services/update_query_from_params_service_spec.rb+++ b/spec/services/update_query_from_params_service_spec.rb@@ -192,7 +192,13 @@ describe UpdateQueryFromParamsService,endcontext "when providing timestamps" do- let(:timestamps) { [Timestamp.parse("2022-10-29T23:01:23Z"), Timestamp.parse("PT0S")] }+ let(:timestamps) do+ [+ Timestamp.parse("2022-10-29T23:01:23Z"),+ Timestamp.parse("lastWeek@12:00"),+ Timestamp.parse("PT0S")+ ]+ endlet(:params) { { timestamps: } }it 'sets the timestamps' do