Commit 1f917cbd authored by James Lopez's avatar James Lopez
Browse files

refactor code based on feedback

parent c2a01bfe
......@@ -36,6 +36,8 @@ sudo chown -R git:git /var/opt/gitlab/git-data/repository-import-<date>
`foo.git` needs to be owned by the git user and git users group.
If you are using an installation from source, replace `/var/opt/gitlab/` with `/home/git`.
### Run the command below depending on your type of installation:
#### Omnibus Installation
......@@ -56,10 +58,15 @@ $ sudo -u git -H bundle exec rake gitlab:import:repos['/var/opt/gitlab/git-data/
#### Example output
```
Processing abcd.git
Processing /var/opt/gitlab/git-data/repository-import-1/a/b/c/blah.git
* Using namespace: a/b/c
* Created blah (a/b/c/blah)
* Skipping repo /var/opt/gitlab/git-data/repository-import-1/a/b/c/blah.wiki.git
Processing /var/opt/gitlab/git-data/repository-import-1/abcd.git
* Created abcd (abcd.git)
Processing group/xyz.git
* Created Group group (2)
Processing /var/opt/gitlab/git-data/repository-import-1/group/xyz.git
* Using namespace: group (2)
* Created xyz (group/xyz.git)
* Skipping repo /var/opt/gitlab/git-data/repository-import-1/@shared/a/b/abcd.git
[...]
```
module Gitlab
module BareRepositoryImport
class Importer
include Gitlab::ShellAdapter
NoAdminError = Class.new(StandardError)
def self.execute(import_path)
import_path << '/' unless import_path.ends_with?('/')
repos_to_import = Dir.glob(import_path + '**/*.git')
unless user = User.admins.order_id_asc.first
raise NoAdminError.new('No admin user found to import repositories')
end
repos_to_import.each do |repo_path|
bare_repo = Gitlab::BareRepositoryImport::Repository.new(import_path, repo_path)
if bare_repo.hashed? || bare_repo.wiki?
log " * Skipping repo #{bare_repo.repo_path}"
log " * Skipping repo #{bare_repo.repo_path}".color(:yellow)
next
end
log "Processing #{repo_path}".color(:yellow)
new(bare_repo).create_project_if_needed
new(user, bare_repo).create_project_if_needed
end
end
......@@ -29,11 +31,8 @@ def self.execute(import_path)
delegate :log, to: :class
delegate :project_name, :project_full_path, :group_path, :repo_path, :wiki_path, to: :bare_repo
def initialize(bare_repo)
unless @user = User.admins.order_id_asc.first
raise NoAdminError.new('No admin user found to import repositories')
end
def initialize(user, bare_repo)
@user = user
@bare_repo = bare_repo
end
......@@ -56,7 +55,6 @@ def create_project
name: project_name,
path: project_name,
skip_disk_validation: true,
import_type: 'gitlab_project',
namespace_id: group&.id).execute
if project.persisted? && mv_repo(project)
......
......@@ -35,7 +35,7 @@ def project_full_path
def repo_relative_path
# Remove root path and `.git` at the end
repo_path.sub(/\A#{@root_path}/, '').sub(/\.git$/, '')
repo_path[@root_path.size...-4]
end
end
end
......
......@@ -5,12 +5,16 @@
let!(:base_dir) { Dir.mktmpdir + '/' }
let(:bare_repository) { Gitlab::BareRepositoryImport::Repository.new(base_dir, File.join(base_dir, "#{project_path}.git")) }
subject(:importer) { described_class.new(bare_repository) }
subject(:importer) { described_class.new(admin, bare_repository) }
before do
allow(described_class).to receive(:log)
end
after do
FileUtils.rm_rf(base_dir)
end
shared_examples 'importing a repository' do
describe '.execute' do
it 'creates a project for a repository in storage' do
......@@ -27,19 +31,17 @@
repo_dir = File.join(base_dir, 'the-group', 'the-project.wiki.git')
FileUtils.mkdir_p(File.join(repo_dir))
expect(described_class).to receive(:log).with(" * Skipping repo #{repo_dir}")
expect(described_class).to receive(:log).with("\e[33m * Skipping repo #{repo_dir}\e[0m")
expect(described_class).not_to receive(:new)
described_class.execute(base_dir)
end
end
describe '#initialize' do
context 'without admin users' do
let(:admin) { nil }
it 'raises an error' do
expect { importer }.to raise_error(Gitlab::BareRepositoryImport::Importer::NoAdminError)
expect { described_class.execute(base_dir) }.to raise_error(Gitlab::BareRepositoryImport::Importer::NoAdminError)
end
end
end
......@@ -106,6 +108,32 @@
it_behaves_like 'importing a repository'
end
context 'without groups' do
let(:project_path) { 'a-project' }
it 'starts an import for a project that did not exist' do
expect(importer).to receive(:create_project)
importer.create_project_if_needed
end
it 'creates a project with the correct path in the database' do
importer.create_project_if_needed
expect(Project.find_by_full_path("#{admin.full_path}/#{project_path}")).not_to be_nil
end
it 'creates the Git repo in disk' do
FileUtils.mkdir_p(File.join(base_dir, "#{project_path}.git"))
importer.create_project_if_needed
project = Project.find_by_full_path("#{admin.full_path}/#{project_path}")
expect(File).to exist(File.join(project.repository_storage_path, project.disk_path + '.git'))
end
end
context 'with Wiki' do
let(:project_path) { 'a-group/a-project' }
let(:existing_group) { create(:group, path: 'a-group') }
......
......@@ -21,7 +21,7 @@
describe '#wiki?' do
it 'returns true if it is a wiki' do
wiki_path = described_class.new('/full/path/', '/full/path/to/my.wiki.git')
wiki_path = described_class.new('/full/path/', '/full/path/to/a/b/my.wiki.git')
expect(wiki_path.wiki?).to eq(true)
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment