enable_namespaces.rake 2.75 KB
Newer Older
1 2
namespace :gitlab do
  desc "GITLAB | Enable usernames and namespaces for user projects"
3
  task enable_namespaces: :environment do
4 5
    warn_user_is_not_gitlab

6 7 8
    migrate_user_namespaces
    migrate_groups
    migrate_projects
9

10 11 12 13 14 15 16 17
    puts "Rebuild Gitolite ... "
    gitolite = Gitlab::Gitolite.new
    gitolite.update_repositories(Project.where('namespace_id IS NOT NULL'))
    puts "... #{"done".green}"
  end

  def migrate_user_namespaces
    puts "\nGenerate usernames for users without one: ".blue
18
    User.find_each(batch_size: 500) do |user|
19 20 21 22
      if user.namespace
        print '-'.cyan
        next
      end
23

24 25 26 27 28 29 30 31 32 33
      username = if user.username.present?
                   # if user already has username filled
                   user.username
                 else
                   build_username(user)
                 end

      begin
        User.transaction do
          user.update_attributes!(username: username)
34 35
          print '.'.green
        end
36 37
      rescue
        print 'F'.red
38 39
      end
    end
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    puts "\nDone"
  end

  def build_username(user)
    username = nil

    # generate username
    username = user.email.match(/^[^@]*/)[0]
    username.gsub!("+", ".")

    # return username if no mathes
    return username unless User.find_by_username(username)

    # look for same username
    (1..10).each do |i|
      suffixed_username = "#{username}#{i}"

      return suffixed_username unless User.find_by_username(suffixed_username)
    end
  end
60

61 62
  def migrate_groups
    puts "\nCreate directories for groups: ".blue
63

64
    Group.find_each(batch_size: 500) do |group|
65 66 67 68 69 70 71 72 73 74 75
      begin
        if group.dir_exists?
          print '-'.cyan
        else
          if group.ensure_dir_exist
            print '.'.green
          else
            print 'F'.red
          end
        end
      rescue
76 77 78
        print 'F'.red
      end
    end
79 80
    puts "\nDone"
  end
81

82
  def migrate_projects
83
    git_path = Gitlab.config.gitolite.repos_path
84
    puts "\nMove projects in groups into respective directories ... ".blue
85 86 87 88 89
    Project.where('namespace_id IS NOT NULL').find_each(batch_size: 500) do |project|
      next unless project.group

      group = project.group

90
      print "#{project.name_with_namespace.yellow} ... "
91 92 93 94

      new_path = File.join(git_path, project.path_with_namespace + '.git')

      if File.exists?(new_path)
95
        puts "already at #{new_path}".green
96 97 98 99
        next
      end

      old_path = File.join(git_path, project.path + '.git')
100

101
      unless File.exists?(old_path)
102
        puts "couldn't find it at #{old_path}".red
103 104
        next
      end
105 106 107

      begin
        Gitlab::ProjectMover.new(project, '', group.path).execute
108
        puts "moved to #{new_path}".green
109
      rescue
110
        puts "failed moving to #{new_path}".red
111 112
      end
    end
113

114
    puts "\nDone"
115 116
  end
end