From 5f9d78fdff746a0b50313cf415acac809569a186 Mon Sep 17 00:00:00 2001 From: "James E. Flemer" Date: Sat, 25 May 2013 11:38:55 -0600 Subject: [PATCH] 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 | 5 ++++ lib/tasks/gitlab/import.rake | 47 +++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/doc/raketasks/maintenance.md b/doc/raketasks/maintenance.md index 13f238df3e9..68c1a72b230 100644 --- a/doc/raketasks/maintenance.md +++ b/doc/raketasks/maintenance.md @@ -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) [...] ``` diff --git a/lib/tasks/gitlab/import.rake b/lib/tasks/gitlab/import.rake index acf6abedd19..c11284e3d78 100644 --- a/lib/tasks/gitlab/import.rake +++ b/lib/tasks/gitlab/import.rake @@ -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