2012-10-30 07:04:56 -04:00
|
|
|
namespace :gitlab do
|
|
|
|
namespace :import do
|
|
|
|
# How to use:
|
|
|
|
#
|
2014-01-20 04:44:40 -05:00
|
|
|
# 1. copy the bare repos under the repos_path (commonly /home/git/repositories)
|
|
|
|
# 2. run: bundle exec rake gitlab:import:repos RAILS_ENV=production
|
2012-10-30 07:04:56 -04:00
|
|
|
#
|
|
|
|
# Notes:
|
2014-01-20 04:44:40 -05:00
|
|
|
# * The project owner will set to the first administator of the system
|
|
|
|
# * Existing projects will be skipped
|
2012-10-30 07:04:56 -04:00
|
|
|
#
|
2013-09-06 19:18:09 -04:00
|
|
|
desc "GITLAB | Import bare repositories from gitlab_shell -> repos_path into GitLab project instance"
|
2013-05-05 10:01:10 -04:00
|
|
|
task repos: :environment do
|
2012-10-30 07:04:56 -04:00
|
|
|
|
2013-02-11 12:16:59 -05:00
|
|
|
git_base_path = Gitlab.config.gitlab_shell.repos_path
|
2013-05-25 13:38:55 -04:00
|
|
|
repos_to_import = Dir.glob(git_base_path + '/**/*.git')
|
2012-10-30 07:04:56 -04:00
|
|
|
|
2012-12-22 17:15:48 -05:00
|
|
|
namespaces = Namespace.pluck(:path)
|
|
|
|
|
2012-10-30 07:04:56 -04:00
|
|
|
repos_to_import.each do |repo_path|
|
2013-05-25 13:38:55 -04:00
|
|
|
# strip repo base path
|
|
|
|
repo_path[0..git_base_path.length] = ''
|
2012-10-30 07:04:56 -04:00
|
|
|
|
2013-05-25 13:38:55 -04:00
|
|
|
path = repo_path.sub(/\.git$/, '')
|
|
|
|
name = File.basename path
|
|
|
|
group_name = File.dirname path
|
|
|
|
group_name = nil if group_name == '.'
|
2012-12-22 17:15:48 -05:00
|
|
|
|
2013-05-25 13:38:55 -04:00
|
|
|
# Skip if group or user
|
|
|
|
next if namespaces.include?(name)
|
2012-12-22 17:18:39 -05:00
|
|
|
|
2013-05-25 13:38:55 -04:00
|
|
|
puts "Processing #{repo_path}".yellow
|
2012-10-30 07:04:56 -04:00
|
|
|
|
2013-08-14 05:45:17 -04:00
|
|
|
if path =~ /.wiki\Z/
|
|
|
|
puts " * Skipping wiki repo"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2012-12-22 17:15:48 -05:00
|
|
|
project = Project.find_with_namespace(path)
|
2012-10-30 07:04:56 -04:00
|
|
|
|
|
|
|
if project
|
2013-05-25 13:38:55 -04:00
|
|
|
puts " * #{project.name} (#{repo_path}) exists"
|
2012-10-30 07:04:56 -04:00
|
|
|
else
|
|
|
|
user = User.admins.first
|
|
|
|
|
|
|
|
project_params = {
|
2013-05-25 13:38:55 -04:00
|
|
|
name: name,
|
2013-08-14 05:45:17 -04:00
|
|
|
path: name
|
2012-10-30 07:04:56 -04:00
|
|
|
}
|
|
|
|
|
2013-05-25 13:38:55 -04:00
|
|
|
# find group namespace
|
|
|
|
if group_name
|
2014-01-19 13:55:59 -05:00
|
|
|
group = Group.find_by(path: group_name)
|
2013-05-25 13:38:55 -04:00
|
|
|
# create group namespace
|
|
|
|
if !group
|
|
|
|
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
|
|
|
|
|
2014-01-16 13:35:21 -05:00
|
|
|
project = Projects::CreateService.new(user, project_params).execute
|
2012-10-30 07:04:56 -04:00
|
|
|
|
|
|
|
if project.valid?
|
2013-05-25 13:38:55 -04:00
|
|
|
puts " * Created #{project.name} (#{repo_path})".green
|
2012-10-30 07:04:56 -04:00
|
|
|
else
|
2013-05-25 13:38:55 -04:00
|
|
|
puts " * Failed trying to create #{project.name} (#{repo_path})".red
|
2012-10-30 07:04:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Done!".green
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|