2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-12 06:04:33 -04:00
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class AttributeCleaner
|
2020-01-17 13:08:41 -05:00
|
|
|
ALLOWED_REFERENCES = [
|
2020-02-26 13:09:24 -05:00
|
|
|
*Gitlab::ImportExport::Project::RelationFactory::PROJECT_REFERENCES,
|
|
|
|
*Gitlab::ImportExport::Project::RelationFactory::USER_REFERENCES,
|
2020-01-17 13:08:41 -05:00
|
|
|
'group_id',
|
|
|
|
'commit_id',
|
|
|
|
'discussion_id',
|
|
|
|
'custom_attributes'
|
|
|
|
].freeze
|
2020-03-26 14:08:03 -04:00
|
|
|
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-08-12 06:04:33 -04:00
|
|
|
|
2020-10-15 14:08:43 -04:00
|
|
|
def self.clean(*args, **kwargs)
|
|
|
|
new(*args, **kwargs).clean
|
2016-10-27 06:22:18 -04:00
|
|
|
end
|
|
|
|
|
2018-05-03 05:02:26 -04:00
|
|
|
def initialize(relation_hash:, relation_class:, excluded_keys: [])
|
2016-10-27 06:22:18 -04:00
|
|
|
@relation_hash = relation_hash
|
|
|
|
@relation_class = relation_class
|
2018-05-03 05:02:26 -04:00
|
|
|
@excluded_keys = excluded_keys
|
2016-10-27 06:22:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def clean
|
|
|
|
@relation_hash.reject do |key, _value|
|
2019-04-29 19:25:09 -04:00
|
|
|
prohibited_key?(key) || !@relation_class.attribute_method?(key) || excluded_key?(key)
|
2016-10-27 06:22:18 -04:00
|
|
|
end.except('id')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def prohibited_key?(key)
|
2019-04-30 20:15:29 -04:00
|
|
|
key =~ PROHIBITED_REFERENCES && !permitted_key?(key)
|
2019-04-23 22:31:20 -04:00
|
|
|
end
|
|
|
|
|
2019-04-29 05:31:16 -04:00
|
|
|
def permitted_key?(key)
|
2019-04-23 22:31:20 -04:00
|
|
|
ALLOWED_REFERENCES.include?(key)
|
2016-08-12 06:04:33 -04:00
|
|
|
end
|
2018-05-03 05:02:26 -04:00
|
|
|
|
|
|
|
def excluded_key?(key)
|
|
|
|
return false if @excluded_keys.empty?
|
|
|
|
|
|
|
|
@excluded_keys.include?(key)
|
|
|
|
end
|
2016-08-12 06:04:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|