shared.rb 1.89 KB
Newer Older
1
2
# frozen_string_literal: true

3
module Gitlab
4
5
  module ImportExport
    class Shared
6
      attr_reader :errors, :project
7

8
      def initialize(project)
9
        @project = project
10
        @errors = []
11
        @logger = Gitlab::Import::Logger.build
12
13
      end

14
15
16
17
      def active_export_count
        Dir[File.join(archive_path, '*')].count { |name| File.directory?(name) }
      end

18
      def export_path
19
20
21
22
23
        @export_path ||= Gitlab::ImportExport.export_path(relative_path: relative_path)
      end

      def archive_path
        @archive_path ||= Gitlab::ImportExport.export_path(relative_path: relative_archive_path)
24
      end
25

26
      def error(error)
27
28
29
30
        log_error(message: error.message, caller: caller[0].dup)
        log_debug(backtrace: error.backtrace&.join("\n"))

        Gitlab::Sentry.track_acceptable_exception(error, extra: log_base_data)
James Lopez's avatar
James Lopez committed
31

32
        add_error_message(error.message)
33
34
      end

35
36
      def add_error_message(message)
        @errors << filtered_error_message(message)
37
38
39
40
41
42
      end

      def after_export_in_progress?
        File.exist?(after_export_lock_file)
      end

43
44
      private

45
      def relative_path
46
        File.join(relative_archive_path, SecureRandom.hex)
47
48
49
      end

      def relative_archive_path
50
        @project.disk_path
51
52
      end

53
54
55
56
57
58
59
60
61
62
63
      def log_error(details)
        @logger.error(log_base_data.merge(details))
      end

      def log_debug(details)
        @logger.debug(log_base_data.merge(details))
      end

      def log_base_data
        {
          importer: 'Import/Export',
64
          import_jid: @project&.import_state&.jid,
65
66
67
68
69
70
71
          project_id: @project&.id,
          project_path: @project&.full_path
        }
      end

      def filtered_error_message(message)
        Projects::ImportErrorFilter.filter_message(message)
72
      end
73
74
75
76

      def after_export_lock_file
        AfterExportStrategies::BaseAfterExportStrategy.lock_file_path(project)
      end
77
78
    end
  end
James Lopez's avatar
James Lopez committed
79
end