diff options
author | Douwe Maan <douwe@gitlab.com> | 2017-04-06 19:37:25 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2017-04-06 19:37:25 +0000 |
commit | 00e00cacf8cb4ce3bfb733bae47e7e594e91e294 (patch) | |
tree | 607671987a66dbb16d49738aef22616c13b6936b /lib/microsoft_teams | |
parent | 73cb71e41c0ade92b9673a5d74c7dd78679fae91 (diff) | |
parent | 1f404065ca602593dc18aa051515fd88132ddfdf (diff) | |
download | gitlab-ce-00e00cacf8cb4ce3bfb733bae47e7e594e91e294.tar.gz |
Merge branch 'microsoft-teams-integration' into 'master'
adds initial microsoft teams integration
See merge request !10412
Diffstat (limited to 'lib/microsoft_teams')
-rw-r--r-- | lib/microsoft_teams/activity.rb | 19 | ||||
-rw-r--r-- | lib/microsoft_teams/notifier.rb | 46 |
2 files changed, 65 insertions, 0 deletions
diff --git a/lib/microsoft_teams/activity.rb b/lib/microsoft_teams/activity.rb new file mode 100644 index 00000000000..d2c420efdaf --- /dev/null +++ b/lib/microsoft_teams/activity.rb @@ -0,0 +1,19 @@ +module MicrosoftTeams + class Activity + def initialize(title:, subtitle:, text:, image:) + @title = title + @subtitle = subtitle + @text = text + @image = image + end + + def prepare + { + 'activityTitle' => @title, + 'activitySubtitle' => @subtitle, + 'activityText' => @text, + 'activityImage' => @image + } + end + end +end diff --git a/lib/microsoft_teams/notifier.rb b/lib/microsoft_teams/notifier.rb new file mode 100644 index 00000000000..3bef68a1bcb --- /dev/null +++ b/lib/microsoft_teams/notifier.rb @@ -0,0 +1,46 @@ +module MicrosoftTeams + class Notifier + def initialize(webhook) + @webhook = webhook + @header = { 'Content-type' => 'application/json' } + end + + def ping(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 = {}) + result = { 'sections' => [] } + + result['title'] = options[:title] + result['summary'] = options[:pretext] + result['sections'] << MicrosoftTeams::Activity.new(options[:activity]).prepare + + attachments = options[:attachments] + unless attachments.blank? + result['sections'] << { + 'title' => 'Details', + 'facts' => [{ 'name' => 'Attachments', 'value' => attachments }] + } + end + + result.to_json + end + end +end |