slackistrano 3.0.1 → 3.1.0.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ require 'forwardable'
2
+ require_relative 'helpers'
3
+
4
+ module Slackistrano
5
+ module Messaging
6
+ class Base
7
+
8
+ include Helpers
9
+
10
+ extend Forwardable
11
+ def_delegators :env, :fetch
12
+
13
+ attr_reader :team, :token, :webhook
14
+
15
+ def initialize(env: nil, team: nil, channel: nil, token: nil, webhook: nil)
16
+ @env = env
17
+ @team = team
18
+ @channel = channel
19
+ @token = token
20
+ @webhook = webhook
21
+ end
22
+
23
+ def payload_for_updating
24
+ {
25
+ text: "#{deployer} has started deploying branch #{branch} of #{application} to #{stage}"
26
+ }
27
+ end
28
+
29
+ def payload_for_reverting
30
+ {
31
+ text: "#{deployer} has started rolling back branch #{branch} of #{application} to #{stage}"
32
+ }
33
+ end
34
+
35
+ def payload_for_updated
36
+ {
37
+ text: "#{deployer} has finished deploying branch #{branch} of #{application} to #{stage}"
38
+ }
39
+ end
40
+
41
+ def payload_for_reverted
42
+ {
43
+ text: "#{deployer} has finished rolling back branch of #{application} to #{stage}"
44
+ }
45
+ end
46
+
47
+ def payload_for_failed
48
+ {
49
+ text: "#{deployer} has failed to #{deploying? ? 'deploy' : 'rollback'} branch #{branch} of #{application} to #{stage}"
50
+ }
51
+ end
52
+
53
+ def channels_for(action)
54
+ @channel
55
+ end
56
+
57
+ ################################################################################
58
+
59
+ def payload_for(action)
60
+ method = "payload_for_#{action}"
61
+ respond_to?(method) && send(method)
62
+ end
63
+
64
+ def via_slackbot?
65
+ @webhook.nil?
66
+ end
67
+
68
+ end
69
+ end
70
+ end
71
+
72
+ require_relative 'default'
73
+ require_relative 'deprecated'
@@ -0,0 +1,31 @@
1
+ module Slackistrano
2
+ module Messaging
3
+ class Default < Base
4
+
5
+ def payload_for_updating
6
+ super
7
+ end
8
+
9
+ def payload_for_reverting
10
+ super
11
+ end
12
+
13
+ def payload_for_updated
14
+ super
15
+ end
16
+
17
+ def payload_for_reverted
18
+ super
19
+ end
20
+
21
+ def payload_for_failed
22
+ super
23
+ end
24
+
25
+ def channels_for(action)
26
+ super
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,75 @@
1
+ module Slackistrano
2
+ module Messaging
3
+ class Deprecated < Base
4
+
5
+ def initialize(env: nil, team: nil, channel: nil, token: nil, webhook: nil)
6
+ run_locally do
7
+ warn("[slackistrano] You are using an outdated configuration that will be removed soon.")
8
+ warn("[slackistrano] Please upgrade soon! <https://github.com/phallstrom/slackistrano>")
9
+ end
10
+
11
+ super
12
+ end
13
+
14
+ def icon_url
15
+ fetch("slack_icon_url".to_sym) || super
16
+ end
17
+
18
+ def icon_emoji
19
+ fetch("slack_icon_emoji".to_sym) || super
20
+ end
21
+
22
+ def username
23
+ fetch("slack_username".to_sym) || super
24
+ end
25
+
26
+ def deployer
27
+ fetch("slack_deploy_user".to_sym) || super
28
+ end
29
+
30
+ def channels_for(action)
31
+ fetch("slack_channel_#{action}".to_sym) || super
32
+ end
33
+
34
+ def payload_for_updating
35
+ make_message(__method__, super)
36
+ end
37
+
38
+ def payload_for_reverting
39
+ make_message(__method__, super)
40
+ end
41
+
42
+ def payload_for_updated
43
+ make_message(__method__, super.merge(color: 'good'))
44
+ end
45
+
46
+ def payload_for_reverted
47
+ make_message(__method__, super.merge(color: 'warning'))
48
+ end
49
+
50
+ def payload_for_failed
51
+ make_message(__method__, super.merge(color: 'danger'))
52
+ end
53
+
54
+ private ##################################################
55
+
56
+ def make_message(method, options={})
57
+ action = method.to_s.sub('payload_for_', '')
58
+
59
+ return nil unless fetch("slack_run".to_sym, true) && fetch("slack_run_#{action}".to_sym, true)
60
+
61
+ attachment = options.merge({
62
+ title: fetch(:"slack_title_#{action}"),
63
+ pretext: fetch(:"slack_pretext_#{action}"),
64
+ text: fetch(:"slack_msg_#{action}", options[:text]),
65
+ fields: fetch(:"slack_fields_#{action}", []),
66
+ fallback: fetch(:"slack_fallback_#{action}"),
67
+ mrkdwn_in: [:text, :pretext]
68
+ }).reject{|k, v| v.nil? }
69
+
70
+ {attachments: [attachment]}
71
+ end
72
+
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,44 @@
1
+ module Slackistrano
2
+ module Messaging
3
+ module Helpers
4
+
5
+ def icon_url
6
+ 'https://raw.githubusercontent.com/phallstrom/slackistrano/master/images/slackistrano.png'
7
+ end
8
+
9
+ def icon_emoji
10
+ nil
11
+ end
12
+
13
+ def username
14
+ 'Slackistrano'
15
+ end
16
+
17
+ def deployer
18
+ ENV['USER'] || ENV['USERNAME']
19
+ end
20
+
21
+ def branch
22
+ fetch(:branch)
23
+ end
24
+
25
+ def application
26
+ fetch(:application)
27
+ end
28
+
29
+ def stage(default = 'an unknown stage')
30
+ fetch(:stage, default)
31
+ end
32
+
33
+ #
34
+ # Return the elapsed running time as a string.
35
+ #
36
+ # Examples: 21-18:26:30, 15:28:37, 01:14
37
+ #
38
+ def elapsed_time
39
+ `ps -p #{$$} -o etime=`.strip
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -2,12 +2,10 @@ namespace :slack do
2
2
  namespace :deploy do
