2019-01-17 05:37:08 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Import
|
|
|
|
class GithubService < Import::BaseService
|
|
|
|
attr_accessor :client
|
|
|
|
attr_reader :params, :current_user
|
|
|
|
|
|
|
|
def execute(access_params, provider)
|
|
|
|
unless authorized?
|
2019-04-15 08:25:48 -04:00
|
|
|
return error(_('This namespace has already been taken! Please choose another one.'), :unprocessable_entity)
|
2019-01-17 05:37:08 -05:00
|
|
|
end
|
|
|
|
|
2020-05-28 23:08:11 -04:00
|
|
|
project = create_project(access_params, provider)
|
2019-01-17 05:37:08 -05:00
|
|
|
|
|
|
|
if project.persisted?
|
|
|
|
success(project)
|
|
|
|
else
|
|
|
|
error(project_save_error(project), :unprocessable_entity)
|
|
|
|
end
|
2020-05-28 23:08:11 -04:00
|
|
|
rescue Octokit::Error => e
|
|
|
|
log_error(e)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_project(access_params, provider)
|
|
|
|
Gitlab::LegacyGithubImport::ProjectCreator.new(
|
|
|
|
repo,
|
|
|
|
project_name,
|
|
|
|
target_namespace,
|
|
|
|
current_user,
|
|
|
|
access_params,
|
|
|
|
type: provider
|
|
|
|
).execute(extra_project_attrs)
|
2019-01-17 05:37:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def repo
|
|
|
|
@repo ||= client.repo(params[:repo_id].to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_name
|
|
|
|
@project_name ||= params[:new_name].presence || repo.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespace_path
|
|
|
|
@namespace_path ||= params[:target_namespace].presence || current_user.namespace_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_namespace
|
|
|
|
@target_namespace ||= find_or_create_namespace(namespace_path, current_user.namespace_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def extra_project_attrs
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorized?
|
|
|
|
can?(current_user, :create_projects, target_namespace)
|
|
|
|
end
|
2020-05-28 23:08:11 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def log_error(exception)
|
|
|
|
Gitlab::Import::Logger.error(
|
|
|
|
message: 'Import failed due to a GitHub error',
|
|
|
|
status: exception.response_status,
|
|
|
|
error: exception.response_body
|
|
|
|
)
|
|
|
|
|
|
|
|
error(_('Import failed due to a GitHub error: %{original}') % { original: exception.response_body }, :unprocessable_entity)
|
|
|
|
end
|
2019-01-17 05:37:08 -05:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Import::GithubService.prepend_if_ee('EE::Import::GithubService')
|