From da60baf2a5fc5e2f7e84ea592b8ba6747a63abb3 Mon Sep 17 00:00:00 2001
From: James Lopez <james@jameslopez.es>
Date: Tue, 12 Apr 2016 10:36:18 +0200
Subject: [PATCH] fixing more importing issues

---
 app/models/merge_request_diff.rb                             | 4 +++-
 app/services/projects/import_export/project_tree_restorer.rb | 3 ++-
 app/services/projects/import_export/relation_factory.rb      | 5 +++--
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/app/models/merge_request_diff.rb b/app/models/merge_request_diff.rb
index 33884118595..03daac2822c 100644
--- a/app/models/merge_request_diff.rb
+++ b/app/models/merge_request_diff.rb
@@ -37,7 +37,9 @@ class MergeRequestDiff < ActiveRecord::Base
   serialize :st_commits
   serialize :st_diffs
 
-  after_create :reload_content
+  after_create :reload_content, unless: :importing
+
+  attr_accessor :importing
 
   def reload_content
     reload_commits
diff --git a/app/services/projects/import_export/project_tree_restorer.rb b/app/services/projects/import_export/project_tree_restorer.rb
index 9512fcb2a1a..c498d7bd9bc 100644
--- a/app/services/projects/import_export/project_tree_restorer.rb
+++ b/app/services/projects/import_export/project_tree_restorer.rb
@@ -71,9 +71,10 @@ module Projects
         # TODO refactor this
         relation_key = relation.keys.first
         tree_hash[relation_key.to_s].each do |relation_item|
-          relation.values.each do |sub_relation|
+          relation.values.flatten.each do |sub_relation|
             relation_hash = relation_item[sub_relation.to_s]
             next if relation_hash.blank?
+            relation_hash.merge!('project_id' => project.id) if sub_relation == :merge_requests
             sub_relation_object = Projects::ImportExport::RelationFactory.create(
               relation_sym: sub_relation, relation_hash: relation_hash, members_map: members_map)
             relation_item[sub_relation.to_s] = sub_relation_object
diff --git a/app/services/projects/import_export/relation_factory.rb b/app/services/projects/import_export/relation_factory.rb
index 6ccaa409ce9..4f1c753ac69 100644
--- a/app/services/projects/import_export/relation_factory.rb
+++ b/app/services/projects/import_export/relation_factory.rb
@@ -7,12 +7,14 @@ module Projects
       USER_REFERENCES = %w(author_id assignee_id updated_by_id).freeze
 
       def create(relation_sym:, relation_hash:, members_map:)
+        #TODO refactor this
         relation_sym = parse_relation_sym(relation_sym)
         klass = relation_class(relation_sym)
         relation_hash.delete('id') #screw IDs for now
         handle_merge_requests(relation_hash) if relation_sym == :merge_requests
         update_user_references(relation_hash, members_map)
-        klass.new(relation_hash)
+        imported_object = klass.new(relation_hash)
+        imported_object.importing = true if imported_object.respond_to?(:importing)
       end
 
       private
@@ -20,7 +22,6 @@ module Projects
       def handle_merge_requests(relation_hash)
         relation_hash['target_project_id'] = relation_hash.delete('project_id')
         relation_hash['source_project_id'] = -1
-        relation_hash['importing'] = true
       end
 
       #TODO nice to have, optimize this to only get called for specific models
-- 
GitLab