1# frozen_string_literal: true 2 3module Gitlab 4 module ImportExport 5 class AttributeCleaner 6 ALLOWED_REFERENCES = [ 7 *Gitlab::ImportExport::Project::RelationFactory::PROJECT_REFERENCES, 8 *Gitlab::ImportExport::Project::RelationFactory::USER_REFERENCES, 9 'group_id', 10 'commit_id', 11 'discussion_id', 12 'custom_attributes' 13 ].freeze 14 PROHIBITED_REFERENCES = Regexp.union( 15 /\Acached_markdown_version\Z/, 16 /_id\Z/, 17 /_ids\Z/, 18 /_html\Z/, 19 /attributes/, 20 /\Aremote_\w+_(url|urls|request_header)\Z/ # carrierwave automatically creates these attribute methods for uploads 21 ).freeze 22 23 def self.clean(*args, **kwargs) 24 new(*args, **kwargs).clean 25 end 26 27 def initialize(relation_hash:, relation_class:, excluded_keys: []) 28 @relation_hash = relation_hash 29 @relation_class = relation_class 30 @excluded_keys = excluded_keys 31 end 32 33 def clean 34 @relation_hash.reject do |key, _value| 35 prohibited_key?(key) || !@relation_class.attribute_method?(key) || excluded_key?(key) 36 end.except('id') 37 end 38 39 private 40 41 def prohibited_key?(key) 42 key =~ PROHIBITED_REFERENCES && !permitted_key?(key) 43 end 44 45 def permitted_key?(key) 46 ALLOWED_REFERENCES.include?(key) 47 end 48 49 def excluded_key?(key) 50 return false if @excluded_keys.empty? 51 52 @excluded_keys.include?(key) 53 end 54 end 55 end 56end 57