3
3
 
4
4
  task :updating do
5
- set(:slack_deploy_or_rollback, 'deploy')
6
5
  Slackistrano::Capistrano.new(self).run(:updating)
7
6
  end
8
7
 
9
8
  task :reverting do
10
- set(:slack_deploy_or_rollback, 'rollback')
11
9
  Slackistrano::Capistrano.new(self).run(:reverting)
12
10
  end
13
11
 
@@ -23,6 +21,10 @@ namespace :slack do
23
21
  Slackistrano::Capistrano.new(self).run(:failed)
24
22
  end
25
23
 
24
+ task :test => %i[updating updated reverting reverted failed] do
25
+ # all tasks run as dependencies
26
+ end
27
+
26
28
  end
27
29
  end
28
30
 
@@ -31,68 +33,3 @@ before 'deploy:reverting', 'slack:deploy:reverting'
31
33
  after 'deploy:finishing', 'slack:deploy:updated'
32
34
  after 'deploy:finishing_rollback', 'slack:deploy:reverted'
33
35
  after 'deploy:failed', 'slack:deploy:failed'
34
-
35
- namespace :load do
36
- task :defaults do
37
-
38
- set :slack_team, -> { nil } # If URL is 'team.slack.com', value is 'team'.
39
- set :slack_token, -> { nil } # Token from Incoming WebHooks.
40
- set :slack_webhook, -> { nil } # Incoming WebHook URL.
41
- set :slack_via_slackbot, -> { false } # Set to true to send the message via slackbot instead of webhook
42
- set :slack_channel, -> { nil } # Channel to post to. Optional. Defaults to WebHook setting.
43
-
44
- # Optional, overridable settings
45
-
46
- set :slack_channel_updating, -> { nil } # Channel to post to. Defaults to :slack_channel.
47
- set :slack_channel_reverting, -> { nil } # Channel to post to. Defaults to :slack_channel.
48
- set :slack_channel_updated, -> { nil } # Channel to post to. Defaults to :slack_channel.
49
- set :slack_channel_reverted, -> { nil } # Channel to post to. Defaults to :slack_channel.
50
- set :slack_channel_failed, -> { nil } # Channel to post to. Defaults to :slack_channel.
51
-
52
- set :slack_icon_url, -> { 'https://raw.githubusercontent.com/phallstrom/slackistrano/master/slackistrano.png' }
53
- set :slack_icon_emoji, -> { nil } # Emoji to use. Overrides icon_url. Must be a string (ex: ':shipit:')
54
- set :slack_username, -> { 'Slackistrano' }
55
-
56
- set :slack_run, -> { true } # Set to false to disable all messages.
57
- set :slack_run_updating, -> { true } # Set to false to disable deploy starting message.
58
- set :slack_run_reverting, -> { true } # Set to false to disable rollback starting message.
59
- set :slack_run_updated, -> { true } # Set to false to disable deploy finished message.
60
- set :slack_run_reverted, -> { true } # Set to false to disable rollback finished message.
61
- set :slack_run_failed, -> { true } # Set to false to disable failure message.
62
-
63
- set :slack_deploy_user, -> { ENV['USER'] || ENV['USERNAME'] }
64
-
65
- set :slack_msg_updating, -> { "#{fetch :slack_deploy_user} has started deploying branch #{fetch :branch} of #{fetch :application} to #{fetch :stage, 'an unknown stage'}" }
66
- set :slack_msg_reverting, -> { "#{fetch :slack_deploy_user} has started rolling back branch #{fetch :branch} of #{fetch :application} to #{fetch :stage, 'an unknown stage'}" }
67
- set :slack_msg_updated, -> { "#{fetch :slack_deploy_user} has finished deploying branch #{fetch :branch} of #{fetch :application} to #{fetch :stage, 'an unknown stage'}" }
68
- set :slack_msg_reverted, -> { "#{fetch :slack_deploy_user} has finished rolling back branch of #{fetch :application} to #{fetch :stage, 'an unknown stage'}" }
69
-
70
- # :slack_or_deploy is set in several tasks above.
71
- set :slack_msg_failed, -> { "#{fetch :slack_deploy_user} has failed to #{fetch(:slack_deploy_or_rollback) || 'deploy'} branch #{fetch :branch} of #{fetch :application} to #{fetch :stage, 'an unknown stage'}" }
72
-
73
- set :slack_fields_updating, -> { [] }
74
- set :slack_fields_reverting, -> { [] }
75
- set :slack_fields_updated, -> { [] }
76
- set :slack_fields_reverted, -> { [] }
77
- set :slack_fields_failed, -> { [] }
78
-
79
- set :slack_fallback_updating, -> { nil }
80
- set :slack_fallback_reverting, -> { nil }
81
- set :slack_fallback_updated, -> { nil }
82
- set :slack_fallback_reverted, -> { nil }
83
- set :slack_fallback_failed, -> { nil }
84
-
85
- set :slack_title_updating, -> { nil }
86
- set :slack_title_reverting, -> { nil }
87
- set :slack_title_updated, -> { nil }
88
- set :slack_title_reverted, -> { nil }
89
- set :slack_title_failed, -> { nil }
90
-
91
- set :slack_pretext_updating, -> { nil }
92
- set :slack_pretext_reverting, -> { nil }
93
- set :slack_pretext_updated, -> { nil }
94
- set :slack_pretext_reverted, -> { nil }
95
- set :slack_pretext_failed, -> { nil }
96
-
97
- end
98
- end
@@ -1,3 +1,3 @@
1
1
  module Slackistrano
