debian-mirror-gitlab/app/services/projects/gitlab_projects_import_service.rb

72 lines
1.8 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
# 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
class GitlabProjectsImportService
2018-11-18 11:00:15 +05:30
include Gitlab::Utils::StrongMemoize
include Gitlab::TemplateHelper
2017-09-10 17:25:29 +05:30
attr_reader :current_user, :params
2018-05-09 12:01:36 +05:30
def initialize(user, import_params, override_params = nil)
@current_user, @params, @override_params = user, import_params.dup, override_params
2017-09-10 17:25:29 +05:30
end
def execute
2018-11-18 11:00:15 +05:30
prepare_template_environment(template_file)
2018-05-09 12:01:36 +05:30
2018-11-18 11:00:15 +05:30
prepare_import_params
2018-03-27 19:54:05 +05:30
::Projects::CreateService.new(current_user, params).execute
2017-09-10 17:25:29 +05:30
end
private
2018-05-09 12:01:36 +05:30
def overwrite_project?
2018-11-18 11:00:15 +05:30
overwrite? && project_with_same_full_path?
2018-05-09 12:01:36 +05:30
end
def project_with_same_full_path?
Project.find_by_full_path("#{current_namespace.full_path}/#{params[:path]}").present?
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-05-09 12:01:36 +05:30
def current_namespace
2018-11-18 11:00:15 +05:30
strong_memoize(:current_namespace) do
Namespace.find_by(id: params[:namespace_id])
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def overwrite?
strong_memoize(:overwrite) do
params.delete(:overwrite)
end
end
def template_file
strong_memoize(:template_file) do
params.delete(:file)
end
end
def prepare_import_params
data = {}
data[:override_params] = @override_params if @override_params
if overwrite_project?
data[:original_path] = params[:path]
params[:path] += "-#{tmp_filename}"
end
if template_file
params[:import_type] = 'gitlab_project'
end
params[:import_data] = { data: data } if data.present?
2018-05-09 12:01:36 +05:30
end
2017-09-10 17:25:29 +05:30
end
end