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

add bcf topic and viewpoint to work package

ulferts97dbb43ab84b
diff --git a/lib/api/decorators/single.rb b/lib/api/decorators/single.rb
index 061b471b47f..032fe228d87 100644
--- a/lib/api/decorators/single.rb
+++ b/lib/api/decorators/single.rb
@@ -80,7 +80,7 @@ module API
class_attribute :to_eager_load
class_attribute :checked_permissions
- def current_user_allowed_to(permission, context:)
+ def current_user_allowed_to(permission, context: represented.respond_to?(:project) ? represented.project : nil)
current_user.allowed_to?(permission, context)
end
diff --git a/modules/bim/lib/api/bim/utilities/path_helper.rb b/modules/bim/lib/api/bim/utilities/path_helper.rb
new file mode 100644
index 00000000000..ccc9413a3ea
--- /dev/null
+++ b/modules/bim/lib/api/bim/utilities/path_helper.rb
@@ -0,0 +1,70 @@
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module Bim
+ module Utilities
+ module PathHelper
+ include API::Utilities::UrlHelper
+
+ # rubocop:disable Naming/ClassAndModuleCamelCase
+ class BCF2_1Path
+ extend API::Utilities::UrlHelper
+
+ # Determining the root_path on every url we want to render is
+ # expensive. As the root_path will not change within a
+ # request, we can cache the first response on each request.
+ def self.root_path
+ RequestStore.store[:cached_root_path] ||= super
+ end
+
+ def self.root
+ "#{root_path}api/bcf/2.1/"
+ end
+
+ def self.project(identifier)
+ "#{root}projects/#{identifier}"
+ end
+
+ def self.topic(project_identifier, uuid)
+ "#{project(project_identifier)}/topics/#{uuid}"
+ end
+
+ def self.viewpoint(project_identifier, topic_uuid, viewpoint_topic)
+ "#{topic(project_identifier, topic_uuid)}/viewpoints/#{viewpoint_topic}"
+ end
+ end
+
+ def bcf_v2_1_paths
+ BCF2_1Path
+ end
+ end
+ end
+ end
+end
diff --git a/modules/bim/lib/open_project/bim/engine.rb b/modules/bim/lib/open_project/bim/engine.rb
index 4d16fa59c41..9df1ca102dc 100644
--- a/modules/bim/lib/open_project/bim/engine.rb
+++ b/modules/bim/lib/open_project/bim/engine.rb
@@ -103,6 +103,8 @@ module OpenProject::Bim
patch_with_namespace :DemoData, :WorkPackageBoardSeeder
extend_api_response(:v3, :work_packages, :work_package) do
+ include API::Bim::Utilities::PathHelper
+
property :bcf,
exec_context: :decorator,
getter: ->(*) {
@@ -119,6 +121,26 @@ module OpenProject::Bim
if: ->(*) {
represented.bcf_issue.present?
}
+
+ link :bcfTopic,
+ cache_if: -> { current_user_allowed_to(:view_linked_issues) } do
+ next unless represented.bcf_issue?
+
+ {
+ href: bcf_v2_1_paths.topic(represented.project.identifier, represented.bcf_issue.uuid)
+ }
+ end
+
+ links :bcfViewpoints,
+ cache_if: -> { current_user_allowed_to(:view_linked_issues) } do
+ next unless represented.bcf_issue?
+
+ represented.bcf_issue.viewpoints.map do |viewpoint|
+ {
+ href: bcf_v2_1_paths.viewpoint(represented.project.identifier, represented.bcf_issue.uuid, viewpoint.uuid)
+ }
+ end
+ end
end
extend_api_response(:v3, :work_packages, :work_package_collection) do
diff --git a/modules/bim/lib/open_project/bim/patches/work_package_patch.rb b/modules/bim/lib/open_project/bim/patches/work_package_patch.rb
index fac56af9cc3..0260d997609 100644
--- a/modules/bim/lib/open_project/bim/patches/work_package_patch.rb
+++ b/modules/bim/lib/open_project/bim/patches/work_package_patch.rb
@@ -32,6 +32,10 @@ module OpenProject::Bim::Patches::WorkPackagePatch
def self.included(base)
base.class_eval do
has_one :bcf_issue, class_name: '::Bim::Bcf::Issue', foreign_key: 'work_package_id'
+
+ def bcf_issue?
+ bcf_issue.present?
+ end
end
end
end
diff --git a/modules/bim/spec/api/v3/work_packages/work_package_representer_spec.rb b/modules/bim/spec/api/v3/work_packages/work_package_representer_spec.rb
index 384095f8721..3e2a53ad360 100644
--- a/modules/bim/spec/api/v3/work_packages/work_package_representer_spec.rb
+++ b/modules/bim/spec/api/v3/work_packages/work_package_representer_spec.rb
@@ -30,6 +30,7 @@ require 'spec_helper'
describe ::API::V3::WorkPackages::WorkPackageRepresenter do
include API::V3::Utilities::PathHelper
+ include API::Bim::Utilities::PathHelper
let(:project) do
work_package.project
@@ -44,11 +45,11 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
end
end
end
- let(:bcf_issue) do
+ let(:bcf_topic) do
FactoryBot.build_stubbed(:bcf_issue_with_comment)
end
let(:work_package) do
- FactoryBot.build_stubbed(:stubbed_work_package, bcf_issue: bcf_issue)
+ FactoryBot.build_stubbed(:stubbed_work_package, bcf_issue: bcf_topic)
end
let(:representer) do
described_class.new(work_package,
@@ -68,12 +69,85 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
it "contains viewpoints" do
is_expected.to be_json_eql([
{
- file_name: bcf_issue.viewpoints.first.attachments.first.filename,
- id: bcf_issue.viewpoints.first.attachments.first.id
+ file_name: bcf_topic.viewpoints.first.attachments.first.filename,
+ id: bcf_topic.viewpoints.first.attachments.first.id
}
].to_json)
.including('id')
.at_path('bcf/viewpoints/')
end
end
+
+ describe '_links' do
+ describe 'bcfTopic' do
+ context 'if a topic is present' do
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'bcfTopic' }
+ let(:href) { "/api/bcf/2.1/projects/#{project.identifier}/topics/#{bcf_topic.uuid}" }
+ end
+ end
+
+ context 'if no topic is present' do
+ let(:bcf_topic) { nil }
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'bcfTopic' }
+ end
+ end
+
+ context 'if permission is lacking' do
+ let(:permissions) { %i[view_work_packages] }
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'bcfTopic' }
+ end
+ end
+ end
+
+ describe 'bcfViewpoints' do
+ context 'if a viewpoint is present' do
+ it_behaves_like 'has a link collection' do
+ let(:link) { 'bcfViewpoints' }
+ let(:hrefs) do
+ [
+ {
+ href: bcf_v2_1_paths.viewpoint(project.identifier, bcf_topic.uuid, bcf_topic.viewpoints[0].uuid)
+ }
+ ]
+ end
+ end
+
+ context 'if no topic is present' do
+ let(:bcf_topic) { nil }
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'bcfViewpoints' }
+ end
+ end
+
+ context 'if no viewpoint is present' do
+ before do
+ allow(bcf_topic)
+ .to receive(:viewpoints)
+ .and_return []
+ end
+
+ it_behaves_like 'has a link collection' do
+ let(:link) { 'bcfViewpoints' }
+ let(:hrefs) do
+ []
+ end
+ end
+ end
+
+ context 'if permission is lacking' do
+ let(:permissions) { %i[view_work_packages] }
+
+ it_behaves_like 'has no link' do
+ let(:link) { 'bcfViewpoints' }
+ end
+ end
+ end
+ end
+ end
end