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
[#46555] Extend Query resource in APIv3 by timestamps
Dombi Attila64c3ad1dcdc7
diff --git a/app/contracts/queries/base_contract.rb b/app/contracts/queries/base_contract.rbindex 98ee35ca516..03fb42596be 100644--- a/app/contracts/queries/base_contract.rb+++ b/app/contracts/queries/base_contract.rb@@ -47,6 +47,7 @@ module Queriesattribute :column_names # => columnsattribute :filters+ attribute :timestampsattribute :sort_criteria # => sortByattribute :group_by # => groupBydiff --git a/app/models/query/timestamps.rb b/app/models/query/timestamps.rbindex e1e6cf7f64a..c111d3c6550 100644--- a/app/models/query/timestamps.rb+++ b/app/models/query/timestamps.rb@@ -32,6 +32,9 @@ module Query::Timestampsincluded doserialize :timestamps, Array+ validates :timestamps, length: { maximum: 2 }+ validate :timestamps_are_parsable+# Returns the timestamps the query should be evaluated at.## In the database, the timestamps are stored as ISO8601 strings.@@ -57,5 +60,15 @@ module Query::Timestampsdef historic?timestamps != [Timestamp.now]end++ private++ def timestamps_are_parsable+ invalid_timestamps = timestamps.reject(&:valid?)++ if invalid_timestamps.any?+ errors.add :timestamps, :invalid, values: invalid_timestamps.join(", ")+ end+ endendenddiff --git a/app/models/timestamp.rb b/app/models/timestamp.rbindex 1ca124177ab..dd08895b232 100644--- a/app/models/timestamp.rb+++ b/app/models/timestamp.rb@@ -140,6 +140,12 @@ class Timestampself != Timestamp.nowend+ def valid?+ self.class.parse(iso8601)+ rescue StandardError+ false+ end+delegate :hash, to: :iso8601class Exception < StandardError; enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 8442830945c..6a82614d2e3 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -807,6 +807,10 @@ en:sort_criteria:invalid: "Can't sort by column: %{value}"format: "%{message}"+ timestamps:+ invalid: "Timestamps contain invalid values: %{values}"+ format: "%{message}"+ too_long: "Timestamps are too long (maximum is %{count} values)."group_by_hierarchies_exclusive: "is mutually exclusive with group by '%{group_by}'. You cannot activate both."filters:custom_fields:diff --git a/lib/api/v3/queries/query_representer.rb b/lib/api/v3/queries/query_representer.rbindex ec95bd6b9d8..e9bb47952bc 100644--- a/lib/api/v3/queries/query_representer.rb+++ b/lib/api/v3/queries/query_representer.rb@@ -298,6 +298,9 @@ module APIproperty :timeline_labels+ property :timestamps,+ getter: ->(*) { timestamps.map(&:to_s) }+# Visible representation of the resultsproperty :display_representationdiff --git a/lib/api/v3/queries/schemas/query_schema_representer.rb b/lib/api/v3/queries/schemas/query_schema_representer.rbindex bc301fd6e63..f6791a977ad 100644--- a/lib/api/v3/queries/schemas/query_schema_representer.rb+++ b/lib/api/v3/queries/schemas/query_schema_representer.rb@@ -122,6 +122,12 @@ module APIhas_default: true,deprecated: true+ schema :timestamps,+ type: '[]Timestamp',+ required: false,+ writable: true,+ has_default: true+schema :highlighting_mode,type: 'String',required: false,diff --git a/spec/lib/api/v3/queries/query_representer_parsing_spec.rb b/spec/lib/api/v3/queries/query_representer_parsing_spec.rbindex fe6a87e0b9c..7f441209baa 100644--- a/spec/lib/api/v3/queries/query_representer_parsing_spec.rb+++ b/spec/lib/api/v3/queries/query_representer_parsing_spec.rb@@ -169,4 +169,16 @@ describe API::V3::Queries::QueryRepresenter, 'parsing' doendendend++ describe 'timestamps' do+ let(:timestamp_params) { [1.week.ago.iso8601, 'P0D'] }+ let(:request_body) do+ { 'timestamps' => timestamp_params }+ end++ it 'sets timestamps' do+ expect(subject.timestamps)+ .to eq([1.week.ago.iso8601, 'P0D'])+ end+ endenddiff --git a/spec/lib/api/v3/queries/query_representer_rendering_spec.rb b/spec/lib/api/v3/queries/query_representer_rendering_spec.rbindex 11f378b5033..79fa02df747 100644--- a/spec/lib/api/v3/queries/query_representer_rendering_spec.rb+++ b/spec/lib/api/v3/queries/query_representer_rendering_spec.rb@@ -576,6 +576,10 @@ describe API::V3::Queries::QueryRepresenter dolet(:value) { query.timeline_labels }end+ it_behaves_like 'property', :timestamps do+ let(:value) { query.timestamps }+ end+it_behaves_like 'property', :public dolet(:value) { query.public }end@@ -596,16 +600,14 @@ describe API::V3::Queries::QueryRepresenter doendend- describe 'timestamps' do- it_behaves_like 'has UTC ISO 8601 date and time' do- let(:date) { query.created_at }- let(:json_path) { 'createdAt' }- end+ it_behaves_like 'has UTC ISO 8601 date and time' do+ let(:date) { query.created_at }+ let(:json_path) { 'createdAt' }+ end- it_behaves_like 'has UTC ISO 8601 date and time' do- let(:date) { query.updated_at }- let(:json_path) { 'updatedAt' }- end+ it_behaves_like 'has UTC ISO 8601 date and time' do+ let(:date) { query.updated_at }+ let(:json_path) { 'updatedAt' }enddescribe 'highlighting' dodiff --git a/spec/lib/api/v3/queries/schemas/query_schema_representer_spec.rb b/spec/lib/api/v3/queries/schemas/query_schema_representer_spec.rbindex 01ec1bc6590..32709e0cc9e 100644--- a/spec/lib/api/v3/queries/schemas/query_schema_representer_spec.rb+++ b/spec/lib/api/v3/queries/schemas/query_schema_representer_spec.rb@@ -274,6 +274,18 @@ describe API::V3::Queries::Schemas::QuerySchemaRepresenter doendend+ describe 'timestamps' do+ let(:path) { 'timestamps' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { '[]Timestamp' }+ let(:name) { Query.human_attribute_name('timestamps') }+ let(:required) { false }+ let(:writable) { true }+ let(:has_default) { true }+ end+ end+describe 'show hierarchies' dolet(:path) { 'showHierarchies' }diff --git a/spec/requests/api/v3/queries/create_form_api_spec.rb b/spec/requests/api/v3/queries/create_form_api_spec.rbindex ebf8be19c19..29712e574fa 100644--- a/spec/requests/api/v3/queries/create_form_api_spec.rb+++ b/spec/requests/api/v3/queries/create_form_api_spec.rb@@ -326,6 +326,7 @@ describe "POST /api/v3/queries/form" dodescribe 'with all parameters given' dolet(:status) { create(:status) }+ let(:timestamps) { [1.week.ago.iso8601, "P0D"] }let(:parameters) do{@@ -333,6 +334,7 @@ describe "POST /api/v3/queries/form" dopublic: true,sums: true,showHierarchies: false,+ timestamps:,filters: [{name: "Status",@@ -449,6 +451,10 @@ describe "POST /api/v3/queries/form" doexpect(form.dig("_embedded", "payload", "_links", "sortBy")).to eq sort_byend+ it 'has the timestamps set' do+ expect(form.dig("_embedded", "payload", "timestamps")).to eq timestamps+ end+context "with the project referred to by its identifier" dolet(:override_params) dolinks = parameters[:_links]@@ -549,6 +555,41 @@ describe "POST /api/v3/queries/form" doendend+ context 'with invalid timestamps' do+ context 'when one timestamp cannot be parsed' do+ let(:override_params) do+ { timestamps: ['invalid', 'P0D'] }+ end++ it "returns a validation error" do+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))+ .to eq "Timestamps contain invalid values: invalid"+ end+ end++ context 'when both timestamps cannot be parsed' do+ let(:override_params) do+ { timestamps: ['invalid', 'invalid2'] }+ end++ it "returns a validation error" do+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))+ .to eq "Timestamps contain invalid values: invalid, invalid2"+ end+ end++ context 'when there are too many values' do+ let(:override_params) do+ { timestamps: [2.weeks.ago.iso8601, 1.week.ago.iso8601, "P0D"] }+ end++ it "returns a validation error" do+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))+ .to eq "Timestamps are too long (maximum is 2 values)."+ end+ end+ end+context "with an unauthorized user trying to set the query public" dolet(:user) { create(:user) }diff --git a/spec/requests/api/v3/queries/create_query_spec.rb b/spec/requests/api/v3/queries/create_query_spec.rbindex de657ea59b7..a5bb888b6db 100644--- a/spec/requests/api/v3/queries/create_query_spec.rb+++ b/spec/requests/api/v3/queries/create_query_spec.rb@@ -29,14 +29,16 @@require 'spec_helper'describe "POST /api/v3/queries" do- let(:user) { create(:admin) }- let(:status) { create(:status) }- let(:project) { create(:project) }+ shared_let(:user) { create(:admin) }+ shared_let(:status) { create(:status) }+ shared_let(:project) { create(:project) }+ shared_let(:timestamps) { [1.week.ago.iso8601, "P0D"] }let(:default_params) do{name: "Dummy Query",showHierarchies: false,+ timestamps:,filters: [{name: "Status",@@ -123,6 +125,7 @@ describe "POST /api/v3/queries" doexpect(query.user).to eq userexpect(query.project).to eq project+ expect(query.timestamps).to eq(timestamps.map { |t| Timestamp.new(t) })expect(query.filters.size).to eq 1filter = query.filters.firstdiff --git a/spec/requests/api/v3/queries/update_form_api_spec.rb b/spec/requests/api/v3/queries/update_form_api_spec.rbindex 320ed80f03a..f75605a0da3 100644--- a/spec/requests/api/v3/queries/update_form_api_spec.rb+++ b/spec/requests/api/v3/queries/update_form_api_spec.rb@@ -35,6 +35,7 @@ describe "POST /api/v3/queries/form" dolet(:user) { create(:admin) }let(:role) { create(:existing_role, permissions:) }let(:permissions) { %i(view_work_packages manage_public_queries) }+ let(:timestamps) { [1.week.ago.iso8601, "P0D"] }let!(:project) { create(:project_with_types, members: { user => role }) }@@ -315,6 +316,7 @@ describe "POST /api/v3/queries/form" doname: "Some Query",public: true,sums: true,+ timestamps:,showHierarchies: false,filters: [{@@ -432,6 +434,10 @@ describe "POST /api/v3/queries/form" doexpect(form.dig("_embedded", "payload", "_links", "sortBy")).to eq sort_byend+ it 'has the timestamps set' do+ expect(form.dig("_embedded", "payload", "timestamps")).to eq timestamps+ end+context "with the project referred to by its identifier" dolet(:override_params) dolinks = parameters[:_links]@@ -516,6 +522,41 @@ describe "POST /api/v3/queries/form" doendend+ context 'with invalid timestamps' do+ context 'when one timestamp cannot be parsed' do+ let(:override_params) do+ { timestamps: ['invalid', 'P0D'] }+ end++ it "returns a validation error" do+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))+ .to eq "Timestamps contain invalid values: invalid"+ end+ end++ context 'when both timestamps cannot be parsed' do+ let(:override_params) do+ { timestamps: ['invalid', 'invalid2'] }+ end++ it "returns a validation error" do+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))+ .to eq "Timestamps contain invalid values: invalid, invalid2"+ end+ end++ context 'when there are too many values' do+ let(:override_params) do+ { timestamps: [2.weeks.ago.iso8601, 1.week.ago.iso8601, "P0D"] }+ end++ it "returns a validation error" do+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))+ .to eq "Timestamps are too long (maximum is 2 values)."+ end+ end+ end+context "with an unauthorized user trying to set the query public" dolet(:user) { create(:user) }let(:permissions) { [:view_work_packages] }diff --git a/spec/requests/api/v3/queries/update_query_spec.rb b/spec/requests/api/v3/queries/update_query_spec.rbindex ed416c87c74..e46a2155108 100644--- a/spec/requests/api/v3/queries/update_query_spec.rb+++ b/spec/requests/api/v3/queries/update_query_spec.rb@@ -29,7 +29,11 @@require 'spec_helper'describe "PATCH /api/v3/queries/:id" do- let(:user) { create(:admin) }+ shared_let(:user) { create(:admin) }+ shared_let(:status) { create(:status) }+ shared_let(:project) { create(:project) }+ shared_let(:timestamps) { [1.week.ago.iso8601, "P0D"] }+let!(:query) docreate(:global_query,@@ -45,6 +49,7 @@ describe "PATCH /api/v3/queries/:id" doname: "Dummy Query",public: true,showHierarchies: false,+ timestamps:,filters: [{name: "Status",@@ -98,8 +103,6 @@ describe "PATCH /api/v3/queries/:id" do}}end- let(:status) { create(:status) }- let(:project) { create(:project) }def jsonJSON.parse last_response.body@@ -137,6 +140,7 @@ describe "PATCH /api/v3/queries/:id" doexpect(query.public).to be trueexpect(query.display_sums).to be false+ expect(query.timestamps).to eq(timestamps.map { |t| Timestamp.new(t) })expect(query.filters.size).to eq 1filter = query.filters.firstdiff --git a/spec/support/queries/shared_get_individual_query_examples.rb b/spec/support/queries/shared_get_individual_query_examples.rbindex f2066de4079..a7198528058 100644--- a/spec/support/queries/shared_get_individual_query_examples.rb+++ b/spec/support/queries/shared_get_individual_query_examples.rb@@ -96,4 +96,48 @@ shared_examples_for 'GET individual query' do.at_path('errorIdentifier')endend++ context 'when providing valid timestamps' do+ let(:path) do+ params = CGI.escape(timestamps)+ "#{base_path}?timestamps=#{params}"+ end++ context 'with a range without a work package' do+ let(:timestamps) do+ [2.hours.ago.iso8601, 1.hour.ago.iso8601].join(",")+ end++ it 'uses the provided timestamp' do+ expect(last_response.body)+ .to be_json_eql(0.to_json)+ .at_path('_embedded/results/total')+ end+ end++ context 'with a range containing a work package' do+ let(:timestamps) do+ [2.hours.ago.iso8601, 'P0D'].join(",")+ end++ it 'uses the provided timestamp' do+ expect(last_response.body)+ .to be_json_eql(1.to_json)+ .at_path('_embedded/results/total')+ end+ end+ end++ context 'when providing an invalid timestamp' do+ let(:path) do+ params = CGI.escape('invalid')+ "#{base_path}?timestamps=#{params}"+ end++ it 'returns an error' do+ expect(last_response.body)+ .to be_json_eql("urn:openproject-org:api:v3:errors:InvalidQuery".to_json)+ .at_path('errorIdentifier')+ end+ endend