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

99 lines
2.9 KiB
Ruby
Raw Normal View History

2016-04-02 18:10:28 +05:30
module Projects
class ImportService < BaseService
include Gitlab::ShellAdapter
2017-08-17 22:00:37 +05:30
Error = Class.new(StandardError)
2016-04-02 18:10:28 +05:30
2018-03-17 18:26:18 +05:30
# 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?
has_importer? && !!importer_class.try(:async?)
end
2016-04-02 18:10:28 +05:30
def execute
2018-03-17 18:26:18 +05:30
add_repository_to_project
2016-04-02 18:10:28 +05:30
import_data
success
2016-06-22 15:30:34 +05:30
rescue => e
2017-09-10 17:25:29 +05:30
error("Error importing repository #{project.import_url} into #{project.full_path} - #{e.message}")
2016-04-02 18:10:28 +05:30
end
private
2016-06-22 15:30:34 +05:30
def add_repository_to_project
2018-03-17 18:26:18 +05:30
if project.external_import? && !unknown_url?
2018-03-26 14:24:53 +05:30
raise Error, 'Blocked import URL.' if Gitlab::UrlBlocker.blocked_url?(project.import_url, valid_ports: Project::VALID_IMPORT_PORTS)
2018-03-17 18:26:18 +05:30
end
# We should skip the repository for a GitHub import or GitLab project import,
# because these importers fetch the project repositories for us.
return if has_importer? && importer_class.try(:imports_repository?)
2016-06-22 15:30:34 +05:30
if unknown_url?
# In this case, we only want to import issues, not a repository.
create_repository
2016-11-03 12:29:30 +05:30
elsif !project.repository_exists?
2016-06-22 15:30:34 +05:30
import_repository
end
end
2016-04-02 18:10:28 +05:30
def create_repository
unless project.create_repository
raise Error, 'The repository could not be created.'
end
end
def import_repository
begin
2018-03-17 18:26:18 +05:30
refmap = importer_class.try(:refmap) if has_importer?
if refmap
project.ensure_repository
project.repository.fetch_as_mirror(project.import_url, refmap: refmap)
2017-08-17 22:00:37 +05:30
else
2018-03-17 18:26:18 +05:30
gitlab_shell.import_repository(project.repository_storage_path, project.disk_path, project.import_url)
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
rescue Gitlab::Shell::Error, Gitlab::Git::RepositoryMirroring::RemoteError => e
2016-11-03 12:29:30 +05:30
# 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
2017-08-17 22:00:37 +05:30
project.repository.expire_content_cache if project.repository_exists?
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
raise Error, e.message
2016-04-02 18:10:28 +05:30
end
end
def import_data
return unless has_importer?
2017-08-17 22:00:37 +05:30
project.repository.expire_content_cache unless project.gitlab_project_import?
2016-06-02 11:05:42 +05:30
2016-04-02 18:10:28 +05:30
unless importer.execute
raise Error, 'The remote data could not be imported.'
end
end
2018-03-17 18:26:18 +05:30
def importer_class
@importer_class ||= Gitlab::ImportSources.importer(project.import_type)
end
2016-04-02 18:10:28 +05:30
def has_importer?
2017-08-17 22:00:37 +05:30
Gitlab::ImportSources.importer_names.include?(project.import_type)
2016-04-02 18:10:28 +05:30
end
def importer
2018-03-17 18:26:18 +05:30
importer_class.new(project)
2016-04-02 18:10:28 +05:30
end
def unknown_url?
project.import_url == Project::UNKNOWN_IMPORT_URL
end
end
end