Commit 05071f04 authored by João Cunha's avatar João Cunha

Refactor the BulkImportService

It's better to have feature specific logic scoped under their own
namespaces instead of the root namespace. So here we're moving:
BulkImportService to BulkImports::CreateService.

I also intend to create other services for the BulkImports. For intance,
the BulkImports::StatusService
parent 901ddc8c
......@@ -37,7 +37,7 @@ class Import::BulkImportsController < ApplicationController
end
def create
response = BulkImportService.new(current_user, create_params, credentials).execute
response = ::BulkImports::CreateService.new(current_user, create_params, credentials).execute
if response.success?
render json: response.payload.to_json(only: [:id])
......
......@@ -23,48 +23,50 @@
# - For each group to be imported (BulkImport::Entity.with_status(:created))
# - Import the group data
# - 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
attr_reader :current_user, :params, :credentials
module BulkImports
class CreateService
attr_reader :current_user, :params, :credentials
def initialize(current_user, params, credentials)
@current_user = current_user
@params = params
@credentials = credentials
end
def initialize(current_user, params, credentials)
@current_user = current_user
@params = params
@credentials = credentials
end
def execute
bulk_import = create_bulk_import
def execute
bulk_import = create_bulk_import
BulkImportWorker.perform_async(bulk_import.id)
BulkImportWorker.perform_async(bulk_import.id)
ServiceResponse.success(payload: bulk_import)
rescue ActiveRecord::RecordInvalid => e
ServiceResponse.error(
message: e.message,
http_status: :unprocessable_entity
)
end
ServiceResponse.success(payload: bulk_import)
rescue ActiveRecord::RecordInvalid => e
ServiceResponse.error(
message: e.message,
http_status: :unprocessable_entity
)
end
private
private
def create_bulk_import
BulkImport.transaction do
bulk_import = BulkImport.create!(user: current_user, source_type: 'gitlab')
bulk_import.create_configuration!(credentials.slice(:url, :access_token))
def create_bulk_import
BulkImport.transaction do
bulk_import = BulkImport.create!(user: current_user, source_type: 'gitlab')
bulk_import.create_configuration!(credentials.slice(:url, :access_token))
params.each do |entity|
BulkImports::Entity.create!(
bulk_import: bulk_import,
source_type: entity[:source_type],
source_full_path: entity[:source_full_path],
destination_name: entity[:destination_name],
destination_namespace: entity[:destination_namespace]
)
end
params.each do |entity|
BulkImports::Entity.create!(
bulk_import: bulk_import,
source_type: entity[:source_type],
source_full_path: entity[:source_full_path],
destination_name: entity[:destination_name],
destination_namespace: entity[:destination_namespace]
)
end
bulk_import
bulk_import
end
end
end
end
......@@ -51,7 +51,7 @@ module API
end
end
post do
response = BulkImportService.new(
response = ::BulkImports::CreateService.new(
current_user,
params[:entities],
url: params[:configuration][:url],
......
......@@ -199,9 +199,9 @@ RSpec.describe Import::BulkImportsController do
session[:bulk_import_gitlab_url] = instance_url
end
it 'executes BulkImportService' do
it 'executes BulkImpors::CreatetService' do
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))
end
......@@ -214,7 +214,7 @@ RSpec.describe Import::BulkImportsController do
it 'returns error when validation fails' do
error_response = ServiceResponse.error(message: 'Record invalid', http_status: :unprocessable_entity)
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)
end
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe BulkImportService do
RSpec.describe BulkImports::CreateService do
let(:user) { create(:user) }
let(:credentials) { { url: 'http://gitlab.example', access_token: 'token' } }
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