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: add `attributesByTimestamp` and `baselineAttributes` to work package representer

Sebastian Fiedlschusteredb3cd0381db
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rb
index 0ad1dd36c97..e33875077b2 100644
--- a/app/models/journable/with_historic_attributes.rb
+++ b/app/models/journable/with_historic_attributes.rb
@@ -159,10 +159,17 @@ class Journable::WithHistoricAttributes < SimpleDelegator
# However, I prefer OpenStruct here because it makes it easier to deal with the
# non existing attributes when using `include_only_changed_attributes: true`.
#
+ # We need to patch the `as_json` method because OpenStruct's `as_json` would
+ # wrap everything into a "table" hash.
+ #
# rubocop:disable Style/OpenStructUse
#
def convert_attributes_hash_to_struct(attributes)
- OpenStruct.new(attributes)
+ Class.new(OpenStruct) do
+ def as_json(options = nil)
+ self.to_h.as_json(options)
+ end
+ end.new(attributes)
end
# rubocop:enable Style/OpenStructUse
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index c0c6dccab06..e7ce5638d09 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -105,7 +105,7 @@ class Timestamp
end
end
- def as_json
+ def as_json(*_args)
to_s
end
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index 7b0aee299fe..2419158b340 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -443,6 +443,17 @@ module API
status_id && status.is_readonly?
end
+ property :baseline_attributes,
+ as: :baselineAttributes,
+ embedded: true,
+ uncachable: true
+
+ property :attributes_by_timestamp,
+ as: :attributesByTimestamp,
+ if: ->(*) { respond_to?(:attributes_by_timestamp) and attributes_by_timestamp.any? },
+ embedded: true,
+ uncachable: true
+
associated_resource :category
associated_resource :type
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 76af1feab5e..9258af7ae0c 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
@@ -519,11 +519,39 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
end
end
+ shared_examples_for 'embeds the properties of the baseline work package' do
+ it 'embeds the properties of the baseline work package in attributesByTimestamp' do
+ expect(collection)
+ .to be_json_eql("The original work package".to_json)
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first.to_s}/subject")
+ end
+ it 'embed the properties of the baseline work package in baselineAttributes' do
+ expect(collection)
+ .to be_json_eql("The original work package".to_json)
+ .at_path("_embedded/elements/0/_embedded/baselineAttributes/subject")
+ end
+ end
+
+ context 'baseline and current' do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
+ it_behaves_like 'includes the properties of the current work package'
+ it_behaves_like 'embeds the properties of the baseline work package'
+ end
+
context 'current only' do
let(:timestamps) { [Timestamp.parse("PT0S")] }
it_behaves_like 'includes the properties of the current work package'
end
+ context 'baseline only' do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
+ it 'includes the properties of the baseline work package' do
+ expect(collection)
+ .to be_json_eql("The original work package".to_json)
+ .at_path('_embedded/elements/0/subject')
+ end
+ end
+
context 'empty' do
let(:timestamps) { [] }
it_behaves_like 'includes the properties of the current work package'
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 d81f1dc60ee..27383ddd52a 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
@@ -1339,6 +1339,28 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
current_journal
end
+ describe 'baselineAttributes' do
+ it 'has the work package attributes at the baseline time' do
+ expect(subject)
+ .to be_json_eql('The original work package'.to_json)
+ .at_path('_embedded/baselineAttributes/subject')
+ end
+ end
+
+ describe 'attributesByTimestamp' do
+ it 'has a hash with timestamp strings as keys' do
+ expect(JSON.parse(subject)['_embedded']['attributesByTimestamp'].keys).to eq timestamps.map(&:to_s)
+ end
+
+ it 'has the historic attributes for each timestamp' do
+ expect(subject)
+ .to be_json_eql('The original work package'.to_json)
+ .at_path("_embedded/attributesByTimestamp/#{timestamps[0]}/subject")
+ expect(subject)
+ .to be_json_eql('The current work package'.to_json)
+ .at_path("_embedded/attributesByTimestamp/#{timestamps[1]}/subject")
+ end
+ end
end
end