Commit 5f9d78fdff746a0b50313cf415acac809569a186

Authored by James E. Flemer
1 parent d58aca06

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.
doc/raketasks/maintenance.md
@@ -116,6 +116,8 @@ bundle exec rake gitlab:satellites:create RAILS_ENV=production @@ -116,6 +116,8 @@ bundle exec rake gitlab:satellites:create RAILS_ENV=production
116 Notes: 116 Notes:
117 117
118 * project owner will be a first admin 118 * project owner will be a first admin
  119 +* groups will be created as needed
  120 +* group owner will be the first admin
119 * existing projects will be skipped 121 * existing projects will be skipped
120 122
121 How to use: 123 How to use:
@@ -132,5 +134,8 @@ Example output: @@ -132,5 +134,8 @@ Example output:
132 ``` 134 ```
133 Processing abcd.git 135 Processing abcd.git
134 * Created abcd (abcd.git) 136 * Created abcd (abcd.git)
  137 +Processing group/xyz.git
  138 + * Created Group group (2)
  139 + * Created xyz (group/xyz.git)
135 [...] 140 [...]
136 ``` 141 ```
lib/tasks/gitlab/import.rake
@@ -13,42 +13,61 @@ namespace :gitlab do @@ -13,42 +13,61 @@ namespace :gitlab do
13 task repos: :environment do 13 task repos: :environment do
14 14
15 git_base_path = Gitlab.config.gitlab_shell.repos_path 15 git_base_path = Gitlab.config.gitlab_shell.repos_path
16 - repos_to_import = Dir.glob(git_base_path + '/*') 16 + repos_to_import = Dir.glob(git_base_path + '/**/*.git')
17 17
18 namespaces = Namespace.pluck(:path) 18 namespaces = Namespace.pluck(:path)
19 19
20 repos_to_import.each do |repo_path| 20 repos_to_import.each do |repo_path|
21 - repo_name = File.basename repo_path 21 + # strip repo base path
  22 + repo_path[0..git_base_path.length] = ''
22 23
23 - # Skip if group or user  
24 - next if namespaces.include?(repo_name) 24 + path = repo_path.sub(/\.git$/, '')
  25 + name = File.basename path
  26 + group_name = File.dirname path
  27 + group_name = nil if group_name == '.'
25 28
26 - # skip if not git repo  
27 - next unless repo_name =~ /.git$/ 29 + # Skip if group or user
  30 + next if namespaces.include?(name)
28 31
29 - next if repo_name == 'gitolite-admin.git' 32 + next if name == 'gitolite-admin'
30 33
31 - path = repo_name.sub(/\.git$/, '') 34 + puts "Processing #{repo_path}".yellow
32 35
33 project = Project.find_with_namespace(path) 36 project = Project.find_with_namespace(path)
34 37
35 - puts "Processing #{repo_name}".yellow  
36 -  
37 if project 38 if project
38 - puts " * #{project.name} (#{repo_name}) exists" 39 + puts " * #{project.name} (#{repo_path}) exists"
39 else 40 else
40 user = User.admins.first 41 user = User.admins.first
41 42
42 project_params = { 43 project_params = {
43 - name: path, 44 + name: name,
44 } 45 }
45 46
  47 + # find group namespace
  48 + if group_name
  49 + group = Group.find_by_path(group_name)
  50 + # create group namespace
  51 + if !group
  52 + group = Group.new(:name => group_name)
  53 + group.path = group_name
  54 + group.owner = user
  55 + if group.save
  56 + puts " * Created Group #{group.name} (#{group.id})".green
  57 + else
  58 + puts " * Failed trying to create group #{group.name}".red
  59 + end
  60 + end
  61 + # set project group
  62 + project_params[:namespace_id] = group.id
  63 + end
  64 +
46 project = Projects::CreateContext.new(user, project_params).execute 65 project = Projects::CreateContext.new(user, project_params).execute
47 66
48 if project.valid? 67 if project.valid?
49 - puts " * Created #{project.name} (#{repo_name})".green 68 + puts " * Created #{project.name} (#{repo_path})".green
50 else 69 else
51 - puts " * Failed trying to create #{project.name} (#{repo_name})".red 70 + puts " * Failed trying to create #{project.name} (#{repo_path})".red
52 end 71 end
53 end 72 end
54 end 73 end