debian-mirror-gitlab/lib/gitlab/bare_repository_import/importer.rb

133 lines
3.9 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Gitlab
module BareRepositoryImport
class Importer
NoAdminError = Class.new(StandardError)
def self.execute(import_path)
2018-12-13 13:39:08 +05:30
unless import_path.ends_with?('/')
import_path = "#{import_path}/"
end
2018-03-17 18:26:18 +05:30
repos_to_import = Dir.glob(import_path + '**/*.git')
unless user = User.admins.order_id_asc.first
2021-06-08 01:23:25 +05:30
raise NoAdminError, 'No admin user found to import repositories'
2018-03-17 18:26:18 +05:30
end
repos_to_import.each do |repo_path|
bare_repo = Gitlab::BareRepositoryImport::Repository.new(import_path, repo_path)
unless bare_repo.processable?
log " * Skipping repo #{bare_repo.repo_path}".color(:yellow)
next
end
log "Processing #{repo_path}".color(:yellow)
new(user, bare_repo).create_project_if_needed
end
end
2018-11-18 11:00:15 +05:30
# This is called from within a rake task only used by Admins, so allow writing
# to STDOUT
def self.log(message)
puts message # rubocop:disable Rails/Output
end
2018-03-17 18:26:18 +05:30
attr_reader :user, :project_name, :bare_repo
delegate :log, to: :class
delegate :project_name, :project_full_path, :group_path, :repo_path, :wiki_path, to: :bare_repo
def initialize(user, bare_repo)
@user = user
@bare_repo = bare_repo
end
def create_project_if_needed
if project = Project.find_by_full_path(project_full_path)
log " * #{project.name} (#{project_full_path}) exists"
return project
end
create_project
end
private
def create_project
group = find_or_create_groups
project = Projects::CreateService.new(user,
name: project_name,
path: project_name,
skip_disk_validation: true,
skip_wiki: bare_repo.wiki_exists?,
import_type: 'bare_repository',
namespace_id: group&.id).execute
2018-11-18 11:00:15 +05:30
if project.persisted? && mv_repositories(project)
2018-03-17 18:26:18 +05:30
log " * Created #{project.name} (#{project_full_path})".color(:green)
2021-10-27 15:23:28 +05:30
project.set_full_path
2018-03-17 18:26:18 +05:30
ProjectCacheWorker.perform_async(project.id)
else
log " * Failed trying to create #{project.name} (#{project_full_path})".color(:red)
log " Errors: #{project.errors.messages}".color(:red) if project.errors.any?
end
project
end
2018-11-18 11:00:15 +05:30
def mv_repositories(project)
mv_repo(bare_repo.repo_path, project.repository)
2018-03-17 18:26:18 +05:30
if bare_repo.wiki_exists?
2018-11-18 11:00:15 +05:30
mv_repo(bare_repo.wiki_path, project.wiki.repository)
2018-03-17 18:26:18 +05:30
end
true
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2018-03-17 18:26:18 +05:30
log " * Failed to move repo: #{e.message}".color(:red)
false
end
2018-11-18 11:00:15 +05:30
def mv_repo(path, repository)
repository.create_from_bundle(bundle(path))
FileUtils.rm_rf(path)
end
2018-10-15 14:42:47 +05:30
def storage_path_for_shard(shard)
Gitlab.config.repositories.storages[shard].legacy_disk_path
end
2018-03-17 18:26:18 +05:30
def find_or_create_groups
2019-07-07 11:18:12 +05:30
return unless group_path.present?
2018-03-17 18:26:18 +05:30
log " * Using namespace: #{group_path}"
Groups::NestedCreateService.new(user, group_path: group_path).execute
end
2018-11-18 11:00:15 +05:30
def bundle(repo_path)
# TODO: we could save some time and disk space by using
# `git bundle create - --all` and streaming the bundle directly to
# Gitaly, rather than writing it on disk first
bundle_path = "#{repo_path}.bundle"
cmd = %W(#{Gitlab.config.git.bin_path} --git-dir=#{repo_path} bundle create #{bundle_path} --all)
output, status = Gitlab::Popen.popen(cmd)
2020-10-24 23:57:45 +05:30
raise output unless status == 0
2018-11-18 11:00:15 +05:30
bundle_path
2018-03-17 18:26:18 +05:30
end
end
end
end