From bfd69be576dbbd0abcf2e331262911d9a4c48ce5 Mon Sep 17 00:00:00 2001 From: Antonio Scuotto Date: Thu, 18 Jan 2018 15:18:05 +0100 Subject: [PATCH] Added support more channels --- README.md | 2 +- capistrano-slackify.gemspec | 2 +- lib/capistrano/tasks/slackify.cap | 30 +++++++++++++++++------------- lib/slackify.rb | 12 ++++++------ spec/lib/slackify_spec.rb | 8 ++++++-- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 92362ff..4cb1bb1 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ As with the other tasks, it is also possible to notify failures manually: Any of the defaults can be over-ridden in `config/deploy.rb`: - set :slack_channel, '#devops' + set :slack_channel, ['#devops', '#other-channel'] set :slack_username, 'Deploybot' set :slack_emoji, ':trollface:' set :slack_user, ENV['GIT_AUTHOR_NAME'] diff --git a/capistrano-slackify.gemspec b/capistrano-slackify.gemspec index 4691949..d353013 100644 --- a/capistrano-slackify.gemspec +++ b/capistrano-slackify.gemspec @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) Gem::Specification.new do |spec| spec.name = 'capistrano-slackify' - spec.version = '2.9.0' + spec.version = '2.10.0' spec.authors = ['seenmyfate'] spec.email = ['seenmyfate@gmail.com'] spec.summary = %q{Publish deployment notifications to Slack via the incoming webhooks integration} diff --git a/lib/capistrano/tasks/slackify.cap b/lib/capistrano/tasks/slackify.cap index cba3f4c..2a93918 100644 --- a/lib/capistrano/tasks/slackify.cap +++ b/lib/capistrano/tasks/slackify.cap @@ -7,10 +7,11 @@ namespace :slack do if fetch(:slack_notify_events).include? :started info 'Notifying Slack of deploy starting' - - execute :curl, '-X POST', '-s', '--data-urlencode', - Slackify::Payload.build(self, :starting), - fetch(:slack_url) + Array(fetch(:slack_channel)).each {|channel| + execute :curl, '-X POST', '-s', '--data-urlencode', + Slackify::Payload.new(self, :starting, channel), + fetch(:slack_url) + } end end end @@ -24,11 +25,12 @@ namespace :slack do if fetch(:slack_notify_events).include? :finished info 'Notifying Slack of deploy finished' - - execute :curl, '-X POST', '-s', '--data-urlencode', - Slackify::Payload.build(self, :success), - fetch(:slack_url) - end + Array(fetch(:slack_channel)).each {|channel| + execute :curl, '-X POST', '-s', '--data-urlencode', + Slackify::Payload.build(self, :success, channel), + fetch(:slack_url) + } + end end end after 'deploy:finished', 'slack:notify_finished' @@ -42,9 +44,11 @@ namespace :slack do if fetch(:slack_notify_events).include? :failed info 'Notifying Slack of deploy failed' - execute :curl, '-X POST', '-s', '--data-urlencode', - Slackify::Payload.build(self, :failed), - fetch(:slack_url) + Array(fetch(:slack_channel)).each {|channel| + execute :curl, '-X POST', '-s', '--data-urlencode', + Slackify::Payload.build(self, :failed, channel), + fetch(:slack_url) + } end end end @@ -53,7 +57,7 @@ end namespace :load do task :defaults do - set :slack_channel, '#general' + set :slack_channel, ['#general'] set :slack_username, 'Capistrano' set :slack_emoji, ':ghost:' set :slack_parse, 'default' diff --git a/lib/slackify.rb b/lib/slackify.rb index f1fc8e5..b51dd01 100644 --- a/lib/slackify.rb +++ b/lib/slackify.rb @@ -8,15 +8,15 @@ def initialize(context, status) @status = status end - def self.build(context, status) - new(context, status).build + def self.build(context, status, channel) + new(context, status).build(channel) end - def build - "'payload=#{payload}'" + def build(channel) + "'payload=#{payload(channel)}'" end - def payload + def payload(channel) fields = fetch(:slack_fields).each_with_object([]) { |field, fields| if fields_map[field].fetch(:value).respond_to?(:call) field_data = fields_map[field] @@ -29,7 +29,7 @@ def payload MultiJson.dump( { - channel: fetch(:slack_channel), + channel: channel, username: fetch(:slack_username), icon_emoji: fetch(:slack_emoji), parse: fetch(:slack_parse), diff --git a/spec/lib/slackify_spec.rb b/spec/lib/slackify_spec.rb index 87c7ad6..279ac9a 100644 --- a/spec/lib/slackify_spec.rb +++ b/spec/lib/slackify_spec.rb @@ -6,7 +6,7 @@ module Slackify let(:context) { { slack_channel: '#general', - slack_username:'Capistrano', + slack_username: 'Capistrano', slack_emoji: ':ghost:', slack_parse: 'default', slack_user: 'You', @@ -42,7 +42,11 @@ module Slackify let(:text) { context.fetch(:slack_text) } let(:builded_payload) { - Payload.build(context, :success) + Payload.build(context, :success, slack_channel) + } + + let(:slack_channel) { + context.fetch(:slack_channel) } it 'returns the payload with the specified text' do