From 6967871fc567cbadd63f26f1ef87c4008cc6387b Mon Sep 17 00:00:00 2001
From: James Lopez <james@jameslopez.es>
Date: Wed, 23 Mar 2016 12:52:50 +0100
Subject: [PATCH] fogbugz importer, also refactored migration again to make it
 easier to add new importers

---
 ...8_remove_wrong_import_url_from_projects.rb | 31 ++++++++++++-------
 lib/gitlab/fogbugz_import/importer.rb         | 13 ++++++--
 lib/gitlab/fogbugz_import/project_creator.rb  |  2 +-
 3 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb b/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb
index 3d97a66c0ff..f3a4fc26be9 100644
--- a/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb
+++ b/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb
@@ -12,11 +12,14 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
   def up
     say("Encrypting and migrating project import credentials...")
 
-    say("Projects and Github projects with a wrong URL")
+    say("Projects and Github projects with a wrong URL. Also, migrating Gitlab projects credentials.")
     in_transaction { process_projects_with_wrong_url }
 
     say("Migrating bitbucket credentials...")
-    in_transaction { process_bitbucket_projects }
+    in_transaction { process_project(import_type: 'bitbucket') }
+
+    say("Migrating fogbugz credentials...")
+    in_transaction { process_project(import_type: 'fogbugz') }
   end
 
   def process_projects_with_wrong_url
@@ -28,12 +31,16 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
     end
   end
 
-  def process_bitbucket_projects
-    bitbucket_projects_with_wrong_import_url.each do |bitbucket_data|
-      data_hash = YAML::load(bitbucket_data['data']) if bitbucket_data['data']
-      if defined?(data_hash) && data_hash && data_hash['bb_session']
-        update_import_data_for_bitbucket(data_hash, bitbucket_data['id'])
-      end
+  def process_project(import_type: )
+    unencrypted_import_data(import_type: import_type).each do |data|
+      replace_data_credentials(data)
+    end
+  end
+
+  def replace_data_credentials(data)
+    data_hash = YAML::load(data['data']) if data['data']
+    if defined?(data_hash) && data_hash
+      update_with_encrypted_data(data_hash, data['id'])
     end
   end
 
@@ -56,7 +63,7 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
     end
   end
 
-  def update_import_data_for_bitbucket(data_hash, import_data_id)
+  def update_with_encrypted_data(data_hash, import_data_id)
     fake_import_data = FakeProjectImportData.new
     fake_import_data.credentials = data_hash
     execute(update_import_data_sql(import_data_id, fake_import_data))
@@ -79,9 +86,9 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
     select_all("SELECT p.id, p.import_url FROM projects p WHERE p.import_url IS NOT NULL AND (p.import_url LIKE '%//%:%@%' OR p.import_url LIKE 'https___#{"_"*40}@github.com%')")
   end
 
-  # All bitbucket imports
-  def bitbucket_projects_with_wrong_import_url
-    select_all("SELECT i.id, p.import_url, i.data FROM projects p INNER JOIN project_import_data i ON p.id = i.project_id WHERE p.import_url IS NOT NULL AND p.import_type = 'bitbucket' ")
+  # All imports with data for import_type
+  def unencrypted_import_data(import_type: )
+    select_all("SELECT i.id, p.import_url, i.data FROM projects p INNER JOIN project_import_data i ON p.id = i.project_id WHERE p.import_url IS NOT NULL AND p.import_type = '#{import_type}' ")
   end
 
   def project_import_data(project_id)
diff --git a/lib/gitlab/fogbugz_import/importer.rb b/lib/gitlab/fogbugz_import/importer.rb
index db580b5e578..c33b3541dd8 100644
--- a/lib/gitlab/fogbugz_import/importer.rb
+++ b/lib/gitlab/fogbugz_import/importer.rb
@@ -8,9 +8,12 @@ module Gitlab
 
         import_data = project.import_data.try(:data)
         repo_data = import_data['repo'] if import_data
-        @repo = FogbugzImport::Repository.new(repo_data)
-
-        @known_labels = Set.new
+        if import_data_credentials && import_data_credentials['repo']
+          @repo = FogbugzImport::Repository.new(repo_data)
+          @known_labels = Set.new
+        else
+          raise Projects::ImportService::Error, "Unable to find project import data credentials for project ID: #{@project.id}"
+        end
       end
 
       def execute
@@ -30,6 +33,10 @@ module Gitlab
 
       private
 
+      def import_data_credentials
+        @import_data_credentials ||= project.import_data.credentials if project.import_data
+      end
+
       def user_map
         @user_map ||= begin
           user_map = Hash.new
diff --git a/lib/gitlab/fogbugz_import/project_creator.rb b/lib/gitlab/fogbugz_import/project_creator.rb
index e0163499e30..73d6272720a 100644
--- a/lib/gitlab/fogbugz_import/project_creator.rb
+++ b/lib/gitlab/fogbugz_import/project_creator.rb
@@ -25,7 +25,7 @@ module Gitlab
         ).execute
 
         project.create_import_data(
-          data: {
+          credentials: {
             'repo' => repo.raw_data,
             'user_map' => user_map,
             'fb_session' => fb_session
-- 
GitLab