From 17512e7efdb2ef03cae94a2697a72d79205a9b2b Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Sun, 13 Dec 2009 03:37:21 +0000 Subject: [PATCH] Copy issue relations when copying a project. (#3367) git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3164 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/project.rb | 31 +++++++++++++++++++++++++++++++ test/unit/project_test.rb | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index e6ec19ef..d6526100 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -529,6 +529,10 @@ class Project < ActiveRecord::Base # Copies issues from +project+ def copy_issues(project) + # Stores the source issue id as a key and the copied issues as the + # value. Used to map the two togeather for issue relations. + issues_map = {} + project.issues.each do |issue| new_issue = Issue.new new_issue.copy_from(issue) @@ -543,6 +547,33 @@ class Project < ActiveRecord::Base new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first end self.issues << new_issue + issues_map[issue.id] = new_issue + end + + # Relations after in case issues related each other + project.issues.each do |issue| + new_issue = issues_map[issue.id] + + # Relations + issue.relations_from.each do |source_relation| + new_issue_relation = IssueRelation.new + new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") + new_issue_relation.issue_to = issues_map[source_relation.issue_to_id] + if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations? + new_issue_relation.issue_to = source_relation.issue_to + end + new_issue.relations_from << new_issue_relation + end + + issue.relations_to.each do |source_relation| + new_issue_relation = IssueRelation.new + new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") + new_issue_relation.issue_from = issues_map[source_relation.issue_from_id] + if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations? + new_issue_relation.issue_from = source_relation.issue_from + end + new_issue.relations_to << new_issue_relation + end end end diff --git a/test/unit/project_test.rb b/test/unit/project_test.rb index 748065ae..40d5b823 100644 --- a/test/unit/project_test.rb +++ b/test/unit/project_test.rb @@ -587,6 +587,41 @@ class ProjectTest < ActiveSupport::TestCase assert_not_equal assigned_version.id, copied_issue.fixed_version.id # Different record end + should "copy issue relations" do + Setting.cross_project_issue_relations = '1' + + second_issue = Issue.generate!(:status_id => 5, + :subject => "copy issue relation", + :tracker_id => 1, + :assigned_to_id => 2, + :project_id => @source_project.id) + source_relation = IssueRelation.generate!(:issue_from => Issue.find(4), + :issue_to => second_issue, + :relation_type => "relates") + source_relation_cross_project = IssueRelation.generate!(:issue_from => Issue.find(1), + :issue_to => second_issue, + :relation_type => "duplicates") + + assert @project.copy(@source_project) + assert_equal @source_project.issues.count, @project.issues.count + copied_issue = @project.issues.find_by_subject("Issue on project 2") # Was #4 + copied_second_issue = @project.issues.find_by_subject("copy issue relation") + + # First issue with a relation on project + assert_equal 1, copied_issue.relations.size, "Relation not copied" + copied_relation = copied_issue.relations.first + assert_equal "relates", copied_relation.relation_type + assert_equal copied_second_issue.id, copied_relation.issue_to_id + assert_not_equal source_relation.id, copied_relation.id + + # Second issue with a cross project relation + assert_equal 2, copied_second_issue.relations.size, "Relation not copied" + copied_relation = copied_second_issue.relations.select {|r| r.relation_type == 'duplicates'}.first + assert_equal "duplicates", copied_relation.relation_type + assert_equal 1, copied_relation.issue_from_id, "Cross project relation not kept" + assert_not_equal source_relation_cross_project.id, copied_relation.id + end + should "copy members" do assert @project.valid? assert @project.members.empty? @@ -692,9 +727,6 @@ class ProjectTest < ActiveSupport::TestCase assert @project.issues.empty? end - should "copy issue relations" - should "link issue relations if cross project issue relations are valid" - end end