diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/services.rb | 9 | ||||
-rw-r--r-- | lib/api/v3/services.rb | 6 | ||||
-rw-r--r-- | lib/microsoft_teams/activity.rb | 6 | ||||
-rw-r--r-- | lib/microsoft_teams/notifier.rb | 35 |
4 files changed, 42 insertions, 14 deletions
diff --git a/lib/api/services.rb b/lib/api/services.rb index 6802a99311e..65f86caaa51 100644 --- a/lib/api/services.rb +++ b/lib/api/services.rb @@ -488,6 +488,14 @@ module API desc: 'The channel name' } ], + 'microsoft-teams' => [ + { + required: true, + name: :webhook, + type: String, + desc: 'The Microsoft Teams webhook. e.g. https://outlook.office.com/webhook/…' + } + ], 'mattermost' => [ { required: true, @@ -550,6 +558,7 @@ module API RedmineService, SlackService, MattermostService, + MicrosoftTeamsService, TeamcityService, ] diff --git a/lib/api/v3/services.rb b/lib/api/v3/services.rb index 3bacaeee032..bbe07ed4212 100644 --- a/lib/api/v3/services.rb +++ b/lib/api/v3/services.rb @@ -501,6 +501,12 @@ module API desc: 'The channel name' } ], + 'microsoft-teams' => [ + required: true, + name: :webhook, + type: String, + desc: 'The Microsoft Teams webhook. e.g. https://outlook.office.com/webhook/…' + ], 'mattermost' => [ { required: true, diff --git a/lib/microsoft_teams/activity.rb b/lib/microsoft_teams/activity.rb index e25ce262cbf..d2c420efdaf 100644 --- a/lib/microsoft_teams/activity.rb +++ b/lib/microsoft_teams/activity.rb @@ -1,19 +1,19 @@ module MicrosoftTeams class Activity - def initialize(title, subtitle, text, image) + def initialize(title:, subtitle:, text:, image:) @title = title @subtitle = subtitle @text = text @image = image end - def to_json + def prepare { 'activityTitle' => @title, 'activitySubtitle' => @subtitle, 'activityText' => @text, 'activityImage' => @image - }.to_json + } end end end diff --git a/lib/microsoft_teams/notifier.rb b/lib/microsoft_teams/notifier.rb index a5d2fe15ab6..406834e0f33 100644 --- a/lib/microsoft_teams/notifier.rb +++ b/lib/microsoft_teams/notifier.rb @@ -2,30 +2,43 @@ module MicrosoftTeams class Notifier def initialize(webhook) @webhook = webhook + @header = { 'Content-type' => 'application/json' } end def ping(options = {}) - HTTParty.post( - @webhook.to_str, - headers: { 'Content-type' => 'application/json' }, - body: body(options) - ) + result = false + + begin + response = HTTParty.post( + @webhook.to_str, + headers: @header, + body: body(options) + ) + + result = true if response + rescue HTTParty::Error, StandardError => error + Rails.logger.info("#{self.class.name}: Error while connecting to #{@webhook}: #{error.message}") + end + + result end private def body(options = {}) - attachments = options[:attachments] result = { 'sections' => [] } result['title'] = options[:title] result['summary'] = options[:pretext] - result['sections'] << options[:activity] + result['sections'] << MicrosoftTeams::Activity.new(options[:activity]).prepare - result['sections'] << { - 'title' => 'Details', - 'facts' => [{ 'name' => 'Attachments', 'value' => attachments }] - } if attachments.present? && attachments.empty? + attachments = options[:attachments] + if attachments && attachments.any? + result['sections'] << { + 'title' => 'Details', + 'facts' => [{ 'name' => 'Attachments', 'value' => attachments }] + } + end result.to_json end |