Welcome to mirror list, hosted at ThFree Co, Russian Federation.

relation_export_service.rb « import_export « projects « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 33da5b39c202c6e063d23c7af0bfc74aba245057 (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
# frozen_string_literal: true

module Projects
  module ImportExport
    class RelationExportService
      include Gitlab::ImportExport::CommandLineUtil

      def initialize(relation_export, jid)
        @relation_export = relation_export
        @jid = jid
        @logger = Gitlab::Export::Logger.build
      end

      def execute
        relation_export.update!(status_event: :start, jid: jid)

        mkdir_p(shared.export_path)
        mkdir_p(shared.archive_path)

        if relation_saver.save
          compress_export_path
          upload_compressed_file
          relation_export.finish!
        else
          fail_export(shared.errors.join(', '))
        end
      rescue StandardError => e
        fail_export(e.message)
      ensure
        FileUtils.remove_entry(shared.export_path) if File.exist?(shared.export_path)
        FileUtils.remove_entry(shared.archive_path) if File.exist?(shared.archive_path)
      end

      private

      attr_reader :relation_export, :jid, :logger

      delegate :relation, :project_export_job, to: :relation_export
      delegate :project, to: :project_export_job

      def shared
        project.import_export_shared
      end

      def relation_saver
        case relation
        when Projects::ImportExport::RelationExport::UPLOADS_RELATION
          Gitlab::ImportExport::UploadsSaver.new(project: project, shared: shared)
        when Projects::ImportExport::RelationExport::REPOSITORY_RELATION
          Gitlab::ImportExport::RepoSaver.new(exportable: project, shared: shared)
        when Projects::ImportExport::RelationExport::WIKI_REPOSITORY_RELATION
          Gitlab::ImportExport::WikiRepoSaver.new(exportable: project, shared: shared)
        when Projects::ImportExport::RelationExport::LFS_OBJECTS_RELATION
          Gitlab::ImportExport::LfsSaver.new(project: project, shared: shared)
        when Projects::ImportExport::RelationExport::SNIPPETS_REPOSITORY_RELATION
          Gitlab::ImportExport::SnippetsRepoSaver.new(project: project, shared: shared, current_user: nil)
        when Projects::ImportExport::RelationExport::DESIGN_REPOSITORY_RELATION
          Gitlab::ImportExport::DesignRepoSaver.new(exportable: project, shared: shared)
        else
          Gitlab::ImportExport::Project::RelationSaver.new(
            project: project,
            shared: shared,
            relation: relation
          )
        end
      end

      def upload_compressed_file
        upload = relation_export.build_upload
        File.open(archive_file_full_path) { |file| upload.export_file = file }
        upload.save!
      end

      def compress_export_path
        tar_czf(archive: archive_file_full_path, dir: shared.export_path)
      end

      def archive_file_full_path
        @archive_file ||= File.join(shared.archive_path, "#{relation}.tar.gz")
      end

      def fail_export(error_message)
        relation_export.update!(status_event: :fail_op, export_error: error_message.truncate(300))

        logger.error(
          message: 'Project relation export failed',
          export_error: error_message,
          relation: relation_export.relation,
          project_export_job_id: project_export_job.id,
          project_name: project.name,
          project_id: project.id
        )
      end
    end
  end
end