summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2017-04-06 19:37:25 +0000
committerDouwe Maan <douwe@gitlab.com>2017-04-06 19:37:25 +0000
commit00e00cacf8cb4ce3bfb733bae47e7e594e91e294 (patch)
tree607671987a66dbb16d49738aef22616c13b6936b /lib
parent73cb71e41c0ade92b9673a5d74c7dd78679fae91 (diff)
parent1f404065ca602593dc18aa051515fd88132ddfdf (diff)
downloadgitlab-ce-00e00cacf8cb4ce3bfb733bae47e7e594e91e294.tar.gz
Merge branch 'microsoft-teams-integration' into 'master'
adds initial microsoft teams integration See merge request !10412
Diffstat (limited to 'lib')
-rw-r--r--lib/api/services.rb9
-rw-r--r--lib/api/v3/services.rb6
-rw-r--r--lib/microsoft_teams/activity.rb19
-rw-r--r--lib/microsoft_teams/notifier.rb46
4 files changed, 80 insertions, 0 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
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