attribute_cleaner.rb 1.15 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
module Gitlab
  module ImportExport
    class AttributeCleaner
6
7
      ALLOWED_REFERENCES = RelationFactory::PROJECT_REFERENCES + RelationFactory::USER_REFERENCES + %w[group_id commit_id discussion_id custom_attributes]
      PROHIBITED_REFERENCES = Regexp.union(/\Acached_markdown_version\Z/, /_id\Z/, /_ids\Z/, /_html\Z/, /attributes/).freeze
8

9
10
11
12
      def self.clean(*args)
        new(*args).clean
      end

13
      def initialize(relation_hash:, relation_class:, excluded_keys: [])
14
15
        @relation_hash = relation_hash
        @relation_class = relation_class
16
        @excluded_keys = excluded_keys
17
18
19
20
      end

      def clean
        @relation_hash.reject do |key, _value|
21
          prohibited_key?(key) || !@relation_class.attribute_method?(key) || excluded_key?(key)
22
23
24
25
26
27
        end.except('id')
      end

      private

      def prohibited_key?(key)
28
        key =~ PROHIBITED_REFERENCES && !permitted_key?(key)
29
30
      end

31
      def permitted_key?(key)
32
        ALLOWED_REFERENCES.include?(key)
33
      end
34
35
36
37
38
39

      def excluded_key?(key)
        return false if @excluded_keys.empty?

        @excluded_keys.include?(key)
      end
40
41
42
    end
  end
end