From 1d4c3fa1507fdfdecdc15091ac8ef8d9f5fb63d9 Mon Sep 17 00:00:00 2001
From: James Lopez <james@jameslopez.es>
Date: Tue, 26 Apr 2016 13:01:51 +0200
Subject: [PATCH] more refactoring, now using custom job for processing project
 imports

---
 .../import/gitlab_projects_controller.rb      |  4 ++--
 app/models/project.rb                         | 10 +++++++++
 .../import/gitlab_projects/new.html.haml      |  2 +-
 app/workers/project_import_worker.rb          | 22 +++++++++++++++++++
 config/routes.rb                              |  1 +
 5 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100644 app/workers/project_import_worker.rb

diff --git a/app/controllers/import/gitlab_projects_controller.rb b/app/controllers/import/gitlab_projects_controller.rb
index c99c97893ad..0e94915765c 100644
--- a/app/controllers/import/gitlab_projects_controller.rb
+++ b/app/controllers/import/gitlab_projects_controller.rb
@@ -26,7 +26,7 @@ class Import::GitlabProjectsController < Import::BaseController
   end
 
   def create
-    @file = params[:file]
+    file = params[:file]
    # @project_name =
 
     repo_owner = current_user.username
@@ -34,7 +34,7 @@ class Import::GitlabProjectsController < Import::BaseController
 
     namespace = get_or_create_namespace || (render and return)
 
-    @project = Gitlab::ImportExport::ImportService.execute(archive_file: file, owner: repo_owner)
+    @project = Project.create_from_import_job(current_user.id, File.expand_path(file.path))
   end
 
   private
diff --git a/app/models/project.rb b/app/models/project.rb
index 0420c6a61ae..18e647045cd 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -359,6 +359,16 @@ class Project < ActiveRecord::Base
     def visible_to_user(user)
       where(id: user.authorized_projects.select(:id).reorder(nil))
     end
+
+    def create_from_import_job(current_user_id:, tmp_file:)
+      job_id = ProjectImportWorker.perform_async(current_user_id, tmp_file)
+
+      if job_id
+        Rails.logger.info "Import job started for #{path_with_namespace} with job ID #{job_id}"
+      else
+        Rails.logger.error "Import job failed to start for #{path_with_namespace}"
+      end
+    end
   end
 
   def team
diff --git a/app/views/import/gitlab_projects/new.html.haml b/app/views/import/gitlab_projects/new.html.haml
index 64ea536698d..9e798b8a63f 100644
--- a/app/views/import/gitlab_projects/new.html.haml
+++ b/app/views/import/gitlab_projects/new.html.haml
@@ -5,7 +5,7 @@
   Import projects from FogBugz
 %hr
 
-= form_tag status_import_gitlab_project_url, class: 'form-horizontal' do
+= form_tag import_gitlab_project_path, class: 'form-horizontal' do
   %p
     To get started you add your project export file below.
   .form-group
diff --git a/app/workers/project_import_worker.rb b/app/workers/project_import_worker.rb
new file mode 100644
index 00000000000..6dd514b56a0
--- /dev/null
+++ b/app/workers/project_import_worker.rb
@@ -0,0 +1,22 @@
+class ProjectImportWorker
+  include Sidekiq::Worker
+  include Gitlab::ShellAdapter
+
+  sidekiq_options queue: :gitlab_shell
+
+  def perform(current_user_id, tmp_file)
+    current_user = User.find(current_user_id)
+
+    project = Gitlab::ImportExport::ImportService.execute(archive_file: tmp_file, owner: current_user)
+
+    # TODO: Move this to import service
+    # if result[:status] == :error
+    #   project.update(import_error: result[:message])
+    #   project.import_fail
+    #   return
+    # end
+
+    project.repository.after_import
+    project.import_finish
+  end
+end
diff --git a/config/routes.rb b/config/routes.rb
index 05e89e81500..99ce0118104 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -159,6 +159,7 @@ Rails.application.routes.draw do
 
     resource :gitlab_project, only: [:create, :new], controller: :gitlab_projects do
       get :status
+      post :create
       get :jobs
     end
   end
-- 
GitLab