Commit 91b381e027b1af653503f40d9a282bcda4479202

Authored by Stephen Crosby
2 parents 72eb7fa7 28e1148f
Exists in master and in 1 other branch production

Merge pull request #842 from jules2689/master

Fix Slack Integration Service
app/models/notification_services/slack_service.rb
1 class NotificationServices::SlackService < NotificationService 1 class NotificationServices::SlackService < NotificationService
2 Label = "slack" 2 Label = "slack"
3 Fields += [ 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' 4 + [:service_url, {
  5 + :placeholder => 'Slack Hook URL (https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX)',
  6 + :label => 'Hook URL'
15 }] 7 }]
16 ] 8 ]
17 9
18 def check_params 10 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." 11 + if Fields.detect {|f| self[f[0]].blank? }
  12 + errors.add :base, "You must specify your Slack Hook url."
21 end 13 end
22 end 14 end
23 15
24 - def url  
25 - "https://#{subdomain}.slack.com/services/hooks/incoming-webhook?token=#{api_token}"  
26 - end  
27 -  
28 def message_for_slack(problem) 16 def message_for_slack(problem)
29 "[#{problem.app.name}][#{problem.environment}][#{problem.where}]: #{problem.error_class} #{problem_url(problem)}" 17 "[#{problem.app.name}][#{problem.environment}][#{problem.where}]: #{problem.error_class} #{problem_url(problem)}"
30 end 18 end
31 19
32 def post_payload(problem) 20 def post_payload(problem)
33 - payload = {:text => message_for_slack(problem) }  
34 - payload[:channel] = room_id unless room_id.empty?  
35 - payload.to_json 21 + {:text => message_for_slack(problem) }.to_json
36 end 22 end
37 23
38 def create_notification(problem) 24 def create_notification(problem)
39 - HTTParty.post(url, :body => post_payload(problem), :headers => { 'Content-Type' => 'application/json' }) 25 + HTTParty.post(service_url, :body => post_payload(problem), :headers => { 'Content-Type' => 'application/json' })
40 end 26 end
41 end 27 end
docs/notifications/slack/airbrake_notification.png 0 → 100644

150 KB

docs/notifications/slack/errbit.png 0 → 100644

4.73 KB

docs/notifications/slack/errbit_notification.png 0 → 100644

83.6 KB

docs/notifications/slack/hook_url.png 0 → 100644

37.8 KB

docs/notifications/slack/index.md 0 → 100644
@@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
  1 +# Slack
  2 +
  3 +The slack notification sends to [Slack](https://www.slack.com/).
  4 +
  5 +## Configuration
  6 +
  7 +### Add the Airbrake Notification Integration on Slack
  8 +
  9 +![Airbrake Notification](airbrake_notification.png)
  10 +
  11 +### Hook URL
  12 +
  13 +Copy the Hook URL specified by the Slack service.
  14 +
  15 +![Hook URL](hook_url.png)
  16 +
  17 +Optionally change the name of the integration to "Errbit", and include the errbit logo for inline messages: ![Errbit](errbit.png)
  18 +
  19 +### Setup in Errbit
  20 +
  21 +On the App Edit Page, click to highlight the slack integration.
  22 +Input the hook url from above into the field and click save.
  23 +
  24 +![Errbit Notification Setup](errbit_notification.png)
0 \ No newline at end of file 25 \ No newline at end of file
spec/fabricators/notification_service_fabricator.rb
@@ -12,6 +12,10 @@ Fabricator :gtalk_notification_service, :from =&gt; :notification_service, :class_n @@ -12,6 +12,10 @@ 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 slack webhook).each do |t| 15 +Fabricator :slack_notification_service, :from => :notification_service, :class_name => "NotificationService::SlackService" do
  16 + service_url { sequence :word }
  17 +end
  18 +
  19 +%w(campfire flowdock hipchat hoiio hubot pushover webhook).each do |t|
16 Fabricator "#{t}_notification_service".to_sym, :from => :notification_service, :class_name => "NotificationService::#{t.camelcase}Service" 20 Fabricator "#{t}_notification_service".to_sym, :from => :notification_service, :class_name => "NotificationService::#{t.camelcase}Service"
17 end 21 end
spec/models/notification_service/slack_service_spec.rb
1 describe NotificationService::SlackService, type: 'model' do 1 describe NotificationService::SlackService, type: 'model' do
2 - it "it should send a notification to Slack with channel" do 2 + it "it should send a notification to Slack with hook url" do
3 # setup 3 # setup
4 notice = Fabricate :notice 4 notice = Fabricate :notice
5 - notification_service = Fabricate :slack_notification_service, :app => notice.app  
6 - problem = notice.problem  
7 -  
8 - # faraday stubbing  
9 - payload = {:text => notification_service.message_for_slack(problem), :channel => notification_service.room_id}.to_json  
10 - expect(HTTParty).to receive(:post).with(notification_service.url, :body => payload, :headers => {"Content-Type" => "application/json"}).and_return(true)  
11 -  
12 - notification_service.create_notification(problem)  
13 - end  
14 -  
15 - it "it should send a notification to Slack without a channel" do  
16 - # setup  
17 - notice = Fabricate :notice  
18 - notification_service = Fabricate :slack_notification_service, :app => notice.app, :room_id => "" 5 + notification_service = Fabricate :slack_notification_service, :app => notice.app, :service_url => "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX"
19 problem = notice.problem 6 problem = notice.problem
20 7
21 # faraday stubbing 8 # faraday stubbing
22 payload = {:text => notification_service.message_for_slack(problem)}.to_json 9 payload = {:text => notification_service.message_for_slack(problem)}.to_json
23 - expect(HTTParty).to receive(:post).with(notification_service.url, :body => payload, :headers => {"Content-Type" => "application/json"}).and_return(true) 10 + expect(HTTParty).to receive(:post).with(notification_service.service_url, :body => payload, :headers => {"Content-Type" => "application/json"}).and_return(true)
24 11
25 notification_service.create_notification(problem) 12 notification_service.create_notification(problem)
26 end 13 end