]> source.dussan.org Git - redmine.git/commitdiff
Copy issue relations when copying a project. (#3367)
authorEric Davis <edavis@littlestreamsoftware.com>
Sun, 13 Dec 2009 03:37:21 +0000 (03:37 +0000)
committerEric Davis <edavis@littlestreamsoftware.com>
Sun, 13 Dec 2009 03:37:21 +0000 (03:37 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3164 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/project.rb
test/unit/project_test.rb

index e6ec19efb7e7d7782b82a537e499831e8908b782..d6526100f6ca3f95fb1a609e3b6e2a938e6a3d23 100644 (file)
@@ -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
 
index 748065ae78715ee4ea6bcc0fbf482f67e7e21fe7..40d5b823b3aac746861638f38566d87c90d63b82 100644 (file)
@@ -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