summaryrefslogtreecommitdiff
path: root/lib/gitlab/bare_repository_importer.rb
blob: 9323bfc7fb23139b827d6964a86800824a2339d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
module Gitlab
  class BareRepositoryImporter
    NoAdminError = Class.new(StandardError)

    def self.execute
      Gitlab.config.repositories.storages.each do |storage_name, repository_storage|
        git_base_path = repository_storage['path']
        repos_to_import = Dir.glob(git_base_path + '/**/*.git')

        repos_to_import.each do |repo_path|
          if repo_path.end_with?('.wiki.git')
            log " * Skipping wiki repo"
            next
          end

          log "Processing #{repo_path}".color(:yellow)

          repo_relative_path = repo_path[repository_storage['path'].length..-1]
                                 .sub(/^\//, '') # Remove leading `/`
                                 .sub(/\.git$/, '') # Remove `.git` at the end
          new(storage_name, repo_relative_path).create_project_if_needed
        end
      end
    end

    attr_reader :storage_name, :full_path, :group_path, :project_path, :user
    delegate :log, to: :class

    def initialize(storage_name, repo_path)
      @storage_name = storage_name
      @full_path = repo_path

      unless @user = User.admins.order_id_asc.first
        raise NoAdminError.new('No admin user found to import repositories')
      end

      @group_path, @project_path = File.split(repo_path)
      @group_path = nil if @group_path == '.'
    end

    def create_project_if_needed
      if project = Project.find_by_full_path(full_path)
        log " * #{project.name} (#{full_path}) exists"
        return project
      end

      create_project
    end

    private

    def create_project
      group = find_or_create_group

      project_params = {
        name: project_path,
        path: project_path,
        repository_storage: storage_name,
        namespace_id: group&.id
      }

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

      if project.persisted?
        log " * Created #{project.name} (#{full_path})".color(:green)
        ProjectCacheWorker.perform_async(project.id)
      else
        log " * Failed trying to create #{project.name} (#{full_path})".color(:red)
        log "   Errors: #{project.errors.messages}".color(:red)
      end

      project
    end

    def find_or_create_group
      return nil unless group_path

      if namespace = Namespace.find_by_full_path(group_path)
        log " * Namespace #{group_path} exists.".color(:green)
        return namespace
      end

      log " * Creating Group: #{group_path}"
      Groups::NestedCreateService.new(user, group_path: group_path).execute
    end

    # This is called from within a rake task only used by Admins, so allow writing
    # to STDOUT
    #
    # rubocop:disable Rails/Output
    def self.log(message)
      puts message
    end
    # rubocop:enable Rails/Output
  end
end