Skip to content

Commit

Permalink
Skip the job if no File link or project folder copy is requested
Browse files Browse the repository at this point in the history
  • Loading branch information
mereghost committed Mar 25, 2024
1 parent 2defda1 commit 70d39d8
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 8 deletions.
12 changes: 12 additions & 0 deletions app/services/projects/copy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ def self.copy_dependencies
]
end

# Project Folders and File Links aren't dependent services anymore,
# so we need to amend the services for the form Representer
def self.copyable_dependencies
super + [{ identifier: "storage_project_folders",
name_source: -> { I18n.t(:label_project_storage_project_folder) },
count_source: ->(source, _) { source.storages.count } },

{ identifier: "file_links",
name_source: -> { I18n.t("projects.copy.work_package_file_links") },
count_source: ->(source, _) { source.work_packages.joins(:file_links).count("file_links.id") } }]
end

protected

##
Expand Down
13 changes: 8 additions & 5 deletions app/workers/copy_project_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,20 @@ def copy_project(target_project_params, associations_to_copy, send_notifications
copy_service = ::Projects::CopyService.new(source: source_project, user:)
result = copy_service.call({ target_project_params:, send_notifications:, only: Array(associations_to_copy) })

enqueue_copy_project_folder_jobs(copy_service.state.copied_project_storages, copy_service.state.work_package_id_lookup)
enqueue_copy_project_folder_jobs(copy_service.state.copied_project_storages,
copy_service.state.work_package_id_lookup,
associations_to_copy)

result
end

def enqueue_copy_project_folder_jobs(copied_storages, work_packages_map)
def enqueue_copy_project_folder_jobs(copied_storages, work_packages_map, only)
return unless only.intersect?("file_links", "storage_project_folders").any?

Array(copied_storages).each do |storage_pair|
batch.enqueue do
Storages::CopyProjectFoldersJob.perform_later(source: storage_pair[:source],
target: storage_pair[:target],
work_packages_map:)
Storages::CopyProjectFoldersJob
.perform_later(source: storage_pair[:source], target: storage_pair[:target], work_packages_map:)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 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 Storages
module FileLinks
class CopyContract < CreateContract
private

def validate_user_allowed_to_manage = true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 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 Storages
module ProjectStorages
class CopyContract < CreateContract
private

def validate_user_allowed_to_manage = true
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def copy_dependency(*)
state.copied_project_storages = source.project_storages.each_with_object([]) do |source_project_storage, array|
project_storage_copy =
::Storages::ProjectStorages::CreateService
.new(user: User.current)
.new(user: User.current, contract_class: ::Storages::ProjectStorages::CopyContract)
.call(storage_id: source_project_storage.storage_id,
project_id: target.id,
project_folder_mode: "inactive")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def create_managed_file_links(source_file_links)
"origin_id" => location_map[source_link.origin_id]
)

CreateService.new(user: @user).call(attributes).on_failure { |failed| log_errors(failed) }
CreateService.new(user: @user, contract_class: CopyContract)
.call(attributes).on_failure { |failed| log_errors(failed) }
end
end

Expand Down Expand Up @@ -108,7 +109,8 @@ def create_unmanaged_file_links(source_file_links)
attributes["creator_id"] = @user.id
attributes["container_id"] = @work_packages_map[source_file_link.container_id]

FileLinks::CreateService.new(user: @user).call(attributes).on_failure { |failed| log_errors(failed) }
FileLinks::CreateService.new(user: @user, contract_class: CopyContract)
.call(attributes).on_failure { |failed| log_errors(failed) }
end
end

Expand Down

0 comments on commit 70d39d8

Please sign in to comment.