From bcff9553909f0f26ae238482ecc43738a08899f1 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Fri, 3 Nov 2017 12:50:21 +0100 Subject: add project repo path class and refactor bare repository importer --- lib/gitlab/bare_repository_importer.rb | 40 +++++++++++++++--------- lib/gitlab/project_repo_path.rb | 28 +++++++++++++++++ spec/lib/gitlab/bare_repository_importer_spec.rb | 10 ++++++ 3 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 lib/gitlab/project_repo_path.rb diff --git a/lib/gitlab/bare_repository_importer.rb b/lib/gitlab/bare_repository_importer.rb index 9c6abffbd8e..52bda6076bf 100644 --- a/lib/gitlab/bare_repository_importer.rb +++ b/lib/gitlab/bare_repository_importer.rb @@ -1,5 +1,7 @@ module Gitlab class BareRepositoryImporter + include Gitlab::ShellAdapter + NoAdminError = Class.new(StandardError) def self.execute(import_path) @@ -7,35 +9,35 @@ module Gitlab repos_to_import = Dir.glob(import_path + '/**/*.git') repos_to_import.each do |repo_path| - if repo_path.end_with?('.wiki.git') + project_repo_path = Gitlab::ProjectRepoPath.new(import_path, repo_path) + + if project_repo_path.wiki? log " * Skipping wiki repo" next end log "Processing #{repo_path}".color(:yellow) - repo_relative_path = repo_path.sub(/\A#{import_path}\//, '').sub(/\.git$/, '') # Remove root path and `.git` at the end - new(repo_relative_path).create_project_if_needed + new(project_repo_path).create_project_if_needed end end - attr_reader :storage_name, :full_path, :group_path, :user, :project_name + attr_reader :storage_name, :user, :project_name delegate :log, to: :class - def initialize(repo_path) + def initialize(project_repo_path) unless @user = User.admins.order_id_asc.first raise NoAdminError.new('No admin user found to import repositories') end - @full_path = repo_path - - # Split path into 'all/the/namespaces' and 'project_name' - @group_path, _sep, @project_name = @full_path.rpartition('/') + @project_repo_path = project_repo_path + @project_name = project_repo_path.project_name end def create_project_if_needed - if project = Project.find_by_full_path(full_path) - log " * #{project.name} (#{full_path}) exists" + if project = Project.find_by_full_path(@project_repo_path.project_full_path) + log " * #{project.name} (#{@project_repo_path.project_full_path}) exists" + return project end @@ -50,20 +52,30 @@ module Gitlab project = Projects::CreateService.new(user, name: project_name, path: project_name, + skip_disk_validation: true, + import_type: 'gitlab_project', namespace_id: group&.id).execute - if project.persisted? - log " * Created #{project.name} (#{full_path})".color(:green) + if project.persisted? && import_repo(project) + + log " * Created #{project.name} (#{@project_repo_path.project_full_path})".color(:green) + ProjectCacheWorker.perform_async(project.id) else - log " * Failed trying to create #{project.name} (#{full_path})".color(:red) + log " * Failed trying to create #{project.name} (#{@project_repo_path.project_full_path})".color(:red) log " Errors: #{project.errors.messages}".color(:red) end project end + def import_repo(project) + gitlab_shell.import_repository(project.repository_storage_path, project.disk_path, @project_repo_path.repo_path) + end + def find_or_create_groups + group_path = @project_repo_path.group_path + return nil unless group_path.present? log " * Using namespace: #{group_path}" diff --git a/lib/gitlab/project_repo_path.rb b/lib/gitlab/project_repo_path.rb new file mode 100644 index 00000000000..7cefd556fc1 --- /dev/null +++ b/lib/gitlab/project_repo_path.rb @@ -0,0 +1,28 @@ +module Gitlab + class ProjectRepoPath + attr_reader :group_path, :project_name, :repo_path + + def initialize(root_path, repo_path) + @root_path = root_path + @repo_path = repo_path + + # Split path into 'all/the/namespaces' and 'project_name' + @group_path, _sep, @project_name = repo_relative_path.rpartition('/') + end + + def wiki? + @wiki ||= @repo_path.end_with?('.wiki.git') + end + + def project_full_path + @project_full_path ||= "#{group_path}/#{project_name}" + end + + private + + def repo_relative_path + # Remove root path and `.git` at the end + repo_path.sub(/\A#{@root_path}\//, '').sub(/\.git$/, '') + end + end +end diff --git a/spec/lib/gitlab/bare_repository_importer_spec.rb b/spec/lib/gitlab/bare_repository_importer_spec.rb index 60eff5caca9..d10042afe83 100644 --- a/spec/lib/gitlab/bare_repository_importer_spec.rb +++ b/spec/lib/gitlab/bare_repository_importer_spec.rb @@ -63,6 +63,16 @@ describe Gitlab::BareRepositoryImporter, repository: true do expect(Project.find_by_full_path(project_path)).not_to be_nil end + + context 'hashed storage enabled' do + it 'creates a project with the correct path in the database' do + stub_application_setting(hashed_storage_enabled: true) + + importer.create_project_if_needed + + expect(Project.find_by_full_path(project_path)).not_to be_nil + end + end end end -- cgit v1.2.1