cap-slack-notify 1.3.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.
@@ -0,0 +1,142 @@
1
+ require 'capistrano'
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'net/https'
5
+
6
+ module Capistrano
7
+ module SlackNotify
8
+ HEX_COLORS = {
9
+ :grey => '#CCCCCC',
10
+ :red => '#BB0000',
11
+ :green => '#7CD197',
12
+ :blue => '#103FFB'
13
+ }
14
+
15
+ def post_to_channel(color, message)
16
+ if use_color?
17
+ call_slack_api(attachment_payload(color, message))
18
+ else
19
+ call_slack_api(regular_payload(message))
20
+ end
21
+ end
22
+
23
+ def call_slack_api(payload)
24
+ uri = URI.parse(slack_webhook_url)
25
+ http = Net::HTTP.new(uri.host, uri.port)
26
+ http.use_ssl = true
27
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
28
+ request = Net::HTTP::Post.new(uri.request_uri)
29
+ request.set_form_data(:payload => payload)
30
+ http.request(request)
31
+ rescue SocketError => e
32
+ puts "#{e.message} or slack may be down"
33
+ end
34
+
35
+ def regular_payload(announcement)
36
+ {
37
+ 'channel' => slack_channel,
38
+ 'username' => slack_username,
39
+ 'text' => announcement,
40
+ 'icon_emoji' => slack_emoji,
41
+ 'mrkdwn' => true
42
+ }.to_json
43
+ end
44
+
45
+ def attachment_payload(color, announcement)
46
+ {
47
+ 'channel' => slack_channel,
48
+ 'username' => slack_username,
49
+ 'attachments' => [{
50
+ 'fallback' => announcement,
51
+ 'text' => announcement,
52
+ 'color' => HEX_COLORS[color],
53
+ 'mrkdwn_in' => %w{text}
54
+ }]
55
+ }.to_json
56
+ end
57
+
58
+ def use_color?
59
+ fetch(:slack_color, true)
60
+ end
61
+
62
+ def slack_webhook_url
63
+ fetch(:slack_webhook_url)
64
+ end
65
+
66
+ def slack_channel
67
+ fetch(:slack_room, '#platform')
68
+ end
69
+
70
+ def slack_username
71
+ fetch(:slack_username, 'capistrano')
72
+ end
73
+
74
+ def slack_emoji
75
+ fetch(:slack_emoji, ':rocket:')
76
+ end
77
+
78
+ def slack_app_name
79
+ fetch(:slack_app_name, fetch(:application))
80
+ end
81
+
82
+ def deployer
83
+ fetch(:deployer)
84
+ end
85
+
86
+ def stage
87
+ fetch(:stage, 'production')
88
+ end
89
+
90
+ def destination
91
+ fetch(:slack_destination, stage)
92
+ end
93
+
94
+ def revision
95
+ @revision ||= `git rev-parse #{branch}`.chomp
96
+ end
97
+
98
+ def deploy_target
99
+ [slack_app_name, branch].join('/') + (revision ? " (#{revision[0..5]})" : "")
100
+ end
101
+
102
+ def self.extended(configuration)
103
+ configuration.load do
104
+ # Add the default hooks by default.
105
+ set :deployer do
106
+ ENV['USER'] || ENV['GIT_AUTHOR_NAME'] || `git config user.name`.chomp
107
+ end
108
+
109
+ namespace :slack do
110
+ desc "Notify Slack that the deploy has started."
111
+ task :starting do
112
+ post_to_channel(:grey, "#{deployer} is deploying #{deploy_target} to #{destination}")
113
+ set(:start_time, Time.now)
114
+ end
115
+
116
+ desc "Notify Slack that the deploy has completed successfully."
117
+ task :finished do
118
+ msg = "#{deployer} deployed #{deploy_target} to #{destination} *successfully*"
119
+
120
+ if start_time = fetch(:start_time, nil)
121
+ msg << " in #{Time.now.to_i - start_time.to_i} seconds."
122
+ else
123
+ msg << "."
124
+ end
125
+
126
+ post_to_channel(:green, msg)
127
+ end
128
+
129
+ desc "Notify Slack that the deploy failed."
130
+ task :failed do
131
+ post_to_channel(:red, "#{deployer} *failed* to deploy #{deploy_target} to #{destination}")
132
+ end
133
+ end # end namespace :slack
134
+ end
135
+ end # end self.extended
136
+
137
+ end
138
+ end
139
+
140
+ if Capistrano::Configuration.instance
141
+ Capistrano::Configuration.instance.extend(Capistrano::SlackNotify)
142
+ end
@@ -0,0 +1 @@
1
+ require 'capistrano/slack_notify'
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cap-slack-notify
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 3
9
+ - 1
10
+ version: 1.3.1
11
+ platform: ruby
12
+ authors:
13
+ - Parker Moore
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2015-04-14 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: capistrano
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 2
31
+ - 10
32
+ version: "2.10"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 1
44
+ segments:
45
+ - 1
46
+ - 7
47
+ version: "1.7"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rake
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 35
59
+ segments:
60
+ - 10
61
+ - 0
62
+ version: "10.0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ description:
66
+ email:
67
+ - parkrmoore@gmail.com
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - lib/capistrano-slack-notify.rb
76
+ - lib/capistrano/slack_notify.rb
77
+ homepage: https://github.com/listen360/capistrano-slack-notify
78
+ licenses:
79
+ - MIT
80
+ post_install_message:
81
+ rdoc_options: []
82
+
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.19
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Minimalist Capistrano 2 notifier for Slack.
110
+ test_files: []
111
+