Skip to content

Commit

Permalink
Merge pull request #19 from onthebeach/feature/more_consistant_messaging
Browse files Browse the repository at this point in the history
Make the 3 Slack messages sent mostly similar
  • Loading branch information
seenmyfate committed Jun 19, 2015
2 parents 35cb24f + 69a8960 commit e9ced2d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 101 deletions.
2 changes: 1 addition & 1 deletion capistrano-slackify.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_runtime_dependency 'capistrano', '>= 3.1.0'
spec.add_runtime_dependency 'capistrano', '>= 3.2.0'
spec.add_runtime_dependency 'multi_json'

spec.add_development_dependency 'bundler', '~> 1.5'
Expand Down
90 changes: 10 additions & 80 deletions lib/capistrano/tasks/slackify.cap
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ namespace :slack do
info 'Notifying Slack of deploy starting'
set :time_started, Time.now.to_i

payload = Slackify::Payload.build self do |default, context|
default[:text] = fetch(:slack_deploy_starting_text)
default
end

execute :curl, '-X POST', '--data-urlencode',
payload,
Slackify::Payload.build(self, :starting),
fetch(:slack_url)
end
end
Expand All @@ -25,42 +20,8 @@ namespace :slack do
info 'Notifying Slack of deploy finished'
set :time_finished, Time.now.to_i

payload = Slackify::Payload.build self do |default, context|
text = fetch(:slack_text)
default[:attachments] = [
{
fallback: text,
color: fetch(:slack_deploy_finished_color),
text: text,
fields: [
{
title: 'Status',
value: 'success',
short: true
},
{
title: 'Stage',
value: fetch(:stage),
short: true
},
{
title: 'Branch',
value: fetch(:branch),
short: true
},
{
title: 'Revision',
value: fetch(:current_revision),
short: true
},
]
}
]
default
end

execute :curl, '-X POST', '--data-urlencode',
payload,
Slackify::Payload.build(self, :success),
fetch(:slack_url)
end
end
Expand All @@ -73,42 +34,8 @@ namespace :slack do
info 'Notifying Slack of deploy failed'
set :time_finished, Time.now.to_i

payload = Slackify::Payload.build self do |default, context|
text = fetch :slack_deploy_failed_text
default[:attachments] = [
{
fallback: text,
color: fetch(:slack_deploy_failed_color),
text: text,
fields: [
{
title: 'Status',
value: 'failed',
short: true
},
{
title: 'Stage',
value: fetch(:stage),
short: true
},
{
title: 'Branch',
value: fetch(:branch),
short: true
},
{
title: 'Revision',
value: fetch(:current_revision),
short: true
},
]
}
]
default
end

execute :curl, '-X POST', '--data-urlencode',
payload,
Slackify::Payload.build(self, :failed),
fetch(:slack_url)
end
end
Expand All @@ -125,15 +52,18 @@ namespace :load do
set :slack_url, -> { fail ':slack_url is not set' }
set :slack_text, lambda {
time_elapsed = Integer(fetch(:time_finished) - fetch(:time_started))
"#{fetch(:application)} deployed by #{fetch(:slack_user)} " \
"in #{time_elapsed} seconds."
"#{fetch(:application)} deploy by #{fetch(:slack_user)}: " \
"successful in #{time_elapsed} seconds."
}
set :slack_deploy_starting_text, -> {
"#{fetch(:stage)} deploy starting with revision/branch #{fetch(:current_revision, fetch(:branch))} for #{fetch(:application)}"
"#{fetch(:application)} deploy by #{fetch(:slack_user)}: " \
"starting."
}
set :slack_deploy_failed_text, -> {
"deploy of #{fetch(:application)}"
"#{fetch(:application)} deploy by #{fetch(:slack_user)}: " \
"failed."
}
set :slack_deploy_starting_color, 'warning'
set :slack_deploy_finished_color, 'good'
set :slack_deploy_failed_color, 'danger'
end
Expand Down
80 changes: 65 additions & 15 deletions lib/slackify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,83 @@
module Slackify
class Payload

def initialize(context)
def initialize(context, status)
@context = context
@status = status
end

def self.build(context, &block)
new(context).build(&block)
def self.build(context, status)
new(context, status).build
end

def build(&block)
"'payload=#{payload(&block)}'"
def build
"'payload=#{payload}'"
end

def payload(&block)
default = {
channel: fetch(:slack_channel),
username: fetch(:slack_username),
icon_emoji: fetch(:slack_emoji),
parse: fetch(:slack_parse)
}

json = yield(default, @context)
MultiJson.dump(json)
def payload
MultiJson.dump(
{
channel: fetch(:slack_channel),
username: fetch(:slack_username),
icon_emoji: fetch(:slack_emoji),
parse: fetch(:slack_parse),
attachments: [
{
fallback: text,
color: color,
text: text,
fields: [
{
title: 'Status',
value: @status,
short: true
},
{
title: 'Stage',
value: fetch(:stage),
short: true
},
{
title: 'Branch',
value: fetch(:branch),
short: true
},
{
title: 'Revision',
value: fetch(:current_revision),
short: true
},
]
}
]
}
)
end

def fetch(*args, &block)
@context.fetch(*args, &block)
end

def text
@text ||= case @status
when :starting
fetch(:slack_deploy_starting_text)
when :success
fetch(:slack_text)
when :failed
fetch(:slack_deploy_failed_text)
end
end

def color
case @status
when :starting
fetch(:slack_deploy_starting_color)
when :success
fetch(:slack_deploy_finished_color)
when :failed
fetch(:slack_deploy_failed_color)
end
end
end
end
11 changes: 6 additions & 5 deletions spec/lib/slackify_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ module Slackify
slack_parse: 'default',
slack_user: 'You',
slack_text: ':boom:',
slack_deploy_finished_color: 'good',
stage: 'sandbox',
branch: 'master',
current_revision: 'SHA',
}
}

let(:payload) {
%{'payload={"channel":"#general","username":"Capistrano","icon_emoji":":ghost:","parse":"default","text":":boom:"}'}
%{'payload={"channel":"#general","username":"Capistrano","icon_emoji":":ghost:","parse":"default","attachments":[{"fallback":":boom:","color":"good","text":":boom:","fields":[{"title":"Status","value":"success","short":true},{"title":"Stage","value":"sandbox","short":true},{"title":"Branch","value":"master","short":true},{"title":"Revision","value":"SHA","short":true}]}]}'}
}

let(:text) { context.fetch(:slack_text) }

let(:builded_payload) {
Payload.build context do |default, context|
default[:text] = text
default
end
Payload.build(context, :success)
}

it 'returns the payload with the specified text' do
Expand Down

0 comments on commit e9ced2d

Please sign in to comment.