debian-mirror-gitlab/lib/tasks/gitlab/import.rake

78 lines
2.3 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
namespace :gitlab do
namespace :import do
# How to use:
#
# 1. copy the bare repos under the repos_path (commonly /home/git/repositories)
# 2. run: bundle exec rake gitlab:import:repos RAILS_ENV=production
#
# Notes:
# * The project owner will set to the first administator of the system
# * Existing projects will be skipped
#
2015-09-11 14:41:01 +05:30
desc "GitLab | Import bare repositories from gitlab_shell -> repos_path into GitLab project instance"
2014-09-02 18:07:02 +05:30
task repos: :environment do
git_base_path = Gitlab.config.gitlab_shell.repos_path
repos_to_import = Dir.glob(git_base_path + '/**/*.git')
repos_to_import.each do |repo_path|
# strip repo base path
repo_path[0..git_base_path.length] = ''
path = repo_path.sub(/\.git$/, '')
2015-04-26 12:48:37 +05:30
group_name, name = File.split(path)
2014-09-02 18:07:02 +05:30
group_name = nil if group_name == '.'
puts "Processing #{repo_path}".yellow
2015-04-26 12:48:37 +05:30
if path.end_with?('.wiki')
2014-09-02 18:07:02 +05:30
puts " * Skipping wiki repo"
next
end
project = Project.find_with_namespace(path)
if project
puts " * #{project.name} (#{repo_path}) exists"
else
2015-09-11 14:41:01 +05:30
user = User.admins.reorder("id").first
2014-09-02 18:07:02 +05:30
project_params = {
name: name,
path: name
}
# find group namespace
if group_name
2015-04-26 12:48:37 +05:30
group = Namespace.find_by(path: group_name)
2014-09-02 18:07:02 +05:30
# create group namespace
2015-04-26 12:48:37 +05:30
unless group
2014-09-02 18:07:02 +05:30
group = Group.new(:name => group_name)
group.path = group_name
group.owner = user
if group.save
puts " * Created Group #{group.name} (#{group.id})".green
else
puts " * Failed trying to create group #{group.name}".red
end
end
# set project group
project_params[:namespace_id] = group.id
end
project = Projects::CreateService.new(user, project_params).execute
2015-09-11 14:41:01 +05:30
if project.persisted?
2014-09-02 18:07:02 +05:30
puts " * Created #{project.name} (#{repo_path})".green
else
puts " * Failed trying to create #{project.name} (#{repo_path})".red
2015-09-11 14:41:01 +05:30
puts " Errors: #{project.errors.messages}".red
2014-09-02 18:07:02 +05:30
end
end
end
puts "Done!".green
end
end
end