2017-10-13 16:50:36 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
module Importer
|
|
|
|
class RepositoryImporter
|
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
|
|
|
attr_reader :project, :client
|
|
|
|
|
|
|
|
def initialize(project, client)
|
|
|
|
@project = project
|
|
|
|
@client = client
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if we should import the wiki for the project.
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-10-13 16:50:36 +00:00
|
|
|
def import_wiki?
|
|
|
|
client.repository(project.import_source)&.has_wiki &&
|
2018-03-27 09:48:11 +00:00
|
|
|
!project.wiki_repository_exists? &&
|
|
|
|
Gitlab::GitalyClient::RemoteService.exists?(wiki_url)
|
2017-10-13 16:50:36 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-10-13 16:50:36 +00:00
|
|
|
|
|
|
|
# Imports the repository data.
|
|
|
|
#
|
|
|
|
# This method will return true if the data was imported successfully or
|
|
|
|
# the repository had already been imported before.
|
|
|
|
def execute
|
|
|
|
imported =
|
|
|
|
# It's possible a repository has already been imported when running
|
|
|
|
# this code, e.g. because we had to retry this job after
|
|
|
|
# `import_wiki?` raised a rate limit error. In this case we'll skip
|
|
|
|
# re-importing the main repository.
|
2017-12-20 18:13:11 +00:00
|
|
|
if project.empty_repo?
|
2017-10-13 16:50:36 +00:00
|
|
|
import_repository
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
update_clone_time if imported
|
|
|
|
|
|
|
|
imported = import_wiki_repository if import_wiki? && imported
|
|
|
|
|
|
|
|
imported
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_repository
|
|
|
|
project.ensure_repository
|
|
|
|
|
2017-11-23 15:51:55 +00:00
|
|
|
refmap = Gitlab::GithubImport.refmap
|
|
|
|
project.repository.fetch_as_mirror(project.import_url, refmap: refmap, forced: true, remote_name: 'github')
|
2017-10-13 16:50:36 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
rescue Gitlab::Git::Repository::NoRepository, Gitlab::Shell::Error => e
|
|
|
|
fail_import("Failed to import the repository: #{e.message}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_wiki_repository
|
|
|
|
wiki_path = "#{project.disk_path}.wiki"
|
|
|
|
|
2018-03-26 18:21:49 +00:00
|
|
|
gitlab_shell.import_repository(project.repository_storage, wiki_path, wiki_url)
|
2017-10-13 16:50:36 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
rescue Gitlab::Shell::Error => e
|
|
|
|
if e.message !~ /repository not exported/
|
2018-02-19 17:56:33 +00:00
|
|
|
project.create_wiki
|
2017-10-13 16:50:36 +00:00
|
|
|
fail_import("Failed to import the wiki: #{e.message}")
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 09:48:11 +00:00
|
|
|
def wiki_url
|
|
|
|
project.import_url.sub(/\.git\z/, '.wiki.git')
|
|
|
|
end
|
|
|
|
|
2017-10-13 16:50:36 +00:00
|
|
|
def update_clone_time
|
|
|
|
project.update_column(:last_repository_updated_at, Time.zone.now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fail_import(message)
|
|
|
|
project.mark_import_as_failed(message)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|