BigW Consortium Gitlab

import.rake 2.39 KB
Newer Older
1 2 3 4
namespace :gitlab do
  namespace :import do
    # How to use:
    #
5 6
    #  1. copy the bare repos under the repos_path (commonly /home/git/repositories)
    #  2. run: bundle exec rake gitlab:import:repos RAILS_ENV=production
7 8
    #
    # Notes:
9 10
    #  * The project owner will set to the first administator of the system
    #  * Existing projects will be skipped
11
    #
12
    desc "GitLab | Import bare repositories from gitlab_shell -> repos_path into GitLab project instance"
13
    task repos: :environment do
14

15
      git_base_path = Gitlab.config.gitlab_shell.repos_path
16
      repos_to_import = Dir.glob(git_base_path + '/**/*.git')
17 18

      repos_to_import.each do |repo_path|
19 20
        # strip repo base path
        repo_path[0..git_base_path.length] = ''
21

22
        path = repo_path.sub(/\.git$/, '')
23
        group_name, name = File.split(path)
24
        group_name = nil if group_name == '.'
25

26
        puts "Processing #{repo_path}".yellow
27

28
        if path.end_with?('.wiki')
29 30 31 32
          puts " * Skipping wiki repo"
          next
        end

33
        project = Project.find_with_namespace(path)
34 35

        if project
36
          puts " * #{project.name} (#{repo_path}) exists"
37
        else
38
          user = User.admins.reorder("id").first
39 40

          project_params = {
41
            name: name,
42
            path: name
43 44
          }

45 46
          # find group namespace
          if group_name
47
            group = Namespace.find_by(path: group_name)
48
            # create group namespace
49
            unless group
50 51 52 53 54 55 56 57 58 59 60 61 62
              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

63
          project = Projects::CreateService.new(user, project_params).execute
64

65
          if project.persisted?
66
            puts " * Created #{project.name} (#{repo_path})".green
67 68
            project.update_repository_size
            project.update_commit_count
69
          else
70
            puts " * Failed trying to create #{project.name} (#{repo_path})".red
71
            puts "   Errors: #{project.errors.messages}".red
72 73 74 75 76 77 78 79
          end
        end
      end

      puts "Done!".green
    end
  end
end