Commit 09b2c77c7dd121f20677258977b1b4ddea16f110

Authored by JD Guzman
1 parent 06d8000e
Exists in master and in 1 other branch production

added slack integration

app/models/notification_services/slack_service.rb 0 → 100644
@@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
  1 +class NotificationServices::SlackService < NotificationService
  2 + Label = "slack"
  3 + Fields += [
  4 + [:subdomain, {
  5 + :placeholder => 'subdomain',
  6 + :label => 'Subdomain portion for Slack service'
  7 + }],
  8 + [:api_token, {
  9 + :placeholder => 'Slack Integration Token',
  10 + :label => 'Token'
  11 + }],
  12 + [:room_id, {
  13 + :placeholder => '#general',
  14 + :label => 'Room where Slack should notify'
  15 + }]
  16 + ]
  17 +
  18 + def check_params
  19 + if Fields.detect {|f| self[f[0]].blank? unless f[0] == :room_id }
  20 + errors.add :base, "You must specify your Slack subdomain and token."
  21 + end
  22 + end
  23 +
  24 + def url
  25 + "https://#{subdomain}.slack.com/services/hooks/incoming-webhook?token=#{api_token}"
  26 + end
  27 +
  28 + def message_for_slack(problem)
  29 + "[#{problem.app.name}][#{problem.environment}][#{problem.where}]: #{problem.error_class} #{problem_url(problem)}"
  30 + end
  31 +
  32 + def post_payload(problem)
  33 + payload = {:text => message_for_slack(problem) }
  34 + payload[:channel] = room_id unless room_id.empty?
  35 + payload
  36 + end
  37 +
  38 + def create_notification(problem)
  39 + HTTParty.post(url, :body => {:payload => post_payload(problem)})
  40 + end
  41 +end
spec/fabricators/notification_service_fabricator.rb
@@ -12,6 +12,6 @@ Fabricator :gtalk_notification_service, :from =&gt; :notification_service, :class_n @@ -12,6 +12,6 @@ Fabricator :gtalk_notification_service, :from =&gt; :notification_service, :class_n
12 service { sequence :word } 12 service { sequence :word }
13 end 13 end
14 14
15 -%w(campfire flowdock hipchat hoiio hubot pushover webhook).each do |t| 15 +%w(campfire flowdock hipchat hoiio hubot pushover slack webhook).each do |t|
16 Fabricator "#{t}_notification_service".to_sym, :from => :notification_service, :class_name => "NotificationService::#{t.camelcase}Service" 16 Fabricator "#{t}_notification_service".to_sym, :from => :notification_service, :class_name => "NotificationService::#{t.camelcase}Service"
17 end 17 end
spec/models/notification_service/slack_service_spec.rb 0 → 100644
@@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
  1 +require 'spec_helper'
  2 +
  3 +describe NotificationService::SlackService do
  4 + it "it should send a notification to Slack with channel" do
  5 + # setup
  6 + notice = Fabricate :notice
  7 + notification_service = Fabricate :slack_notification_service, :app => notice.app
  8 + problem = notice.problem
  9 +
  10 + # faraday stubbing
  11 + expect(HTTParty).to receive(:post).with(notification_service.url, :body => {:payload => {:text => an_instance_of(String), :channel => notification_service.room_id}}).and_return(true)
  12 +
  13 + notification_service.create_notification(problem)
  14 + end
  15 +
  16 + it "it should send a notification to Slack without a channel" do
  17 + # setup
  18 + notice = Fabricate :notice
  19 + notification_service = Fabricate :slack_notification_service, :app => notice.app, :room_id => ""
  20 + problem = notice.problem
  21 +
  22 + # faraday stubbing
  23 + expect(HTTParty).to receive(:post).with(notification_service.url, :body => {:payload => {:text => an_instance_of(String)}}).and_return(true)
  24 +
  25 + notification_service.create_notification(problem)
  26 + end
  27 +end