2020-05-25 09:08:30 +00: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 15:08:37 +00:00
|
|
|
string = case data
|
|
|
|
when Array then flatten_array(data)
|
|
|
|
when Hash then flatten_hash(data)
|
|
|
|
else
|
|
|
|
data.to_s
|
|
|
|
end
|
2020-05-25 09:08:30 +00:00
|
|
|
|
2020-06-25 15:08:37 +00:00
|
|
|
Digest::SHA1.hexdigest(string)
|
2020-05-25 09:08:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def flatten_array(array)
|
|
|
|
array.flatten.map!(&:to_s).join
|
|
|
|
end
|
2020-06-25 15:08:37 +00: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 09:08:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|