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 ancestor list to project resource

ulferts633fa3c8b845
diff --git a/app/models/project.rb b/app/models/project.rb
index d439e283c80..e92be04bca2 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -34,6 +34,7 @@ class Project < ApplicationRecord
include Projects::Storage
include Projects::Activity
+ include Projects::AncestorsFromRoot
include ::Scopes::Scoped
# Maximum length for project identifiers
diff --git a/app/models/projects/ancestors_from_root.rb b/app/models/projects/ancestors_from_root.rb
new file mode 100644
index 00000000000..cefa2cafcfb
--- /dev/null
+++ b/app/models/projects/ancestors_from_root.rb
@@ -0,0 +1,43 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 Projects::AncestorsFromRoot
+ extend ActiveSupport::Concern
+
+ included do
+ attr_writer :ancestors_from_root
+
+ # Retrieve stored eager loaded ancestors
+ # or use awesome_nested_set#ancestors to fetch them.
+ def ancestors_from_root
+ @ancestors_from_root ||= ancestors.reorder(lft: :asc)
+ end
+ end
+end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 714af0c086f..dc30e75819c 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -3068,6 +3068,7 @@ en:
undisclosed:
parent: Undisclosed - The selected parent is invisible because of lacking permissions.
+ ancestor: Undisclosed - The ancestor is invisible because of lacking permissions.
doorkeeper:
pre_authorization:
diff --git a/docs/api/apiv3/components/examples/project.yml b/docs/api/apiv3/components/examples/project.yml
index 6f307b100bb..fb3fd7d79d0 100644
--- a/docs/api/apiv3/components/examples/project.yml
+++ b/docs/api/apiv3/components/examples/project.yml
@@ -24,9 +24,17 @@ value:
href: /api/v3/memberships?filters=[{"project":{"operator":"=","values":["1"]}}]
customField456:
href: "/api/v3/users/315"
- method: A user
- projects:
+ title: A user
+ parent:
href: "/api/v3/projects/123"
+ title: "Parent project"
+ ancestors:
+ - href: "/api/v3/projects/2"
+ title: "Root project"
+ - href: "/api/v3/projects/12"
+ title: "Grandparent project"
+ - href: "/api/v3/projects/123"
+ title: "Parent project"
status:
href: "/api/v3/project_statuses/on_track"
title: On track
diff --git a/docs/api/apiv3/components/examples/project_body.yml b/docs/api/apiv3/components/examples/project_body.yml
index 2e038df3f09..d10fe8a0c64 100644
--- a/docs/api/apiv3/components/examples/project_body.yml
+++ b/docs/api/apiv3/components/examples/project_body.yml
@@ -8,6 +8,7 @@ value:
raw: Lorem **ipsum** dolor sit amet
customField123: 123
_links:
+ parent:
+ href: "/api/v3/projects/123"
customField456:
href: "/api/v3/users/315"
- method: A user
diff --git a/docs/api/apiv3/tags/projects.yml b/docs/api/apiv3/tags/projects.yml
index be58e564f06..83e852117d0 100644
--- a/docs/api/apiv3/tags/projects.yml
+++ b/docs/api/apiv3/tags/projects.yml
@@ -16,21 +16,22 @@ description: |-
## Linked Properties
- | Link | Description | Type | Constraints | Supported operations |Condition |
- | :----------: | ------------- | ---- | ----------- | -------------------- |----------------------------------------- |
- | self | This project | Project | not null | READ | |
- | categories | Categories available in this project | Collection | not null | READ | |
- | types | Types available in this project | Collection | not null | READ | **Permission**: view work packages or manage types |
- | versions | Versions available in this project | Collection | not null | READ | **Permission**: view work packages or manage versions |
- | memberships | Memberships in the project | Collection | not null | READ | **Permission**: view members |
- | workPackages | Work Packages of this project | Collection | not null | READ | |
- | parent | Parent project of the project | Project | | READ/WRITE | **Permission** edit project |
- | status | Denotes the status of the project, so whether the project is on track, at risk or is having trouble. | ProjectStatus | | READ/WRITE | **Permission** edit project |
+ | Link | Description | Type | Constraints | Supported operations |Condition |
+ | :----------: | ------------- | ---- | ----------- | -------------------- |----------------------------------------- |
+ | self | This project | Project | not null | READ | |
+ | ancestors | Array of all ancestors of the project, down from the root node (first element) to the parent (last element). | Collection | not null | READ | **Permission** view project on the ancestor project. Non visible projects will be omitted |
+ | categories | Categories available in this project | Collection | not null | READ | |
+ | types | Types available in this project | Collection | not null | READ | **Permission**: view work packages or manage types |
+ | versions | Versions available in this project | Collection | not null | READ | **Permission**: view work packages or manage versions |
+ | memberships | Memberships in the project | Collection | not null | READ | **Permission**: view members |
+ | workPackages | Work Packages of this project | Collection | not null | READ | |
+ | parent | Parent project of the project | Project | | READ/WRITE | **Permission** edit project |
+ | status | Denotes the status of the project, so whether the project is on track, at risk or is having trouble. | ProjectStatus | | READ/WRITE | **Permission** edit project |
Depending on custom fields defined for projects, additional links might exist.
- Note, that the parent link may contain the "undisclosed uri" `urn:openproject-org:api:v3:undisclosed` in case a
- parent project is defined but the client lacks permission to see it. See the
+ Note, that the parent and ancestor links may contain the "undisclosed uri" `urn:openproject-org:api:v3:undisclosed` in case an
+ ancestor project is defined but the client lacks permission to see it. See the
[general introduction into links' properties](https://www.openproject.org/docs/api/basic-objects/#local-properties) for more information.
## Local Properties
diff --git a/lib/api/v3/projects/project_eager_loading_wrapper.rb b/lib/api/v3/projects/project_eager_loading_wrapper.rb
index 84bca3cacb0..644f6f64fb7 100644
--- a/lib/api/v3/projects/project_eager_loading_wrapper.rb
+++ b/lib/api/v3/projects/project_eager_loading_wrapper.rb
@@ -36,12 +36,27 @@ module API
class << self
def wrap(projects)
- custom_fields = if projects.present?
- projects.first.available_custom_fields
- end
+ custom_fields = projects.first.available_custom_fields if projects.present?
+ ancestors = ancestor_projects(projects) if projects.present?
super
- .each { |project| project.available_custom_fields = custom_fields }
+ .each do |project|
+ project.available_custom_fields = custom_fields
+ project.ancestors_from_root = ancestors.select { |a| a.is_ancestor_of?(project) }.sort_by(&:lft)
+ end
+ end
+
+ def ancestor_projects(projects)
+ ancestor_selector = projects[1..].inject(ancestor_project_select(projects[0])) do |select, project|
+ select.or(ancestor_project_select(project))
+ end
+ Project.where(ancestor_selector).to_a
+ end
+
+ def ancestor_project_select(project)
+ projects_table = Project.arel_table
+
+ projects_table[:lft].lt(project.lft).and(projects_table[:rgt].gt(project.rgt))
end
end
end
diff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rb
index cca7a20c529..6584f5ec7c6 100644
--- a/lib/api/v3/projects/project_representer.rb
+++ b/lib/api/v3/projects/project_representer.rb
@@ -153,12 +153,31 @@ module API
}
end
+ links :ancestors,
+ uncacheable: true do
+ represented.ancestors_from_root.map do |ancestor|
+ # Explicitly check for admin as an archived project
+ # will lead to the admin loosing permissions in the project.
+ if current_user.admin? || ancestor.visible?
+ {
+ href: api_v3_paths.project(ancestor.id),
+ title: ancestor.name
+ }
+ else
+ {
+ href: API::V3::URN_UNDISCLOSED,
+ title: I18n.t(:'api_v3.undisclosed.ancestor')
+ }
+ end
+ end
+ end
+
associated_resource :parent,
v3_path: :project,
representer: ::API::V3::Projects::ProjectRepresenter,
uncacheable_link: true,
undisclosed: true,
- skip_render: ->(*) { represented.parent && !represented.parent.visible? }
+ skip_render: ->(*) { represented.parent && !represented.parent.visible? && !current_user.admin? }
property :id
property :identifier,
diff --git a/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb b/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb
index 6a8e818f9d9..f45b15d8fcf 100644
--- a/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb
+++ b/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb
@@ -37,9 +37,9 @@ module API
included do
# Because of the ruby method lookup,
- # wrapping the work_package here and define the
+ # wrapping the object here and define the
# available_custom_fields methods on the wrapper does not suffice.
- # We thus extend each work package.
+ # We thus extend each object (e.g. work package).
def initialize(object)
super
object.extend(CustomFieldAccessorPatch)
diff --git a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb b/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
index b4216dc947d..0724c7b6671 100644
--- a/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/projects/project_representer_rendering_spec.rb
@@ -50,13 +50,25 @@ describe ::API::V3::Projects::ProjectRepresenter, 'rendering' do
.to receive(:custom_value_for)
.with(version_custom_field)
.and_return(version_custom_value)
+
+ allow(p)
+ .to receive(:ancestors_from_root)
+ .and_return(ancestors)
end
end
let(:status) do
FactoryBot.build_stubbed(:project_status)
end
- let(:parent_project) { FactoryBot.build_stubbed(:project) }
+ let(:parent_project) do
+ FactoryBot.build_stubbed(:project).tap do |parent|
+ allow(parent)
+ .to receive(:visible?)
+ .and_return(parent_visible)
+ end
+ end
let(:representer) { described_class.create(project, current_user: user, embed_links: true) }
+ let(:parent_visible) { true }
+ let(:ancestors) { [parent_project] }
let(:user) do
FactoryBot.build_stubbed(:user).tap do |u|
@@ -196,40 +208,147 @@ describe ::API::V3::Projects::ProjectRepresenter, 'rendering' do
end
describe 'parent' do
- before do
- allow(parent_project)
- .to receive(:visible?)
- .and_return(visible)
- end
- let(:visible) { true }
+ let(:link) { 'parent' }
it_behaves_like 'has a titled link' do
- let(:link) { 'parent' }
let(:href) { api_v3_paths.project(parent_project.id) }
let(:title) { parent_project.name }
end
context 'if lacking the permissions to see the parent' do
- let(:visible) { false }
+ let(:parent_visible) { false }
it_behaves_like 'has a titled link' do
- let(:link) { 'parent' }
let(:href) { API::V3::URN_UNDISCLOSED }
let(:title) { I18n.t(:'api_v3.undisclosed.parent') }
end
end
+ context 'if lacking the permissions to see the parent but being an admin (archived project)' do
+ let(:parent_visible) { false }
+
+ before do
+ allow(user)
+ .to receive(:admin?)
+ .and_return(true)
+ end
+
+ it_behaves_like 'has a titled link' do
+ let(:href) { api_v3_paths.project(parent_project.id) }
+ let(:title) { parent_project.name }
+ end
+ end
+
context 'without a parent' do
let(:parent_project) { nil }
+ let(:ancestors) { [] }
it_behaves_like 'has an untitled link' do
- let(:link) { 'parent' }
let(:href) { nil }
end
end
end
- context 'status' do
+ # rubocop:disable RSpec/MultipleMemoizedHelpers
+ describe 'ancestors' do
+ let(:link) { 'ancestors' }
+ let(:grandparent_project) do
+ FactoryBot.build_stubbed(:project).tap do |p|
+ allow(p)
+ .to receive(:visible?)
+ .and_return(true)
+ end
+ end
+ let(:root_project) do
+ FactoryBot.build_stubbed(:project).tap do |p|
+ allow(p)
+ .to receive(:visible?)
+ .and_return(true)
+ end
+ end
+ let(:ancestors) { [root_project, grandparent_project, parent_project] }
+
+ it_behaves_like 'has a link collection' do
+ let(:hrefs) do
+ [
+ {
+ href: api_v3_paths.project(root_project.id),
+ title: root_project.name
+ },
+ {
+ href: api_v3_paths.project(grandparent_project.id),
+ title: grandparent_project.name
+ },
+ {
+ href: api_v3_paths.project(parent_project.id),
+ title: parent_project.name
+ }
+ ]
+ end
+ end
+
+ context 'if lacking the permissions to see the parent but being allowed to see the other ancestors' do
+ let(:parent_visible) { false }
+
+ it_behaves_like 'has a link collection' do
+ let(:hrefs) do
+ [
+ {
+ href: api_v3_paths.project(root_project.id),
+ title: root_project.name
+ },
+ {
+ href: api_v3_paths.project(grandparent_project.id),
+ title: grandparent_project.name
+ },
+ {
+ href: API::V3::URN_UNDISCLOSED,
+ title: I18n.t(:'api_v3.undisclosed.ancestor')
+ }
+ ]
+ end
+ end
+ end
+
+ context 'if lacking the permissions to see the parent but being an admin (archived project)' do
+ let(:parent_visible) { false }
+
+ before do
+ allow(user)
+ .to receive(:admin?)
+ .and_return(true)
+ end
+
+ it_behaves_like 'has a link collection' do
+ let(:hrefs) do
+ [
+ {
+ href: api_v3_paths.project(root_project.id),
+ title: root_project.name
+ },
+ {
+ href: api_v3_paths.project(grandparent_project.id),
+ title: grandparent_project.name
+ },
+ {
+ href: api_v3_paths.project(parent_project.id),
+ title: parent_project.name
+ }
+ ]
+ end
+ end
+ end
+
+ context 'without an ancestor' do
+ let(:parent_project) { nil }
+ let(:ancestors) { [] }
+
+ it_behaves_like 'has an empty link collection'
+ end
+ end
+ # rubocop:enable RSpec/MultipleMemoizedHelpers
+
+ describe 'status' do
it_behaves_like 'has a titled link' do
let(:link) { 'status' }
let(:href) { api_v3_paths.project_status(project.status.code) }
@@ -333,7 +452,7 @@ describe ::API::V3::Projects::ProjectRepresenter, 'rendering' do
end
end
- context 'link custom field' do
+ describe 'link custom field' do
context 'if the user is admin and the field is invisible' do
before do
allow(user)
diff --git a/spec/requests/api/v3/project_resource_spec.rb b/spec/requests/api/v3/project_resource_spec.rb
index 7911c26d563..eaba66a77cc 100644
--- a/spec/requests/api/v3/project_resource_spec.rb
+++ b/spec/requests/api/v3/project_resource_spec.rb
@@ -82,7 +82,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
last_response
end
- context 'logged in user' do
+ context 'for a logged in user' do
it 'responds with 200 OK' do
expect(subject.status).to eq(200)
end
@@ -92,10 +92,14 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
expect(subject.body).to be_json_eql(project.identifier.to_json).at_path('identifier')
end
- it 'links to the parent project' do
+ it 'links to the parent/ancestor project' do
expect(subject.body)
.to be_json_eql(api_v3_paths.project(parent_project.id).to_json)
.at_path('_links/parent/href')
+
+ expect(subject.body)
+ .to be_json_eql(api_v3_paths.project(parent_project.id).to_json)
+ .at_path('_links/ancestors/0/href')
end
it 'includes custom fields' do
@@ -116,7 +120,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
.at_path("_links/status/href")
end
- context 'requesting nonexistent project' do
+ context 'when requesting nonexistent project' do
let(:get_path) { api_v3_paths.project 9999 }
before do
@@ -126,7 +130,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
it_behaves_like 'not found'
end
- context 'requesting project without sufficient permissions' do
+ context 'when requesting project without sufficient permissions' do
let(:get_path) { api_v3_paths.project other_project.id }
before do
@@ -136,8 +140,9 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
it_behaves_like 'not found'
end
- context 'not being allowed to see the parent project' do
+ context 'when not being allowed to see the parent project' do
let!(:parent_memberships) do
+ # no parent memberships
end
it 'shows the `undisclosed` uri' do
@@ -171,7 +176,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
end
end
- context 'not logged in user' do
+ context 'for a not logged in user' do
let(:current_user) { FactoryBot.create(:anonymous) }
before do
@@ -236,7 +241,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
end
let(:get_path) do
- "#{api_v3_paths.projects}?filters=#{CGI.escape(JSON.dump(filter_query))}"
+ api_v3_paths.path_for :projects, filters: filter_query
end
it_behaves_like 'API V3 collection response', 1, 1, 'Project'
@@ -256,7 +261,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
let(:role) { FactoryBot.create(:role, permissions: [:copy_projects]) }
let(:get_path) do
- api_v3_paths.path_for :projects, filters: [{ "user_action": { "operator": "=", "values": ["projects/copy"] } }]
+ api_v3_paths.path_for :projects, filters: [{ user_action: { operator: "=", values: ["projects/copy"] } }]
end
it_behaves_like 'API V3 collection response', 1, 1, 'Project'
@@ -268,7 +273,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
end
end
- context 'filtering for principals (members)' do
+ context 'when filtering for principals (members)' do
let(:other_project) do
Role.non_member
FactoryBot.create(:public_project)
@@ -281,7 +286,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
end
let(:get_path) do
- "#{api_v3_paths.projects}?filters=#{CGI.escape(JSON.dump(filter_query))}"
+ api_v3_paths.path_for :projects, filters: filter_query
end
it 'returns the filtered for value' do
@@ -297,7 +302,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
end
let(:get_path) do
- "#{api_v3_paths.projects}?filters=#{CGI.escape(JSON.dump(filter_query))}"
+ api_v3_paths.path_for :projects, filters: filter_query
end
it 'returns the projects not matching the value' do
@@ -331,7 +336,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
end
let(:get_path) do
- api_v3_paths.path_for :projects, filters: [{ "visible": { "operator": "=", "values": [other_user.id.to_s] } }]
+ api_v3_paths.path_for :projects, filters: [{ visible: { operator: "=", values: [other_user.id.to_s] } }]
end
current_user { admin }
@@ -439,9 +444,9 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
expect(last_response.body)
.to be_json_eql(
{
- "format": "markdown",
- "html": "<p class=\"op-uc-p\">Some explanation.</p>",
- "raw": "Some explanation."
+ format: "markdown",
+ html: "<p class=\"op-uc-p\">Some explanation.</p>",
+ raw: "Some explanation."
}.to_json
)
.at_path("statusExplanation")
@@ -462,7 +467,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
identifier: 'new_project_identifier',
name: 'Project name',
"customField#{custom_field.id}": {
- "raw": "CF text"
+ raw: "CF text"
}
}.to_json
end
@@ -597,7 +602,7 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
let(:body) do
{
"customField#{custom_field.id}": {
- "raw": "CF text"
+ raw: "CF text"
}
}
end
@@ -653,9 +658,9 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
expect(last_response.body)
.to be_json_eql(
{
- "format": "markdown",
- "html": "<p class=\"op-uc-p\">Some explanation.</p>",
- "raw": "Some explanation."
+ format: "markdown",
+ html: "<p class=\"op-uc-p\">Some explanation.</p>",
+ raw: "Some explanation."
}.to_json
)
.at_path("statusExplanation")
@@ -684,9 +689,9 @@ describe 'API v3 Project resource', type: :request, content_type: :json do
expect(last_response.body)
.to be_json_eql(
{
- "format": "markdown",
- "html": "<p class=\"op-uc-p\">Some explanation.</p>",
- "raw": "Some explanation."
+ format: "markdown",
+ html: "<p class=\"op-uc-p\">Some explanation.</p>",
+ raw: "Some explanation."
}.to_json
)
.at_path("statusExplanation")