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
baseline api: handle special-value timestamps like "now" or "2y"
Sebastian Fiedlschuster3231f1d7f990
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rbindex 9b739bf9500..586f674d3dc 100644--- a/app/models/timestamp.rb+++ b/app/models/timestamp.rb@@ -41,10 +41,13 @@ class Timestampdef self.parse(iso8601_string)iso8601_string.strip!+ iso8601_string = substitute_special_shortcut_values(iso8601_string)if iso8601_string.start_with? "P" # ISO8601 "Period"- ActiveSupport::Duration.parse(iso8601_string)- elsif Time.zone.parse(iso8601_string).blank?- raise ArgumentError, "The string \"#{iso8601_string}\" cannot be parsed to a Time."+ iso8601_string = ActiveSupport::Duration.parse(iso8601_string).iso8601+ elsif (time = Time.zone.parse(iso8601_string)).present?+ iso8601_string = time.iso8601+ else+ raise ArgumentError, "The string \"#{iso8601_string}\" cannot be parsed to Time or ActiveSupport::Duration."endTimestamp.new(iso8601_string)end@@ -78,6 +81,10 @@ class Timestamp@timestamp_iso8601_string.to_send+ def to_iso8601+ iso8601+ end+def inspect"#<Timestamp \"#{iso8601}\">"end@@ -115,4 +122,31 @@ class Timestampendclass Exception < StandardError; end++ # rubocop:disable Metrics/AbcSize+ # rubocop:disable Metrics/PerceivedComplexity+ def self.substitute_special_shortcut_values(string)+ # map now to PT0S+ string = "PT0S" if string == "now"++ # map 1y to P1Y, 1m to P1M, 1w to P1W, 1d to P1D+ # map -1y to P-1Y, -1m to P-1M, -1w to P-1W, -1d to P-1D+ # map -1y1d to P-1Y-1D+ sign = "-" if string.start_with? "-"+ years = string.scan(/(\d+)y/).flatten.first+ months = string.scan(/(\d+)m/).flatten.first+ weeks = string.scan(/(\d+)w/).flatten.first+ days = string.scan(/(\d+)d/).flatten.first+ if years || months || weeks || days+ string = "P" \+ "#{sign if years}#{years}#{'Y' if years}" \+ "#{sign if months}#{months}#{'M' if months}" \+ "#{sign if weeks}#{weeks}#{'W' if weeks}" \+ "#{sign if days}#{days}#{'D' if days}"+ end++ string+ end+ # rubocop:enable Metrics/AbcSize+ # rubocop:enable Metrics/PerceivedComplexityenddiff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rbindex c49a2dff3cf..696d7e70f1e 100644--- a/spec/models/timestamp_spec.rb+++ b/spec/models/timestamp_spec.rb@@ -110,6 +110,51 @@ describe Timestamp doendend+ describe "when providing a special shortcut value" do+ describe "now" do+ subject { described_class.parse("now") }++ it "returns a Timestamp representing the current time" do+ expect(subject).to be_a described_class+ expect(subject.to_iso8601).to eq "PT0S"+ expect(subject.relative?).to be true+ end+ end++ describe "-1y" do+ subject { described_class.parse("-1y") }++ it "returns a Timestamp representing a time ago that duration" do+ expect(subject).to be_a described_class+ expect(subject.to_iso8601).to eq "P-1Y"+ expect(subject.to_duration).to eq ActiveSupport::Duration.build(-1.year)+ expect(subject.relative?).to be true+ end+ end++ describe "-1y2m" do+ subject { described_class.parse("-1y2m") }++ it "returns a Timestamp representing a time ago that duration" do+ expect(subject).to be_a described_class+ expect(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+ end+ end++ describe "2022-01-01" do+ subject { described_class.parse("2022-01-01") }++ it "returns a Timestamp representing that absolute time" do+ expect(subject).to be_a described_class+ expect(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+ end+ end+ end+describe "when providing something invalid" dosubject { described_class.parse("foo") }diff --git a/spec/services/api/v3/parse_query_params_service_spec.rb b/spec/services/api/v3/parse_query_params_service_spec.rbindex 7bb9aebba2a..1ec1befb08b 100644--- a/spec/services/api/v3/parse_query_params_service_spec.rb+++ b/spec/services/api/v3/parse_query_params_service_spec.rb@@ -336,6 +336,11 @@ describe ::API::V3::ParseQueryParamsService,let(:expected) { { timestamps: [Timestamp.parse("2022-10-29T23:01:23Z"), Timestamp.parse("P-0Y")] } }end+ it_behaves_like 'transforms' do+ let(:params) { { timestamps: "-1y, now" } }+ let(:expected) { { timestamps: [Timestamp.new("P-1Y"), Timestamp.parse("P-0Y")] } }+ end+describe "for invalid parameters" dolet(:params) { { timestamps: "foo,bar" } }diff --git a/spec/services/update_query_from_params_service_spec.rb b/spec/services/update_query_from_params_service_spec.rbindex 7c2a052669e..1c8e4a1a4c2 100644--- a/spec/services/update_query_from_params_service_spec.rb+++ b/spec/services/update_query_from_params_service_spec.rb@@ -191,8 +191,8 @@ describe UpdateQueryFromParamsService,endend- context "timestamps" do- let(:timestamps) { [Timestamp.parse("2022-10-29T23:01:23Z"), Timestamp.parse("P-0Y")] }+ context "when providing timestamps" do+ let(:timestamps) { [Timestamp.parse("2022-10-29T23:01:23Z"), Timestamp.parse("PT0S")] }let(:params) { { timestamps: } }it 'sets the timestamps' do