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
[#66513] File links from other project folders are not copied when user copies a project
Pavel Balashou6e3fc6a44398
diff --git a/app/workers/copy_project_job.rb b/app/workers/copy_project_job.rbindex 8b2da04c3ee..7209ea10c34 100644--- a/app/workers/copy_project_job.rb+++ b/app/workers/copy_project_job.rb@@ -153,14 +153,14 @@ class CopyProjectJob < ApplicationJobresultend- def enqueue_copy_project_folder_jobs(copied_storages, work_packages_map, only)+ def enqueue_copy_project_folder_jobs(copied_project_storages, work_packages_map, only)return unless only.intersect?(%w[file_links storage_project_folders])- Array(copied_storages).each do |storage_pair|+ Array(copied_project_storages).each do |project_storage_pair|batch.enqueue doStorages::CopyProjectFoldersJob- .perform_later(source: storage_pair[:source],- target: storage_pair[:target],+ .perform_later(source: project_storage_pair[:source],+ target: project_storage_pair[:target],work_packages_map: work_packages_map.stringify_keys)endenddiff --git a/modules/storages/app/services/storages/file_links/copy_file_links_service.rb b/modules/storages/app/services/storages/file_links/copy_file_links_service.rbindex a78021ebe06..d2acb8835b6 100644--- a/modules/storages/app/services/storages/file_links/copy_file_links_service.rb+++ b/modules/storages/app/services/storages/file_links/copy_file_links_service.rb@@ -58,7 +58,7 @@ module Storagescopy_file_links(source_file_links)endend- info "File link creation finished"+ info "File links creation finished"@resultend@@ -66,86 +66,74 @@ module Storagesdef copy_file_links(source_file_links)if @source.project_folder_automatic?- create_managed_file_links(source_file_links).or do |error|+ create_file_links_when_folder_is_managed(source_file_links).or do |error|log_adapter_error(error)@result.success = falseendelse- create_unmanaged_file_links(source_file_links)+ create_file_links_when_folder_is_unmanaged(source_file_links)endend- def create_managed_file_links(source_file_links)+ def create_file_links_when_folder_is_managed(source_file_links)info "Getting information about the source file links"- source_files_info(source_file_links).bind do |source_info|+ query_source_files_info(source_file_links).bind do |source_files_info|info "Getting information about the copied target files"- target_files_map.bind do |target_map|- info "Building equivalency map..."- location_map = build_location_map(source_info, target_map)- info "Creating file links based on the location map #{location_map}"- source_file_links.find_each do |source_link|- target_location = location_map[source_link.origin_id]- next if target_location.blank?-- create_target_file_link(source_link, target_location)+ query_target_project_folder_files_map.bind do |target_project_folder_files_map|+ target_project_folder_files_map.transform_keys! { |key| key.starts_with?("/") ? key : "/#{key}" }+ source_files_info.each do |info|+ potential_file_location_in_target_folder =+ info.clean_location&.gsub(@source.managed_project_folder_path,+ @target.managed_project_folder_path)+ storage_file_id =+ potential_file_location_in_target_folder.present? &&+ target_project_folder_files_map[potential_file_location_in_target_folder]+ source_link = source_file_links.find { |link| link.origin_id == info.id }+ if storage_file_id.present?+ create_file_link(source_link, storage_file_id.id)+ else+ create_file_link(source_link, source_link.origin_id)+ endendSuccess()endendend- def create_target_file_link(source_link, remote_id)+ def create_file_link(source_link, origin_id)attributes = source_link.dup.attributesattributes.merge!("storage_id" => @target.storage_id,"creator_id" => @user.id,"container_id" => @work_packages_map[source_link.container_id],- "origin_id" => remote_id+ "origin_id" => origin_id)CreateService.new(user: @user, contract_class: CopyContract).call(attributes)end- def build_location_map(source_files, target_location_map)- # We need this due to inconsistencies of how we represent the File Path- target_location_map.transform_keys! { |key| key.starts_with?("/") ? key : "/#{key}" }-- source_files.filter_map do |info|- # in case file has been deleted location is nil- next nil if info.location.blank?-- target = info.clean_location.gsub(@source.managed_project_folder_path, @target.managed_project_folder_path)- [info.id.to_s, target_location_map[target]&.id || id]- end.to_h- end-def auth_strategyAdapters::Registry.resolve("#{@source.storage}.authentication.userless").callend- def source_files_info(source_file_links)+ def query_source_files_info(source_file_links)Adapters::Input::FilesInfo.build(file_ids: source_file_links.pluck(:origin_id)).bind do |input_data|Adapters::Registry.resolve("#{@source.storage}.queries.files_info").call(storage: @source.storage, auth_strategy:, input_data:)endend- def target_files_map+ def query_target_project_folder_files_mapAdapters::Input::FilePathToIdMap.build(folder: @target.project_folder_location).bind do |input_data|Adapters::Registry.resolve("#{@target.storage}.queries.file_path_to_id_map")- .call(storage: @target.storage, auth_strategy:, input_data:)+ .call(storage: @target.storage, auth_strategy:, input_data:)endend- def create_unmanaged_file_links(source_file_links)+ def create_file_links_when_folder_is_unmanaged(source_file_links)source_file_links.find_each do |source_file_link|- attributes = source_file_link.dup.attributes- attributes["storage_id"] = @target.storage_id- attributes["creator_id"] = @user.id- attributes["container_id"] = @work_packages_map[source_file_link.container_id]-- FileLinks::CreateService.new(user: @user, contract_class: CopyContract).call(attributes)+ create_file_link(source_file_link, source_file_link.origin_id)endendenddiff --git a/modules/storages/app/services/storages/project_storages/copy_project_folders_service.rb b/modules/storages/app/services/storages/project_storages/copy_project_folders_service.rbindex 283746cd6fb..7f3b3714477 100644--- a/modules/storages/app/services/storages/project_storages/copy_project_folders_service.rb+++ b/modules/storages/app/services/storages/project_storages/copy_project_folders_service.rb@@ -49,7 +49,7 @@ module Storagesreturn @result.map { @data.with(id: source.project_folder_id) } if manually_managed_source?(source)info "Initiating copy of project folder #{source.managed_project_folder_path} to #{target.managed_project_folder_path}"- copy_result = initiate_copy(source.storage, source.project_folder_location, target.managed_project_folder_path)+ copy_result = copy_project_folder(source.storage, source.project_folder_location, target.managed_project_folder_path)copy_result.either(->(success) { @result.map { success } },->(failed) { @result.map { failed } }@@ -74,7 +74,7 @@ module Storagesendend- def initiate_copy(storage, source_path, destination_path)+ def copy_project_folder(storage, source_path, destination_path)Adapters::Input::CopyTemplateFolder.build(source: source_path, destination: destination_path).bind do |input_data|Adapters::Registry.resolve("#{storage}.commands.copy_template_folder")diff --git a/modules/storages/app/workers/storages/copy_project_folders_job.rb b/modules/storages/app/workers/storages/copy_project_folders_job.rbindex c55cdaa87e2..a3a652929b0 100644--- a/modules/storages/app/workers/storages/copy_project_folders_job.rb+++ b/modules/storages/app/workers/storages/copy_project_folders_job.rb@@ -38,6 +38,13 @@ module Storages}discard_on HTTPX::HTTPError+ # Performs project folder copying.+ #+ # @param source [Storages::ProjectStorage]+ # @param target [Storages::ProjectStorage]+ # @param work_packages_map [Hash]+ #+ # @return [nil]def perform(source:, target:, work_packages_map:)@source = sourceuser = batch.properties[:user]@@ -50,7 +57,9 @@ module Storagesproject_folder_mode: source.project_folder_mode).on_failure { |failed| log_failure(failed) and return failed }- FileLinks::CopyFileLinksService.call(source: @source, target: target.reload, user:, work_packages_map:)+ # TODO why do we have to reload target here?+ FileLinks::CopyFileLinksService.call(source:, target: target.reload, user:, work_packages_map:)+ nilendprivatediff --git a/modules/storages/spec/services/storages/file_links/copy_file_links_service_spec.rb b/modules/storages/spec/services/storages/file_links/copy_file_links_service_spec.rbindex 56105b732a2..c3c6e9541ac 100644--- a/modules/storages/spec/services/storages/file_links/copy_file_links_service_spec.rb+++ b/modules/storages/spec/services/storages/file_links/copy_file_links_service_spec.rb@@ -124,13 +124,43 @@ RSpec.describe Storages::FileLinks::CopyFileLinksService, :webmock dopath_to_ids.delete(File.join(target_storage.managed_project_folder_path, link.origin_name))end- it "creates links for only copied files" do- expect { service.call }.to change(Storages::FileLink, :count).by(4)+ it "creates 4 links to the newly copied files and one to the deleted one" do+ expect { service.call }.to change(Storages::FileLink, :count).by(5)- Storages::FileLink.last(4).each do |link|- expect(link.origin_id).to match /_target$/+ last_5_links = Storages::FileLink.last(5)+ last_5_links.each do |link|expect(link.storage_id).to eq(target.id)end+ last_5_links_origin_ids = last_5_links.pluck(:origin_id)+ expect(last_5_links_origin_ids.count { |link| link =~ /_target$/ }).to eq(4)+ expect(last_5_links_origin_ids.count { |link| link !~ /_target$/ }).to eq(1)+ end+ end++ context "when one file_link points to a file outside of managed project folder" do+ before do+ link = source_links[-1]+ location = File.join("/", link.origin_name)+ old_location = File.join(target_storage.managed_project_folder_path, link.origin_name)+ remote_source_info[-1] = Storages::StorageFileInfo.new(status: "ok",+ status_code: 200,+ id: link.origin_id,+ name: link.origin_name,+ location:)++ path_to_ids.delete(old_location)+ end++ it "creates 4 links to the newly copied files and one to the file outside of managed project folder" do+ expect { service.call }.to change(Storages::FileLink, :count).by(5)++ last_5_links = Storages::FileLink.last(5)+ last_5_links.each do |link|+ expect(link.storage_id).to eq(target.id)+ end+ last_5_links_origin_ids = last_5_links.pluck(:origin_id)+ expect(last_5_links_origin_ids.count { |link| link =~ /_target$/ }).to eq(4)+ expect(last_5_links_origin_ids.count { |link| link !~ /_target$/ }).to eq(1)endendend