Commit 59b36f20 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Use gitlab-shell to move repos. Requires gitlab-shell v1.1.0

parent b1e42551
...@@ -3,29 +3,27 @@ ...@@ -3,29 +3,27 @@
# Used for transfer project to another namespace # Used for transfer project to another namespace
# #
class ProjectTransferService class ProjectTransferService
include Gitolited
attr_accessor :project attr_accessor :project
def transfer(project, new_namespace) def transfer(project, new_namespace)
Project.transaction do Project.transaction do
old_namespace = project.namespace old_path = project.path_with_namespace
project.namespace = new_namespace new_path = File.join(new_namespace.try(:path) || '', project.path)
old_dir = old_namespace.try(:path) || ''
new_dir = new_namespace.try(:path) || ''
old_repo = if old_dir.present?
File.join(old_dir, project.path)
else
project.path
end
if Project.where(path: project.path, namespace_id: new_namespace.try(:id)).present? if Project.where(path: project.path, namespace_id: new_namespace.try(:id)).present?
raise TransferError.new("Project with same path in target namespace already exists") raise TransferError.new("Project with same path in target namespace already exists")
end end
Gitlab::ProjectMover.new(project, old_dir, new_dir).execute project.namespace = new_namespace
project.save! project.save!
unless gitlab_shell.mv_repository(old_path, new_path)
raise TransferError.new('Cannot move project')
end
true
end end
rescue Gitlab::ProjectMover::ProjectMoveError => ex rescue Gitlab::ProjectMover::ProjectMoveError => ex
raise Project::TransferError.new(ex.message) raise Project::TransferError.new(ex.message)
......
...@@ -24,6 +24,18 @@ module Gitlab ...@@ -24,6 +24,18 @@ module Gitlab
system("#{gitlab_shell_user_home}/gitlab-shell/bin/gitlab-projects import-project #{name}.git #{url}") system("#{gitlab_shell_user_home}/gitlab-shell/bin/gitlab-projects import-project #{name}.git #{url}")
end end
# Move repository
#
# path - project path with namespace
# new_path - new project path with namespace
#
# Ex.
# mv_repository("gitlab/gitlab-ci", "randx/gitlab-ci-new.git")
#
def mv_repository(path, new_path)
system("#{gitlab_shell_user_home}/gitlab-shell/bin/gitlab-projects mv-project #{path}.git #{new_path}.git")
end
# Remove repository from file system # Remove repository from file system
# #
# name - project path with namespace # name - project path with namespace
...@@ -56,7 +68,7 @@ module Gitlab ...@@ -56,7 +68,7 @@ module Gitlab
def url_to_repo path def url_to_repo path
Gitlab.config.gitlab_shell.ssh_path_prefix + "#{path}.git" Gitlab.config.gitlab_shell.ssh_path_prefix + "#{path}.git"
end end
def gitlab_shell_user_home def gitlab_shell_user_home
File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}") File.expand_path("~#{Gitlab.config.gitlab_shell.ssh_user}")
end end
......
# ProjectMover class
#
# Used for moving project repositories from one subdir to another
module Gitlab
class ProjectMover
class ProjectMoveError < StandardError; end
attr_reader :project, :old_dir, :new_dir
def initialize(project, old_dir, new_dir)
@project = project
@old_dir = old_dir
@new_dir = new_dir
end
def execute
# Create new dir if missing
new_dir_path = File.join(Gitlab.config.gitlab_shell.repos_path, new_dir)
FileUtils.mkdir( new_dir_path, mode: 0770 ) unless File.exists?(new_dir_path)
old_path = File.join(Gitlab.config.gitlab_shell.repos_path, old_dir, "#{project.path}.git")
new_path = File.join(new_dir_path, "#{project.path}.git")
if File.exists? new_path
raise ProjectMoveError.new("Destination #{new_path} already exists")
end
begin
FileUtils.mv( old_path, new_path )
log_info "Project #{project.name} was moved from #{old_path} to #{new_path}"
true
rescue Exception => e
message = "Project #{project.name} cannot be moved from #{old_path} to #{new_path}"
log_info "Error! #{message} (#{e.message})"
raise ProjectMoveError.new(message)
end
end
protected
def log_info message
Gitlab::AppLogger.info message
end
end
end
require 'spec_helper'
describe Gitlab::ProjectMover do
let(:base_path) { Rails.root.join('tmp', 'rspec-sandbox') }
before do
FileUtils.rm_rf base_path if File.exists? base_path
FileUtils.mkdir_p base_path
Gitlab.config.gitlab_shell.stub(repos_path: base_path)
@project = create(:project)
end
after do
FileUtils.rm_rf base_path
end
it "should move project to subdir" do
mk_dir base_path, '', @project.path
mover = Gitlab::ProjectMover.new(@project, '', 'opensource')
mover.execute.should be_true
moved?('opensource', @project.path).should be_true
end
it "should move project from one subdir to another" do
mk_dir base_path, 'vsizov', @project.path
mover = Gitlab::ProjectMover.new(@project, 'vsizov', 'randx')
mover.execute.should be_true
moved?('randx', @project.path).should be_true
end
it "should move project from subdir to base" do
mk_dir base_path, 'vsizov', @project.path
mover = Gitlab::ProjectMover.new(@project, 'vsizov', '')
mover.execute.should be_true
moved?('', @project.path).should be_true
end
it "should raise if destination exists" do
mk_dir base_path, '', @project.path
mk_dir base_path, 'vsizov', @project.path
mover = Gitlab::ProjectMover.new(@project, 'vsizov', '')
expect { mover.execute }.to raise_error(Gitlab::ProjectMover::ProjectMoveError)
end
it "should raise if move failed" do
mk_dir base_path
mover = Gitlab::ProjectMover.new(@project, 'vsizov', '')
expect { mover.execute }.to raise_error(Gitlab::ProjectMover::ProjectMoveError)
end
def mk_dir base_path, namespace = '', project_path = ''
FileUtils.mkdir_p File.join(base_path, namespace, project_path + ".git")
end
def moved? namespace, path
File.exists?(File.join(base_path, namespace, path + '.git'))
end
end
require 'spec_helper'
describe ProjectTransferService do
context 'namespace -> namespace' do
let(:user) { create(:user) }
let(:group) { create(:group) }
let(:project) { create(:project, namespace: user.namespace) }
before do
@result = service.transfer(project, group)
end
it { @result.should be_true }
it { project.namespace.should == group }
end
context 'namespace -> no namespace' do
let(:user) { create(:user) }
let(:project) { create(:project, namespace: user.namespace) }
before do
@result = service.transfer(project, nil)
end
it { @result.should be_true }
it { project.namespace.should == nil }
end
context 'no namespace -> namespace' do
let(:project) { create(:project) }
let(:user) { create(:user) }
before do
@result = service.transfer(project, user.namespace)
end
it { @result.should be_true }
it { project.namespace.should == user.namespace }
end
def service
service = ProjectTransferService.new
service.gitlab_shell.stub(mv_repository: true)
service
end
end
Markdown is supported
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