From e14dd6f6f49d70f29adbfdb5b82d6d0e66b96af0 Mon Sep 17 00:00:00 2001 From: Robert Beckett Date: Fri, 6 Apr 2018 16:53:56 +0000 Subject: [PATCH] models/issue: copy subtask issue relationships While copying subtasks, copy any interdependencies between the subtasks. Signed-off-by: Robert Beckett --- app/models/issue.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/issue.rb b/app/models/issue.rb index 8583d43..64c9c4f 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -1619,12 +1619,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? logger.error "Subtask ##{child.id} was not copied during ##{@copied_from.id} copy because it is not visible to the current user" if logger @@ -1636,12 +1636,27 @@ 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 copy.save logger.error "Could not copy subtask ##{child.id} while copying ##{@copied_from.id} to ##{id} due to validation errors: #{copy.errors.full_messages.join(', ')}" if logger 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 -- 2.7.4