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

46 lines
1 KiB
Ruby
Raw Normal View History

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
2019-09-30 21:07:59 +05:30
Rails.logger.info("#{self.class.name}: Error while connecting to #{@webhook}: #{error.message}") # rubocop:disable Gitlab/RailsLogger
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-09 13:42:32 +05:30
result['sections'] << { text: attachments }
2017-08-17 22:00:37 +05:30
end
result.to_json
end
end
end