summaryrefslogtreecommitdiff
path: root/lib/gitlab/import_export/shared.rb
blob: 947caaaefee0bf8a98789c55399d5d910754e36c (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
# frozen_string_literal: true

module Gitlab
  module ImportExport
    class Shared
      attr_reader :errors, :project

      def initialize(project)
        @project = project
        @errors = []
        @logger = Gitlab::Import::Logger.build
      end

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

      def export_path
        @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)
      end

      def error(error)
        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)

        add_error_message(error.message)
      end

      def add_error_message(message)
        @errors << filtered_error_message(message)
      end

      def after_export_in_progress?
        File.exist?(after_export_lock_file)
      end

      private

      def relative_path
        File.join(relative_archive_path, SecureRandom.hex)
      end

      def relative_archive_path
        @project.disk_path
      end

      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',
          import_jid: @project&.import_state&.import_jid,
          project_id: @project&.id,
          project_path: @project&.full_path
        }
      end

      def filtered_error_message(message)
        Projects::ImportErrorFilter.filter_message(message)
      end

      def after_export_lock_file
        AfterExportStrategies::BaseAfterExportStrategy.lock_file_path(project)
      end
    end
  end
end