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

linting

ulferts47a90a3cef41
diff --git a/lib/api/decorators/sql/hal.rb b/lib/api/decorators/sql/hal.rb
index 74de1447c36..185f1868b42 100644
--- a/lib/api/decorators/sql/hal.rb
+++ b/lib/api/decorators/sql/hal.rb
@@ -266,6 +266,14 @@ module API
link[:href] ? link[:href].call(walker_result) : "format('#{api_v3_paths.send(path_name, '%s')}', #{column})"
end
+
+ def sql_offset(walker_result)
+ (walker_result.offset - 1) * walker_result.page_size
+ end
+
+ def sql_limit(walker_result)
+ walker_result.page_size
+ end
end
end
end
diff --git a/lib/api/decorators/sql_collection_representer.rb b/lib/api/decorators/sql_collection_representer.rb
index 41280981dcb..3150324494e 100644
--- a/lib/api/decorators/sql_collection_representer.rb
+++ b/lib/api/decorators/sql_collection_representer.rb
@@ -52,14 +52,6 @@ module API
def href_query(walker_results, overrides)
walker_results.url_query.merge(overrides).to_query
end
-
- def sql_offset(walker_result)
- (walker_result.offset - 1) * walker_result.page_size
- end
-
- def sql_limit(walker_result)
- walker_result.page_size
- end
end
property :_type,
diff --git a/lib/api/v3/projects/project_sql_representer.rb b/lib/api/v3/projects/project_sql_representer.rb
index 4dea6698885..881240978e7 100644
--- a/lib/api/v3/projects/project_sql_representer.rb
+++ b/lib/api/v3/projects/project_sql_representer.rb
@@ -48,18 +48,11 @@ module API
(
SELECT
origin.id,
- CASE
- WHEN ancestors.id IS NOT NULL AND visible_ancestors.id IS NOT NULL
- THEN json_build_object('href', format('/api/v3/projects/%s', ancestors.id), 'title', ancestors.name)
- WHEN ancestors.id IS NOT NULL AND visible_ancestors.id IS NULL
- THEN json_build_object('href', '#{API::V3::URN_UNDISCLOSED}', 'title', '#{I18n.t(:'api_v3.undisclosed.ancestor')}')
- ELSE NULL
- END link
+ #{ancestor_projection} link
FROM projects origin
LEFT OUTER JOIN projects ancestors
ON ancestors.lft < origin.lft AND ancestors.rgt > origin.rgt
- LEFT OUTER JOIN (#{Project.visible.to_sql}) visible_ancestors
- ON visible_ancestors.id = ancestors.id
+ #{visibility_join}
WHERE origin.id IN (#{origin_subselect(walker_result).select(:id).to_sql})
ORDER by origin.id, ancestors.lft
) ancestors
@@ -69,11 +62,44 @@ module API
def origin_subselect(walker_result)
if walker_result.page_size
- walker_result.filter_scope.limit(walker_result.page_size).offset((walker_result.offset - 1) * walker_result.page_size)
+ walker_result.filter_scope.limit(sql_limit(walker_result)).offset(sql_offset(walker_result))
else
walker_result.filter_scope
end
end
+
+ def ancestor_projection
+ if User.current.admin?
+ <<-SQL.squish
+ CASE
+ WHEN ancestors.id IS NOT NULL
+ THEN json_build_object('href', format('/api/v3/projects/%s', ancestors.id), 'title', ancestors.name)
+ ELSE NULL
+ END
+ SQL
+ else
+ <<-SQL.squish
+ CASE
+ WHEN ancestors.id IS NOT NULL AND visible_ancestors.id IS NOT NULL
+ THEN json_build_object('href', format('/api/v3/projects/%s', ancestors.id), 'title', ancestors.name)
+ WHEN ancestors.id IS NOT NULL AND visible_ancestors.id IS NULL
+ THEN json_build_object('href', '#{API::V3::URN_UNDISCLOSED}', 'title', '#{I18n.t(:'api_v3.undisclosed.ancestor')}')
+ ELSE NULL
+ END
+ SQL
+ end
+ end
+
+ def visibility_join
+ if User.current.admin?
+ ''
+ else
+ <<-SQL.squish
+ LEFT OUTER JOIN (#{Project.visible.to_sql}) visible_ancestors
+ ON visible_ancestors.id = ancestors.id
+ SQL
+ end
+ end
end
link :self,
diff --git a/lib/api/v3/utilities/endpoints/sql_index.rb b/lib/api/v3/utilities/endpoints/sql_index.rb
index 8bf1b2f06d1..bfca63ae36c 100644
--- a/lib/api/v3/utilities/endpoints/sql_index.rb
+++ b/lib/api/v3/utilities/endpoints/sql_index.rb
@@ -72,7 +72,7 @@ module API
end
def nested_hash(path)
- { path[0] => path.length > 1 ? nested_hash(path[1..-1]) : {} }
+ { path[0] => path.length > 1 ? nested_hash(path[1..]) : {} }
end
end
end
diff --git a/spec/lib/api/v3/projects/project_sql_representer_renderning_spec.rb b/spec/lib/api/v3/projects/project_sql_representer_rendering_spec.rb
similarity index 82%
rename from spec/lib/api/v3/projects/project_sql_representer_renderning_spec.rb
rename to spec/lib/api/v3/projects/project_sql_representer_rendering_spec.rb
index 01fe6d34911..cae929cc517 100644
--- a/spec/lib/api/v3/projects/project_sql_representer_renderning_spec.rb
+++ b/spec/lib/api/v3/projects/project_sql_representer_rendering_spec.rb
@@ -29,6 +29,16 @@ require 'spec_helper'
describe ::API::V3::Projects::ProjectSqlRepresenter, 'rendering' do
include ::API::V3::Utilities::PathHelper
+ subject(:json) do
+ ::API::V3::Utilities::SqlRepresenterWalker
+ .new(scope,
+ embed: {},
+ select: select,
+ current_user: current_user)
+ .walk(described_class)
+ .to_json
+ end
+
let(:scope) do
Project
.where(id: project.id)
@@ -40,24 +50,14 @@ describe ::API::V3::Projects::ProjectSqlRepresenter, 'rendering' do
let(:role) { create(:role) }
+ let(:select) { { 'id' => {}, '_type' => {}, 'self' => {}, 'name' => {}, 'ancestors' => {} } }
+
current_user do
create(:user,
member_in_project: project,
member_through_role: role)
end
- let(:select) { { 'id' => {}, '_type' => {}, 'self' => {}, 'name' => {}, 'ancestors' => {} } }
-
- subject(:json) do
- ::API::V3::Utilities::SqlRepresenterWalker
- .new(scope,
- embed: {},
- select: select,
- current_user: current_user)
- .walk(described_class)
- .to_json
- end
-
context 'without an ancestor' do
it 'renders as expected' do
expect(json)
@@ -158,4 +158,46 @@ describe ::API::V3::Projects::ProjectSqlRepresenter, 'rendering' do
)
end
end
+
+ context 'with an archived ancestor but with the user being admin' do
+ let!(:parent) do
+ create(:project, active: false).tap do |parent|
+ project.parent = parent
+ project.save
+ end
+ end
+
+ let!(:grandparent) do
+ create(:project).tap do |grandparent|
+ parent.parent = grandparent
+ parent.save
+ end
+ end
+
+ let(:select) { { 'ancestors' => {} } }
+
+ current_user do
+ create(:admin)
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(
+ {
+ _links: {
+ ancestors: [
+ {
+ href: api_v3_paths.project(grandparent.id),
+ title: grandparent.name
+ },
+ {
+ href: api_v3_paths.project(parent.id),
+ title: parent.name
+ }
+ ]
+ }
+ }.to_json
+ )
+ end
+ end
end
diff --git a/spec/requests/api/v3/capability_resource_spec.rb b/spec/requests/api/v3/capability_resource_spec.rb
index be867888a75..798f932520e 100644
--- a/spec/requests/api/v3/capability_resource_spec.rb
+++ b/spec/requests/api/v3/capability_resource_spec.rb
@@ -344,7 +344,7 @@ describe 'API v3 capabilities resource', type: :request, content_type: :json do
{
id: "memberships/update/p#{project.id}-#{current_user.id}"
}
- ]
+ ]
}
}