fluent-plugin-slack 0.6.4 → 0.6.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 401cd80a470edd2acf64f3530ee125df42fa9393
4
- data.tar.gz: 3c4238b53bf9ded7336eaf4148e572f1b18a9cc9
3
+ metadata.gz: abca37bc729bcbae33c6550af3a6a271f979e70d
4
+ data.tar.gz: d7124e7eed5b0c920bdf0125324683668ff938b2
5
5
  SHA512:
6
- metadata.gz: bd6b4662dfe23aefb7ebaf856b413e025d09ff0efa43ad75cd764129024a055ac280dfb32d94f5d2771e5d721e4c23d2163fb125776ee4e520f945019b1c017b
7
- data.tar.gz: be176edc0387bc6848214744931250aff8b38078ba2b4cd0ac1f7a086b81919b26237d5de446dcd85230a03c0fea4424ff72c0a63aa8117ee35a36227b9d962f
6
+ metadata.gz: b7380082aca202039dee0eef3c69a4c6ee80752245be769c182de7e35dc994967b960c4555cd5658d4b93a53aa5f412d5a75e8f2a8288835a1e7f1f867fc8f49
7
+ data.tar.gz: c73f180c32d084c5d7d91434b56a94b189ec19722327d7008a406abe5825eb507be84c2c3e149db86640cfce083e56a4001c07534418796b8e541c6e46105e01
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.6.5 (2017/05/20)
2
+
3
+ Enhancements:
4
+
5
+ * Avoid Encoding::UndefinedConversionError from ASCII-8BIT to UTF-8 on to_json by doing String#scrub! (thanks @yoheimuta)
6
+
1
7
  ## 0.6.4 (2016/07/07)
2
8
 
3
9
  Enhancements:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.4
1
+ 0.6.5
@@ -1 +1 @@
1
- out_slack.rb
1
+ lib/fluent/plugin/out_slack.rb
@@ -97,6 +97,35 @@ module Fluent
97
97
  end
98
98
  end
99
99
  end
100
+
101
+ def to_json_with_scrub!(params)
102
+ retries = 1
103
+ begin
104
+ params.to_json
105
+ rescue Encoding::UndefinedConversionError => e
106
+ recursive_scrub!(params)
107
+ if (retries -= 1) >= 0 # one time retry
108
+ log.warn "out_slack: to_json `#{params}` failed. retry after scrub!. #{e.message}"
109
+ retry
110
+ else
111
+ raise e
112
+ end
113
+ end
114
+ end
115
+
116
+ def recursive_scrub!(params)
117
+ case params
118
+ when Hash
119
+ params.each {|k, v| recursive_scrub!(v)}
120
+ when Array
121
+ params.each {|elm| recursive_scrub!(elm)}
122
+ when String
123
+ params.force_encoding(Encoding::UTF_8) if params.encoding == Encoding::ASCII_8BIT
124
+ params.scrub!('?') if params.respond_to?(:scrub!)
125
+ else
126
+ params
127
+ end
128
+ end
100
129
  end
101
130
 
102
131
  # Slack client for Incoming Webhook
@@ -115,7 +144,7 @@ module Fluent
115
144
 
116
145
  def encode_body(params = {})
117
146
  # https://api.slack.com/docs/formatting
118
- params.to_json.gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;')
147
+ to_json_with_scrub!(params).gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;')
119
148
  end
120
149
 
121
150
  def response_check(res, params)
@@ -237,7 +266,7 @@ module Fluent
237
266
  def encode_body(params = {})
238
267
  body = params.dup
239
268
  if params[:attachments]
240
- body[:attachments] = params[:attachments].to_json
269
+ body[:attachments] = to_json_with_scrub!(params[:attachments])
241
270
  end
242
271
  URI.encode_www_form(body)
243
272
  end
@@ -92,6 +92,15 @@ if ENV['WEBHOOK_URL'] and ENV['SLACKBOT_URL'] and ENV['SLACK_API_TOKEN']
92
92
  }
93
93
  end
94
94
 
95
+ def valid_utf8_encoded_string
96
+ "#general \xE3\x82\xA4\xE3\x83\xB3\xE3\x82\xB9\xE3\x83\x88\xE3\x83\xBC\xE3\x83\xAB\n"
97
+ end
98
+
99
+ def invalid_ascii8bit_encoded_utf8_string
100
+ str = "#general \xE3\x82\xA4\xE3\x83\xB3\xE3\x82\xB9\xE3\x83\x88\xE3\x83\xBC\xE3\x83\xAB\x81\n"
101
+ str.force_encoding(Encoding::ASCII_8BIT)
102
+ end
103
+
95
104
  # Notification via Mention works for all three with plain text payload
96
105
  def test_post_message_plain_payload_mention
97
106
  [@incoming, @slackbot, @api].each do |slack|
@@ -232,5 +241,42 @@ if ENV['WEBHOOK_URL'] and ENV['SLACKBOT_URL'] and ENV['SLACK_API_TOKEN']
232
241
  )
233
242
  end
234
243
  end
244
+
245
+ # IncomingWebhook posts "#general インストール"
246
+ def test_post_message_utf8_encoded_text
247
+ [@incoming].each do |slack|
248
+ assert_nothing_raised do
249
+ slack.post_message(default_payload(slack).merge({
250
+ text: valid_utf8_encoded_string,
251
+ }))
252
+ end
253
+ end
254
+ end
255
+
256
+ # IncomingWebhook posts "#general インストール?"
257
+ def test_post_message_ascii8bit_encoded_utf8_text
258
+ [@incoming].each do |slack|
259
+ assert_nothing_raised do
260
+ slack.post_message(default_payload(slack).merge({
261
+ text: invalid_ascii8bit_encoded_utf8_string,
262
+ }))
263
+ end
264
+ end
265
+ end
266
+
267
+ # IncomingWebhook and API posts "#general インストール?"
268
+ def test_post_message_ascii8bit_encoded_utf8_attachments
269
+ [@incoming, @api].each do |slack|
270
+ assert_nothing_raised do
271
+ slack.post_message(default_payload(slack).merge({
272
+ attachments: [default_attachment.merge({
273
+ color: 'good',
274
+ fallback: invalid_ascii8bit_encoded_utf8_string,
275
+ text: invalid_ascii8bit_encoded_utf8_string,
276
+ })]
277
+ }))
278
+ end
279
+ end
280
+ end
235
281
  end
236
282
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-slack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keisuke SOGAWA
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-07-07 00:00:00.000000000 Z
12
+ date: 2017-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fluentd
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  version: '0'
170
170
  requirements: []
171
171
  rubyforge_project:
172
- rubygems_version: 2.5.1
172
+ rubygems_version: 2.6.11
173
173
  signing_key:
174
174
  specification_version: 4
175
175
  summary: fluent Slack plugin