2018-10-08 10:50:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-03 11:37:23 -04:00
|
|
|
module MicrosoftTeams
|
|
|
|
class Notifier
|
|
|
|
def initialize(webhook)
|
|
|
|
@webhook = webhook
|
2017-04-04 11:10:21 -04:00
|
|
|
@header = { 'Content-type' => 'application/json' }
|
2017-04-03 11:37:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ping(options = {})
|
2017-04-04 11:10:21 -04:00
|
|
|
result = false
|
|
|
|
|
|
|
|
begin
|
2018-03-13 18:38:25 -04:00
|
|
|
response = Gitlab::HTTP.post(
|
2017-04-04 11:10:21 -04:00
|
|
|
@webhook.to_str,
|
|
|
|
headers: @header,
|
2018-03-13 18:38:25 -04:00
|
|
|
allow_local_requests: true,
|
2017-04-04 11:10:21 -04:00
|
|
|
body: body(options)
|
|
|
|
)
|
|
|
|
|
|
|
|
result = true if response
|
2018-03-13 18:38:25 -04:00
|
|
|
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
|
2017-04-03 11:37:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def body(options = {})
|
|
|
|
result = { 'sections' => [] }
|
|
|
|
|
2017-04-04 05:16:24 -04:00
|
|
|
result['title'] = options[:title]
|
2018-06-11 04:28:30 -04:00
|
|
|
result['summary'] = options[:summary]
|
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
|
2017-04-03 11:37:23 -04:00
|
|
|
|
|
|
|
result.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|