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: pass timestamps to representer on show endpoint

Sebastian Fiedlschuster8dce4df30ef8
diff --git a/lib/api/v3/work_packages/show_end_point.rb b/lib/api/v3/work_packages/show_end_point.rb
new file mode 100644
index 00000000000..0cfd21f17a7
--- /dev/null
+++ b/lib/api/v3/work_packages/show_end_point.rb
@@ -0,0 +1,39 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module API::V3::WorkPackages
+ class ShowEndPoint < API::V3::Utilities::Endpoints::Show
+ def render(request)
+ API::V3::WorkPackages::WorkPackageRepresenter
+ .create(request.instance_exec(request.params, &instance_generator),
+ current_user: request.current_user,
+ embed_links: true,
+ timestamps: Timestamp.parse_multiple(request.params[:timestamps]))
+ end
+ end
+end
\ No newline at end of file
diff --git a/lib/api/v3/work_packages/work_packages_api.rb b/lib/api/v3/work_packages/work_packages_api.rb
index 8c5ddfbd087..fee7914d2ff 100644
--- a/lib/api/v3/work_packages/work_packages_api.rb
+++ b/lib/api/v3/work_packages/work_packages_api.rb
@@ -76,7 +76,7 @@ module API
end
end
- get &::API::V3::Utilities::Endpoints::Show.new(model: WorkPackage).mount
+ get &API::V3::WorkPackages::ShowEndPoint.new(model: WorkPackage).mount
patch &::API::V3::WorkPackages::UpdateEndPoint.new(model: WorkPackage,
parse_service: ::API::V3::WorkPackages::ParseParamsService,
diff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rb
index 39e8169647e..87428aa519e 100644
--- a/spec/requests/api/v3/work_packages/show_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/show_resource_spec.rb
@@ -207,4 +207,94 @@ describe 'API v3 Work package resource',
I18n.t('api_v3.errors.not_found.work_package')
end
end
+
+ describe 'GET /api/v3/work_packages/:id?timestamps=' do
+ let(:get_path) { api_v3_paths.work_package(work_package.id) + "?timestamps=" + timestamps.map(&:to_s).join(',') }
+
+ describe 'response body' do
+ subject do
+ login_as current_user
+ get get_path
+ last_response.body
+ end
+
+ context 'when providing timestamps' do
+ let(:timestamps) { [Timestamp.parse('2015-01-01T00:00:00Z'), Timestamp.now] }
+ let(:baseline_time) { timestamps.first.to_time }
+
+ let(:work_package) do
+ 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
+ let(:original_journal) do
+ create_journal(journable: work_package, timestamp: baseline_time - 1.day,
+ version: 1,
+ attributes: { subject: "The original work package" })
+ end
+ let(:current_journal) do
+ create_journal(journable: work_package, timestamp: 1.day.ago,
+ version: 2,
+ attributes: { subject: "The current work package" })
+ end
+
+ def create_journal(journable:, version:, timestamp:, attributes: {})
+ work_package_attributes = work_package.attributes.except("id")
+ journal_attributes = work_package_attributes \
+ .extract!(*Journal::WorkPackageJournal.attribute_names) \
+ .symbolize_keys.merge(attributes)
+ create(:work_package_journal, version:,
+ journable:, created_at: timestamp, updated_at: timestamp,
+ data: build(:journal_work_package_journal, journal_attributes))
+ end
+
+ before do
+ WorkPackage.destroy_all
+ work_package
+ Journal.destroy_all
+ original_journal
+ current_journal
+ end
+
+ it 'responds with 200' do
+ expect(subject && last_response.status).to eq(200)
+ end
+
+ it 'embeds the baselineAttributes' do
+ expect(subject)
+ .to be_json_eql("The original work package".to_json)
+ .at_path('_embedded/baselineAttributes/subject')
+ end
+
+ it 'embeds the attributesByTimestamp' do
+ expect(subject)
+ .to be_json_eql("The original work package".to_json)
+ .at_path("_embedded/attributesByTimestamp/#{timestamps.first.to_s}/subject")
+ expect(subject)
+ .to be_json_eql("The current work package".to_json)
+ .at_path("_embedded/attributesByTimestamp/#{timestamps.last.to_s}/subject")
+ end
+
+ it 'has the current attributes as attributes' do
+ expect(subject)
+ .to be_json_eql("The current work package".to_json)
+ .at_path('subject')
+ end
+
+ it 'has an embedded link to the baseline work package' do
+ expect(subject)
+ .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 absolute timestamps within the self link' do
+ Timecop.freeze do
+ expect(subject)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.map(&:absolute)).to_json)
+ .at_path('_links/self/href')
+ end
+ end
+ end
+ end
+ end
end