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

baseline api: parse timestamps in ParseQueryParamsService

Sebastian Fiedlschuster151adf555135
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index a27bab38f5c..9b739bf9500 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -40,6 +40,7 @@ class Timestamp
end
def self.parse(iso8601_string)
+ iso8601_string.strip!
if iso8601_string.start_with? "P" # ISO8601 "Period"
ActiveSupport::Duration.parse(iso8601_string)
elsif Time.zone.parse(iso8601_string).blank?
@@ -48,6 +49,15 @@ class Timestamp
Timestamp.new(iso8601_string)
end
+ # Take a comma-separated string of ISO-8601 timestamps and convert it
+ # into an array of Timestamp objects.
+ #
+ def self.parse_multiple(comma_separated_iso8601_string)
+ comma_separated_iso8601_string.to_s.split(",").compact_blank.collect do |iso8601_string|
+ Timestamp.parse(iso8601_string)
+ end
+ end
+
def self.now
new(ActiveSupport::Duration.build(0).iso8601)
end
@@ -100,5 +110,9 @@ class Timestamp
iso8601 == other.iso8601
end
+ def eql?(other)
+ self == other
+ end
+
class Exception < StandardError; end
end
diff --git a/app/services/api/v3/parse_query_params_service.rb b/app/services/api/v3/parse_query_params_service.rb
index 7fb5dc7f305..00c1f4021f0 100644
--- a/app/services/api/v3/parse_query_params_service.rb
+++ b/app/services/api/v3/parse_query_params_service.rb
@@ -37,8 +37,9 @@ module API
return json_parsed unless json_parsed.success?
parsed = parsed_params(params)
+ return parsed unless parsed.success?
- result = without_empty(parsed.merge(json_parsed.result), determine_allowed_empty(params))
+ result = without_empty(parsed.result.merge(json_parsed.result), determine_allowed_empty(params))
ServiceResult.success(result:)
end
@@ -60,7 +61,7 @@ module API
end
def parsed_params(params)
- {
+ ServiceResult.success result: {
group_by: group_by_from_params(params),
columns: columns_from_params(params),
display_sums: boolearize(params[:showSums]),
@@ -70,8 +71,13 @@ module API
highlighted_attributes: highlighted_attributes_from_params(params),
display_representation: params[:displayRepresentation],
show_hierarchies: boolearize(params[:showHierarchies]),
- include_subprojects: boolearize(params[:includeSubprojects])
+ include_subprojects: boolearize(params[:includeSubprojects]),
+ timestamps: Timestamp.parse_multiple(params[:timestamps])
}
+ rescue ArgumentError => e
+ result = ServiceResult.failure
+ result.errors.add(:base, e.message)
+ result
end
def determine_allowed_empty(params)
diff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rb
index b2ed5870899..c49a2dff3cf 100644
--- a/spec/models/timestamp_spec.rb
+++ b/spec/models/timestamp_spec.rb
@@ -119,6 +119,56 @@ describe Timestamp do
end
end
+ describe ".parse_multiple" do
+ describe "when providing an empty string" do
+ subject { described_class.parse_multiple("") }
+
+ it "returns an empty array" do
+ expect(subject).to eq []
+ end
+ end
+
+ describe "when providing a single timestamp" do
+ subject { described_class.parse_multiple("PT10S") }
+
+ it "returns an array containing that timestamp" do
+ expect(subject).to eq [described_class.new("PT10S")]
+ end
+ end
+
+ describe "when providing multiple comma-separated timestamps" do
+ subject { described_class.parse_multiple("PT10S,PT20S") }
+
+ it "returns an array containing those timestamps" do
+ expect(subject).to eq [described_class.new("PT10S"), described_class.new("PT20S")]
+ end
+ end
+
+ describe "when providing multiple comma-separated timestamps with whitespace" do
+ subject { described_class.parse_multiple("PT10S, PT20S") }
+
+ it "returns an array containing those timestamps" do
+ expect(subject).to eq [described_class.new("PT10S"), described_class.new("PT20S")]
+ end
+ end
+
+ describe "when providing multiple comma-separated timestamps with whitespace and empty strings" do
+ subject { described_class.parse_multiple("PT10S, , PT20S") }
+
+ it "returns an array containing those timestamps" do
+ expect(subject).to eq [described_class.new("PT10S"), described_class.new("PT20S")]
+ end
+ end
+
+ describe "when providing something invalid" do
+ subject { described_class.parse_multiple("foo") }
+
+ it "raises an error" do
+ expect { subject }.to raise_error ArgumentError
+ end
+ end
+ end
+
describe "#relative?" do
subject { timestamp.relative? }
diff --git a/spec/services/api/v3/parse_query_params_service_spec.rb b/spec/services/api/v3/parse_query_params_service_spec.rb
index a6f019b0b73..7bb9aebba2a 100644
--- a/spec/services/api/v3/parse_query_params_service_spec.rb
+++ b/spec/services/api/v3/parse_query_params_service_spec.rb
@@ -319,5 +319,35 @@ describe ::API::V3::ParseQueryParamsService,
let(:expected) { { include_subprojects: false } }
end
end
+
+ context 'with timestamps' do
+ it_behaves_like 'transforms' do
+ let(:params) { { timestamps: "" } }
+ let(:expected) { { timestamps: [] } }
+ end
+
+ it_behaves_like 'transforms' do
+ let(:params) { { timestamps: "P-0Y" } }
+ let(:expected) { { timestamps: [Timestamp.parse("P-0Y")] } }
+ end
+
+ it_behaves_like 'transforms' do
+ let(:params) { { timestamps: "2022-10-29T23:01:23Z, P-0Y" } }
+ let(:expected) { { timestamps: [Timestamp.parse("2022-10-29T23:01:23Z"), Timestamp.parse("P-0Y")] } }
+ end
+
+ describe "for invalid parameters" do
+ let(:params) { { timestamps: "foo,bar" } }
+
+ it 'is not success' do
+ expect(subject).not_to be_success
+ end
+
+ it 'returns the error' do
+ expect(subject.errors.messages[:base].length).to be(1)
+ expect(subject.errors.messages[:base][0]).to include "\"foo\""
+ end
+ end
+ end
end
end