2014-09-02 18:07:02 +05:30
|
|
|
namespace :gitlab do
|
|
|
|
namespace :import do
|
|
|
|
# How to use:
|
|
|
|
#
|
2016-08-24 12:49:21 +05:30
|
|
|
# 1. copy the bare repos under the repository storage paths (commonly the default path is /home/git/repositories)
|
2014-09-02 18:07:02 +05:30
|
|
|
# 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
|
|
|
|
#
|
2016-08-24 12:49:21 +05:30
|
|
|
desc "GitLab | Import bare repositories from repositories -> storages into GitLab project instance"
|
2014-09-02 18:07:02 +05:30
|
|
|
task repos: :environment do
|
2017-08-17 22:00:37 +05:30
|
|
|
Gitlab.config.repositories.storages.each_value do |repository_storage|
|
|
|
|
git_base_path = repository_storage['path']
|
2016-08-24 12:49:21 +05:30
|
|
|
repos_to_import = Dir.glob(git_base_path + '/**/*.git')
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
repos_to_import.each do |repo_path|
|
|
|
|
# strip repo base path
|
|
|
|
repo_path[0..git_base_path.length] = ''
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
path = repo_path.sub(/\.git$/, '')
|
|
|
|
group_name, name = File.split(path)
|
|
|
|
group_name = nil if group_name == '.'
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
puts "Processing #{repo_path}".color(:yellow)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
if path.end_with?('.wiki')
|
|
|
|
puts " * Skipping wiki repo"
|
|
|
|
next
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
project = Project.find_by_full_path(path)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
if project
|
|
|
|
puts " * #{project.name} (#{repo_path}) exists"
|
|
|
|
else
|
|
|
|
user = User.admins.reorder("id").first
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
project_params = {
|
|
|
|
name: name,
|
|
|
|
path: name
|
|
|
|
}
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
# find group namespace
|
|
|
|
if group_name
|
|
|
|
group = Namespace.find_by(path: group_name)
|
|
|
|
# create group namespace
|
|
|
|
unless group
|
2017-08-17 22:00:37 +05:30
|
|
|
group = Group.new(name: group_name)
|
2016-08-24 12:49:21 +05:30
|
|
|
group.path = group_name
|
|
|
|
group.owner = user
|
|
|
|
if group.save
|
|
|
|
puts " * Created Group #{group.name} (#{group.id})".color(:green)
|
|
|
|
else
|
|
|
|
puts " * Failed trying to create group #{group.name}".color(:red)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
# set project group
|
|
|
|
project_params[:namespace_id] = group.id
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
project = Projects::CreateService.new(user, project_params).execute
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
if project.persisted?
|
|
|
|
puts " * Created #{project.name} (#{repo_path})".color(:green)
|
2017-08-17 22:00:37 +05:30
|
|
|
ProjectCacheWorker.perform_async(project.id)
|
2016-08-24 12:49:21 +05:30
|
|
|
else
|
|
|
|
puts " * Failed trying to create #{project.name} (#{repo_path})".color(:red)
|
|
|
|
puts " Errors: #{project.errors.messages}".color(:red)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
puts "Done!".color(:green)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|