pivotoolz 1.3.0 → 2.0.0
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/exe/post-slack-message +21 -31
- data/lib/pivotoolz/post_slack_message.rb +51 -0
- data/lib/pivotoolz/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bb165619a9f870e9bddf016cc7f848b0e4540469afa55815b73ecd72ec120b3
|
4
|
+
data.tar.gz: ac122af7c59ba532b2db9c1c10b962a95c8c4f8f5ca5f9553544aff1099ad3a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f62d291e64171cc4648046fd35fb5dbb45ed2a7959f2427adaceba71b92661b6890a81ae132fd9200ae73b21d434a1ad8ee36187d54ed78b8dfa30dd6095243c
|
7
|
+
data.tar.gz: 6b776e7f9be4954b3fbab03c2d74cd3e97bb0e9809268815d7c591750d4e24832a6f99f9a92251a24afb5aa6024f048f384fab83b53aace6d9994652a27bc791
|
data/exe/post-slack-message
CHANGED
@@ -2,42 +2,32 @@
|
|
2
2
|
|
3
3
|
require 'rest-client'
|
4
4
|
require 'json'
|
5
|
+
require_relative '../lib/pivotoolz/post_slack_message'
|
5
6
|
|
6
|
-
URL = ENV
|
7
|
+
URL = ENV.fetch('SLACK_WEBHOOK_URL') { '' }
|
8
|
+
CHANNEL_URL_MAP = ENV.fetch('SLACK_WEBHOOK_CHANNEL_URLS') { {} }
|
9
|
+
|
10
|
+
if URL.empty? && CHANNEL_URL_MAP.empty?
|
11
|
+
puts "Need either SLACK_WEBHOOK_URL or SLACK_WEBHOOK_CHANNEL_URLS to be defined!"
|
12
|
+
exit 0
|
13
|
+
end
|
7
14
|
|
8
15
|
channel = ARGV.shift
|
16
|
+
if channel.to_s.empty?
|
17
|
+
puts "Channel to post to is required!!"
|
18
|
+
exit 0
|
19
|
+
end
|
20
|
+
|
9
21
|
content = ARGV.empty? ? ARGF.read : StringIO.new(ARGV.join("\n")).read
|
10
22
|
exit 0 if content.strip.empty?
|
11
23
|
|
12
|
-
|
13
|
-
|
14
|
-
if !json.empty?
|
15
|
-
post_data = json.any? { |s| s.include? 'blocks' } ? JSON.parse(json.first) : {attachments: json.map { |j| JSON.parse(j) }}
|
16
|
-
begin
|
17
|
-
RestClient.post(
|
18
|
-
URL,
|
19
|
-
post_data.merge({text: text || ''}).to_json,
|
20
|
-
{content_type: :json, accept: :json}
|
21
|
-
)
|
22
|
-
rescue RestClient::Exceptions => e
|
23
|
-
puts "Error posting to slack #{e.message}:\n#{e.backtrace}"
|
24
|
-
end
|
25
|
-
|
26
|
-
exit 0
|
27
|
-
end
|
28
|
-
rescue JSON::ParserError => e
|
29
|
-
end
|
24
|
+
psm = PostSlackMessage.new
|
25
|
+
webhook_url = psm.select_webhook_url(URL, CHANNEL_URL_MAP, channel)
|
30
26
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
payload: {
|
35
|
-
username: ENV['SLACKBOT_USERNAME'] || 'slackbot',
|
36
|
-
channel: channel,
|
37
|
-
text: content,
|
38
|
-
icon_emoji: ":ghost:"
|
39
|
-
}.to_json
|
40
|
-
)
|
41
|
-
rescue RestClient::Exceptions => e
|
42
|
-
puts "Error posting to slack #{e.message}:\n#{e.backtrace}"
|
27
|
+
if webhook_url.nil?
|
28
|
+
puts "Channel '#{channel}' webhook url not found! Please define it in SLACK_WEBHOOK_CHANNEL_URLS environment variable"
|
29
|
+
exit 0
|
43
30
|
end
|
31
|
+
|
32
|
+
result = psm.post_message(webhook_url, channel, content)
|
33
|
+
puts result if !result.empty?
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class PostSlackMessage
|
2
|
+
def select_webhook_url(url, channel_map, channel)
|
3
|
+
return url if channel_map.empty?
|
4
|
+
|
5
|
+
channel_webhook_map = channel_map.split(',').reduce({}) do |reduced, channel_map_string|
|
6
|
+
c, url = channel_map_string.split('->')
|
7
|
+
reduced[c] = url
|
8
|
+
reduced["@#{c}"] = url if !c.start_with?('@')
|
9
|
+
reduced["##{c}"] = url if !c.start_with?('#')
|
10
|
+
reduced
|
11
|
+
end
|
12
|
+
|
13
|
+
channel_webhook_map[channel]
|
14
|
+
end
|
15
|
+
|
16
|
+
def post_message(url, channel, content)
|
17
|
+
begin
|
18
|
+
text, *json = content.split("\n")
|
19
|
+
return post_basic_message(url, channel, content) if json.empty?
|
20
|
+
|
21
|
+
post_data = json.any? { |s| s.include? 'blocks' } ? JSON.parse(json.first) : {attachments: json.map { |j| JSON.parse(j) }}
|
22
|
+
begin
|
23
|
+
RestClient.post(
|
24
|
+
url,
|
25
|
+
post_data.merge({text: text || ''}).to_json,
|
26
|
+
{content_type: :json, accept: :json}
|
27
|
+
)
|
28
|
+
rescue RestClient::Exceptions => e
|
29
|
+
return "Error posting to slack #{e.message}:\n#{e.backtrace}"
|
30
|
+
end
|
31
|
+
rescue JSON::ParserError => e
|
32
|
+
"Error parsing json: #{e}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def post_basic_message(url, channel, content)
|
37
|
+
begin
|
38
|
+
RestClient.post(
|
39
|
+
url,
|
40
|
+
payload: {
|
41
|
+
username: ENV['SLACKBOT_USERNAME'] || 'slackbot',
|
42
|
+
channel: channel,
|
43
|
+
text: content,
|
44
|
+
icon_emoji: ":ghost:"
|
45
|
+
}.to_json
|
46
|
+
)
|
47
|
+
rescue RestClient::Exceptions => e
|
48
|
+
puts "Error posting to slack #{e.message}:\n#{e.backtrace}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/pivotoolz/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pivotoolz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sufyan Adam
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-11-
|
11
|
+
date: 2019-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- exe/tag-it
|
113
113
|
- lib/pivotoolz.rb
|
114
114
|
- lib/pivotoolz/git_branch.rb
|
115
|
+
- lib/pivotoolz/post_slack_message.rb
|
115
116
|
- lib/pivotoolz/version.rb
|
116
117
|
- pivotoolz.gemspec
|
117
118
|
homepage: https://github.com/sufyanadam/pivotoolz
|