gitlab-org--gitlab-foss/lib/microsoft_teams/notifier.rb

48 lines
1.1 KiB
Ruby
Raw Normal View History

module MicrosoftTeams
class Notifier
def initialize(webhook)
@webhook = webhook
2017-04-04 11:10:21 -04:00
@header = { 'Content-type' => 'application/json' }
end
def ping(options = {})
2017-04-04 11:10:21 -04:00
result = false
begin
response = Gitlab::HTTP.post(
2017-04-04 11:10:21 -04:00
@webhook.to_str,
headers: @header,
allow_local_requests: true,
2017-04-04 11:10:21 -04:00
body: body(options)
)
result = true if response
rescue Gitlab::HTTP::Error, StandardError => error
2017-04-04 11:10:21 -04:00
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]
result['summary'] = options[:pretext]
2017-04-04 11:10:21 -04:00
result['sections'] << MicrosoftTeams::Activity.new(options[:activity]).prepare
2017-04-03 13:16:24 -04:00
2017-04-04 11:10:21 -04:00
attachments = options[:attachments]
unless attachments.blank?
result['sections'] << {
'title' => 'Details',
'facts' => [{ 'name' => 'Attachments', 'value' => attachments }]
}
end
result.to_json
end
end
end