slackistrano 2.0.0 → 2.0.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +1 -1
- data/lib/slackistrano/capistrano.rb +148 -32
- data/lib/slackistrano/tasks/slack.rake +10 -114
- data/lib/slackistrano/version.rb +1 -1
- data/spec/dry_run_spec.rb +17 -0
- data/spec/posting_to_multiple_channels_spec.rb +19 -0
- data/spec/task_hooks_spec.rb +35 -0
- data/spec/tasks_spec.rb +9 -53
- metadata +8 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 851d5d5bb98527246ddb446956250f5670007aca
         | 
| 4 | 
            +
              data.tar.gz: bf3821250a52f2c3c5fcd7212a192e38878fb56b
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 36d3edcd7e42fbcb65ca35e641922666ce9d3ee7b00acf3bcda56478041cb1d12fc46b8923a1fd4abfb992efd120daaa10aadb732638f3262188562068014734
         | 
| 7 | 
            +
              data.tar.gz: d8ebee206e6a4ab2ef7fa5f5304cfbe47c4d8d984fe59d4e847d502c3420e25510112ebab258a65b05f27277c99d6c6b444ba99cad92bf8318593afa283f505a
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -83,6 +83,7 @@ set :slack_icon_url,           -> { 'http://gravatar.com/avatar/885e1c523b7975c4 | |
| 83 83 | 
             
            set :slack_icon_emoji,         -> { nil } # Emoji to use. Overrides icon_url. Must be a string (ex: ':shipit:')
         | 
| 84 84 | 
             
            set :slack_username,           -> { 'Slackistrano' }
         | 
| 85 85 |  | 
| 86 | 
            +
            set :slack_run,                -> { true } # Set to false to disable all messages.
         | 
| 86 87 | 
             
            set :slack_run_updating,       -> { true } # Set to false to disable deploy starting message.
         | 
| 87 88 | 
             
            set :slack_run_reverting,      -> { true } # Set to false to disable rollback starting message.
         | 
| 88 89 | 
             
            set :slack_run_updated,        -> { true } # Set to false to disable deploy finished message.
         | 
| @@ -184,7 +185,6 @@ you specified. | |
| 184 185 | 
             
            ## TODO
         | 
| 185 186 |  | 
| 186 187 | 
             
            - Notify about incorrect configuration settings.
         | 
| 187 | 
            -
            - Notify about unsuccessfull HTTP POSTs.
         | 
| 188 188 |  | 
| 189 189 | 
             
            ## Contributing
         | 
| 190 190 |  | 
| @@ -1,50 +1,166 @@ | |
| 1 1 | 
             
            require 'net/http'
         | 
| 2 2 | 
             
            require 'json'
         | 
| 3 | 
            +
            require 'forwardable'
         | 
| 3 4 |  | 
| 4 5 | 
             
            load File.expand_path("../tasks/slack.rake", __FILE__)
         | 
| 5 6 |  | 
| 6 7 | 
             
            module Slackistrano
         | 
| 8 | 
            +
              class Capistrano
         | 
| 7 9 |  | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
                 | 
| 13 | 
            -
             | 
| 14 | 
            -
                 | 
| 15 | 
            -
                   | 
| 10 | 
            +
                extend Forwardable
         | 
| 11 | 
            +
                def_delegators :env, :fetch, :run_locally
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                #
         | 
| 14 | 
            +
                #
         | 
| 15 | 
            +
                #
         | 
| 16 | 
            +
                def initialize(env)
         | 
| 17 | 
            +
                  @env = env
         | 
| 16 18 | 
             
                end
         | 
| 17 | 
            -
              rescue => e
         | 
| 18 | 
            -
                error("[slackistrano] Error notifying Slack!")
         | 
| 19 | 
            -
                error("[slackistrano]   Error: #{e.inspect}")
         | 
| 20 | 
            -
              end
         | 
| 21 19 |  | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 20 | 
            +
                #
         | 
| 21 | 
            +
                #
         | 
| 22 | 
            +
                #
         | 
| 23 | 
            +
                def run(action)
         | 
| 24 | 
            +
                  should_run = fetch("slack_run".to_sym)
         | 
| 25 | 
            +
                  should_run &&= fetch("slack_run_#{action}".to_sym)
         | 
| 26 | 
            +
                  return unless should_run
         | 
| 27 27 |  | 
| 28 | 
            -
             | 
| 28 | 
            +
                  _self = self
         | 
| 29 | 
            +
                  run_locally { _self.post(action, self) }
         | 
| 29 30 |  | 
| 30 | 
            -
                Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
         | 
| 31 | 
            -
                  http.request_post uri, text
         | 
| 32 31 | 
             
                end
         | 
| 33 | 
            -
              end
         | 
| 34 32 |  | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 33 | 
            +
                #
         | 
| 34 | 
            +
                #
         | 
| 35 | 
            +
                #
         | 
| 36 | 
            +
                def post(action, backend)
         | 
| 37 | 
            +
                  team = fetch(:slack_team)
         | 
| 38 | 
            +
                  token = fetch(:slack_token)
         | 
| 39 | 
            +
                  webhook = fetch(:slack_webhook)
         | 
| 40 | 
            +
                  via_slackbot = fetch(:slack_via_slackbot)
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                  channels = fetch(:slack_channel)
         | 
| 43 | 
            +
                  channel_action = "slack_channel_#{action}".to_sym
         | 
| 44 | 
            +
                  channels = fetch(channel_action) if fetch(channel_action)
         | 
| 45 | 
            +
                  channels = Array(channels)
         | 
| 46 | 
            +
                  if via_slackbot == false && channels.empty?
         | 
| 47 | 
            +
                    channels = [nil] # default webhook channel
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                  payload = {
         | 
| 51 | 
            +
                    username: fetch(:slack_username),
         | 
| 52 | 
            +
                    icon_url: fetch(:slack_icon_url),
         | 
| 53 | 
            +
                    icon_emoji: fetch(:slack_icon_emoji),
         | 
| 54 | 
            +
                  }
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                  payload[:attachments] = case action
         | 
| 57 | 
            +
                                          when :updated
         | 
| 58 | 
            +
                                            make_attachments(action, color: 'good')
         | 
| 59 | 
            +
                                          when :reverted
         | 
| 60 | 
            +
                                            make_attachments(action, color: '#4CBDEC')
         | 
| 61 | 
            +
                                          when :failed
         | 
| 62 | 
            +
                                            make_attachments(action, color: 'danger')
         | 
| 63 | 
            +
                                          else
         | 
| 64 | 
            +
                                            make_attachments(action)
         | 
| 65 | 
            +
                                          end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                  channels.each do |channel|
         | 
| 68 | 
            +
                    payload[:channel] = channel
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                    # This is a nasty hack, but until Capistrano provides an official way to determine if
         | 
| 71 | 
            +
                    # --dry-run was passed this is the only option.
         | 
| 72 | 
            +
                    # See https://github.com/capistrano/capistrano/issues/1462
         | 
| 73 | 
            +
                    if ::Capistrano::Configuration.env.send(:config)[:sshkit_backend] == SSHKit::Backend::Printer
         | 
| 74 | 
            +
                      backend.info("[slackistrano] Slackistrano Dry Run:")
         | 
| 75 | 
            +
                      backend.info("[slackistrano]   Team: #{team}")
         | 
| 76 | 
            +
                      backend.info("[slackistrano]   Webhook: #{webhook}")
         | 
| 77 | 
            +
                      backend.info("[slackistrano]   Via Slackbot: #{via_slackbot}")
         | 
| 78 | 
            +
                      backend.info("[slackistrano]   Payload: #{payload.to_json}")
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                    # Post to the channel.
         | 
| 81 | 
            +
                    else
         | 
| 82 | 
            +
             | 
| 83 | 
            +
                      begin
         | 
| 84 | 
            +
                        response = post_to_slack(team: team,
         | 
| 85 | 
            +
                                                 token: token,
         | 
| 86 | 
            +
                                                 webhook: webhook,
         | 
| 87 | 
            +
                                                 via_slackbot: via_slackbot,
         | 
| 88 | 
            +
                                                 payload: payload)
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                      rescue => e
         | 
| 91 | 
            +
                        backend.warn("[slackistrano] Error notifying Slack!")
         | 
| 92 | 
            +
                        backend.warn("[slackistrano]   Error: #{e.inspect}")
         | 
| 93 | 
            +
                      end
         | 
| 94 | 
            +
             | 
| 95 | 
            +
                      if response.code !~ /^2/
         | 
| 96 | 
            +
                        warn("[slackistrano] Slack API Failure!")
         | 
| 97 | 
            +
                        warn("[slackistrano]   URI: #{response.uri}")
         | 
| 98 | 
            +
                        warn("[slackistrano]   Code: #{response.code}")
         | 
| 99 | 
            +
                        warn("[slackistrano]   Message: #{response.message}")
         | 
| 100 | 
            +
                        warn("[slackistrano]   Body: #{response.body}") if response.message != response.body && response.body !~ /<html/
         | 
| 101 | 
            +
                      end
         | 
| 102 | 
            +
                    end
         | 
| 103 | 
            +
                  end
         | 
| 40 104 |  | 
| 41 | 
            -
                if webhook.nil?
         | 
| 42 | 
            -
                  webhook = "https://#{team}.slack.com/services/hooks/incoming-webhook"
         | 
| 43 | 
            -
                  params.merge!('token' => token)
         | 
| 44 105 | 
             
                end
         | 
| 45 106 |  | 
| 46 | 
            -
                 | 
| 47 | 
            -
                 | 
| 48 | 
            -
             | 
| 107 | 
            +
                #
         | 
| 108 | 
            +
                #
         | 
| 109 | 
            +
                #
         | 
| 110 | 
            +
                def make_attachments(action, options={})
         | 
| 111 | 
            +
                  attachments = options.merge({
         | 
| 112 | 
            +
                    title:      fetch(:"slack_title_#{action}"),
         | 
| 113 | 
            +
                    pretext:    fetch(:"slack_pretext_#{action}"),
         | 
| 114 | 
            +
                    text:       fetch(:"slack_msg_#{action}"),
         | 
| 115 | 
            +
                    fields:     fetch(:"slack_fields_#{action}"),
         | 
| 116 | 
            +
                    fallback:   fetch(:"slack_fallback_#{action}"),
         | 
| 117 | 
            +
                    mrkdwn_in:  [:text, :pretext]
         | 
| 118 | 
            +
                  }).reject{|k, v| v.nil? }
         | 
| 119 | 
            +
                  [attachments]
         | 
| 120 | 
            +
                end
         | 
| 121 | 
            +
                private :make_attachments
         | 
| 122 | 
            +
             | 
| 123 | 
            +
                #
         | 
| 124 | 
            +
                #
         | 
| 125 | 
            +
                #
         | 
| 126 | 
            +
                def post_to_slack(via_slackbot: nil, team: nil, token: nil, webhook: nil, payload: {})
         | 
| 127 | 
            +
                  if via_slackbot
         | 
| 128 | 
            +
                    post_as_slackbot(team: team, token: token, webhook: webhook, payload: payload)
         | 
| 129 | 
            +
                  else
         | 
| 130 | 
            +
                    post_as_webhook(team: team, token: token, webhook: webhook, payload: payload)
         | 
| 131 | 
            +
                  end
         | 
| 132 | 
            +
                end
         | 
| 133 | 
            +
                private :post_to_slack
         | 
| 134 | 
            +
             | 
| 135 | 
            +
                #
         | 
| 136 | 
            +
                #
         | 
| 137 | 
            +
                #
         | 
| 138 | 
            +
                def post_as_slackbot(team: nil, token: nil, webhook: nil, payload: {})
         | 
| 139 | 
            +
                  uri = URI(URI.encode("https://#{team}.slack.com/services/hooks/slackbot?token=#{token}&channel=#{payload[:channel]}"))
         | 
| 140 | 
            +
             | 
| 141 | 
            +
                  text = payload[:attachments].collect { |a| a[:text] }.join("\n")
         | 
| 142 | 
            +
             | 
| 143 | 
            +
                  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
         | 
| 144 | 
            +
                    http.request_post uri, text
         | 
| 145 | 
            +
                  end
         | 
| 146 | 
            +
                end
         | 
| 147 | 
            +
                private :post_as_slackbot
         | 
| 148 | 
            +
             | 
| 149 | 
            +
                #
         | 
| 150 | 
            +
                #
         | 
| 151 | 
            +
                #
         | 
| 152 | 
            +
                def post_as_webhook(team: nil, token: nil, webhook: nil, payload: {})
         | 
| 153 | 
            +
                  params = {'payload' => payload.to_json}
         | 
| 154 | 
            +
             | 
| 155 | 
            +
                  if webhook.nil?
         | 
| 156 | 
            +
                    webhook = "https://#{team}.slack.com/services/hooks/incoming-webhook"
         | 
| 157 | 
            +
                    params.merge!('token' => token)
         | 
| 158 | 
            +
                  end
         | 
| 49 159 |  | 
| 160 | 
            +
                  uri = URI(webhook)
         | 
| 161 | 
            +
                  Net::HTTP.post_form(uri, params)
         | 
| 162 | 
            +
                end
         | 
| 163 | 
            +
                private :post_as_webhook
         | 
| 164 | 
            +
             | 
| 165 | 
            +
              end
         | 
| 50 166 | 
             
            end
         | 
| @@ -1,131 +1,26 @@ | |
| 1 1 | 
             
            namespace :slack do
         | 
| 2 2 | 
             
              namespace :deploy do
         | 
| 3 3 |  | 
| 4 | 
            -
                def make_attachments(stage, options={})
         | 
| 5 | 
            -
                  attachments = options.merge({
         | 
| 6 | 
            -
                    title: fetch(:"slack_title_#{stage}"),
         | 
| 7 | 
            -
                    pretext: fetch(:"slack_pretext_#{stage}"),
         | 
| 8 | 
            -
                    text: fetch(:"slack_msg_#{stage}"),
         | 
| 9 | 
            -
                    fields: fetch(:"slack_fields_#{stage}"),
         | 
| 10 | 
            -
                    fallback: fetch(:"slack_fallback_#{stage}"),
         | 
| 11 | 
            -
                    mrkdwn_in: [:text, :pretext]
         | 
| 12 | 
            -
                  }).reject{|k, v| v.nil? }
         | 
| 13 | 
            -
                  [attachments]
         | 
| 14 | 
            -
                end
         | 
| 15 | 
            -
             | 
| 16 | 
            -
                def make_payload(stage)
         | 
| 17 | 
            -
                  payload = {
         | 
| 18 | 
            -
                    username: fetch(:slack_username),
         | 
| 19 | 
            -
                    icon_url: fetch(:slack_icon_url),
         | 
| 20 | 
            -
                    icon_emoji: fetch(:slack_icon_emoji),
         | 
| 21 | 
            -
                  }
         | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
                  payload[:attachments] = case stage
         | 
| 25 | 
            -
                                        when :updated
         | 
| 26 | 
            -
                                          make_attachments(stage, color: 'good')
         | 
| 27 | 
            -
                                        when :reverted
         | 
| 28 | 
            -
                                          make_attachments(stage, color: '#4CBDEC')
         | 
| 29 | 
            -
                                        when :failed
         | 
| 30 | 
            -
                                          make_attachments(stage, color: 'danger')
         | 
| 31 | 
            -
                                        else
         | 
| 32 | 
            -
                                          make_attachments(stage)
         | 
| 33 | 
            -
                                        end
         | 
| 34 | 
            -
             | 
| 35 | 
            -
                  payload
         | 
| 36 | 
            -
                end
         | 
| 37 | 
            -
             | 
| 38 | 
            -
                def post_message(stage)
         | 
| 39 | 
            -
                  team = fetch(:slack_team)
         | 
| 40 | 
            -
                  token = fetch(:slack_token)
         | 
| 41 | 
            -
                  webhook = fetch(:slack_webhook)
         | 
| 42 | 
            -
                  via_slackbot = fetch(:slack_via_slackbot)
         | 
| 43 | 
            -
                  payload = make_payload(stage)
         | 
| 44 | 
            -
             | 
| 45 | 
            -
                  channels = fetch(:slack_channel)
         | 
| 46 | 
            -
                  stage_channel = "slack_channel_#{stage.to_s}".to_sym
         | 
| 47 | 
            -
                  if fetch(stage_channel)
         | 
| 48 | 
            -
                    channels = fetch(stage_channel)
         | 
| 49 | 
            -
                  end
         | 
| 50 | 
            -
                  channels = Array(channels)
         | 
| 51 | 
            -
                  if via_slackbot == false && channels.empty?
         | 
| 52 | 
            -
                    channels = [nil] # default webhook channel
         | 
| 53 | 
            -
                  end
         | 
| 54 | 
            -
             | 
| 55 | 
            -
                  channels.each do |channel|
         | 
| 56 | 
            -
                    payload[:channel] = channel
         | 
| 57 | 
            -
             | 
| 58 | 
            -
                    # This is a nasty hack, but until Capistrano provides an official way to determine if
         | 
| 59 | 
            -
                    # --dry-run was passed this is the only option.
         | 
| 60 | 
            -
                    # See https://github.com/capistrano/capistrano/issues/1462
         | 
| 61 | 
            -
                    if Capistrano::Configuration.env.send(:config)[:sshkit_backend] == SSHKit::Backend::Printer
         | 
| 62 | 
            -
                      info("[slackistrano] Slackistrano Dry Run:")
         | 
| 63 | 
            -
                      info("[slackistrano]   Team: #{team}")
         | 
| 64 | 
            -
                      info("[slackistrano]   Webhook: #{webhook}")
         | 
| 65 | 
            -
                      info("[slackistrano]   Via Slackbot: #{via_slackbot}")
         | 
| 66 | 
            -
                      info("[slackistrano]   Payload: #{payload.to_json}")
         | 
| 67 | 
            -
             | 
| 68 | 
            -
                    # Post to the channel.
         | 
| 69 | 
            -
                    else
         | 
| 70 | 
            -
                      http_response = Slackistrano.post(team: team,
         | 
| 71 | 
            -
                                                        token: token,
         | 
| 72 | 
            -
                                                        webhook: webhook,
         | 
| 73 | 
            -
                                                        via_slackbot: via_slackbot,
         | 
| 74 | 
            -
                                                        payload: payload)
         | 
| 75 | 
            -
                      if http_response.code !~ /^2/
         | 
| 76 | 
            -
                        error("[slackistrano] Slack API Failure!")
         | 
| 77 | 
            -
                        error("[slackistrano]   URI: #{http_response.uri}")
         | 
| 78 | 
            -
                        error("[slackistrano]   Code: #{http_response.code}")
         | 
| 79 | 
            -
                        error("[slackistrano]   Message: #{http_response.message}")
         | 
| 80 | 
            -
                        error("[slackistrano]   Body: #{http_response.body}") if http_response.message != http_response.body
         | 
| 81 | 
            -
                      end
         | 
| 82 | 
            -
                    end
         | 
| 83 | 
            -
                  end
         | 
| 84 | 
            -
             | 
| 85 | 
            -
                end
         | 
| 86 | 
            -
             | 
| 87 | 
            -
                ######################################################################
         | 
| 88 | 
            -
             | 
| 89 4 | 
             
                task :updating do
         | 
| 90 5 | 
             
                  set(:slack_deploy_or_rollback, 'deploy')
         | 
| 91 | 
            -
                   | 
| 92 | 
            -
                    run_locally do
         | 
| 93 | 
            -
                      post_message(:updating)
         | 
| 94 | 
            -
                    end
         | 
| 95 | 
            -
                  end
         | 
| 6 | 
            +
                  Slackistrano::Capistrano.new(self).run(:updating)
         | 
| 96 7 | 
             
                end
         | 
| 97 8 |  | 
| 98 9 | 
             
                task :reverting do
         | 
| 99 10 | 
             
                  set(:slack_deploy_or_rollback, 'rollback')
         | 
| 100 | 
            -
                   | 
| 101 | 
            -
                    run_locally do
         | 
| 102 | 
            -
                      post_message(:reverting)
         | 
| 103 | 
            -
                    end
         | 
| 104 | 
            -
                  end
         | 
| 11 | 
            +
                  Slackistrano::Capistrano.new(self).run(:reverting)
         | 
| 105 12 | 
             
                end
         | 
| 106 13 |  | 
| 107 14 | 
             
                task :updated do
         | 
| 108 | 
            -
                   | 
| 109 | 
            -
                    run_locally do
         | 
| 110 | 
            -
                       post_message(:updated)
         | 
| 111 | 
            -
                    end
         | 
| 112 | 
            -
                  end
         | 
| 15 | 
            +
                  Slackistrano::Capistrano.new(self).run(:updated)
         | 
| 113 16 | 
             
                end
         | 
| 114 17 |  | 
| 115 18 | 
             
                task :reverted do
         | 
| 116 | 
            -
                   | 
| 117 | 
            -
                    run_locally do
         | 
| 118 | 
            -
                      post_message(:reverted)
         | 
| 119 | 
            -
                    end
         | 
| 120 | 
            -
                  end
         | 
| 19 | 
            +
                  Slackistrano::Capistrano.new(self).run(:reverted)
         | 
| 121 20 | 
             
                end
         | 
| 122 21 |  | 
| 123 22 | 
             
                task :failed do
         | 
| 124 | 
            -
                   | 
| 125 | 
            -
                    run_locally do
         | 
| 126 | 
            -
                      post_message(:failed)
         | 
| 127 | 
            -
                    end
         | 
| 128 | 
            -
                  end
         | 
| 23 | 
            +
                  Slackistrano::Capistrano.new(self).run(:failed)
         | 
| 129 24 | 
             
                end
         | 
| 130 25 |  | 
| 131 26 | 
             
              end
         | 
| @@ -140,11 +35,11 @@ after  'deploy:failed',             'slack:deploy:failed' | |
| 140 35 | 
             
            namespace :load do
         | 
| 141 36 | 
             
              task :defaults do
         | 
| 142 37 |  | 
| 143 | 
            -
                set :slack_team,               -> { nil } | 
| 144 | 
            -
                set :slack_token,              -> { nil } | 
| 145 | 
            -
                set :slack_webhook,            -> { nil } | 
| 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.
         | 
| 146 41 | 
             
                set :slack_via_slackbot,       -> { false } # Set to true to send the message via slackbot instead of webhook
         | 
| 147 | 
            -
                set :slack_channel,            -> { nil } | 
| 42 | 
            +
                set :slack_channel,            -> { nil }   # Channel to post to. Optional. Defaults to WebHook setting.
         | 
| 148 43 |  | 
| 149 44 | 
             
                # Optional, overridable settings
         | 
| 150 45 |  | 
| @@ -158,6 +53,7 @@ namespace :load do | |
| 158 53 | 
             
                set :slack_icon_emoji,         -> { nil } # Emoji to use. Overrides icon_url. Must be a string (ex: ':shipit:')
         | 
| 159 54 | 
             
                set :slack_username,           -> { 'Slackistrano' }
         | 
| 160 55 |  | 
| 56 | 
            +
                set :slack_run,                -> { true } # Set to false to disable all messages.
         | 
| 161 57 | 
             
                set :slack_run_updating,       -> { true } # Set to false to disable deploy starting message.
         | 
| 162 58 | 
             
                set :slack_run_reverting,      -> { true } # Set to false to disable rollback starting message.
         | 
| 163 59 | 
             
                set :slack_run_updated,        -> { true } # Set to false to disable deploy finished message.
         | 
    
        data/lib/slackistrano/version.rb
    CHANGED
    
    
| @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe Slackistrano do
         | 
| 4 | 
            +
              before(:each) do
         | 
| 5 | 
            +
                Rake::Task['load:defaults'].execute
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              %w[updating reverting updated reverted failed].each do |stage|
         | 
| 9 | 
            +
                it "does not post to slack on slack:deploy:#{stage}" do
         | 
| 10 | 
            +
                  set "slack_run_#{stage}".to_sym, ->{ true }
         | 
| 11 | 
            +
                  expect(Capistrano::Configuration.env.send(:config)).to receive(:[]).with(:sshkit_backend).and_return(SSHKit::Backend::Printer)
         | 
| 12 | 
            +
                  expect(Slackistrano).not_to receive(:post)
         | 
| 13 | 
            +
                  Rake::Task["slack:deploy:#{stage}"].execute
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            end
         | 
| @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe Slackistrano do
         | 
| 4 | 
            +
              before(:each) do
         | 
| 5 | 
            +
                Rake::Task['load:defaults'].execute
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              context "when :slack_channel is an array" do
         | 
| 9 | 
            +
                %w[updating reverting updated reverted failed].each do |stage|
         | 
| 10 | 
            +
                  it "posts to slack on slack:deploy:#{stage} in every channel" do
         | 
| 11 | 
            +
                    set "slack_channel".to_sym, ->{ %w[one two] }
         | 
| 12 | 
            +
                    set "slack_run_#{stage}".to_sym, ->{ true }
         | 
| 13 | 
            +
                    expect_any_instance_of(Slackistrano::Capistrano).to receive(:post_to_slack).twice.and_return(double(code: '200'))
         | 
| 14 | 
            +
                    Rake::Task["slack:deploy:#{stage}"].execute
         | 
| 15 | 
            +
                  end
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            end
         | 
| @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe Slackistrano do
         | 
| 4 | 
            +
              before(:each) do
         | 
| 5 | 
            +
                Rake::Task['load:defaults'].execute
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              describe "before/after hooks" do
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                it "invokes slack:deploy:updating before deploy:updating" do
         | 
| 11 | 
            +
                  expect(Rake::Task['deploy:updating'].prerequisites).to include 'slack:deploy:updating'
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                it "invokes slack:deploy:reverting before deploy:reverting" do
         | 
| 15 | 
            +
                  expect(Rake::Task['deploy:reverting'].prerequisites).to include 'slack:deploy:reverting'
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                it "invokes slack:deploy:updated after deploy:finishing" do
         | 
| 19 | 
            +
                  expect(Rake::Task['slack:deploy:updated']).to receive(:invoke)
         | 
| 20 | 
            +
                  Rake::Task['deploy:finishing'].execute
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                it "invokes slack:deploy:reverted after deploy:finishing_rollback" do
         | 
| 24 | 
            +
                  expect(Rake::Task['slack:deploy:reverted']).to receive(:invoke)
         | 
| 25 | 
            +
                  Rake::Task['deploy:finishing_rollback'].execute
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                it "invokes slack:deploy:failed after deploy:failed" do
         | 
| 29 | 
            +
                  expect(Rake::Task['slack:deploy:failed']).to receive(:invoke)
         | 
| 30 | 
            +
                  Rake::Task['deploy:failed'].execute
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
              end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            end
         | 
    
        data/spec/tasks_spec.rb
    CHANGED
    
    | @@ -5,70 +5,27 @@ describe Slackistrano do | |
| 5 5 | 
             
                Rake::Task['load:defaults'].execute
         | 
| 6 6 | 
             
              end
         | 
| 7 7 |  | 
| 8 | 
            -
              describe "before/after hooks" do
         | 
| 9 | 
            -
             | 
| 10 | 
            -
                it "invokes slack:deploy:updating before deploy:updating" do
         | 
| 11 | 
            -
                  expect(Rake::Task['deploy:updating'].prerequisites).to include 'slack:deploy:updating'
         | 
| 12 | 
            -
                end
         | 
| 13 | 
            -
             | 
| 14 | 
            -
                it "invokes slack:deploy:reverting before deploy:reverting" do
         | 
| 15 | 
            -
                  expect(Rake::Task['deploy:reverting'].prerequisites).to include 'slack:deploy:reverting'
         | 
| 16 | 
            -
                end
         | 
| 17 | 
            -
             | 
| 18 | 
            -
                it "invokes slack:deploy:updated after deploy:finishing" do
         | 
| 19 | 
            -
                  expect(Rake::Task['slack:deploy:updated']).to receive(:invoke)
         | 
| 20 | 
            -
                  Rake::Task['deploy:finishing'].execute
         | 
| 21 | 
            -
                end
         | 
| 22 | 
            -
             | 
| 23 | 
            -
                it "invokes slack:deploy:reverted after deploy:finishing_rollback" do
         | 
| 24 | 
            -
                  expect(Rake::Task['slack:deploy:reverted']).to receive(:invoke)
         | 
| 25 | 
            -
                  Rake::Task['deploy:finishing_rollback'].execute
         | 
| 26 | 
            -
                end
         | 
| 27 | 
            -
             | 
| 28 | 
            -
                it "invokes slack:deploy:failed after deploy:failed" do
         | 
| 29 | 
            -
                  expect(Rake::Task['slack:deploy:failed']).to receive(:invoke)
         | 
| 30 | 
            -
                  Rake::Task['deploy:failed'].execute
         | 
| 31 | 
            -
                end
         | 
| 32 | 
            -
             | 
| 33 | 
            -
              end
         | 
| 34 | 
            -
             | 
| 35 8 | 
             
              %w[updating reverting updated reverted failed].each do |stage|
         | 
| 36 9 | 
             
                it "posts to slack on slack:deploy:#{stage}" do
         | 
| 37 10 | 
             
                  set "slack_run_#{stage}".to_sym, ->{ true }
         | 
| 38 | 
            -
                   | 
| 11 | 
            +
                  expect_any_instance_of(Slackistrano::Capistrano).to receive(:post_to_slack).and_return(double(code: '200'))
         | 
| 39 12 | 
             
                  Rake::Task["slack:deploy:#{stage}"].execute
         | 
| 40 13 | 
             
                end
         | 
| 41 14 |  | 
| 42 15 | 
             
                it "does not post to slack on slack:deploy:#{stage} when disabled" do
         | 
| 43 16 | 
             
                  set "slack_run_#{stage}".to_sym, ->{ false }
         | 
| 44 | 
            -
                   | 
| 17 | 
            +
                  expect_any_instance_of(Slackistrano::Capistrano).not_to receive(:post_to_slack)
         | 
| 45 18 | 
             
                  Rake::Task["slack:deploy:#{stage}"].execute
         | 
| 46 19 | 
             
                end
         | 
| 47 | 
            -
              end
         | 
| 48 | 
            -
             | 
| 49 | 
            -
              context "when :slack_channel is an array" do
         | 
| 50 | 
            -
                %w[updating reverting updated reverted failed].each do |stage|
         | 
| 51 | 
            -
                  it "posts to slack on slack:deploy:#{stage} in every channel" do
         | 
| 52 | 
            -
                    set "slack_channel".to_sym, ->{ %w[one two] }
         | 
| 53 | 
            -
                    set "slack_run_#{stage}".to_sym, ->{ true }
         | 
| 54 | 
            -
                    expect(Slackistrano).to receive(:post).twice.and_return(double(code: '200'))
         | 
| 55 | 
            -
                    Rake::Task["slack:deploy:#{stage}"].execute
         | 
| 56 | 
            -
                  end
         | 
| 57 | 
            -
                end
         | 
| 58 | 
            -
              end
         | 
| 59 20 |  | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
                   | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
                    expect(Slackistrano).not_to receive(:post)
         | 
| 66 | 
            -
                    Rake::Task["slack:deploy:#{stage}"].execute
         | 
| 67 | 
            -
                  end
         | 
| 21 | 
            +
                it "does not post to slack on slack:deploy:#{stage} when disabled globally" do
         | 
| 22 | 
            +
                  set "slack_run".to_sym, ->{ false }
         | 
| 23 | 
            +
                  set "slack_run_#{stage}".to_sym, ->{ true }
         | 
| 24 | 
            +
                  expect_any_instance_of(Slackistrano::Capistrano).not_to receive(:post_to_slack)
         | 
| 25 | 
            +
                  Rake::Task["slack:deploy:#{stage}"].execute
         | 
| 68 26 | 
             
                end
         | 
| 69 27 | 
             
              end
         | 
| 70 28 |  | 
| 71 | 
            -
             | 
| 72 29 | 
             
              [ # stage, color, channel
         | 
| 73 30 | 
             
                ['updating', nil, nil],
         | 
| 74 31 | 
             
                ['reverting', nil, nil],
         | 
| @@ -81,7 +38,7 @@ describe Slackistrano do | |
| 81 38 | 
             
                ['failed', 'danger', 'failed_channel'],
         | 
| 82 39 | 
             
              ].each do |stage, color, channel_for_stage|
         | 
| 83 40 |  | 
| 84 | 
            -
                it "calls  | 
| 41 | 
            +
                it "calls post_as_slack with the right arguments for stage=#{stage}, color=#{color}, channel_for_stage=#{channel_for_stage}" do
         | 
| 85 42 | 
             
                  set :"slack_run_#{stage}", -> { true }
         | 
| 86 43 | 
             
                  set :slack_team,           -> { 'team' }
         | 
| 87 44 | 
             
                  set :slack_token,          -> { 'token' }
         | 
| @@ -102,7 +59,7 @@ describe Slackistrano do | |
| 102 59 | 
             
                    mrkdwn_in: [:text, :pretext]
         | 
| 103 60 | 
             
                  }.reject{|k,v| v.nil?}
         | 
