importer.rb 2.3 KB
Newer Older
1 2 3
module Gitlab
  module ImportExport
    class Importer
4 5 6
      def initialize(project)
        @archive_file = project.import_source
        @current_user = project.creator
7 8
        @project = project
        @shared = Gitlab::ImportExport::Shared.new(relative_path: path_with_namespace)
9 10
      end

11
      def execute
12
        if import_file && check_version! && [project_tree, repo_restorer, wiki_restorer, uploads_restorer].all?(&:restore)
13
          project_tree.restored_project
14
        else
15
          raise Projects::ImportService::Error.new(@shared.errors.join(', '))
16
        end
17 18

        remove_import_file
19 20 21 22
      end

      private

23 24 25 26 27
      def import_file
        Gitlab::ImportExport::FileImporter.import(archive_file: @archive_file,
                                                  shared: @shared)
      end

28 29 30 31 32 33 34 35 36 37 38 39 40
      def check_version!
        Gitlab::ImportExport::VersionChecker.check!(shared: @shared)
      end

      def project_tree
        @project_tree ||= Gitlab::ImportExport::ProjectTreeRestorer.new(user: @current_user,
                                                                        shared: @shared,
                                                                        project: @project)
      end

      def repo_restorer
        Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path,
                                               shared: @shared,
41
                                               project: project_tree.restored_project)
42 43 44 45 46
      end

      def wiki_restorer
        Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: wiki_repo_path,
                                               shared: @shared,
47
                                               project: ProjectWiki.new(project_tree.restored_project),
48 49 50 51
                                               wiki: true)
      end

      def uploads_restorer
52
        Gitlab::ImportExport::UploadsRestorer.new(project: project_tree.restored_project, shared: @shared)
53 54
      end

55 56
      def path_with_namespace
        File.join(@project.namespace.path, @project.path)
57 58 59 60 61 62 63 64 65
      end

      def repo_path
        File.join(@shared.export_path, 'project.bundle')
      end

      def wiki_repo_path
        File.join(@shared.export_path, 'project.wiki.bundle')
      end
66 67 68 69

      def remove_import_file
        FileUtils.rm_rf(@archive_file)
      end
70 71 72
    end
  end
end