Class: Slackistrano::Capistrano

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/slackistrano/capistrano.rb

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Capistrano

Returns a new instance of Capistrano.



16
17
18
# File 'lib/slackistrano/capistrano.rb', line 16

def initialize(env)
  @env = env
end

Instance Method Details

#post(action, backend) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/slackistrano/capistrano.rb', line 36

def post(action, backend)
  team = fetch(:slack_team)
  token = fetch(:slack_token)
  webhook = fetch(:slack_webhook)
  via_slackbot = fetch(:slack_via_slackbot)

  channels = fetch(:slack_channel)
  channel_action = "slack_channel_#{action}".to_sym
  channels = fetch(channel_action) if fetch(channel_action)
  channels = Array(channels)
  if via_slackbot == false && channels.empty?
    channels = [nil] # default webhook channel
  end

  payload = {
    username: fetch(:slack_username),
    icon_url: fetch(:slack_icon_url),
    icon_emoji: fetch(:slack_icon_emoji),
  }

  payload[:attachments] = case action
                          when :updated
                            make_attachments(action, color: 'good')
                          when :reverted
                            make_attachments(action, color: '#4CBDEC')
                          when :failed
                            make_attachments(action, color: 'danger')
                          else
                            make_attachments(action)
                          end

  channels.each do |channel|
    payload[:channel] = channel

    # This is a nasty hack, but until Capistrano provides an official way to determine if
    # --dry-run was passed this is the only option.
    # See https://github.com/capistrano/capistrano/issues/1462
    if ::Capistrano::Configuration.env.send(:config)[:sshkit_backend] == SSHKit::Backend::Printer
      backend.info("[slackistrano] Slackistrano Dry Run:")
      backend.info("[slackistrano]   Team: #{team}")
      backend.info("[slackistrano]   Webhook: #{webhook}")
      backend.info("[slackistrano]   Via Slackbot: #{via_slackbot}")
      backend.info("[slackistrano]   Payload: #{payload.to_json}")

    # Post to the channel.
    else

      begin
        response = post_to_slack(team: team,
                                 token: token,
                                 webhook: webhook,
                                 via_slackbot: via_slackbot,
                                 payload: payload)

      rescue => e
        backend.warn("[slackistrano] Error notifying Slack!")
        backend.warn("[slackistrano]   Error: #{e.inspect}")
      end

      if response.code !~ /^2/
        warn("[slackistrano] Slack API Failure!")
        warn("[slackistrano]   URI: #{response.uri}")
        warn("[slackistrano]   Code: #{response.code}")
        warn("[slackistrano]   Message: #{response.message}")
        warn("[slackistrano]   Body: #{response.body}") if response.message != response.body && response.body !~ /<html/
      end
    end
  end

end

#run(action) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/slackistrano/capistrano.rb', line 23

def run(action)
  should_run = fetch("slack_run".to_sym)
  should_run &&= fetch("slack_run_#{action}".to_sym)
  return unless should_run

  _self = self
  run_locally { _self.post(action, self) }

end