Commit 78c34e2fa7c690014599644679d468b767aaf893
Exists in
master
and in
1 other branch
Merge pull request #688 from jdguzman/slack_integration
Slack integration
Showing
6 changed files
with
71 additions
and
1 deletions
Show diff stats
5.89 KB
5.89 KB
5.68 KB
... | ... | @@ -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.to_json | |
36 | + end | |
37 | + | |
38 | + def create_notification(problem) | |
39 | + HTTParty.post(url, :body => post_payload(problem), :headers => { 'Content-Type' => 'application/json' }) | |
40 | + end | |
41 | +end | ... | ... |
spec/fabricators/notification_service_fabricator.rb
... | ... | @@ -12,6 +12,6 @@ Fabricator :gtalk_notification_service, :from => :notification_service, :class_n |
12 | 12 | service { sequence :word } |
13 | 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 | 16 | Fabricator "#{t}_notification_service".to_sym, :from => :notification_service, :class_name => "NotificationService::#{t.camelcase}Service" |
17 | 17 | end | ... | ... |
... | ... | @@ -0,0 +1,29 @@ |
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 | + payload = {:text => notification_service.message_for_slack(problem), :channel => notification_service.room_id}.to_json | |
12 | + expect(HTTParty).to receive(:post).with(notification_service.url, :body => payload, :headers => {"Content-Type" => "application/json"}).and_return(true) | |
13 | + | |
14 | + notification_service.create_notification(problem) | |
15 | + end | |
16 | + | |
17 | + it "it should send a notification to Slack without a channel" do | |
18 | + # setup | |
19 | + notice = Fabricate :notice | |
20 | + notification_service = Fabricate :slack_notification_service, :app => notice.app, :room_id => "" | |
21 | + problem = notice.problem | |
22 | + | |
23 | + # faraday stubbing | |
24 | + payload = {:text => notification_service.message_for_slack(problem)}.to_json | |
25 | + expect(HTTParty).to receive(:post).with(notification_service.url, :body => payload, :headers => {"Content-Type" => "application/json"}).and_return(true) | |
26 | + | |
27 | + notification_service.create_notification(problem) | |
28 | + end | |
29 | +end | ... | ... |