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
|
|
|
|
|
|
|
def execute
|
2016-06-14 14:32:19 -04:00
|
|
|
add_repository_to_project unless project.gitlab_project_import?
|
2016-01-21 13:09:32 -05:00
|
|
|
|
|
|
|
import_data
|
|
|
|
|
|
|
|
success
|
2016-06-14 14:32:19 -04:00
|
|
|
rescue => e
|
2017-07-20 05:34:09 -04:00
|
|
|
error("Error importing repository #{project.import_url} into #{project.full_path} - #{e.message}")
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-14 14:32:19 -04:00
|
|
|
def add_repository_to_project
|
|
|
|
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
|
|
|
|
raise Error, 'The repository could not be created.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_repository
|
2017-04-03 14:48:09 -04:00
|
|
|
raise Error, 'Blocked import URL.' if Gitlab::UrlBlocker.blocked_url?(project.import_url)
|
|
|
|
|
2016-01-21 13:09:32 -05:00
|
|
|
begin
|
2017-04-03 14:48:09 -04:00
|
|
|
if project.github_import? || project.gitea_import?
|
|
|
|
fetch_repository
|
|
|
|
else
|
|
|
|
clone_repository
|
|
|
|
end
|
|
|
|
rescue Gitlab::Shell::Error => e
|
2016-09-23 03:42:07 -04:00
|
|
|
# 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-04-03 14:48:09 -04:00
|
|
|
project.repository.expire_content_cache if project.repository_exists?
|
2016-09-23 03:42:07 -04:00
|
|
|
|
2017-04-03 14:48:09 -04:00
|
|
|
raise Error, e.message
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-03 14:48:09 -04:00
|
|
|
def clone_repository
|
2017-07-21 00:13:26 -04:00
|
|
|
gitlab_shell.import_repository(project.repository_storage_path, project.disk_path, project.import_url)
|
2017-04-03 14:48:09 -04:00
|
|
|
end
|
2017-03-27 10:43:03 -04:00
|
|
|
|
2017-04-03 14:48:09 -04:00
|
|
|
def fetch_repository
|
|
|
|
project.create_repository
|
|
|
|
project.repository.add_remote(project.import_type, project.import_url)
|
2017-04-03 15:43:27 -04:00
|
|
|
project.repository.set_remote_as_mirror(project.import_type)
|
2017-04-03 14:48:09 -04:00
|
|
|
project.repository.fetch_remote(project.import_type, forced: true)
|
2017-03-27 10:43:03 -04:00
|
|
|
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
|
|
|
|
raise Error, 'The remote data could not be imported.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2016-12-16 03:15:30 -05:00
|
|
|
Gitlab::ImportSources.importer(project.import_type).new(project)
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unknown_url?
|
|
|
|
project.import_url == Project::UNKNOWN_IMPORT_URL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|