Skip to content

Commit

Permalink
- Disable the command while we figure out the best approach to handle…
Browse files Browse the repository at this point in the history
… async copy

- Fix old inst_vars references.
- Renames parameters on CopyFolder#call
- Mirror the project_folder_location on project_folder_path
  • Loading branch information
mereghost committed Feb 21, 2024
1 parent f2001a2 commit f29cac3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def build_origin_urls(source_path:, destination_path:)
end

def remote_folder_does_not_exist?(destination_url)
response = OpenProject.httpx.basic_auth(@username, @password).head(destination_url)
response = OpenProject.httpx.basic_auth(@storage.username, @storage.password).head(destination_url)

case response
in { status: 200..299 }
Expand All @@ -101,7 +101,7 @@ def remote_folder_does_not_exist?(destination_url)
def copy_folder(source_url:, destination_url:)
response = OpenProject
.httpx
.basic_auth(@username, @password)
.basic_auth(@storage.username, @storage.password)
.request('COPY', source_url, headers: { 'Destination' => destination_url, 'Depth' => 'infinity' })

case response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ module OneDrive

Commands = Dry::Container::Namespace.new('commands') do
namespace('one_drive') do
register(:copy_template_folder, CopyTemplateFolderCommand)
register(:create_folder, CreateFolderCommand)
register(:delete_folder, DeleteFolderCommand)
register(:rename_file, RenameFileCommand)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,25 @@ module StorageInteraction
module OneDrive
class CopyTemplateFolderCommand
def self.call(storage:, source_path:, destination_path:)
raise ArgumentError if source_path.blank? || destination_path.blank?
if source_path.blank? || destination_path.blank?
return ServiceResult.failure(
result: :error,
errors: StorageError.new(code: :error,
log_message: 'Both source and destination paths need to be present')
)
end

new(storage).call(source_path, destination_path)
new(storage).call(source_location: source_path, destination_name: destination_path)
end

def initialize(storage)
@storage = storage
end

def call(source_path, destination_path)
def call(source_location:, destination_name:)
Util.using_admin_token(@storage) do |httpx|
handle_response(
httpx.post(copy_path_for(source_path), json: payload(destination_path))
httpx.post(copy_path_for(source_location), json: { name: destination_name })
)
end
end
Expand Down Expand Up @@ -79,10 +85,8 @@ def extract_id_from_url(url)
match_data[:item_id] if match_data
end

def payload(destination_path) = { name: destination_path }

def copy_path_for(path)
"/v1.0/drives/#{@storage.drive_id}/items/#{path}/[email protected]=fail"
def copy_path_for(source_location)
"/v1.0/drives/#{@storage.drive_id}/items/#{source_location}/[email protected]=fail"
end
end
end
Expand Down
12 changes: 8 additions & 4 deletions modules/storages/app/models/storages/project_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ def automatic_management_possible?
end

def project_folder_path
return "#{storage.group_folder}/#{project.name.tr('/', '|')} (#{project.id})/" if storage.provider_type_nextcloud?
return "#{project.name.gsub(/[\\<>+?:"|\/]/, '_')} (#{project.id})" if storage.provider_type_one_drive?

raise 'Unknown Storage'
case storage.short_provider_type
when 'nextcloud'
"#{storage.group_folder}/#{project.name.tr('/', '|')} (#{project.id})/"
when 'one_drive'
"#{project.name.gsub(/[\\<>+?:"|\/]/, '_')} (#{project.id})"
else
raise 'Unknown Storage'
end
end

def project_folder_location
Expand Down
51 changes: 28 additions & 23 deletions modules/storages/app/models/storages/storage_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,37 @@
# See COPYRIGHT and LICENSE files for more details.
#++

class Storages::StorageError
extend ActiveModel::Naming
module Storages
class StorageError
extend ActiveModel::Naming

attr_reader :code, :log_message, :data
attr_reader :code, :log_message, :data

def initialize(code:, log_message: nil, data: nil)
@code = code
@log_message = log_message
@data = data
end
def initialize(code:, log_message: nil, data: nil)
@code = code
@log_message = log_message
@data = data
end

def to_active_model_errors
errors = ActiveModel::Errors.new(self)
errors.add(:storage_error, code, message: log_message)
errors
end
def to_active_model_errors
errors = ActiveModel::Errors.new(self)
errors.add(:storage_error, code, message: log_message)
errors
end

def to_s
output = code.to_s
output << " | #{log_message}" unless log_message.nil?
output << " | #{data}" unless data.nil?
output
end
def to_s
output = code.to_s
output << " | #{log_message}" unless log_message.nil?
output << " | #{data}" unless data.nil?
output
end

def storage_error = "storage error"

def storage_error = "storage error"
def read_attribute_for_validation(attr) = send(attr)
def self.human_attribute_name(attr, _options = {}) = attr
def self.lookup_ancestors = [self]
def read_attribute_for_validation(attr) = send(attr)

def self.human_attribute_name(attr, _options = {}) = attr

def self.lookup_ancestors = [self]
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@

shared_let(:source_path) { base_template_folder.id }

it 'is registered under commands.one_drive.copy_template_folder' do
it 'is registered under commands.one_drive.copy_template_folder',
skip: 'Skipped while we decide on what to do with the copy project folder' do
expect(Storages::Peripherals::Registry.resolve('commands.one_drive.copy_template_folder')).to eq(described_class)
end

Expand All @@ -65,7 +66,11 @@
end

it "destination_path and source_path can't be empty" do
expect { described_class.call(storage:, source_path: '', destination_path: nil) }.to raise_error ArgumentError
missing_source = described_class.call(storage:, source_path: '', destination_path: 'Path')
missing_path = described_class.call(storage:, source_path: 'Path', destination_path: nil)
missing_both = described_class.call(storage:, source_path: nil, destination_path: '')

expect([missing_both, missing_path, missing_source]).to all(be_failure)
end

describe '#call' do
Expand Down

0 comments on commit f29cac3

Please sign in to comment.