fastlane-plugin-slack_bot 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7340f09d68383428d22988f1cee11d6a175bfdb3d4443af48dc070a2a366dee4
4
- data.tar.gz: ef98d678fac65cb75943ba53daee4e5da38b9c21d1bda396b790b4d0736a9a93
3
+ metadata.gz: 04a1032a218db318334c1eeec0023420f5ca16de0799d7126207dd94dd7e2524
4
+ data.tar.gz: a10469f4218ccd56b18bbd84abd2fcd711c8421eb4065f7c6dbc088ae7f62bc7
5
5
  SHA512:
6
- metadata.gz: b4f4e61b04ed004d81436de2202cb66df05420e34d94583e39c145048fba2e00bd0c60392fa38b9a8163bb7c1e1d8c891bcf218674aced8c82da46f625598c0b
7
- data.tar.gz: 36fbd80160b344bc8d53e5d6a346baf77de81159f1ec9cc2d116173ed8505d7b8f6737d0133651277eef0c2a4fafec5c1dcf47cf686a697c8418ac41e05b9cab
6
+ metadata.gz: 8562f7d29c2bbd3cdf5423bf6a0031a373f2afe96c5b13d981a73781b1e57a3e15616b66411c4a1695d4138f1d306e2172eb15a6b465b3069046523f1a53190a
7
+ data.tar.gz: e3e767e9cefbd08dfc15ab3d98b907d3e509c81ea09484f431ec565086e912e069a3c25fc51f0cff8a9d761a093197064bc891c46d8b9017c6ea779164e7f83f
data/README.md CHANGED
@@ -1,15 +1,15 @@
1
1
  ## Fastlane `slack_bot` plugin
2
2
 
