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

57 lines
1.5 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 = [
2020-04-08 14:13:33 +05:30
*Gitlab::ImportExport::Project::RelationFactory::PROJECT_REFERENCES,
*Gitlab::ImportExport::Project::RelationFactory::USER_REFERENCES,
2020-03-13 15:44:24 +05:30
'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
2021-01-03 14:25:43 +05:30
def self.clean(*args, **kwargs)
new(*args, **kwargs).clean
2016-11-03 12:29:30 +05:30
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