capistrano-slack-notify 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/capistrano/slack_notify.rb +80 -23
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcd05a29bff6366c6eba507d2c491df5809ea39c
4
- data.tar.gz: 62fbc194ae3fc389203a451f7427975a89f6b9b9
3
+ metadata.gz: 597babae6822e2dffabad72f6607efe7b008718e
4
+ data.tar.gz: 5b89b96750008e0fc649c748f109285a82762c67
5
5
  SHA512:
6
- metadata.gz: 1ce6698903ff89f0a9f2602304a6454a74409ebd0da2c3fa4b8978f8038462e4bfbb28c2404d37dacb0246fb5f90354104cbe5b54b3e17aa375f7fcfe4475bb7
7
- data.tar.gz: 7cb96ebc8c94152c0618c81871b9554a2eba9ffff6661471de1b28fba0e9ecd35ec0b6779d1e7babe0e852e9f71150f8a7da81e45c59ac1c41bc4ce3fd8be5fe
6
+ metadata.gz: 5ab71fcc8597b01deff8578c7733e5b3d1514ea8f40bd21d9d38bf8753137c7ef790bb8a0935ad637f03aee91695fbfa0f2c8c6b5568f9e52a154fb3e3d8380a
7
+ data.tar.gz: b8cfc1bdc15e1cd897a1ee276283065780c6a244ec18312c54a7160cac5ecf525c3c55a75ef2df2ef5a86e73afc00ef54e9b04ff46d313d45d57d2d21dd52ca2
@@ -4,46 +4,102 @@ require 'net/http'
4
4
 
5
5
  module Capistrano
6
6
  module SlackNotify
7
- def call_slack_api(message)
7
+ HEX_COLORS = {
8
+ :grey => '#CCCCCC',
9
+ :red => '#BB0000',
10
+ :green => '#7CD197',
11
+ :blue => '#103FFB'
12
+ }
13
+
14
+ def post_to_channel(color, message)
15
+ if use_color?
16
+ call_slack_api(attachment_payload(color, message))
17
+ else
18
+ call_slack_api(regular_payload(message))
19
+ end
20
+ end
21
+
22
+ def call_slack_api(payload)
8
23
  uri = URI.parse(slack_webhook_url)
9
24
  http = Net::HTTP.new(uri.host, uri.port)
10
25
  http.use_ssl = true
11
26
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
12
27
  request = Net::HTTP::Post.new(uri.request_uri)
13
- request.set_form_data(:payload => payload(message))
28
+ request.set_form_data(:payload => payload)
14
29
  http.request(request)
15
30
  rescue SocketError => e
16
31
  puts "#{e.message} or slack may be down"
17
32
  end
18
33
 
19
- def slack_defaults
34
+ def regular_payload(announcement)
35
+ {
36
+ 'channel' => slack_channel,
37
+ 'username' => slack_username,
38
+ 'text' => announcement,
39
+ 'icon_emoji' => slack_emoji,
40
+ 'mrkdwn' => true
41
+ }.to_json
20
42
  end
21
43
 
22
- def payload(announcement)
44
+ def attachment_payload(color, announcement)
23
45
  {
24
- 'channel' => fetch(:slack_room, '#platform'),
25
- 'username' => fetch(:slack_username, 'capistrano'),
26
- 'text' => announcement,
27
- 'icon_emoji' => fetch(:slack_emoji, ':rocket:')
46
+ 'channel' => slack_channel,
47
+ 'username' => slack_username,
48
+ 'attachments' => [{
49
+ 'fallback' => announcement,
50
+ 'text' => announcement,
51
+ 'color' => HEX_COLORS[color],
52
+ 'mrkdwn_in' => %w{text}
53
+ }]
28
54
  }.to_json
29
55
  end
30
56
 
57
+ def use_color?
58
+ fetch(:slack_color, true)
59
+ end
60
+
31
61
  def slack_webhook_url
32
62
  fetch(:slack_webhook_url)
33
63
  end
34
64
 
65
+ def slack_channel
66
+ fetch(:slack_room, '#platform')
67
+ end
68
+
69
+ def slack_username
70
+ fetch(:slack_username, 'capistrano')
71
+ end
72
+
73
+ def slack_emoji
74
+ fetch(:slack_emoji, ':rocket:')
75
+ end
76
+
35
77
  def slack_app_name
36
78
  fetch(:slack_app_name, fetch(:application))
37
79
  end
38
80
 
81
+ def deployer
82
+ fetch(:deployer)
83
+ end
84
+
85
+ def stage
86
+ fetch(:stage, 'production')
87
+ end
88
+
89
+ def revision
90
+ `git rev-parse #{branch}`.chomp
91
+ end
92
+
93
+ def deploy_target
94
+ [
95
+ slack_app_name,
96
+ branch
97
+ ].join('/') + " (#{revision[0..5]})"
98
+ end
99
+
39
100
  def self.extended(configuration)
40
101
  configuration.load do
41
102
  # Add the default hooks by default.
42
- if fetch(:slack_deploy_defaults, true)
43
- before 'deploy', 'slack:starting'
44
- after 'deploy', 'slack:finished'
45
- end
46
-
47
103
  set :deployer do
48
104
  ENV['USER'] || ENV['GIT_AUTHOR_NAME'] || `git config user.name`.chomp
49
105
  end
@@ -51,25 +107,26 @@ module Capistrano
51
107
  namespace :slack do
52
108
  desc "Notify Slack that the deploy has started."
53
109
  task :starting do
54
- msg = if branch = fetch(:branch, nil)
55
- "#{fetch(:deployer)} is deploying #{slack_app_name}/#{branch} to #{fetch(:stage, 'production')}"
56
- else
57
- "#{fetch(:deployer)} is deploying #{slack_app_name} to #{fetch(:stage, 'production')}"
58
- end
59
- call_slack_api(msg)
110
+ post_to_channel(:grey, "#{deployer} is deploying #{deploy_target} to #{stage}")
60
111
  set(:start_time, Time.now)
61
112
  end
62
113
 
63
114
  desc "Notify Slack that the deploy has completed successfully."
64
115
  task :finished do
65
- msg = "#{fetch(:deployer)} deployed to #{slack_app_name} successfully"
116
+ msg = "#{deployer} deployed #{deploy_target} to #{stage} *successfully*"
117
+
66
118
  if start_time = fetch(:start_time, nil)
67
- elapsed = Time.now.to_i - start_time.to_i
68
- msg << " in #{elapsed} seconds."
119
+ msg << " in #{Time.now.to_i - start_time.to_i} seconds."
69
120
  else
70
121
  msg << "."
71
122
  end
72
- call_slack_api(msg)
123
+
124
+ post_to_channel(:green, msg)
125
+ end
126
+
127
+ desc "Notify Slack that the deploy failed."
128
+ task :failed do
129
+ post_to_channel(:red, "#{deployer} *failed* to deploy #{deploy_target} to #{stage}")
73
130
  end
74
131
  end # end namespace :slack
75
132
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-slack-notify
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parker Moore
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-12 00:00:00.000000000 Z
11
+ date: 2015-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capistrano