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
Refactor code and fix specs.
Pavel Balashouf52c2612a838
diff --git a/modules/storages/app/services/projects/copy/file_links_dependent_service.rb b/modules/storages/app/services/projects/copy/file_links_dependent_service.rbindex 2b051d6c8c7..a7521e941f8 100644--- a/modules/storages/app/services/projects/copy/file_links_dependent_service.rb+++ b/modules/storages/app/services/projects/copy/file_links_dependent_service.rb@@ -38,6 +38,8 @@ module Projects::Copyprotected+ # rubocop:disable Metrics/AbcSize+ # rubocop:disable Metrics/PerceivedComplexitydef copy_dependency(*)# If no work packages were copied, we cannot copy their file_linksreturn unless state.work_package_id_lookup@@ -46,72 +48,73 @@ module Projects::CopyStorages::FileLink.where(container_id: source_wp_ids, container_type: "WorkPackage").group_by(&:storage_id)+ .filter { |_storage_id, source_file_links| source_file_links.any? }.each do |(storage_id, source_file_links)|- if source_file_links.any?- tmp = state- .copied_project_storages- .find { |item| item["source"].storage_id == storage_id }- source_project_storage = tmp['source']- target_project_storage = tmp['target']- storage_requests = Storages::Peripherals::StorageRequests.new(storage: source_project_storage.storage)+ tmp = state+ .copied_project_storages+ .find { |item| item["source"].storage_id == storage_id }+ source_project_storage = tmp['source']+ target_project_storage = tmp['target']+ storage_requests = Storages::Peripherals::StorageRequests.new(storage: source_project_storage.storage)- if source_project_storage.project_folder_mode == 'automatic'- files_info_query_result = files_info_query(storage_requests:,- file_ids: source_file_links.map(&:origin_id))- folder_files_file_ids_deep_query_result = folder_files_file_ids_deep_query(- storage_requests:,- path: target_project_storage.project_folder_path- )- source_file_links.each do |old_file_link|- attributes = {- storage_id: old_file_link.storage_id,- creator_id: User.current.id,- container_id: state.work_package_id_lookup[old_file_link.container_id.to_s],- container_type: 'WorkPackage',- origin_name: old_file_link.origin_name,- origin_mime_type: old_file_link.origin_mime_type- }+ if source_project_storage.project_folder_mode == 'automatic'+ files_info_query_result = files_info_query(storage_requests:,+ file_ids: source_file_links.map(&:origin_id))+ folder_files_file_ids_deep_query_result = folder_files_file_ids_deep_query(+ storage_requests:,+ path: target_project_storage.project_folder_path+ )+ source_file_links.each do |old_file_link|+ attributes = {+ storage_id: old_file_link.storage_id,+ creator_id: User.current.id,+ container_id: state.work_package_id_lookup[old_file_link.container_id.to_s],+ container_type: 'WorkPackage',+ origin_name: old_file_link.origin_name,+ origin_mime_type: old_file_link.origin_mime_type+ }- original_file_location = files_info_query_result- .find { |i| i.id.to_i == old_file_link.origin_id.to_i }- .location+ original_file_location = files_info_query_result+ .find { |i| i.id.to_i == old_file_link.origin_id.to_i }+ .location- attributes['origin_id'] =- if source_project_storage.file_inside_project_folder?(original_file_location)- new_file_location = original_file_location.gsub(- source_project_storage.project_folder_path_escaped,- target_project_storage.project_folder_path_escaped- )- new_file_location = CGI.unescape(new_file_location[1..])- folder_files_file_ids_deep_query_result[new_file_location]['fileid']- else- old_file_link.origin_id- end- Storages::FileLinks::CreateService.new(user: User.current).call(attributes)- end- else- source_file_links.each do |old_file_link|- attributes = {- storage_id: old_file_link.storage_id,- creator_id: User.current.id,- container_id: state.work_package_id_lookup[old_file_link.container_id.to_s],- container_type: 'WorkPackage',- origin_name: old_file_link.origin_name,- origin_mime_type: old_file_link.origin_mime_type,- origin_id: old_file_link.origin_id- }- Storages::FileLinks::CreateService.new(user: User.current).call(attributes)- end+ attributes['origin_id'] =+ if source_project_storage.file_inside_project_folder?(original_file_location)+ new_file_location = original_file_location.gsub(+ source_project_storage.project_folder_path_escaped,+ target_project_storage.project_folder_path_escaped+ )+ new_file_location = CGI.unescape(new_file_location[1..])+ folder_files_file_ids_deep_query_result[new_file_location]['fileid']+ else+ old_file_link.origin_id+ end+ Storages::FileLinks::CreateService.new(user: User.current).call(attributes)+ end+ else+ source_file_links.each do |old_file_link|+ attributes = {+ storage_id: old_file_link.storage_id,+ creator_id: User.current.id,+ container_id: state.work_package_id_lookup[old_file_link.container_id.to_s],+ container_type: 'WorkPackage',+ origin_name: old_file_link.origin_name,+ origin_mime_type: old_file_link.origin_mime_type,+ origin_id: old_file_link.origin_id+ }+ Storages::FileLinks::CreateService.new(user: User.current).call(attributes)endendendend+ # rubocop:enable Metrics/AbcSize+ # rubocop:enable Metrics/PerceivedComplexitydef files_info_query(storage_requests:, file_ids:)storage_requests.files_info_query.call(user: User.current, file_ids:)- .on_failure { |result| raise "files_info_query failed: #{result}" }+ .on_failure { |r| add_error!("files_info_query", r.to_active_model_errors) }.resultend@@ -119,7 +122,7 @@ module Projects::Copystorage_requests.folder_files_file_ids_deep_query.call(path:)- .on_failure { |result| raise "folder_files_file_ids_deep_query failed: #{result}" }+ .on_failure { |r| add_error!("folder_files_file_ids_deep_query", r.to_active_model_errors) }.resultendenddiff --git a/modules/storages/app/services/projects/copy/storages_dependent_service.rb b/modules/storages/app/services/projects/copy/storages_dependent_service.rbindex 0f89e6d56a1..0123eef042c 100644--- a/modules/storages/app/services/projects/copy/storages_dependent_service.rb+++ b/modules/storages/app/services/projects/copy/storages_dependent_service.rb@@ -40,6 +40,7 @@ module Projects::Copyprotected+ # rubocop:disable Metrics/AbcSizedef copy_dependency(*)state.copied_project_storages = source.projects_storages.each_with_object([]) do |source_project_storage, array|project_storage_copy =@@ -48,10 +49,12 @@ module Projects::Copy.call(storage_id: source_project_storage.storage_id,project_id: target.id,project_folder_mode: 'inactive')- .on_failure { |result| raise "ProjectStorage creation failed: #{result}" }.result+ .on_failure { |r| add_error!(source_project_storage.class.to_s, r.to_active_model_errors) }+ .resultarray << { source: source_project_storage, target: project_storage_copy }endend+ # rubocop:enable Metrics/AbcSizeendenddiff --git a/spec/features/projects/copy_spec.rb b/spec/features/projects/copy_spec.rbindex 6d024c1b3cf..8e9e99ebefc 100644--- a/spec/features/projects/copy_spec.rb+++ b/spec/features/projects/copy_spec.rb@@ -29,9 +29,7 @@require 'spec_helper'# rubocop:disable RSpec:MultipleMemoizedHelpers-RSpec.describe 'Projects copy',- js: true,- with_cuprite: true do+RSpec.describe 'Projects copy', :with_cuprite, js: true dodescribe 'with a full copy example' dolet!(:project) docreate(:project,@@ -66,7 +64,7 @@ RSpec.describe 'Projects copy',create(:text_wp_custom_field)endlet(:active_types) do- [create(:type), create(:type)]+ create_list(:type, 2)endlet!(:inactive_type) docreate(:type)@@ -77,7 +75,15 @@ RSpec.describe 'Projects copy',permissions:)endlet(:permissions) do- %i(copy_projects edit_project add_subprojects manage_types view_work_packages select_custom_fields work_package_assigned)+ %i(copy_projects+ edit_project+ add_subprojects+ manage_types+ view_work_packages+ select_custom_fields+ manage_storages_in_project+ manage_file_links+ work_package_assigned)endlet(:wp_user) douser = create(:user)@@ -162,8 +168,7 @@ RSpec.describe 'Projects copy',copied_project = Project.find_by(name: 'Copied project')- expect(copied_project)- .to be_present+ expect(copied_project).to be_present# Will redirect to the new project automatically once the copy process is doneexpect(page).to have_current_path(Regexp.new("#{project_path(copied_project)}/?"))@@ -209,42 +214,25 @@ RSpec.describe 'Projects copy',# Using the db directly due to performance and claritycopied_work_packages = copied_project.work_packages- expect(copied_work_packages.length)- .to eql 1+ expect(copied_work_packages.length).to eql 1copied_work_package = copied_work_packages[0]- expect(copied_work_package.subject)- .to eql work_package.subject- expect(copied_work_package.author)- .to eql user- expect(copied_work_package.assigned_to)- .to eql work_package.assigned_to- expect(copied_work_package.responsible)- .to eql work_package.responsible- expect(copied_work_package.status)- .to eql work_package.status- expect(copied_work_package.done_ratio)- .to eql work_package.done_ratio- expect(copied_work_package.description)- .to eql work_package.description- expect(copied_work_package.category)- .to eql copied_project.categories.find_by(name: category.name)- expect(copied_work_package.version)- .to eql copied_project.versions.find_by(name: version.name)- expect(copied_work_package.custom_value_attributes)- .to eql(wp_custom_field.id => 'Some wp cf text')- expect(copied_work_package.attachments.map(&:filename))- .to eq ['work_package_attachment.pdf']-- expect(ActionMailer::Base.deliveries.count)- .to eql(1)-- expect(ActionMailer::Base.deliveries.last.subject)- .to eql("Created project Copied project")-- expect(ActionMailer::Base.deliveries.last.to)- .to match_array([user.mail])+ expect(copied_work_package.subject).to eql work_package.subject+ expect(copied_work_package.author).to eql user+ expect(copied_work_package.assigned_to).to eql work_package.assigned_to+ expect(copied_work_package.responsible).to eql work_package.responsible+ expect(copied_work_package.status).to eql work_package.status+ expect(copied_work_package.done_ratio).to eql work_package.done_ratio+ expect(copied_work_package.description).to eql work_package.description+ expect(copied_work_package.category).to eql copied_project.categories.find_by(name: category.name)+ expect(copied_work_package.version).to eql copied_project.versions.find_by(name: version.name)+ expect(copied_work_package.custom_value_attributes).to eql(wp_custom_field.id => 'Some wp cf text')+ expect(copied_work_package.attachments.map(&:filename)).to eq ['work_package_attachment.pdf']++ expect(ActionMailer::Base.deliveries.count).to eql(1)+ expect(ActionMailer::Base.deliveries.last.subject).to eql("Created project Copied project")+ expect(ActionMailer::Base.deliveries.last.to).to match_array([user.mail])endenddiff --git a/spec/services/projects/copy_service_integration_spec.rb b/spec/services/projects/copy_service_integration_spec.rbindex d9502171ad8..6adb1ecc86b 100644--- a/spec/services/projects/copy_service_integration_spec.rb+++ b/spec/services/projects/copy_service_integration_spec.rb@@ -216,8 +216,10 @@ RSpec.describe(context 'with all modules selected' dolet(:only_args) { all_modules }+ # rubocop:disable RSpec/IndexedLetlet(:storage1) { source_automatic_project_storage.storage }let(:storage2) { source_manual_project_storage.storage }+ # rubocop:enable RSpec/IndexedLetlet(:host) { storage1.host }let!(:file_outside_project_folder_link) docreate(:file_link,@@ -433,7 +435,8 @@ RSpec.describe(expect(file_outside_project_folder_link_copy.storage_id).to eq(file_outside_project_folder_link.storage_id)expect(file_inside_automatic_project_folder_link_copy.id).not_to eq(file_inside_automatic_project_folder_link.id)expect(file_inside_automatic_project_folder_link_copy.origin_id).to eq("430")- expect(file_inside_automatic_project_folder_link_copy.storage_id).to eq(file_inside_automatic_project_folder_link.storage_id)+ expect(file_inside_automatic_project_folder_link_copy.storage_id)+ .to eq(file_inside_automatic_project_folder_link.storage_id)expect(file_inside_manual_project_folder_link_copy.id).not_to eq(file_inside_manual_project_folder_link.id)expect(file_inside_manual_project_folder_link_copy.origin_id).to eq("102")expect(file_inside_manual_project_folder_link_copy.storage_id).to eq(file_inside_manual_project_folder_link.storage_id)