3
- [![fastlane Plugin Badge](https://rawcdn.githack.com/fastlane/fastlane/master/fastlane/assets/plugin-badge.svg)](https://rubygems.org/gems/fastlane-plugin-slack_bot)
3
+ [![fastlane Plugin Badge](https://rawcdn.githack.com/fastlane/fastlane/master/fastlane/assets/plugin-badge.svg)](https://rubygems.org/gems/fastlane-plugin-slack_bot) ![![License]](https://img.shields.io/badge/license-MIT-green.svg?style=flat) [![Gem Version](https://badge.fury.io/rb/fastlane-plugin-slack_bot.svg)](https://badge.fury.io/rb/fastlane-plugin-slack_bot) ![![RubyGem Download Badge]](https://ruby-gem-downloads-badge.herokuapp.com/fastlane-plugin-slack_bot?type=total) [![Twitter: @manish](https://img.shields.io/badge/contact-@manish-blue.svg?style=flat)](https://twitter.com/manish_rathi_)
4
4
 
5
5
  ## About slack_bot
6
6
 
7
- A fastlane plugin to post slack message using bot api token. 🚀
8
- Note: `Fastlane` comes with `slack` plugin by default, which uses slack webhook url, which can't send direct message & other limitations.
7
+ A fastlane plugin to post slack message using bot api token. 🚀\
8
+ Note: `Fastlane` comes with `slack` plugin by default, which uses slack webhook url, which can't send direct message & other webhook limitations.
9
9
 
10
10
  ## Getting Started
11
11
 
12
- 1. Generate `Slack token` for `Fastlane` bot
12
+ 1. [Generate `Slack token` for `Fastlane` bot](https://slack.com/intl/en-nl/help/articles/115005265703-Create-a-bot-for-your-workspace)
13
13
  - From your Slack organization page, go to `Manage` -> `Custom Integrations`
14
14
  - Open `Bots`
15
15
  - Add Configuration
@@ -28,6 +28,25 @@ bundle exec fastlane add_plugin slack_bot
28
28
 
29
29
  3. Add `slack_bot` to your lane in `Fastfile` whenever you want to post a slack message
30
30
 
31
+ ## Examples
32
+
33
+ In the following example lets send a message to the default slack bot channel.
34
+
35
+ ```ruby
36
+ # share on Slack
37
+ post_to_slack(message: "App successfully released!")
38
+ ```
39
+
40
+ In the following example lets send a direct message to a slack user that unit tests CI has been failed.
41
+
42
+ ```ruby
43
+ # share on Slack
44
+ post_to_slack(
45
+ message: "CI: Your unit tests on #{ENV['CI_COMMIT_REF_NAME']} failed",
46
+ channel: "@SlackUsername" # This can be Slack userID, instead of username i.e @UXXXXX
47
+ )
48
+ ```
49
+
31
50
  In the following example lets send slack message to `#ios-team` channel for test-flight build.
32
51
 
33
52
  ```ruby
@@ -41,13 +60,39 @@ lane :beta do
41
60
 
42
61
  # share on Slack
43
62
  post_to_slack(
44
- api_token: "xyz", # Preferably configure as ENV['SLACK_API_TOKEN']
45
63
  message: "Hi team, we have a new test-flight beta build: #{beta_release_name}",
46
64
  channel: "#ios-team"
47
65
  )
48
66
  end
49
67
  ```
50
68
 
69
+ In the following example lets send slack message with custom payload.
70
+
71
+ ```ruby
72
+ # share on Slack
73
+ post_to_slack(
74
+ api_token: "xyz", # Preferably configure as ENV['SLACK_API_TOKEN']
75
+ message: "App successfully released!",
76
+ channel: "#channel", # Optional, by default will post to the default channel configured for the Slack Bot.
77
+ success: true, # Optional, defaults to true.
78
+ payload: { # Optional, lets you specify any number of your own Slack attachments.
79
+ "Build Date" => Time.new.to_s,
80
+ "Built by" => "Jenkins",
81
+ },
82
+ default_payloads: [:git_branch, :git_author], # Optional, lets you specify an allowlist of default payloads to include. Pass an empty array to suppress all the default payloads.
83
+ # Don't add this key, or pass nil, if you want all the default payloads. The available default payloads are: `lane`, `test_result`, `git_branch`, `git_author`, `last_git_commit`, `last_git_commit_hash`.
84
+ attachment_properties: { # Optional, lets you specify any other properties available for attachments in the slack API (see https://api.slack.com/docs/attachments).
85
+ # This hash is deep merged with the existing properties set using the other properties above. This allows your own fields properties to be appended to the existing fields that were created using the `payload` property for instance.
86
+ thumb_url: "http://example.com/path/to/thumb.png",
87
+ fields: [{
88
+ title: "My Field",
89
+ value: "My Value",
90
+ short: true
91
+ }]
92
+ }
93
+ )
94
+ ```
95
+
51
96
  ## Issues and Feedback
52
97
 
53
98
  For any other issues and feedback about this plugin, please submit it to this repository.
@@ -3,18 +3,21 @@ require_relative '../helper/slack_bot_helper'
3
3
 
4
4
  module Fastlane
5
5
  module Actions
6
+ module SharedValues
7
+ POST_TO_SLACK_RESULT = :POST_TO_SLACK_RESULT
8
+ end
9
+
6
10
  class PostToSlackAction < Action
7
11
  def self.run(options)
8
12
  require 'slack-notifier'
9
13
 
10
14
  options[:message] = (options[:message].to_s || '').gsub('\n', "\n")
11
15
  options[:message] = Slack::Notifier::Util::LinkFormatter.format(options[:message])
12
-
13
16
  options[:pretext] = options[:pretext].gsub('\n', "\n") unless options[:pretext].nil?
14
17
 
15
18
  if options[:channel].to_s.length > 0
16
19
  slack_channel = options[:channel]
17
- slack_channel = ('#' + options[:channel]) unless ['#', '@'].include?(slack_channel[0]) # send message to channel by default
20
+ slack_channel = ('#' + options[:channel]) unless ['#', 'C', '@'].include?(slack_channel[0]) # Add prefix(#) by default, if needed
18
21
  end
19
22
 
20
23
  slack_attachment = SlackAction.generate_slack_attachments(options)
@@ -24,16 +27,38 @@ module Fastlane
24
27
 
25
28
  api_url = "https://slack.com/api/chat.postMessage"
26
29
  headers = { "Content-Type": "application/json", "Authorization": "Bearer #{options[:api_token]}" }
27
- payload = { channel: slack_channel, attachments: [slack_attachment] }.to_json
30
+ payload = { channel: slack_channel, attachments: [slack_attachment] }
31
+ payload[:thread_ts] = options[:thread_ts] unless options[:thread_ts].nil?
32
+ payload = payload.to_json
28
33
 
29
- Excon.post(api_url, headers: headers, body: payload)
34
+ response = Excon.post(api_url, headers: headers, body: payload)
35
+ result = self.formatted_result(response)
30
36
  rescue => exception
31
37
  UI.error("Exception: #{exception}")
38
+ return nil
32
39
  else
33
40
  UI.success("Successfully sent Slack notification")
41
+ Actions.lane_context[SharedValues::POST_TO_SLACK_RESULT] = result
42
+ return result
34
43
  end
35
44
  end
36
45
 
46
+ def self.formatted_result(response)
47
+ result = {
48
+ status: response[:status],
49
+ body: response.body || "",
50
+ json: self.parse_json(response.body) || {}
51
+ }
52
+ end
53
+
54
+ def self.parse_json(value)
55
+ require 'json'
56
+
57
+ JSON.parse(value)
58
+ rescue JSON::ParserError
59
+ nil
60
+ end
61
+
37
62
  def self.description
38
63
  "Post a slack message"
39
64
  end
@@ -85,7 +110,11 @@ module Fastlane
85
110
  description: "Was this successful? (true/false)",
86
111
  optional: true,
87
112
  default_value: true,
88
- is_string: false)
113
+ is_string: false),
114
+ FastlaneCore::ConfigItem.new(key: :thread_ts,
115
+ env_name: "FL_POST_TO_SLACK_THREAD_TS",
116
+ description: "Provide another message's ts value to make this message a reply",
117
+ optional: true)
89
118
  ]
90
119
  end
91
120
 
@@ -0,0 +1,148 @@
1
+ require 'fastlane/action'
2
+ require_relative '../helper/slack_bot_helper'
3
+
4
+ module Fastlane
5
+ module Actions
6
+ module SharedValues
7
+ UPDATE_SLACK_MESSAGE_RESULT = :UPDATE_SLACK_MESSAGE_RESULT
8
+ end
9
+ class UpdateSlackMessageAction < Action
10
+ def self.run(options)
11
+ require 'slack-notifier'
12
+
13
+ options[:message] = (options[:message].to_s || '').gsub('\n', "\n")
14
+ options[:message] = Slack::Notifier::Util::LinkFormatter.format(options[:message])
15
+ options[:pretext] = options[:pretext].gsub('\n', "\n") unless options[:pretext].nil?
16
+ slack_attachment = SlackAction.generate_slack_attachments(options)
17
+
18
+ begin
19
+ require 'excon'
20
+
21
+ api_url = "https://slack.com/api/chat.update"
22
+ headers = { "Content-Type": "application/json", "Authorization": "Bearer #{options[:api_token]}" }
23
+ payload = { channel: options[:channel], attachments: [slack_attachment], ts: options[:ts] }.to_json
24
+
25
+ response = Excon.post(api_url, headers: headers, body: payload)
26
+ result = self.formatted_result(response)
27
+ rescue => exception
28
+ UI.error("Exception: #{exception}")
29
+ return nil
30
+ else
31
+ UI.success("Successfully updated the Slack message")
32
+ Actions.lane_context[SharedValues::UPDATE_SLACK_MESSAGE_RESULT] = result
33
+ return result
34
+ end
35
+ end
36
+
37
+ def self.formatted_result(response)
38
+ result = {
39
+ status: response[:status],
40
+ body: response.body || "",
41
+ json: self.parse_json(response.body) || {}
42
+ }
43
+ end
44
+
45
+ def self.parse_json(value)
46
+ require 'json'
47
+
48
+ JSON.parse(value)
49
+ rescue JSON::ParserError
50
+ nil
51
+ end
52
+
53
+ def self.description
54
+ "Update a slack message using time-stamp(ts) value"
55
+ end
56
+
57
+ def self.available_options
58
+ [
59
+ FastlaneCore::ConfigItem.new(key: :api_token,
60
+ env_name: "FL_UPDATE_SLACK_MESSAGE_BOT_TOKEN",
61
+ description: "Slack bot Token",
62
+ sensitive: true,
63
+ code_gen_sensitive: true,
64
+ is_string: true,
65
+ default_value: ENV["SLACK_API_TOKEN"],
66
+ default_value_dynamic: true,
67
+ optional: false),
68
+ FastlaneCore::ConfigItem.new(key: :ts,
69
+ env_name: "FL_UPDATE_SLACK_MESSAGE_TS",
70
+ description: "Timestamp of the message to be updated",
71
+ optional: false),
72
+ FastlaneCore::ConfigItem.new(key: :channel,
73
+ env_name: "FL_UPDATE_SLACK_MESSAGE_CHANNEL",
74
+ description: "Slack channel i.e C1234567890",
75
+ optional: false),
76
+ FastlaneCore::ConfigItem.new(key: :pretext,
77
+ env_name: "FL_UPDATE_SLACK_MESSAGE_PRETEXT",
78
+ description: "This is optional text that appears above the message attachment block. This supports the standard Slack markup language",
79
+ optional: true),
80
+ FastlaneCore::ConfigItem.new(key: :message,
81
+ env_name: "FL_UPDATE_SLACK_MESSAGE_MESSAGE",
82
+ description: "The message that should be displayed on Slack",
83
+ optional: true),
84
+ FastlaneCore::ConfigItem.new(key: :payload,
85
+ env_name: "FL_UPDATE_SLACK_MESSAGE_PAYLOAD",
86
+ description: "Add additional information to this post. payload must be a hash containing any key with any value",
87
+ default_value: {},
88
+ is_string: false),
89
+ FastlaneCore::ConfigItem.new(key: :default_payloads,
90
+ env_name: "FL_UPDATE_SLACK_MESSAGE_DEFAULT_PAYLOADS",
91
+ description: "Remove some of the default payloads. More information about the available payloads on GitHub",
92
+ optional: true,
93
+ type: Array),
94
+ FastlaneCore::ConfigItem.new(key: :attachment_properties,
95
+ env_name: "FL_UPDATE_SLACK_MESSAGE_ATTACHMENT_PROPERTIES",
96
+ description: "Merge additional properties in the slack attachment, see https://api.slack.com/docs/attachments",
97
+ default_value: {},
98
+ is_string: false),
99
+ FastlaneCore::ConfigItem.new(key: :success,
100
+ env_name: "FL_UPDATE_SLACK_MESSAGE_SUCCESS",
101
+ description: "Was this successful? (true/false)",
102
+ optional: true,
103
+ default_value: true,
104
+ is_string: false)
105
+ ]
106
+ end
107
+
108
+ def self.authors
109
+ ["crazymanish"]
110
+ end
111
+
112
+ def self.example_code
113
+ [
114
+ 'update_slack_message(
115
+ ts: "1609711037.000100",
116
+ channel: "C1234567890",
117
+ message: "Update: App successfully released!"
118
+ )',
119
+ 'update_slack_message(
120
+ ts: "1609711037.000100",
121
+ channel: "C1234567890",
122
+ message: "Update: App successfully released!",
123
+ success: true, # Optional, defaults to true.
124
+ payload: { # Optional, lets you specify any number of your own Slack attachments.
125
+ "Build Date" => Time.new.to_s,
126
+ "Built by" => "Jenkins",
127
+ },
128
+ default_payloads: [:git_branch, :git_author], # Optional, lets you specify a whitelist of default payloads to include. Pass an empty array to suppress all the default payloads.
129
+ # Don\'t add this key, or pass nil, if you want all the default payloads. The available default payloads are: `lane`, `test_result`, `git_branch`, `git_author`, `last_git_commit`, `last_git_commit_hash`.
130
+ attachment_properties: { # Optional, lets you specify any other properties available for attachments in the slack API (see https://api.slack.com/docs/attachments).
131
+ # This hash is deep merged with the existing properties set using the other properties above. This allows your own fields properties to be appended to the existing fields that were created using the `payload` property for instance.
132
+ thumb_url: "http://example.com/path/to/thumb.png",
133
+ fields: [{
134
+ title: "My Field",
135
+ value: "My Value",
136
+ short: true
137
+ }]
138
+ }
139
+ )'
140
+ ]
141
+ end
142
+
143
+ def self.is_supported?(platform)
144
+ true
145
+ end
146
+ end
147
+ end
148
+ end
@@ -1,5 +1,5 @@
1
1
  module Fastlane
2
2
  module SlackBot
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastlane-plugin-slack_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manish Rathi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-07 00:00:00.000000000 Z
11
+ date: 2021-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -147,6 +147,7 @@ files:
147
147
  - lib/fastlane/plugin/slack_bot.rb
148
148
  - lib/fastlane/plugin/slack_bot/actions/fetch_files_slack.rb
149
149
  - lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb
150
+ - lib/fastlane/plugin/slack_bot/actions/update_slack_message.rb
150
151
  - lib/fastlane/plugin/slack_bot/helper/slack_bot_helper.rb
151
152
  - lib/fastlane/plugin/slack_bot/version.rb
152
153
  homepage: https://github.com/crazymanish/fastlane-plugin-slack_bot
@@ -168,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
169
  - !ruby/object:Gem::Version
169
170
  version: '0'
170
171
  requirements: []
171
- rubygems_version: 3.0.3
172
+ rubygems_version: 3.1.4
172
173
  signing_key:
173
174
  specification_version: 4
174
175
  summary: "A fastlane plugin to post slack message using bot api token. \U0001F680"