diff --git a/app/models/issue.rb b/app/models/issue.rb --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -1750,12 +1750,12 @@ class Issue < ActiveRecord::Base unless @copied_from.leaf? || @copy_options[:subtasks] == false copy_options = (@copy_options || {}).merge(:subtasks => false) - copied_issue_ids = {@copied_from.id => self.id} + copied_issues = {@copied_from.id => self} @copied_from.reload.descendants.reorder("#{Issue.table_name}.lft").each do |child| # Do not copy self when copying an issue as a descendant of the copied issue next if child == self # Do not copy subtasks of issues that were not copied - next unless copied_issue_ids[child.parent_id] + next unless copied_issues[child.parent_id] # Do not copy subtasks that are not visible to avoid potential disclosure of private data unless child.visible? if logger diff --git a/app/models/issue.rb b/app/models/issue.rb --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -1773,25 +1773,40 @@ class Issue < ActiveRecord::Base end copy.author = author copy.project = project - copy.parent_issue_id = copied_issue_ids[child.parent_id] + copy.parent_issue_id = copied_issues[child.parent_id].id unless child.fixed_version.present? && child.fixed_version.status == 'open' copy.fixed_version_id = nil end unless child.assigned_to_id.present? && child.assigned_to.status == User::STATUS_ACTIVE copy.assigned_to = nil end unless copy.save if logger logger.error( "Could not copy subtask ##{child.id} " \ "while copying ##{@copied_from.id} to ##{id} due to validation errors: " \ "#{copy.errors.full_messages.join(', ')}" ) end next end - copied_issue_ids[child.id] = copy.id + copied_issues[child.id] = copy + end + + # Replicate dependencies between copied children + @copied_from.reload.descendants.reorder("#{Issue.table_name}.lft").each do |child| + copy = copied_issues[child.id] + next unless copy + child.relations_to.each do |rel| + next unless copied_issues.key?(rel.issue_from.id) + from = Issue.find_by_id(copied_issues[rel.issue_from.id].id) + next unless from + relation = IssueRelation.new(:issue_from => from, :issue_to => copy, :relation_type => rel.relation_type) + unless relation.save + logger.error "Could not create relation while copying ##{child.id} to ##{copy.id} due to validation errors: #{relation.errors.full_messages.join(', ')}" if logger + end + end end end @after_create_from_copy_handled = true