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

77 lines
1.7 KiB
Ruby
Raw Normal View History

2016-04-02 18:10:28 +05:30
module Projects
class ImportService < BaseService
include Gitlab::ShellAdapter
class Error < StandardError; end
ALLOWED_TYPES = [
'bitbucket',
'fogbugz',
'gitlab',
'github',
2016-06-22 15:30:34 +05:30
'google_code',
'gitlab_project'
2016-04-02 18:10:28 +05:30
]
def execute
2016-06-22 15:30:34 +05:30
add_repository_to_project unless project.gitlab_project_import?
2016-04-02 18:10:28 +05:30
import_data
success
2016-06-22 15:30:34 +05:30
rescue => e
2016-04-02 18:10:28 +05:30
error(e.message)
end
private
2016-06-22 15:30:34 +05:30
def add_repository_to_project
if unknown_url?
# In this case, we only want to import issues, not a repository.
create_repository
else
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
2016-08-24 12:49:21 +05:30
gitlab_shell.import_repository(project.repository_storage_path, project.path_with_namespace, project.import_url)
rescue => e
raise Error, "Error importing repository #{project.import_url} into #{project.path_with_namespace} - #{e.message}"
2016-04-02 18:10:28 +05:30
end
end
def import_data
return unless has_importer?
2016-06-22 15:30:34 +05:30
project.repository.before_import 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
def has_importer?
ALLOWED_TYPES.include?(project.import_type)
end
def importer
2016-06-22 15:30:34 +05:30
return Gitlab::ImportExport::Importer.new(project) if @project.gitlab_project_import?
2016-04-02 18:10:28 +05:30
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