debian-mirror-gitlab/lib/gitlab/alert_management/fingerprint.rb

36 lines
745 B
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# 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-07-28 23:09:34 +05:30
string = case data
when Array then flatten_array(data)
when Hash then flatten_hash(data)
else
data.to_s
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
Digest::SHA1.hexdigest(string)
2020-06-23 00:09:42 +05:30
end
private
def flatten_array(array)
array.flatten.map!(&:to_s).join
end
2020-07-28 23:09:34 +05:30
def flatten_hash(hash)
# Sort hash so SHA generated is the same
Gitlab::Utils::SafeInlineHash.merge_keys!(hash).sort.to_s
end
2020-06-23 00:09:42 +05:30
end
end
end