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

fixup! [#46555] Extend Query resource in APIv3 by timestamps

Dombi Attilac91c4f03d7f2
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index dd08895b232..1a847a2bcde 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -27,44 +27,93 @@
#++
class Timestamp
- def initialize(arg = Timestamp.now.to_s)
- if arg.is_a? String
- @timestamp_iso8601_string = arg
- elsif arg.respond_to? :iso8601
- @timestamp_iso8601_string = arg.iso8601
- else
- raise Timestamp::Exception, \
- "Argument type not supported. " \
- "Please provide an ISO-8601 String or anything that responds to :iso8601, e.g. a Time."
+ delegate :hash, to: :iso8601
+
+ class Exception < StandardError; end
+
+ class ISO8601Parser
+ def initialize(string)
+ @iso8601_string = string
end
- end
- def self.parse(iso8601_string)
- return iso8601_string if iso8601_string.is_a?(Timestamp)
+ def parse!
+ @iso8601_string = self.class.substitute_special_shortcut_values(@iso8601_string)
- iso8601_string = iso8601_string.strip
- iso8601_string = substitute_special_shortcut_values(iso8601_string)
- if iso8601_string.start_with? /[+-]?P/ # ISO8601 "Period"
- 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."
+ if @iso8601_string.start_with? /[+-]?P/ # ISO8601 "Period"
+ ActiveSupport::Duration.parse(@iso8601_string).iso8601
+ else
+ Time.zone.iso8601(@iso8601_string).iso8601
+ end
+ rescue ArgumentError => e
+ raise e.class, "The string \"#{@iso8601_string}\" cannot be parsed to Time or ActiveSupport::Duration."
+ end
+
+ class << self
+ # rubocop:disable Metrics/AbcSize
+ # rubocop:disable Metrics/PerceivedComplexity
+ def 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 = scan_for_shortcut_value(string:, unit: "y")
+ months = scan_for_shortcut_value(string:, unit: "m")
+ weeks = scan_for_shortcut_value(string:, unit: "w")
+ days = scan_for_shortcut_value(string:, unit: "d")
+ 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/PerceivedComplexity
+
+ def scan_for_shortcut_value(string:, unit:)
+ string.scan(/(\d+)#{unit}/).flatten.first
+ end
end
- 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)
+ class << self
+ def parse(iso8601_string)
+ return iso8601_string if iso8601_string.is_a?(Timestamp)
+
+ iso8601_string = ISO8601Parser.new(iso8601_string.strip).parse!
+ new(iso8601_string)
+ end
+
+ # Take a comma-separated string of ISO-8601 timestamps and convert it
+ # into an array of Timestamp objects.
+ #
+ def 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 now
+ new(ActiveSupport::Duration.build(0).iso8601)
end
end
- def self.now
- new(ActiveSupport::Duration.build(0).iso8601)
+ def initialize(arg = Timestamp.now.to_s)
+ if arg.is_a? String
+ @timestamp_iso8601_string = ISO8601Parser.substitute_special_shortcut_values(arg)
+ elsif arg.respond_to? :iso8601
+ @timestamp_iso8601_string = arg.iso8601
+ else
+ raise Timestamp::Exception, \
+ "Argument type not supported. " \
+ "Please provide an ISO-8601 String or anything that responds to :iso8601, e.g. a Time."
+ end
end
def relative?
@@ -145,39 +194,4 @@ class Timestamp
rescue StandardError
false
end
-
- delegate :hash, to: :iso8601
-
- class 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 = scan_for_shortcut_value(string:, unit: "y")
- months = scan_for_shortcut_value(string:, unit: "m")
- weeks = scan_for_shortcut_value(string:, unit: "w")
- days = scan_for_shortcut_value(string:, unit: "d")
- 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/PerceivedComplexity
-
- def self.scan_for_shortcut_value(string:, unit:)
- string.scan(/(\d+)#{unit}/).flatten.first
- end
end
diff --git a/spec/requests/api/v3/queries/create_form_api_spec.rb b/spec/requests/api/v3/queries/create_form_api_spec.rb
index 714f6267dca..2a725e50b5d 100644
--- a/spec/requests/api/v3/queries/create_form_api_spec.rb
+++ b/spec/requests/api/v3/queries/create_form_api_spec.rb
@@ -575,6 +575,17 @@ describe "POST /api/v3/queries/form" do
end
end
+ context 'when one timestamp cannot be parsed (malformed)' do
+ let(:override_params) do
+ { timestamps: ['2022-03-02 invalid string 20:45:56Z', 'P0D'] }
+ end
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps contain invalid values: 2022-03-02 invalid string 20:45:56Z"
+ end
+ end
+
context 'when both timestamps cannot be parsed' do
let(:override_params) do
{ timestamps: ['invalid', 'invalid2'] }