Commit 639e65cc authored by Bob Van Landuyt's avatar Bob Van Landuyt
Browse files

Store override params as import data on projects

This means import data doesn't necessarily have to have an import_url
anymore.

The `ProjectImportData` could just contain the override data in it's
serialized data attribute.

The import data is automatically cleaned up after it is finished by
the state machine.
parent a17583e819c7
......@@ -630,7 +630,7 @@ def valid_import_url?
end
def create_or_update_import_data(data: nil, credentials: nil)
return unless import_url.present? && valid_import_url?
return if data.nil? && credentials.nil?
project_import_data = import_data || build_import_data
if data
......
......@@ -5,8 +5,8 @@ module Projects
class GitlabProjectsImportService
attr_reader :current_user, :params
def initialize(user, params)
@current_user, @params = user, params.dup
def initialize(user, import_params, override_params = nil)
@current_user, @params, @override_params = user, import_params.dup, override_params
end
def execute
......@@ -17,6 +17,7 @@ def execute
params[:import_type] = 'gitlab_project'
params[:import_source] = import_upload_path
params[:import_data] = { data: { override_params: @override_params } } if @override_params
::Projects::CreateService.new(current_user, params).execute
end
......
module API
class ProjectImport < Grape::API
include PaginationParams
include Helpers::ProjectsHelpers
helpers do
def import_params
......@@ -25,6 +26,11 @@ def validate_file!
requires :path, type: String, desc: 'The new project path and name'
requires :file, type: File, desc: 'The project export file to be imported'
optional :namespace, type: String, desc: "The ID or name of the namespace that the project will be imported into. Defaults to the current user's namespace."
optional :override_params,
type: Hash,
desc: 'New project params to override values in the export' do
use :optional_project_params
end
end
desc 'Create a new project import' do
detail 'This feature was introduced in GitLab 10.6.'
......@@ -47,7 +53,11 @@ def validate_file!
file: import_params[:file]['tempfile']
}
project = ::Projects::GitlabProjectsImportService.new(current_user, project_params).execute
override_params = import_params.delete(:override_params)
project = ::Projects::GitlabProjectsImportService.new(
current_user, project_params, override_params
).execute
render_api_error!(project.errors.full_messages&.first, 400) unless project.saved?
......
......@@ -40,7 +40,7 @@
expect(response).to have_gitlab_http_status(201)
end
it 'schedules an import at the user namespace level' do
it 'does not shedule an import for a nampespace that does not exist' do
expect_any_instance_of(Project).not_to receive(:import_schedule)
expect(::Projects::CreateService).not_to receive(:new)
......@@ -71,6 +71,34 @@
expect(json_response['error']).to eq('file is invalid')
end
it 'allows overriding project params' do
stub_import(namespace)
override_params = { 'description' => 'Hello world' }
post api('/projects/import', user),
path: 'test-import',
file: fixture_file_upload(file),
namespace: namespace.id,
override_params: override_params
import_project = Project.find(json_response['id'])
expect(import_project.import_data.data['override_params']).to eq(override_params)
end
it 'does store params that are not allowed' do
stub_import(namespace)
override_params = { 'not_allowed' => 'Hello world' }
post api('/projects/import', user),
path: 'test-import',
file: fixture_file_upload(file),
namespace: namespace.id,
override_params: override_params
import_project = Project.find(json_response['id'])
expect(import_project.import_data.data['override_params']).to be_empty
end
def stub_import(namespace)
expect_any_instance_of(Project).to receive(:import_schedule)
expect(::Projects::CreateService).to receive(:new).with(user, hash_including(namespace_id: namespace.id)).and_call_original
......
......@@ -2,8 +2,10 @@
describe Projects::GitlabProjectsImportService do
set(:namespace) { create(:namespace) }
let(:path) { 'test-path' }
let(:file) { fixture_file_upload(Rails.root + 'spec/fixtures/doc_sample.txt', 'text/plain') }
subject { described_class.new(namespace.owner, { namespace_id: namespace.id, path: path, file: file }) }
let(:import_params) { { namespace_id: namespace.id, path: path, file: file } }
subject { described_class.new(namespace.owner, import_params) }
describe '#execute' do
context 'with an invalid path' do
......@@ -18,8 +20,6 @@
end
context 'with a valid path' do
let(:path) { 'test-path' }
it 'creates a project' do
project = subject.execute
......@@ -27,5 +27,15 @@
expect(project).to be_valid
end
end
context 'override params' do
it 'stores them as import data when passed' do
project = described_class
.new(namespace.owner, import_params, description: 'Hello')
.execute
expect(project.import_data.data['override_params']['description']).to eq('Hello')
end
end
end
end
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