diff options
author | Tiago Botelho <tiagonbotelho@hotmail.com> | 2017-04-04 18:10:21 +0300 |
---|---|---|
committer | Tiago Botelho <tiagonbotelho@hotmail.com> | 2017-04-06 21:47:07 +0300 |
commit | 1f404065ca602593dc18aa051515fd88132ddfdf (patch) | |
tree | e1fb01a2584c2137ad603500fc22652ab099bb46 /lib | |
parent | 05f69ef21246f1a2c0b83540d3e205c9e4d77819 (diff) |
adds relevant tests
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..3bef68a1bcb 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] + unless attachments.blank? + result['sections'] << { + 'title' => 'Details', + 'facts' => [{ 'name' => 'Attachments', 'value' => attachments }] + } + end result.to_json end |