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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#50234] Add storage filter to project and project storage collection
Eric Schubert2b50c522ccf9
diff --git a/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rb b/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rbindex be8138e2c36..5b0114b4928 100644--- a/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rb+++ b/modules/storages/app/models/queries/storages/project_storages/filter/storage_id_filter.rb@@ -39,7 +39,7 @@ module Queries::Storages::ProjectStorages::Filterenddef allowed_values- Storages::Storage.visible.pluck(:id).map { |id| [id, id.to_s] }+ values.map { |value| [nil, value] }endendenddiff --git a/modules/storages/app/models/queries/storages/project_storages/filter/storage_url_filter.rb b/modules/storages/app/models/queries/storages/project_storages/filter/storage_url_filter.rbnew file mode 100644index 00000000000..6d233de287a--- /dev/null+++ b/modules/storages/app/models/queries/storages/project_storages/filter/storage_url_filter.rb@@ -0,0 +1,61 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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 Queries::Storages::ProjectStorages::Filter+ class StorageUrlFilter < ::Queries::Filters::Base+ self.model = Storages::ProjectStorage++ def type+ :list+ end++ def human_name+ ::Storages::ProjectStorage.human_attribute_name(name)+ end++ def allowed_values+ values.map { |value| [nil, value] }+ end++ def joins+ [:storage]+ end++ def where+ operator_strategy.sql_for_field(unescape_hosts(values), Storages::Storage.table_name, 'host')+ end++ private++ def unescape_hosts(hosts)+ hosts.map { |host| CGI.unescape(host).gsub(/\/+$/, '') }+ end+ end+enddiff --git a/modules/storages/app/models/queries/storages/projects/filter/storage_id_filter.rb b/modules/storages/app/models/queries/storages/projects/filter/storage_id_filter.rbnew file mode 100644index 00000000000..5dcf38145f6--- /dev/null+++ b/modules/storages/app/models/queries/storages/projects/filter/storage_id_filter.rb@@ -0,0 +1,48 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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 Queries::Storages::Projects::Filter+ class StorageIdFilter < ::Queries::Projects::Filters::ProjectFilter+ def type+ :list+ end++ def allowed_values+ values.map { |value| [nil, value] }+ end++ def joins+ [:storages]+ end++ def where+ operator_strategy.sql_for_field(values, Storages::Storage.table_name, 'id')+ end+ end+enddiff --git a/modules/storages/app/models/queries/storages/projects/filter/storage_url_filter.rb b/modules/storages/app/models/queries/storages/projects/filter/storage_url_filter.rbnew file mode 100644index 00000000000..595a3d19dc1--- /dev/null+++ b/modules/storages/app/models/queries/storages/projects/filter/storage_url_filter.rb@@ -0,0 +1,54 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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 Queries::Storages::Projects::Filter+ class StorageUrlFilter < ::Queries::Projects::Filters::ProjectFilter+ def type+ :list+ end++ def allowed_values+ values.map { |value| [nil, value] }+ end++ def joins+ [:storages]+ end++ def where+ operator_strategy.sql_for_field(unescape_hosts(values), Storages::Storage.table_name, 'host')+ end++ private++ def unescape_hosts(hosts)+ hosts.map { |host| CGI.unescape(host).gsub(/\/+$/, '') }+ end+ end+enddiff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rbindex a2830174fce..653bce0582e 100644--- a/modules/storages/lib/open_project/storages/engine.rb+++ b/modules/storages/lib/open_project/storages/engine.rb@@ -200,12 +200,18 @@ module OpenProject::Storagesexclude filterend+ ::Queries::Register.register(::Queries::Projects::ProjectQuery) do+ filter ::Queries::Storages::Projects::Filter::StorageIdFilter+ filter ::Queries::Storages::Projects::Filter::StorageUrlFilter+ end+::Queries::Register.register(::Queries::Storages::FileLinks::FileLinkQuery) dofilter ::Queries::Storages::FileLinks::Filter::StorageFilterend::Queries::Register.register(::Queries::Storages::ProjectStorages::ProjectStoragesQuery) dofilter ::Queries::Storages::ProjectStorages::Filter::StorageIdFilter+ filter ::Queries::Storages::ProjectStorages::Filter::StorageUrlFilterfilter ::Queries::Storages::ProjectStorages::Filter::ProjectIdFilterendenddiff --git a/modules/storages/spec/requests/api/v3/project_storages/project_storages_spec.rb b/modules/storages/spec/requests/api/v3/project_storages/project_storages_spec.rbindex b0bd29c2a29..1416bfeafa9 100644--- a/modules/storages/spec/requests/api/v3/project_storages/project_storages_spec.rb+++ b/modules/storages/spec/requests/api/v3/project_storages/project_storages_spec.rb@@ -29,7 +29,7 @@require 'spec_helper'require_module_spec_helper-RSpec.describe 'API v3 project storages resource', content_type: :json, webmock: true do+RSpec.describe 'API v3 project storages resource', :webmock, content_type: :json doinclude API::V3::Utilities::PathHelperlet(:view_permissions) { %i(view_work_packages view_file_links) }@@ -108,7 +108,7 @@ RSpec.describe 'API v3 project storages resource', content_type: :json, webmock:endend- context 'with storage filter' do+ context 'with storage id filter' dolet(:filters) { [{ storageId: { operator: "=", values: [storage_id] } }] }let(:path) { api_v3_paths.path_for(:project_storages, filters:) }@@ -120,10 +120,12 @@ RSpec.describe 'API v3 project storages resource', content_type: :json, webmock:endend- context 'with invalid storage id' do+ context 'with unknown storage id' dolet(:storage_id) { '1337' }- it_behaves_like 'invalid filters'+ it_behaves_like 'API V3 collection response', 0, 0, 'ProjectStorage', 'Collection' do+ let(:elements) { [] }+ endendcontext 'with storage id of storage with no linked projects' do@@ -135,6 +137,34 @@ RSpec.describe 'API v3 project storages resource', content_type: :json, webmock:endendend++ context 'with storage url filter' do+ let(:filters) { [{ storageUrl: { operator: "=", values: [storage_url] } }] }+ let(:path) { api_v3_paths.path_for(:project_storages, filters:) }++ describe 'gets all project storages of the filtered project' do+ let(:storage_url) { CGI.escape(storage3.host) }++ it_behaves_like 'API V3 collection response', 2, 2, 'ProjectStorage', 'Collection' do+ let(:elements) { [project_storage23, project_storage13] }+ end+ end++ context 'with invalid storage url' do+ let(:storage_url) { nil }++ it_behaves_like 'invalid filters'+ end++ context 'with storage url of storage with no linked projects' do+ let(:storage) { create(:nextcloud_storage) }+ let(:storage_url) { storage.host }++ it_behaves_like 'API V3 collection response', 0, 0, 'ProjectStorage', 'Collection' do+ let(:elements) { [] }+ end+ end+ endendcontext 'as user with permissions' dodiff --git a/modules/storages/spec/requests/api/v3/projects/projects_storages_filter_spec.rb b/modules/storages/spec/requests/api/v3/projects/projects_storages_filter_spec.rbnew file mode 100644index 00000000000..f5252432a29--- /dev/null+++ b/modules/storages/spec/requests/api/v3/projects/projects_storages_filter_spec.rb@@ -0,0 +1,118 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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.+#++++require 'spec_helper'+require_module_spec_helper++RSpec.describe 'API v3 projects resource with filters for the linked storages',+ content_type: :json do+ include API::V3::Utilities::PathHelper++ let(:user) { create(:user) }+ let(:role) { create(:role, permissions: %i[view_work_packages]) }++ # rubocop:disable RSpec/IndexedLet+ let!(:project1) { create(:project, members: { user => role }) }+ let!(:project2) { create(:project, members: { user => role }) }+ let!(:project3) { create(:project, members: { user => role }) }+ let!(:project4) { create(:project, members: { user => role }) }+ let!(:storage1) { create(:nextcloud_storage) }+ let!(:storage2) { create(:one_drive_storage) }+ let!(:storage3) { create(:one_drive_storage) }+ let!(:project_storage11) { create(:project_storage, project: project1, storage: storage1) }+ let!(:project_storage12) { create(:project_storage, project: project1, storage: storage2) }+ let!(:project_storage13) { create(:project_storage, project: project1, storage: storage3) }+ let!(:project_storage23) { create(:project_storage, project: project2, storage: storage3) }+ let!(:project_storage21) { create(:project_storage, project: project2, storage: storage1) }+ let!(:project_storage31) { create(:project_storage, project: project3, storage: storage1) }+ # rubocop:enable RSpec/IndexedLet++ subject(:response) { last_response }++ before do+ login_as user+ end++ describe 'GET /api/v3/projects' do+ let(:path) { api_v3_paths.path_for :projects, filters: }++ before do+ get path+ end++ context 'with filter for storage id' do+ let(:storage_id) { storage1.id }+ let(:filters) { [{ storageId: { operator: '=', values: [storage_id] } }] }++ it_behaves_like 'API V3 collection response', 3, 3, 'Project', 'Collection' do+ let(:elements) { [project3, project2, project1] }+ end++ context 'if a project has the storages module deactivated' do+ let(:project1) { create(:project, disable_modules: :storages) }++ it_behaves_like 'API V3 collection response', 2, 2, 'Project', 'Collection' do+ let(:elements) { [project3, project2] }+ end+ end++ context 'if the filter is set to an unknown storage id' do+ let(:storage_id) { '1337' }++ it_behaves_like 'API V3 collection response', 0, 0, 'Project', 'Collection' do+ let(:elements) { [] }+ end+ end+ end++ context 'with filter for storage url' do+ let(:storage_url) { CGI.escape(storage1.host) }+ let(:filters) { [{ storageUrl: { operator: '=', values: [storage_url] } }] }++ it_behaves_like 'API V3 collection response', 3, 3, 'Project', 'Collection' do+ let(:elements) { [project3, project2, project1] }+ end++ context 'if a project has the storages module deactivated' do+ let(:project1) { create(:project, disable_modules: :storages) }++ it_behaves_like 'API V3 collection response', 2, 2, 'Project', 'Collection' do+ let(:elements) { [project3, project2] }+ end+ end++ context 'if the filter is set to an unknown storage url' do+ let(:storage_url) { CGI.escape("https://not.my-domain.org") }++ it_behaves_like 'API V3 collection response', 0, 0, 'Project', 'Collection' do+ let(:elements) { [] }+ end+ end+ end+ end+end