debian-mirror-gitlab/lib/gitlab/import_export/attribute_cleaner.rb

43 lines
1.2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
module Gitlab
module ImportExport
class AttributeCleaner
2020-01-14 00:54:23 +05:30
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
2016-11-03 12:29:30 +05:30
def self.clean(*args)
new(*args).clean
end
2018-06-03 19:52:53 +05:30
def initialize(relation_hash:, relation_class:, excluded_keys: [])
2016-11-03 12:29:30 +05:30
@relation_hash = relation_hash
@relation_class = relation_class
2018-06-03 19:52:53 +05:30
@excluded_keys = excluded_keys
2016-11-03 12:29:30 +05:30
end
def clean
@relation_hash.reject do |key, _value|
2018-06-03 19:52:53 +05:30
prohibited_key?(key) || !@relation_class.attribute_method?(key) || excluded_key?(key)
2016-11-03 12:29:30 +05:30
end.except('id')
end
private
def prohibited_key?(key)
2019-06-05 12:25:43 +05:30
key =~ PROHIBITED_REFERENCES && !permitted_key?(key)
end
def permitted_key?(key)
ALLOWED_REFERENCES.include?(key)
2016-11-03 12:29:30 +05:30
end
2018-06-03 19:52:53 +05:30
def excluded_key?(key)
return false if @excluded_keys.empty?
@excluded_keys.include?(key)
end
2016-11-03 12:29:30 +05:30
end
end
end