2
- VERSION = '3.0.1'
2
+ VERSION = '3.1.0.beta'
3
3
  end
data/slackistrano.gemspec CHANGED
@@ -21,7 +21,6 @@ Gem::Specification.new do |gem|
21
21
  gem.require_paths = ["lib"]
22
22
 
23
23
  gem.add_dependency 'capistrano', '>= 3.5.0'
24
- gem.add_dependency 'json'
25
24
  gem.add_development_dependency 'rake'
26
25
  gem.add_development_dependency 'rspec'
27
26
  gem.add_development_dependency 'pry'
data/spec/dry_run_spec.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Slackistrano do
4
- before(:each) do
5
- Rake::Task['load:defaults'].execute
4
+ before(:all) do
5
+ set :slackistrano, { klass: Slackistrano::Messaging::Default }
6
6
  end
7
7
 
8
8
  %w[updating reverting updated reverted failed].each do |stage|
9
9
  it "does not post to slack on slack:deploy:#{stage}" do
10
- set "slack_run_#{stage}".to_sym, -> { true }
11
- expect(Capistrano::Configuration).to receive(:dry_run?).and_return(true)
12
- expect(Slackistrano).not_to receive(:post)
10
+ allow_any_instance_of(Slackistrano::Capistrano).to receive(:dry_run?).and_return(true)
11
+ expect_any_instance_of(Slackistrano::Capistrano).to receive(:post_dry_run)
12
+ expect_any_instance_of(Slackistrano::Capistrano).not_to receive(:post_to_slack)
13
13
  Rake::Task["slack:deploy:#{stage}"].execute
14
14
  end
15
15
  end
@@ -1,27 +1,29 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Slackistrano do
3
+ describe Slackistrano::Messaging::Deprecated do
4
4
  before(:each) do
5
- Rake::Task['load:defaults'].execute
5
+ set :slackistrano, {}
6
6
  end
7
7
 
8
8
  %w[updating reverting updated reverted failed].each do |stage|
