Skip to content

Commit

Permalink
Merge pull request #33 from Scuottolinx/feat/more-channels
Browse files Browse the repository at this point in the history
Added support more channels
  • Loading branch information
paulingham committed Jan 26, 2018
2 parents a28592f + e6106f2 commit 6f420ba
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion capistrano-slackify.gemspec
Expand Up @@ -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}
Expand Down
30 changes: 17 additions & 13 deletions lib/capistrano/tasks/slackify.cap
Expand Up @@ -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.build(self, :starting, channel),
fetch(:slack_url)
}
end
end
end
Expand All @@ -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'
Expand All @@ -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
Expand All @@ -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'
Expand Down
12 changes: 6 additions & 6 deletions lib/slackify.rb
Expand Up @@ -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]
Expand All @@ -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),
Expand Down
8 changes: 6 additions & 2 deletions spec/lib/slackify_spec.rb
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6f420ba

Please sign in to comment.