Commit 69086674 authored by Tetiana Chupryna's avatar Tetiana Chupryna

Merge branch 'jcunha-extrac-controller-logic-to-service' into 'master'

Move the BulkImportService to BulkImports::CreateService

See merge request gitlab-org/gitlab!71494
parents d002e7f3 05071f04
...@@ -37,7 +37,7 @@ class Import::BulkImportsController < ApplicationController ...@@ -37,7 +37,7 @@ class Import::BulkImportsController < ApplicationController
end end
def create def create
response = BulkImportService.new(current_user, create_params, credentials).execute response = ::BulkImports::CreateService.new(current_user, create_params, credentials).execute
if response.success? if response.success?
render json: response.payload.to_json(only: [:id]) render json: response.payload.to_json(only: [:id])
......
...@@ -23,48 +23,50 @@ ...@@ -23,48 +23,50 @@
# - For each group to be imported (BulkImport::Entity.with_status(:created)) # - For each group to be imported (BulkImport::Entity.with_status(:created))
# - Import the group data # - Import the group data
# - Create entities for each subgroup of the imported group # - Create entities for each subgroup of the imported group
# - Enqueue a BulkImportService job (Pn) to import the new entities (subgroups) # - Enqueue a BulkImports::CreateService job (Pn) to import the new entities (subgroups)
# #
class BulkImportService module BulkImports
attr_reader :current_user, :params, :credentials class CreateService
attr_reader :current_user, :params, :credentials
def initialize(current_user, params, credentials) def initialize(current_user, params, credentials)
@current_user = current_user @current_user = current_user
@params = params @params = params
@credentials = credentials @credentials = credentials
end end
def execute def execute
bulk_import = create_bulk_import bulk_import = create_bulk_import
BulkImportWorker.perform_async(bulk_import.id) BulkImportWorker.perform_async(bulk_import.id)
ServiceResponse.success(payload: bulk_import) ServiceResponse.success(payload: bulk_import)
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
ServiceResponse.error( ServiceResponse.error(
message: e.message, message: e.message,
http_status: :unprocessable_entity http_status: :unprocessable_entity
) )
end end
private private
def create_bulk_import def create_bulk_import
BulkImport.transaction do BulkImport.transaction do
bulk_import = BulkImport.create!(user: current_user, source_type: 'gitlab') bulk_import = BulkImport.create!(user: current_user, source_type: 'gitlab')
bulk_import.create_configuration!(credentials.slice(:url, :access_token)) bulk_import.create_configuration!(credentials.slice(:url, :access_token))
params.each do |entity| params.each do |entity|
BulkImports::Entity.create!( BulkImports::Entity.create!(
bulk_import: bulk_import, bulk_import: bulk_import,
source_type: entity[:source_type], source_type: entity[:source_type],
source_full_path: entity[:source_full_path], source_full_path: entity[:source_full_path],
destination_name: entity[:destination_name], destination_name: entity[:destination_name],
destination_namespace: entity[:destination_namespace] destination_namespace: entity[:destination_namespace]
) )
end end
bulk_import bulk_import
end
end end
end end
end end
...@@ -51,7 +51,7 @@ module API ...@@ -51,7 +51,7 @@ module API
end end
end end
post do post do
response = BulkImportService.new( response = ::BulkImports::CreateService.new(
current_user, current_user,
params[:entities], params[:entities],
url: params[:configuration][:url], url: params[:configuration][:url],
......
...@@ -199,9 +199,9 @@ RSpec.describe Import::BulkImportsController do ...@@ -199,9 +199,9 @@ RSpec.describe Import::BulkImportsController do
session[:bulk_import_gitlab_url] = instance_url session[:bulk_import_gitlab_url] = instance_url
end end
it 'executes BulkImportService' do it 'executes BulkImpors::CreatetService' do
expect_next_instance_of( expect_next_instance_of(
BulkImportService, user, bulk_import_params, { url: instance_url, access_token: pat }) do |service| ::BulkImports::CreateService, user, bulk_import_params, { url: instance_url, access_token: pat }) do |service|
allow(service).to receive(:execute).and_return(ServiceResponse.success(payload: bulk_import)) allow(service).to receive(:execute).and_return(ServiceResponse.success(payload: bulk_import))
end end
...@@ -214,7 +214,7 @@ RSpec.describe Import::BulkImportsController do ...@@ -214,7 +214,7 @@ RSpec.describe Import::BulkImportsController do
it 'returns error when validation fails' do it 'returns error when validation fails' do
error_response = ServiceResponse.error(message: 'Record invalid', http_status: :unprocessable_entity) error_response = ServiceResponse.error(message: 'Record invalid', http_status: :unprocessable_entity)
expect_next_instance_of( expect_next_instance_of(
BulkImportService, user, bulk_import_params, { url: instance_url, access_token: pat }) do |service| ::BulkImports::CreateService, user, bulk_import_params, { url: instance_url, access_token: pat }) do |service|
allow(service).to receive(:execute).and_return(error_response) allow(service).to receive(:execute).and_return(error_response)
end end
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe BulkImportService do RSpec.describe BulkImports::CreateService do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:credentials) { { url: 'http://gitlab.example', access_token: 'token' } } let(:credentials) { { url: 'http://gitlab.example', access_token: 'token' } }
let(:params) do let(:params) do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment