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

45 lines
1,016 B
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,
2021-02-22 17:27:13 +05:30
body: body(**options)
2017-08-17 22:00:37 +05:30
)
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
2021-02-22 17:27:13 +05:30
def body(title: nil, summary: nil, attachments: nil, activity:)
2017-08-17 22:00:37 +05:30
result = { 'sections' => [] }
2021-02-22 17:27:13 +05:30
result['title'] = title
result['summary'] = summary
2021-09-04 01:27:46 +05:30
result['sections'] << ::MicrosoftTeams::Activity.new(**activity).prepare
2017-08-17 22:00:37 +05:30
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