Project

General

Profile

Defect #17068 » 0001-models-issue-copy-subtask-issue-relationships.patch

models/issue: copy subtask issue relationships - Robert Beckett, 2018-04-06 19:33

View differences:

app/models/issue.rb
1619 1619

  
1620 1620
    unless @copied_from.leaf? || @copy_options[:subtasks] == false
1621 1621
      copy_options = (@copy_options || {}).merge(:subtasks => false)
1622
      copied_issue_ids = {@copied_from.id => self.id}
1622
      copied_issues = {@copied_from.id => self}
1623 1623
      @copied_from.reload.descendants.reorder("#{Issue.table_name}.lft").each do |child|
1624 1624
        # Do not copy self when copying an issue as a descendant of the copied issue
1625 1625
        next if child == self
1626 1626
        # Do not copy subtasks of issues that were not copied
1627
        next unless copied_issue_ids[child.parent_id]
1627
        next unless copied_issues[child.parent_id]
1628 1628
        # Do not copy subtasks that are not visible to avoid potential disclosure of private data
1629 1629
        unless child.visible?
1630 1630
          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 1636
        end
1637 1637
        copy.author = author
1638 1638
        copy.project = project
1639
        copy.parent_issue_id = copied_issue_ids[child.parent_id]
1639
        copy.parent_issue_id = copied_issues[child.parent_id].id
1640 1640
        unless copy.save
1641 1641
          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
1642 1642
          next
1643 1643
        end
1644
        copied_issue_ids[child.id] = copy.id
1644
        copied_issues[child.id] = copy
1645
      end
1646

  
1647
      # Replicate dependencies between copied children
1648
      @copied_from.reload.descendants.reorder("#{Issue.table_name}.lft").each do |child|
1649
        copy = copied_issues[child.id]
1650
        next unless copy
1651
        child.relations_to.each do |rel|
1652
          next unless copied_issues.key?(rel.issue_from.id)
1653
          from = Issue.find_by_id(copied_issues[rel.issue_from.id].id)
1654
          next unless from
1655
          relation = IssueRelation.new(:issue_from => from, :issue_to => copy, :relation_type => rel.relation_type)
1656
          unless relation.save
1657
            logger.error "Could not create relation while copying ##{child.id} to ##{copy.id} due to validation errors: #{relation.errors.full_messages.join(', ')}" if logger
1658
          end
1659
        end
1645 1660
      end
1646 1661
    end
1647 1662
    @after_create_from_copy_handled = true
(1-1/2)