2018-11-17 00:37:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-12 10:04:33 +00:00
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class AttributeCleaner
|
2016-10-18 17:31:10 +00:00
|
|
|
ALLOWED_REFERENCES = RelationFactory::PROJECT_REFERENCES + RelationFactory::USER_REFERENCES + ['group_id']
|
2019-05-01 00:15:29 +00:00
|
|
|
PROHIBITED_REFERENCES = Regexp.union(/\Acached_markdown_version\Z/, /_id\Z/, /_html\Z/).freeze
|
2016-08-12 10:04:33 +00:00
|
|
|
|
2016-10-27 10:22:18 +00:00
|
|
|
def self.clean(*args)
|
|
|
|
new(*args).clean
|
|
|
|
end
|
|
|
|
|
2018-05-03 09:02:26 +00:00
|
|
|
def initialize(relation_hash:, relation_class:, excluded_keys: [])
|
2016-10-27 10:22:18 +00:00
|
|
|
@relation_hash = relation_hash
|
|
|
|
@relation_class = relation_class
|
2018-05-03 09:02:26 +00:00
|
|
|
@excluded_keys = excluded_keys
|
2016-10-27 10:22:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def clean
|
|
|
|
@relation_hash.reject do |key, _value|
|
2019-04-29 23:25:09 +00:00
|
|
|
prohibited_key?(key) || !@relation_class.attribute_method?(key) || excluded_key?(key)
|
2016-10-27 10:22:18 +00:00
|
|
|
end.except('id')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def prohibited_key?(key)
|
2019-05-01 00:15:29 +00:00
|
|
|
key =~ PROHIBITED_REFERENCES && !permitted_key?(key)
|
2019-04-24 02:31:20 +00:00
|
|
|
end
|
|
|
|
|
2019-04-29 09:31:16 +00:00
|
|
|
def permitted_key?(key)
|
2019-04-24 02:31:20 +00:00
|
|
|
ALLOWED_REFERENCES.include?(key)
|
2016-08-12 10:04:33 +00:00
|
|
|
end
|
2018-05-03 09:02:26 +00:00
|
|
|
|
|
|
|
def excluded_key?(key)
|
|
|
|
return false if @excluded_keys.empty?
|
|
|
|
|
|
|
|
@excluded_keys.include?(key)
|
|
|
|
end
|
2016-08-12 10:04:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|