2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-21 13:09:32 -05:00
|
|
|
module Projects
|
|
|
|
class ImportService < BaseService
|
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
2017-03-01 06:00:37 -05:00
|
|
|
Error = Class.new(StandardError)
|
2016-01-21 13:09:32 -05:00
|
|
|
|
2017-10-13 12:50:36 -04:00
|
|
|
# Returns true if this importer is supposed to perform its work in the
|
|
|
|
# background.
|
|
|
|
#
|
|
|
|
# This method will only return `true` if async importing is explicitly
|
|
|
|
# supported by an importer class (`Gitlab::GithubImport::ParallelImporter`
|
|
|
|
# for example).
|
|
|
|
def async?
|
2017-11-15 07:27:37 -05:00
|
|
|
has_importer? && !!importer_class.try(:async?)
|
2017-10-13 12:50:36 -04:00
|
|
|
end
|
|
|
|
|
2016-01-21 13:09:32 -05:00
|
|
|
def execute
|
2017-11-15 07:27:37 -05:00
|
|
|
add_repository_to_project
|
2016-01-21 13:09:32 -05:00
|
|
|
|
2018-06-06 12:42:18 -04:00
|
|
|
download_lfs_objects
|
|
|
|
|
2016-01-21 13:09:32 -05:00
|
|
|
import_data
|
|
|
|
|
|
|
|
success
|
2018-12-05 08:31:43 -05:00
|
|
|
rescue Gitlab::UrlBlocker::BlockedUrlError => e
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, project_path: project.full_path, importer: project.import_type)
|
2018-12-05 08:31:43 -05:00
|
|
|
|
2019-04-15 08:25:48 -04:00
|
|
|
error(s_("ImportProjects|Error importing repository %{project_safe_import_url} into %{project_full_path} - %{message}") % { project_safe_import_url: project.safe_import_url, project_full_path: project.full_path, message: e.message })
|
2018-12-05 08:31:43 -05:00
|
|
|
rescue => e
|
|
|
|
message = Projects::ImportErrorFilter.filter_message(e.message)
|
|
|
|
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(e, project_path: project.full_path, importer: project.import_type)
|
2018-12-05 08:31:43 -05:00
|
|
|
|
2019-04-15 08:25:48 -04:00
|
|
|
error(s_("ImportProjects|Error importing repository %{project_safe_import_url} into %{project_full_path} - %{message}") % { project_safe_import_url: project.safe_import_url, project_full_path: project.full_path, message: message })
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-14 14:32:19 -04:00
|
|
|
def add_repository_to_project
|
2017-11-15 07:27:37 -05:00
|
|
|
if project.external_import? && !unknown_url?
|
2018-03-28 13:27:16 -04:00
|
|
|
begin
|
2018-06-01 07:43:53 -04:00
|
|
|
Gitlab::UrlBlocker.validate!(project.import_url, ports: Project::VALID_IMPORT_PORTS)
|
2018-03-28 13:27:16 -04:00
|
|
|
rescue Gitlab::UrlBlocker::BlockedUrlError => e
|
2019-04-15 08:25:48 -04:00
|
|
|
raise e, s_("ImportProjects|Blocked import URL: %{message}") % { message: e.message }
|
2018-03-28 13:27:16 -04:00
|
|
|
end
|
2017-11-15 07:27:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# We should skip the repository for a GitHub import or GitLab project import,
|
|
|
|
# because these importers fetch the project repositories for us.
|
2018-06-06 12:42:18 -04:00
|
|
|
return if importer_imports_repository?
|
2017-11-15 07:27:37 -05:00
|
|
|
|
2016-06-14 14:32:19 -04:00
|
|
|
if unknown_url?
|
|
|
|
# In this case, we only want to import issues, not a repository.
|
|
|
|
create_repository
|
2016-10-19 08:21:27 -04:00
|
|
|
elsif !project.repository_exists?
|
2017-04-03 14:48:09 -04:00
|
|
|
import_repository
|
2016-06-14 14:32:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-21 13:09:32 -05:00
|
|
|
def create_repository
|
|
|
|
unless project.create_repository
|
2019-04-15 08:25:48 -04:00
|
|
|
raise Error, s_('ImportProjects|The repository could not be created.')
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_repository
|
2020-01-23 10:08:46 -05:00
|
|
|
refmap = importer_class.try(:refmap) if has_importer?
|
2016-09-23 03:42:07 -04:00
|
|
|
|
2020-01-23 10:08:46 -05:00
|
|
|
if refmap
|
|
|
|
project.ensure_repository
|
|
|
|
project.repository.fetch_as_mirror(project.import_url, refmap: refmap)
|
|
|
|
else
|
|
|
|
gitlab_shell.import_project_repository(project)
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
2020-01-23 10:08:46 -05:00
|
|
|
rescue Gitlab::Shell::Error => e
|
|
|
|
# Expire cache to prevent scenarios such as:
|
|
|
|
# 1. First import failed, but the repo was imported successfully, so +exists?+ returns true
|
|
|
|
# 2. Retried import, repo is broken or not imported but +exists?+ still returns true
|
|
|
|
project.repository.expire_content_cache if project.repository_exists?
|
|
|
|
|
|
|
|
raise Error, e.message
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
|
|
|
|
2018-06-06 12:42:18 -04:00
|
|
|
def download_lfs_objects
|
|
|
|
# In this case, we only want to import issues
|
|
|
|
return if unknown_url?
|
|
|
|
|
|
|
|
# If it has its own repository importer, it has to implements its own lfs import download
|
|
|
|
return if importer_imports_repository?
|
|
|
|
|
|
|
|
return unless project.lfs_enabled?
|
|
|
|
|
2019-04-30 04:21:21 -04:00
|
|
|
result = Projects::LfsPointers::LfsImportService.new(project).execute
|
2018-06-06 12:42:18 -04:00
|
|
|
|
2019-04-30 04:21:21 -04:00
|
|
|
if result[:status] == :error
|
|
|
|
# To avoid aborting the importing process, we silently fail
|
|
|
|
# if any exception raises.
|
|
|
|
Gitlab::AppLogger.error("The Lfs import process failed. #{result[:message]}")
|
2018-06-06 12:42:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-21 13:09:32 -05:00
|
|
|
def import_data
|
|
|
|
return unless has_importer?
|
|
|
|
|
2017-04-03 14:48:09 -04:00
|
|
|
project.repository.expire_content_cache unless project.gitlab_project_import?
|
2016-04-04 18:35:39 -04:00
|
|
|
|
2016-01-21 13:09:32 -05:00
|
|
|
unless importer.execute
|
2019-04-15 08:25:48 -04:00
|
|
|
raise Error, s_('ImportProjects|The remote data could not be imported.')
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-18 15:46:05 -04:00
|
|
|
def importer_class
|
2017-11-15 07:27:37 -05:00
|
|
|
@importer_class ||= Gitlab::ImportSources.importer(project.import_type)
|
2017-10-18 15:46:05 -04:00
|
|
|
end
|
|
|
|
|
2016-01-21 13:09:32 -05:00
|
|
|
def has_importer?
|
2016-12-16 03:15:30 -05:00
|
|
|
Gitlab::ImportSources.importer_names.include?(project.import_type)
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def importer
|
2017-10-13 12:50:36 -04:00
|
|
|
importer_class.new(project)
|
|
|
|
end
|
|
|
|
|
2016-01-21 13:09:32 -05:00
|
|
|
def unknown_url?
|
|
|
|
project.import_url == Project::UNKNOWN_IMPORT_URL
|
|
|
|
end
|
2018-06-06 12:42:18 -04:00
|
|
|
|
|
|
|
def importer_imports_repository?
|
|
|
|
has_importer? && importer_class.try(:imports_repository?)
|
|
|
|
end
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
|
|
|
end
|