2020-05-25 05:08:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module AlertManagement
|
|
|
|
class Fingerprint
|
|
|
|
def self.generate(data)
|
|
|
|
new.generate(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate(data)
|
|
|
|
return unless data.present?
|
|
|
|
|
2020-06-25 11:08:37 -04:00
|
|
|
string = case data
|
|
|
|
when Array then flatten_array(data)
|
|
|
|
when Hash then flatten_hash(data)
|
|
|
|
else
|
|
|
|
data.to_s
|
|
|
|
end
|
2020-05-25 05:08:30 -04:00
|
|
|
|
2020-06-25 11:08:37 -04:00
|
|
|
Digest::SHA1.hexdigest(string)
|
2020-05-25 05:08:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def flatten_array(array)
|
|
|
|
array.flatten.map!(&:to_s).join
|
|
|
|
end
|
2020-06-25 11:08:37 -04:00
|
|
|
|
|
|
|
def flatten_hash(hash)
|
|
|
|
# Sort hash so SHA generated is the same
|
|
|
|
Gitlab::Utils::SafeInlineHash.merge_keys!(hash).sort.to_s
|
|
|
|
end
|
2020-05-25 05:08:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|