2016-01-21 13:09:32 -05:00
|
|
|
module Projects
|
|
|
|
class ImportService < BaseService
|
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
|
|
|
class Error < StandardError; end
|
|
|
|
|
|
|
|
ALLOWED_TYPES = [
|
|
|
|
'bitbucket',
|
|
|
|
'fogbugz',
|
|
|
|
'gitlab',
|
|
|
|
'github',
|
2016-06-14 06:47:07 -04:00
|
|
|
'google_code',
|
|
|
|
'gitlab_project'
|
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
|
2016-01-21 13:09:32 -05:00
|
|
|
error(e.message)
|
|
|
|
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?
|
2016-06-14 14:32:19 -04:00
|
|
|
import_repository
|
|
|
|
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
|
|
|
|
begin
|
2016-06-22 17:04:51 -04:00
|
|
|
gitlab_shell.import_repository(project.repository_storage_path, project.path_with_namespace, project.import_url)
|
2016-07-12 10:21:28 -04:00
|
|
|
rescue => 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
|
|
|
|
project.repository.before_import if project.repository_exists?
|
|
|
|
|
2016-05-31 04:53:50 -04:00
|
|
|
raise Error, "Error importing repository #{project.import_url} into #{project.path_with_namespace} - #{e.message}"
|
2016-01-21 13:09:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_data
|
|
|
|
return unless has_importer?
|
|
|
|
|
2016-06-14 14:32:19 -04:00
|
|
|
project.repository.before_import 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?
|
|
|
|
ALLOWED_TYPES.include?(project.import_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def importer
|
2016-06-14 14:32:19 -04:00
|
|
|
return Gitlab::ImportExport::Importer.new(project) if @project.gitlab_project_import?
|
2016-06-14 06:47:07 -04:00
|
|
|
|
2016-01-21 13:09:32 -05: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
|