9
9
  it "posts to slack on slack:deploy:#{stage}" do
10
+ set :slack_run, ->{ true }
10
11
  set "slack_run_#{stage}".to_sym, ->{ true }
11
- expect_any_instance_of(Slackistrano::Capistrano).to receive(:post_to_slack).and_return(double(code: '200'))
12
+ expect_any_instance_of(Slackistrano::Capistrano).to receive(:post)
12
13
  Rake::Task["slack:deploy:#{stage}"].execute
13
14
  end
14
15
 
15
16
  it "does not post to slack on slack:deploy:#{stage} when disabled" do
17
+ set :slack_run, ->{ true }
16
18
  set "slack_run_#{stage}".to_sym, ->{ false }
17
- expect_any_instance_of(Slackistrano::Capistrano).not_to receive(:post_to_slack)
19
+ expect_any_instance_of(Slackistrano::Capistrano).not_to receive(:post)
18
20
  Rake::Task["slack:deploy:#{stage}"].execute
19
21
  end
20
22
 
21
23
  it "does not post to slack on slack:deploy:#{stage} when disabled globally" do
22
- set "slack_run".to_sym, ->{ false }
24
+ set :slack_run, ->{ false }
23
25
  set "slack_run_#{stage}".to_sym, ->{ true }
24
- expect_any_instance_of(Slackistrano::Capistrano).not_to receive(:post_to_slack)
26
+ expect_any_instance_of(Slackistrano::Capistrano).not_to receive(:post)
25
27
  Rake::Task["slack:deploy:#{stage}"].execute
26
28
  end
27
29
  end
@@ -30,25 +32,26 @@ describe Slackistrano do
30
32
  ['updating', nil, nil],
31
33
  ['reverting', nil, nil],
32
34
  ['updated', 'good', nil],
33
- ['reverted', '#4CBDEC', nil],
35
+ ['reverted', 'warning', nil],
34
36
  ['failed', 'danger', nil],
35
37
  ['updating', nil, 'starting_channel'],
36
38
  ['updated', 'good', 'finished_channel'],
37
- ['reverted', '#4CBDEC', 'rollback_channel'],
39
+ ['reverted', 'warning', 'rollback_channel'],
38
40
  ['failed', 'danger', 'failed_channel'],
39
41
  ].each do |stage, color, channel_for_stage|
40
42
 
41
- it "calls post_as_slack with the right arguments for stage=#{stage}, color=#{color}, channel_for_stage=#{channel_for_stage}" do
42
- set :"slack_run_#{stage}", -> { true }
43
+ it "calls post_to_slack with the right arguments for stage=#{stage}, color=#{color}, channel_for_stage=#{channel_for_stage}" do
44
+ set :slack_run, ->{ true }
45
+ set "slack_run_#{stage}".to_sym, -> { true }
43
46
  set :slack_team, -> { 'team' }
44
47
  set :slack_token, -> { 'token' }
45
48
  set :slack_webhook, -> { 'webhook' }
46
49
  set :slack_channel, -> { 'channel' }
47
50
  set :slack_icon_url, -> { 'http://icon.url' }
48
51
  set :slack_icon_emoji, -> { ':emoji:' }
49
- set :"slack_msg_#{stage}", -> { 'text message' }
52
+ set "slack_msg_#{stage}".to_sym, -> { 'text message' }
50
53
 
51
- set :"slack_channel_#{stage}", -> { channel_for_stage }
54
+ set "slack_channel_#{stage}".to_sym, -> { channel_for_stage }
52
55
  expected_channel = channel_for_stage || 'channel'
53
56
 
54
57
  attachment = {
@@ -59,19 +62,13 @@ describe Slackistrano do
59
62
  mrkdwn_in: [:text, :pretext]
60
63
  }.reject{|k,v| v.nil?}
61
64
 
62
- expect_any_instance_of(Slackistrano::Capistrano).to receive(:post_to_slack).with(
63
- team: 'team',
64
- token: 'token',
65
- webhook: 'webhook',
66
- via_slackbot: false,
67
- payload: {
65
+ expect_any_instance_of(Slackistrano::Capistrano).to receive(:post_to_slack).with({
68
66
  channel: expected_channel,
69
67
  username: 'Slackistrano',
70
68
  icon_url: 'http://icon.url',
71
69
  icon_emoji: ':emoji:',
72
70
  attachments: [attachment]
73
- }
74
- ).and_return(double(code: '200'))
71
+ }).and_return(double(code: '200'))
75
72
  Rake::Task["slack:deploy:#{stage}"].execute
76
73
  end
77
74
  end