Merge pull request #3909 from opf/22126-fix-copy-project

[22126] Fix copy project
pull/3915/head
Oliver Günther 9 years ago
commit 6289c8b6f1
  1. 3
      app/controllers/copy_projects_controller.rb
  2. 4
      app/models/permitted_params.rb
  3. 39
      app/workers/copy_project_job.rb
  4. 2
      config/application.rb
  5. 3
      features/projects/copy_project.feature

@ -41,9 +41,10 @@ class CopyProjectsController < ApplicationController
@copy_project.attributes = permitted_params.project @copy_project.attributes = permitted_params.project
if @copy_project.valid? if @copy_project.valid?
modules = permitted_params.project[:enabled_module_names] || params[:enabled_modules] modules = permitted_params.project[:enabled_module_names] || params[:enabled_modules]
copy_project_job = CopyProjectJob.new(user_id: User.current.id, copy_project_job = CopyProjectJob.new(user_id: User.current.id,
source_project_id: @project.id, source_project_id: @project.id,
target_project_params: permitted_params.project, target_project_params: permitted_params.project.to_hash,
enabled_modules: modules, enabled_modules: modules,
associations_to_copy: params[:only], associations_to_copy: params[:only],
send_mails: params[:notifications] == '1') send_mails: params[:notifications] == '1')

@ -295,6 +295,10 @@ class PermittedParams
whitelist.permit(enabled_module_names: []) whitelist.permit(enabled_module_names: [])
end end
if instance && current_user.allowed_to?(:add_subprojects, instance)
whitelist.permit(:parent_id)
end
unless params[:project][:custom_field_values].nil? unless params[:project][:custom_field_values].nil?
whitelist[:custom_field_values] = params[:project][:custom_field_values] whitelist[:custom_field_values] = params[:project][:custom_field_values]
end end

@ -40,12 +40,12 @@ class CopyProjectJob
def initialize(user_id:, source_project_id:, target_project_params:, def initialize(user_id:, source_project_id:, target_project_params:,
enabled_modules:, associations_to_copy:, send_mails: false) enabled_modules:, associations_to_copy:, send_mails: false)
@user_id = user_id @user_id = user_id
@source_project_id = source_project_id @source_project_id = source_project_id
@target_project_params = target_project_params @target_project_params = target_project_params
@enabled_modules = enabled_modules @enabled_modules = enabled_modules
@associations_to_copy = associations_to_copy @associations_to_copy = associations_to_copy
@send_mails = send_mails @send_mails = send_mails
end end
def perform def perform
@ -84,13 +84,13 @@ class CopyProjectJob
associations_to_copy, associations_to_copy,
send_mails) send_mails)
target_project = nil target_project = nil
errors = [] errors = []
UserMailer.with_deliveries(send_mails) do UserMailer.with_deliveries(send_mails) do
parent_id = target_project_params[:parent_id] parent_id = target_project_params[:parent_id]
target_project = Project.new.tap do |p| target_project = Project.new.tap do |project|
p.attributes = target_project_params project.attributes = target_project_params
p.enabled_module_names = enabled_modules project.enabled_module_names = enabled_modules
end end
if validate_parent_id(target_project, parent_id) && target_project.save if validate_parent_id(target_project, parent_id) && target_project.save
@ -110,12 +110,21 @@ class CopyProjectJob
end end
end end
else else
errors = target_project.errors.full_messages errors = target_project.errors.full_messages
target_project = nil target_project = nil
end end
end end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound => e
logger.error("Entity missing: #{e.message} #{e.backtrace.join("\n")}")
rescue StandardError => e
logger.error('Encountered an error when trying to copy project '\
"'#{source_project_id}' : #{e.message} #{e.backtrace.join("\n")}")
ensure ensure
unless errors.empty?
logger.info('Encountered an errors while trying to copy related objects for '\
"project '#{source_project_id}': #{errors.inspect}")
end
return target_project, errors return target_project, errors
end end
@ -123,13 +132,21 @@ class CopyProjectJob
# TODO: move it to Project model in a validation that depends on User.current # TODO: move it to Project model in a validation that depends on User.current
def validate_parent_id(project, parent_id) def validate_parent_id(project, parent_id)
return true if User.current.admin? return true if User.current.admin?
if parent_id || project.new_record? if parent_id || project.new_record?
parent = parent_id.blank? ? nil : Project.find_by(id: parent_id.to_i) parent = parent_id.blank? ? nil : Project.find_by(id: parent_id.to_i)
unless project.allowed_parents.include?(parent) unless project.allowed_parents.include?(parent)
project.errors.add :parent_id, :invalid project.errors.add :parent_id, :invalid
return false return false
end end
end end
true true
end end
def logger
Delayed::Worker.logger
end
end end

@ -168,5 +168,7 @@ module OpenProject
config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')] config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]
OpenProject::Configuration.configure_cache(config) OpenProject::Configuration.configure_cache(config)
config.active_job.queue_adapter = :delayed_job
end end
end end

@ -175,9 +175,8 @@ Feature: Project Settings
And I go to the page of the planning element "pe2" of the project called "Copied Project" And I go to the page of the planning element "pe2" of the project called "Copied Project"
Then I should see "pe2" within "#content" Then I should see "pe2" within "#content"
@javascript @wip @javascript
Scenario: Copying a project with a complex issue Scenario: Copying a project with a complex issue
# FIXME 16364 assignee is not shown on work package views (full and split screen)
Given the project "project1" has 1 version with: Given the project "project1" has 1 version with:
| name | version1 | | name | version1 |
| description | yeah, boy | | description | yeah, boy |

Loading…
Cancel
Save