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: include as meta information at which timestamps the work package matches the query filters

Sebastian Fiedlschuster4e8a84ffbd93
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rb
index e33875077b2..7a0cb6636e9 100644
--- a/app/models/journable/with_historic_attributes.rb
+++ b/app/models/journable/with_historic_attributes.rb
@@ -193,6 +193,10 @@ class Journable::WithHistoricAttributes < SimpleDelegator
matches_query_at_timestamps.include?(current_timestamp)
end
+ def matches_query_filter_at_timestamp?(timestamp)
+ matches_query_at_timestamps.include?(timestamp)
+ end
+
def self.query_work_packages(query:, timestamp: nil)
query = query.dup
query.timestamps = [timestamp] if timestamp
diff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
index 20c89dad043..d28846fffe1 100644
--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
@@ -36,6 +36,10 @@ module API::V3::WorkPackages::EagerLoading
work_package.baseline_attributes = work_package_with_historic_attributes.baseline_attributes
work_package.attributes_by_timestamp = work_package_with_historic_attributes.attributes_by_timestamp
work_package.baseline_timestamp = work_package_with_historic_attributes.baseline_timestamp
+ work_package.matches_query_filter_at_baseline_timestamp = work_package_with_historic_attributes.matches_query_filter_at_baseline_timestamp?
+ work_package.matches_query_at_timestamps = work_package_with_historic_attributes.matches_query_at_timestamps
+
+ # TODO: rename `matches_query_at_timestamps` to `matches_query_filters_at_timestamps`?
end
def self.module
@@ -57,7 +61,17 @@ module API::V3::WorkPackages::EagerLoading
extend ActiveSupport::Concern
included do
- attr_accessor :baseline_attributes, :attributes_by_timestamp, :baseline_timestamp
+ attr_accessor :baseline_attributes, :attributes_by_timestamp, :baseline_timestamp,
+ :matches_query_filter_at_baseline_timestamp,
+ :matches_query_at_timestamps
+ end
+
+ def matches_query_filter_at_baseline_timestamp?
+ matches_query_filter_at_baseline_timestamp
+ end
+
+ def matches_query_filter_at_timestamp?(timestamp)
+ matches_query_at_timestamps.include?(timestamp)
end
end
end
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index 023fb908e80..5db849c6ec9 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -456,6 +456,10 @@ module API
"self": {
"href": API::V3::Utilities::PathHelper::ApiV3Path.work_package(id, timestamps: baseline_timestamp)
}
+ },
+ "_meta": {
+ "timestamp": baseline_timestamp.to_s,
+ "matchesFilters": matches_query_filter_at_baseline_timestamp?
}
})
end,
@@ -472,6 +476,9 @@ module API
"self": {
"href": API::V3::Utilities::PathHelper::ApiV3Path.work_package(id, timestamps: timestamp)
}
+ },
+ "_meta": {
+ "matchesFilters": matches_query_filter_at_timestamp?(timestamp)
}
})]
end.to_h
diff --git a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
index 754edd87538..580ef598ebe 100644
--- a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
@@ -505,7 +505,7 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
context 'when passing timestamps' do
let(:work_pacakges) { WorkPackage.where(id: work_package.id) }
let(:work_package) do
- new_work_package = create(:work_package, subject: "The current work package")
+ new_work_package = create(:work_package, subject: "The current work package", project:)
new_work_package.update_columns created_at: baseline_time - 1.day
new_work_package
end
@@ -520,6 +520,7 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
attributes: { subject: "The current work package" })
end
let(:baseline_time) { "2022-01-01".to_time }
+ let(:project) { create(:project) }
def create_journal(journable:, version:, timestamp:, attributes: {})
work_package_attributes = work_package.attributes.except("id")
@@ -622,5 +623,47 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
.at_path('_embedded/elements/0/_links/self/href')
end
end
+
+ context 'when passing a query' do
+ let(:search_term) { 'original' }
+ let(:_query) do
+ login_as(current_user)
+ build(:query, user: current_user, project: nil).tap do |query|
+ query.filters.clear
+ query.add_filter 'subject', '~', search_term
+ query.timestamps = timestamps
+ end
+ end
+ let(:current_user) do
+ create(:user,
+ firstname: 'user',
+ lastname: '1',
+ member_in_project: project,
+ member_with_permissions: %i[view_work_packages view_file_links])
+ end
+
+ context 'baseline and current' do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
+
+ describe 'baselineAttributes' do
+ it 'states whether the work package matches the query filters at the baseline time' do
+ expect(subject)
+ .to be_json_eql(true.to_json)
+ .at_path('_embedded/elements/0/_embedded/baselineAttributes/_meta/matchesFilters')
+ end
+ end
+
+ describe 'attributesByTimestamp' do
+ it 'states whether the work package matches the query filters at the timestamp' do
+ expect(subject)
+ .to be_json_eql(true.to_json)
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps[0]}/_meta/matchesFilters")
+ expect(subject)
+ .to be_json_eql(false.to_json)
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps[1]}/_meta/matchesFilters")
+ end
+ end
+ end
+ end
end
end
diff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
index 95a77afc2f6..d74f6bb34be 100644
--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
@@ -1306,7 +1306,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
let(:baseline_time) { Time.zone.parse("2022-01-01") }
let(:work_pacakges) { WorkPackage.where(id: work_package.id) }
let(:work_package) do
- new_work_package = create(:work_package, subject: "The current work package")
+ new_work_package = create(:work_package, subject: "The current work package", project:)
new_work_package.update_columns created_at: baseline_time - 1.day
new_work_package
end
@@ -1320,6 +1320,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
version: 2,
attributes: { subject: "The current work package" })
end
+ let(:project) { create(:project) }
def create_journal(journable:, version:, timestamp:, attributes: {})
work_package_attributes = work_package.attributes.except("id")
@@ -1359,6 +1360,12 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
.to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: [timestamps.first]).to_json)
.at_path('_embedded/baselineAttributes/_links/self/href')
end
+
+ it 'has the baseline timestamp as meta information' do
+ expect(subject)
+ .to be_json_eql(timestamps.first.to_s.to_json)
+ .at_path('_embedded/baselineAttributes/_meta/timestamp')
+ end
end
describe 'attributesByTimestamp' do
@@ -1384,6 +1391,44 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
.at_path("_embedded/attributesByTimestamp/#{timestamps[1]}/_links/self/href")
end
end
+
+ context 'when passing a query' do
+ let(:search_term) { 'original' }
+ let(:query) do
+ login_as(current_user)
+ build(:query, user: current_user, project: nil).tap do |query|
+ query.filters.clear
+ query.add_filter 'subject', '~', search_term
+ query.timestamps = timestamps
+ end
+ end
+ let(:current_user) do
+ create(:user,
+ firstname: 'user',
+ lastname: '1',
+ member_in_project: project,
+ member_with_permissions: %i[view_work_packages view_file_links])
+ end
+
+ describe 'baselineAttributes' do
+ it 'states whether the work package matches the query filters at the baseline time' do
+ expect(subject)
+ .to be_json_eql(true.to_json)
+ .at_path('_embedded/baselineAttributes/_meta/matchesFilters')
+ end
+ end
+
+ describe 'attributesByTimestamp' do
+ it 'states whether the work package matches the query filters at the timestamp' do
+ expect(subject)
+ .to be_json_eql(true.to_json)
+ .at_path("_embedded/attributesByTimestamp/#{timestamps[0]}/_meta/matchesFilters")
+ expect(subject)
+ .to be_json_eql(false.to_json)
+ .at_path("_embedded/attributesByTimestamp/#{timestamps[1]}/_meta/matchesFilters")
+ end
+ end
+ end
end
end