2016-01-21 18:09:32 +00:00
|
|
|
module Projects
|
|
|
|
class ImportService < BaseService
|
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
|
|
|
class Error < StandardError; end
|
|
|
|
|
|
|
|
ALLOWED_TYPES = [
|
|
|
|
'bitbucket',
|
|
|
|
'fogbugz',
|
|
|
|
'gitlab',
|
|
|
|
'github',
|
2016-06-14 10:47:07 +00:00
|
|
|
'google_code',
|
|
|
|
'gitlab_project'
|
2016-01-21 18:09:32 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def execute
|
2016-06-14 18:32:19 +00:00
|
|
|
add_repository_to_project unless project.gitlab_project_import?
|
2016-01-21 18:09:32 +00:00
|
|
|
|
|
|
|
import_data
|
|
|
|
|
|
|
|
success
|
2016-06-14 18:32:19 +00:00
|
|
|
rescue => e
|
2016-01-21 18:09:32 +00:00
|
|
|
error(e.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-14 18:32:19 +00: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 12:21:27 +00:00
|
|
|
elsif !project.repository_exists?
|
2016-06-14 18:32:19 +00:00
|
|
|
import_repository
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-21 18:09:32 +00:00
|
|
|
def create_repository
|
|
|
|
unless project.create_repository
|
|
|
|
raise Error, 'The repository could not be created.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_repository
|
|
|
|
begin
|
2016-06-22 21:04:51 +00:00
|
|
|
gitlab_shell.import_repository(project.repository_storage_path, project.path_with_namespace, project.import_url)
|
2016-07-12 14:21:28 +00:00
|
|
|
rescue => e
|
2016-09-23 07:42:07 +00: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
|
|
|
|
project.repository.before_import if project.repository_exists?
|
|
|
|
|
2016-05-31 08:53:50 +00:00
|
|
|
raise Error, "Error importing repository #{project.import_url} into #{project.path_with_namespace} - #{e.message}"
|
2016-01-21 18:09:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_data
|
|
|
|
return unless has_importer?
|
|
|
|
|
2016-06-14 18:32:19 +00:00
|
|
|
project.repository.before_import unless project.gitlab_project_import?
|
2016-04-04 22:35:39 +00:00
|
|
|
|
2016-01-21 18:09:32 +00:00
|
|
|
unless importer.execute
|
|
|
|
raise Error, 'The remote data could not be imported.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_importer?
|
|
|
|
ALLOWED_TYPES.include?(project.import_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def importer
|
2016-06-14 18:32:19 +00:00
|
|
|
return Gitlab::ImportExport::Importer.new(project) if @project.gitlab_project_import?
|
2016-06-14 10:47:07 +00:00
|
|
|
|
2016-01-21 18:09:32 +00:00
|
|
|
class_name = "Gitlab::#{project.import_type.camelize}Import::Importer"
|
|
|
|
class_name.constantize.new(project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unknown_url?
|
|
|
|
project.import_url == Project::UNKNOWN_IMPORT_URL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|