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
[#44841] adding storages via api v3
Eric Schubert66aaa14613fb
diff --git a/modules/storages/app/services/storages/storages/set_attributes_service.rb b/modules/storages/app/services/storages/storages/set_attributes_service.rbindex aa1f1870f8d..c633aa22cd6 100644--- a/modules/storages/app/services/storages/storages/set_attributes_service.rb+++ b/modules/storages/app/services/storages/storages/set_attributes_service.rb@@ -33,7 +33,6 @@ module Storages::Storagesdef set_default_attributes(_params)storage.creator ||= user- storage.provider_type = Storages::Storage::PROVIDER_TYPE_NEXTCLOUDstorage.name ||= I18n.t("storages.provider_types.#{storage.provider_type}.default_name")enddiff --git a/modules/storages/lib/api/v3/storages/create_endpoint.rb b/modules/storages/lib/api/v3/storages/create_endpoint.rbnew file mode 100644index 00000000000..8687155fb2b--- /dev/null+++ b/modules/storages/lib/api/v3/storages/create_endpoint.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.+#++++class API::V3::Storages::CreateEndpoint < API::Utilities::Endpoints::Create+ include ::API::V3::Utilities::Endpoints::V3Deductions+ include ::API::V3::Utilities::Endpoints::V3PresentSingle++ def present_success(request, service_call)+ API::V3::Storages::OAuthCredentialsRepresenter.create(+ service_call.dependent_results.first.result,+ current_user: request.current_user+ )+ end+enddiff --git a/modules/storages/lib/api/v3/storages/oauth_credentials_representer.rb b/modules/storages/lib/api/v3/storages/oauth_credentials_representer.rbnew file mode 100644index 00000000000..59d88e69396--- /dev/null+++ b/modules/storages/lib/api/v3/storages/oauth_credentials_representer.rb@@ -0,0 +1,43 @@+#-- 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::Storages+ class OAuthCredentialsRepresenter < ::API::Decorators::Single+ property :uid, as: :clientId++ property :plaintext_secret, as: :clientSecret++ def _type+ 'OauthCredentials'+ end++ link :self do+ { href: "#{::API::V3::URN_PREFIX}storages:oauth_credentials:no_link_provided" }+ end+ end+enddiff --git a/modules/storages/lib/api/v3/storages/storage_payload_representer.rb b/modules/storages/lib/api/v3/storages/storage_payload_representer.rbnew file mode 100644index 00000000000..ceac416d94a--- /dev/null+++ b/modules/storages/lib/api/v3/storages/storage_payload_representer.rb@@ -0,0 +1,33 @@+#-- 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::Storages+ class StoragePayloadRepresenter < StorageRepresenter+ include ::API::Utilities::PayloadRepresenter+ end+enddiff --git a/modules/storages/lib/api/v3/storages/storage_representer.rb b/modules/storages/lib/api/v3/storages/storage_representer.rbindex e755b55a90f..dfec895091f 100644--- a/modules/storages/lib/api/v3/storages/storage_representer.rb+++ b/modules/storages/lib/api/v3/storages/storage_representer.rb@@ -35,12 +35,33 @@ module API::V3::StoragesURN_CONNECTION_AUTH_FAILED = "#{::API::V3::URN_PREFIX}storages:authorization:FailedAuthorization".freezeURN_CONNECTION_ERROR = "#{::API::V3::URN_PREFIX}storages:authorization:Error".freeze+ URN_STORAGE_TYPE_NEXTCLOUD = "#{::API::V3::URN_PREFIX}storages:Nextcloud".freeze+class StorageRepresenter < ::API::Decorators::Single# LinkedResource module defines helper methods to describe attributesinclude API::Decorators::LinkedResourceinclude API::Decorators::DatePropertyinclude Storages::Peripherals::StorageUrlHelper+ module ClassMethods+ private++ def link_without_resource(name, getter:, setter:)+ link name do+ instance_eval(&getter)+ end++ property name,+ exec_context: :decorator,+ getter: ->(*) {},+ setter:,+ skip_render: true,+ linked_resource: true+ end+ end++ extend ClassMethods+def initialize(model, current_user:, embed_links: nil)@connection_manager =::OAuthClients::ConnectionManager.new(user: current_user, oauth_client: model.oauth_client)@@ -58,16 +79,26 @@ module API::V3::Storagesself_link- link :type do- {- href: "#{::API::V3::URN_PREFIX}storages:Nextcloud",- title: 'Nextcloud'- }- end-- link :origin do- { href: represented.host }- end+ link_without_resource :type,+ getter: ->(*) {+ { href: URN_STORAGE_TYPE_NEXTCLOUD, title: 'Nextcloud' }+ },+ setter: ->(fragment:, **) {+ break if fragment['href'].blank?++ represented.provider_type = case fragment['href']+ when URN_STORAGE_TYPE_NEXTCLOUD+ Storages::Storage::PROVIDER_TYPE_NEXTCLOUD+ end+ }++ link_without_resource :origin,+ getter: ->(*) { { href: represented.host } },+ setter: ->(fragment:, **) {+ break if fragment['href'].blank?++ represented.host = fragment['href'].gsub(/\/+$/, '')+ }link :open do{ href: storage_url_open(represented) }diff --git a/modules/storages/lib/api/v3/storages/storages_api.rb b/modules/storages/lib/api/v3/storages/storages_api.rbindex 8330d2afcf9..4250a9693fb 100644--- a/modules/storages/lib/api/v3/storages/storages_api.rb+++ b/modules/storages/lib/api/v3/storages/storages_api.rb@@ -39,6 +39,8 @@ class API::V3::Storages::StoragesAPI < ::API::OpenProjectAPI# The `:resources` keyword defines the API namespace -> /api/v3/storages/...resources :storages do+ post &::API::V3::Storages::CreateEndpoint.new(model: ::Storages::Storage).mount+# `route_param` extends the route by a route parameter of the endpoint.# The input parameter value is parsed into the `:storage_id` symbol.route_param :storage_id, type: Integer, desc: 'Storage id' do@@ -58,6 +60,8 @@ class API::V3::Storages::StoragesAPI < ::API::OpenProjectAPI# route.get &::API::V3::Utilities::Endpoints::Show.new(model: ::Storages::Storage).mount+ put &::API::V3::Utilities::Endpoints::Update.new(model: ::Storages::Storage).mount+delete &::API::V3::Utilities::Endpoints::Delete.new(model: ::Storages::Storage).mountmount API::V3::StorageFiles::StorageFilesAPI