2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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,
|
|
|
|
body: body(options)
|
|
|
|
)
|
|
|
|
|
|
|
|
result = true if response
|
2018-03-26 14:24:53 +05:30
|
|
|
rescue Gitlab::HTTP::Error, StandardError => error
|
2020-11-24 15:15:51 +05:30
|
|
|
Gitlab::AppLogger.info("#{self.class.name}: Error while connecting to #{@webhook}: #{error.message}")
|
2017-08-17 22:00:37 +05:30
|
|
|
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?
|
2020-03-13 15:44:24 +05:30
|
|
|
result['sections'] << { text: attachments }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
result.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|