From 06b36c00d55df38cd2aaa4d5251185485c8abe5c Mon Sep 17 00:00:00 2001
From: James Lopez <james@jameslopez.es>
Date: Fri, 4 Mar 2016 12:21:53 +0100
Subject: [PATCH] some refactoring in the migration. Also fixed github import
 issue and updated spec

---
 ...52808_remove_wrong_import_url_from_projects.rb | 15 ++++++++++-----
 db/schema.rb                                      |  4 +++-
 lib/gitlab/github_import/project_creator.rb       | 12 ++++++++++--
 .../gitlab/github_import/project_creator_spec.rb  |  3 ++-
 4 files changed, 25 insertions(+), 9 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 dda7648fb8..dfa9f2d4de 100644
--- a/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb
+++ b/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb
@@ -23,11 +23,16 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
   end
 
   def up
-    projects_with_wrong_import_url.each do |project|
-      sanitizer = ImportUrlSanitizer.new(project.import_urls)
-      project.update_columns(import_url: sanitizer.sanitized_url)
-      if project.import_data
-        project.import_data.update_columns(credentials: sanitizer.credentials)
+    projects_with_wrong_import_url.each do |project_id|
+      project = Project.find(project_id["id"])
+      sanitizer = ImportUrlSanitizer.new(project.import_url)
+
+      ActiveRecord::Base.transaction do
+        project.update_columns(import_url: sanitizer.sanitized_url)
+        if project.import_data
+          project.import_data.credentials = sanitizer.credentials
+          project.save!
+        end
       end
     end
   end
diff --git a/db/schema.rb b/db/schema.rb
index 53a941d30d..05c19fc7a2 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20160222153918) do
+ActiveRecord::Schema.define(version: 20160302152808) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -658,6 +658,8 @@ ActiveRecord::Schema.define(version: 20160222153918) do
   create_table "project_import_data", force: :cascade do |t|
     t.integer "project_id"
     t.text    "data"
+    t.text    "encrypted_credentials"
+    t.text    "encrypted_credentials_iv"
   end
 
   create_table "projects", force: :cascade do |t|
diff --git a/lib/gitlab/github_import/project_creator.rb b/lib/gitlab/github_import/project_creator.rb
index 474927069a..d6cab3c2d2 100644
--- a/lib/gitlab/github_import/project_creator.rb
+++ b/lib/gitlab/github_import/project_creator.rb
@@ -20,13 +20,21 @@ module Gitlab
           visibility_level: repo.private ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::VisibilityLevel::PUBLIC,
           import_type: "github",
           import_source: repo.full_name,
-          import_url: repo.clone_url.sub("https://", "https://#{@session_data[:github_access_token]}@"),
+          import_url: repo.clone_url,
           wiki_enabled: !repo.has_wiki? # If repo has wiki we'll import it later
         ).execute
 
-        project.create_import_data(data: { "github_session" => session_data } )
+        create_import_data(project)
         project
       end
+
+      private
+
+      def create_import_data(project)
+        project.create_import_data(
+          credentials: session_data.delete(:github_access_token),
+          data: { "github_session" => session_data })
+      end
     end
   end
 end
diff --git a/spec/lib/gitlab/github_import/project_creator_spec.rb b/spec/lib/gitlab/github_import/project_creator_spec.rb
index c93a3ebdae..36abe87f52 100644
--- a/spec/lib/gitlab/github_import/project_creator_spec.rb
+++ b/spec/lib/gitlab/github_import/project_creator_spec.rb
@@ -26,7 +26,8 @@ describe Gitlab::GithubImport::ProjectCreator, lib: true do
     project_creator = Gitlab::GithubImport::ProjectCreator.new(repo, namespace, user, access_params)
     project = project_creator.execute
 
-    expect(project.import_url).to eq("https://asdffg@gitlab.com/asd/vim.git")
+    expect(project.import_url).to eq("https://gitlab.com/asd/vim.git")
+    expect(project.import_data.credentials).to eq("asdffg")
     expect(project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE)
   end
 end
-- 
2.30.9