Include groups in import with import repos rake task

Expand the import glob to include `**/*.git` to find projects in groups
as well as top level projects.  Check for existing group, and create
group if needed.  Set namespace_id for imported projects.
This commit is contained in:
James E. Flemer 2013-05-25 11:38:55 -06:00
parent d58aca0693
commit 5f9d78fdff
2 changed files with 38 additions and 14 deletions

View file

@ -116,6 +116,8 @@ bundle exec rake gitlab:satellites:create RAILS_ENV=production
Notes:
* project owner will be a first admin
* groups will be created as needed
* group owner will be the first admin
* existing projects will be skipped
How to use:
@ -132,5 +134,8 @@ Example output:
```
Processing abcd.git
* Created abcd (abcd.git)
Processing group/xyz.git
* Created Group group (2)
* Created xyz (group/xyz.git)
[...]
```

View file

@ -13,42 +13,61 @@ namespace :gitlab do
task repos: :environment do
git_base_path = Gitlab.config.gitlab_shell.repos_path
repos_to_import = Dir.glob(git_base_path + '/*')
repos_to_import = Dir.glob(git_base_path + '/**/*.git')
namespaces = Namespace.pluck(:path)
repos_to_import.each do |repo_path|
repo_name = File.basename repo_path
# strip repo base path
repo_path[0..git_base_path.length] = ''
path = repo_path.sub(/\.git$/, '')
name = File.basename path
group_name = File.dirname path
group_name = nil if group_name == '.'
# Skip if group or user
next if namespaces.include?(repo_name)
next if namespaces.include?(name)
# skip if not git repo
next unless repo_name =~ /.git$/
next if name == 'gitolite-admin'
next if repo_name == 'gitolite-admin.git'
path = repo_name.sub(/\.git$/, '')
puts "Processing #{repo_path}".yellow
project = Project.find_with_namespace(path)
puts "Processing #{repo_name}".yellow
if project
puts " * #{project.name} (#{repo_name}) exists"
puts " * #{project.name} (#{repo_path}) exists"
else
user = User.admins.first
project_params = {
name: path,
name: name,
}
# find group namespace
if group_name
group = Group.find_by_path(group_name)
# 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
project = Projects::CreateContext.new(user, project_params).execute
if project.valid?
puts " * Created #{project.name} (#{repo_name})".green
puts " * Created #{project.name} (#{repo_path})".green
else
puts " * Failed trying to create #{project.name} (#{repo_name})".red
puts " * Failed trying to create #{project.name} (#{repo_path})".red
end
end
end