| 104 61 |  | 
| 105 | 
            -
                   | 
| 62 | 
            +
                  expect_any_instance_of(Slackistrano::Capistrano).to receive(:post_to_slack).with(
         | 
| 106 63 | 
             
                    team: 'team',
         | 
| 107 64 | 
             
                    token: 'token',
         | 
| 108 65 | 
             
                    webhook: 'webhook',
         | 
| @@ -119,5 +76,4 @@ describe Slackistrano do | |
| 119 76 | 
             
                end
         | 
| 120 77 | 
             
              end
         | 
| 121 78 |  | 
| 122 | 
            -
             | 
| 123 79 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: slackistrano
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 2.0. | 
| 4 | 
            +
              version: 2.0.1
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Philip Hallstrom
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2016-01- | 
| 11 | 
            +
            date: 2016-01-20 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: capistrano
         | 
| @@ -101,7 +101,10 @@ files: | |
| 101 101 | 
             
            - lib/slackistrano/version.rb
         | 
| 102 102 | 
             
            - slackistrano.gemspec
         | 
| 103 103 | 
             
            - spec/capistrano_deploy_stubs.rake
         | 
| 104 | 
            +
            - spec/dry_run_spec.rb
         | 
| 105 | 
            +
            - spec/posting_to_multiple_channels_spec.rb
         | 
| 104 106 | 
             
            - spec/spec_helper.rb
         | 
| 107 | 
            +
            - spec/task_hooks_spec.rb
         | 
| 105 108 | 
             
            - spec/tasks_spec.rb
         | 
| 106 109 | 
             
            homepage: https://github.com/phallstrom/slackistrano
         | 
| 107 110 | 
             
            licenses:
         | 
| @@ -133,5 +136,8 @@ specification_version: 4 | |
| 133 136 | 
             
            summary: Send notifications to Slack about Capistrano deployments.
         | 
| 134 137 | 
             
            test_files:
         | 
| 135 138 | 
             
            - spec/capistrano_deploy_stubs.rake
         | 
| 139 | 
            +
            - spec/dry_run_spec.rb
         | 
| 140 | 
            +
            - spec/posting_to_multiple_channels_spec.rb
         | 
| 136 141 | 
             
            - spec/spec_helper.rb
         | 
| 142 | 
            +
            - spec/task_hooks_spec.rb
         | 
| 137 143 | 
             
            - spec/tasks_spec.rb
         |