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

45 lines
1,016 B
Ruby
Raw Normal View History

# frozen_string_literal: true
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,
body: body(**options)
2017-04-04 11:10:21 -04:00
)
result = true if response
rescue Gitlab::HTTP::Error, StandardError => error
Gitlab::AppLogger.info("#{self.class.name}: Error while connecting to #{@webhook}: #{error.message}")
2017-04-04 11:10:21 -04:00
end
result
end
private
def body(title: nil, summary: nil, attachments: nil, activity:)
result = { 'sections' => [] }
result['title'] = title
result['summary'] = summary
result['sections'] << ::MicrosoftTeams::Activity.new(**activity).prepare
2017-04-03 13:16:24 -04:00
2017-04-04 11:10:21 -04:00
unless attachments.blank?
result['sections'] << { text: attachments }
2017-04-04 11:10:21 -04:00
end
result.to_json
end
end
end