2017-07-26 08:21:53 -04:00
|
|
|
# This service is an adapter used to for the GitLab Import feature, and
|
|
|
|
# creating a project from a template.
|
|
|
|
# The latter will under the hood just import an archive supplied by GitLab.
|
|
|
|
module Projects
|
2017-08-01 08:34:11 -04:00
|
|
|
class GitlabProjectsImportService
|
2017-07-26 08:21:53 -04:00
|
|
|
attr_reader :current_user, :params
|
|
|
|
|
2018-03-29 07:57:21 -04:00
|
|
|
def initialize(user, import_params, override_params = nil)
|
|
|
|
@current_user, @params, @override_params = user, import_params.dup, override_params
|
2017-07-26 08:21:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
FileUtils.mkdir_p(File.dirname(import_upload_path))
|
2018-02-12 08:57:25 -05:00
|
|
|
|
|
|
|
file = params.delete(:file)
|
2017-07-26 08:21:53 -04:00
|
|
|
FileUtils.copy_entry(file.path, import_upload_path)
|
|
|
|
|
2018-04-06 11:23:49 -04:00
|
|
|
@overwrite = params.delete(:overwrite)
|
|
|
|
data = {}
|
|
|
|
data[:override_params] = @override_params if @override_params
|
|
|
|
|
|
|
|
if overwrite_project?
|
|
|
|
data[:original_path] = params[:path]
|
|
|
|
params[:path] += "-#{tmp_filename}"
|
|
|
|
end
|
|
|
|
|
2018-02-12 08:57:25 -05:00
|
|
|
params[:import_type] = 'gitlab_project'
|
|
|
|
params[:import_source] = import_upload_path
|
2018-04-06 11:23:49 -04:00
|
|
|
params[:import_data] = { data: data } if data.present?
|
2018-02-12 08:57:25 -05:00
|
|
|
|
|
|
|
::Projects::CreateService.new(current_user, params).execute
|
2017-07-26 08:21:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def import_upload_path
|
2017-07-19 10:32:56 -04:00
|
|
|
@import_upload_path ||= Gitlab::ImportExport.import_upload_path(filename: tmp_filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tmp_filename
|
2018-01-04 00:42:52 -05:00
|
|
|
SecureRandom.hex
|
2017-07-26 08:21:53 -04:00
|
|
|
end
|
2018-04-06 11:23:49 -04:00
|
|
|
|
|
|
|
def overwrite_project?
|
|
|
|
@overwrite && project_with_same_full_path?
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_with_same_full_path?
|
|
|
|
Project.find_by_full_path("#{current_namespace.full_path}/#{params[:path]}").present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_namespace
|
|
|
|
@current_namespace ||= Namespace.find_by(id: params[:namespace_id])
|
|
|
|
end
|
2017-07-26 08:21:53 -04:00
|
|
|
end
|
|
|
|
end
|