debian-mirror-gitlab/lib/microsoft_teams/notifier.rb

48 lines
1.1 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
module MicrosoftTeams
class Notifier
def initialize(webhook)
@webhook = webhook
@header = { 'Content-type' => 'application/json' }
end
def ping(options = {})
result = false
begin
2018-03-26 14:24:53 +05:30
response = Gitlab::HTTP.post(
2017-08-17 22:00:37 +05:30
@webhook.to_str,
headers: @header,
2018-03-26 14:24:53 +05:30
allow_local_requests: true,
2017-08-17 22:00:37 +05:30
body: body(options)
)
result = true if response
2018-03-26 14:24:53 +05:30
rescue Gitlab::HTTP::Error, StandardError => error
2017-08-17 22:00:37 +05:30
Rails.logger.info("#{self.class.name}: Error while connecting to #{@webhook}: #{error.message}")
end
result
end
private
def body(options = {})
result = { 'sections' => [] }
result['title'] = options[:title]
2018-11-08 19:23:39 +05:30
result['summary'] = options[:summary]
2017-08-17 22:00:37 +05:30
result['sections'] << MicrosoftTeams::Activity.new(options[:activity]).prepare
attachments = options[:attachments]
unless attachments.blank?
result['sections'] << {
'title' => 'Details',
'facts' => [{ 'name' => 'Attachments', 'value' => attachments }]
}
end
result.to_json
end
end
end