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

57 lines
1.4 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-03-13 15:44:24 +05:30
ALLOWED_REFERENCES = [
*ProjectRelationFactory::PROJECT_REFERENCES,
*ProjectRelationFactory::USER_REFERENCES,
'group_id',
'commit_id',
'discussion_id',
'custom_attributes'
].freeze
2020-03-28 13:19:24 +05:30
PROHIBITED_REFERENCES = Regexp.union(
/\Acached_markdown_version\Z/,
/_id\Z/,
/_ids\Z/,
/_html\Z/,
/attributes/,
/\Aremote_\w+_(url|urls|request_header)\Z/ # carrierwave automatically creates these attribute methods for uploads
).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