diff --git a/app/models/notification_services/slack_service.rb b/app/models/notification_services/slack_service.rb new file mode 100644 index 0000000..351eeeb --- /dev/null +++ b/app/models/notification_services/slack_service.rb @@ -0,0 +1,41 @@ +class NotificationServices::SlackService < NotificationService + Label = "slack" + Fields += [ + [:subdomain, { + :placeholder => 'subdomain', + :label => 'Subdomain portion for Slack service' + }], + [:api_token, { + :placeholder => 'Slack Integration Token', + :label => 'Token' + }], + [:room_id, { + :placeholder => '#general', + :label => 'Room where Slack should notify' + }] + ] + + def check_params + if Fields.detect {|f| self[f[0]].blank? unless f[0] == :room_id } + errors.add :base, "You must specify your Slack subdomain and token." + end + end + + def url + "https://#{subdomain}.slack.com/services/hooks/incoming-webhook?token=#{api_token}" + end + + def message_for_slack(problem) + "[#{problem.app.name}][#{problem.environment}][#{problem.where}]: #{problem.error_class} #{problem_url(problem)}" + end + + def post_payload(problem) + payload = {:text => message_for_slack(problem) } + payload[:channel] = room_id unless room_id.empty? + payload + end + + def create_notification(problem) + HTTParty.post(url, :body => {:payload => post_payload(problem)}) + end +end diff --git a/spec/fabricators/notification_service_fabricator.rb b/spec/fabricators/notification_service_fabricator.rb index bf11b6d..832672c 100644 --- a/spec/fabricators/notification_service_fabricator.rb +++ b/spec/fabricators/notification_service_fabricator.rb @@ -12,6 +12,6 @@ Fabricator :gtalk_notification_service, :from => :notification_service, :class_n service { sequence :word } end -%w(campfire flowdock hipchat hoiio hubot pushover webhook).each do |t| +%w(campfire flowdock hipchat hoiio hubot pushover slack webhook).each do |t| Fabricator "#{t}_notification_service".to_sym, :from => :notification_service, :class_name => "NotificationService::#{t.camelcase}Service" end diff --git a/spec/models/notification_service/slack_service_spec.rb b/spec/models/notification_service/slack_service_spec.rb new file mode 100644 index 0000000..c50eb19 --- /dev/null +++ b/spec/models/notification_service/slack_service_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe NotificationService::SlackService do + it "it should send a notification to Slack with channel" do + # setup + notice = Fabricate :notice + notification_service = Fabricate :slack_notification_service, :app => notice.app + problem = notice.problem + + # faraday stubbing + expect(HTTParty).to receive(:post).with(notification_service.url, :body => {:payload => {:text => an_instance_of(String), :channel => notification_service.room_id}}).and_return(true) + + notification_service.create_notification(problem) + end + + it "it should send a notification to Slack without a channel" do + # setup + notice = Fabricate :notice + notification_service = Fabricate :slack_notification_service, :app => notice.app, :room_id => "" + problem = notice.problem + + # faraday stubbing + expect(HTTParty).to receive(:post).with(notification_service.url, :body => {:payload => {:text => an_instance_of(String)}}).and_return(true) + + notification_service.create_notification(problem) + end +end -- libgit2 0.21.2