fastlane-plugin-slack_bot 0.4.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f2aabbc8c122fde9ab66c2f19a43ee068eeb1905fe71676b58f3f97615df758
4
- data.tar.gz: 2f14989f7e4afcf43fd873748b385bb9fb30c5e8b419d4c4ad35b5f910dedd8b
3
+ metadata.gz: d41be99af2fa9df13b2a8b613f0afc6ca95ec7e78abe90c07231b2564e9d6431
4
+ data.tar.gz: f5b92143896f6a54b893b06daf6a2e4f6b464fba24473851b98f70c16995a15e
5
5
  SHA512:
6
- metadata.gz: 4aa6547bd6c2d4b3f4275be0296027171e1e4f47e90540bd676e1adedc92840bc5945972e1462ada73f1a30eabf8ba7eeebaf9d46e76205d81053bc2595ceb3b
7
- data.tar.gz: 15a515660c49d9ca4b7d9de700c4212692402c05d53feba5b039fadc82c00f7bf03fcd6255b8467c659ef8545028f823723ef3e30cde8efbd7144fc1b1e8ebfc
6
+ metadata.gz: 40e353d5df69d1822acceb88aaced1b4eb79f5c56d615bf36abb82b56c593311e0e50bbe82362a662e63c9c5a343eae50a0a2fe666b3e48e044090da5d557c77
7
+ data.tar.gz: 34fb53d69b3fa99084615db23e2ab03c862928af864b6c28239004af7b7ab4e747f91c756dabe6e855008ed58706802618a1c8becfbfc2cc26c80f8810524f35
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  ![![License]](https://img.shields.io/badge/license-MIT-green.svg?style=flat)
5
5
  [![Gem Version](https://badge.fury.io/rb/fastlane-plugin-slack_bot.svg)](https://badge.fury.io/rb/fastlane-plugin-slack_bot)
6
6
  ![![RubyGem Download Badge]](https://ruby-gem-downloads-badge.herokuapp.com/fastlane-plugin-slack_bot?type=total)
7
- [![Twitter: @manish](https://img.shields.io/badge/contact-@manish-blue.svg?style=flat)](https://twitter.com/manish_rathi_)
7
+ [![Twitter: @manish](https://img.shields.io/badge/contact-@manish-blue.svg?style=flat)](https://twitter.com/iammanishrathi)
8
8
 
9
9
  A fastlane plugin to customize your automation workflow(s) with a **Slack Bot** 🤖 using the [Slack APIs](https://api.slack.com/)
10
10
 
@@ -29,9 +29,7 @@ i.e Listing couple of slack **webhook url** limitations:
29
29
  ## Getting Started
30
30
 
31
31
  1. [Generate `Slack token` for `Fastlane` bot](https://slack.com/intl/en-nl/help/articles/115005265703-Create-a-bot-for-your-workspace)
32
- - From your Slack organization page, go to `Manage` -> `Custom Integrations`
33
- - Open `Bots`
34
- - Add Configuration
32
+ - Add a slack `Bot` user
35
33
  - Choose a name for your bot, e.g. `"fastlane"`
36
34
  - Save `API Token`
37
35
 
@@ -54,7 +52,8 @@ Using this `slack_bot` plugin, you can:
54
52
  - [x] Post a message to a _public_ channel
55
53
  - [x] Post a message to a _private_ channel
56
54
  - [x] Post a message to a _slack user_ (DM)
57
- - [x] Post a message inside a _slack thread_ 🧵 (DM)
55
+ - [x] Post a message inside a _slack thread_ 🧵
56
+ - [x] Post a message with _custom Bot username and icon_
58
57
 
59
58
  - Update a message using [chat.update](https://api.slack.com/methods/chat.update) Slack API
60
59
  - [x] update a message in a channel
@@ -66,8 +65,8 @@ Using this `slack_bot` plugin, you can:
66
65
  - [x] Upload a file to a _public_ channel
67
66
  - [x] Upload a file to a _private_ channel
68
67
  - [x] Upload a file to a _slack user_ (DM)
69
- - [x] Upload a file inside a _slack thread_ 🧵 (DM)
70
- - [x] Upload a file with multiple channels/users
68
+ - [x] Upload a file inside a _slack thread_ 🧵
69
+ - [x] Upload a file with _multiple channels/users_
71
70
 
72
71
 
73
72
  ## Examples (Post a message)
@@ -168,6 +167,17 @@ lane :release do
168
167
  end
169
168
  ```
170
169
 
170
+ Let’s post a message with custom slack bot username and icon
171
+
172
+ ```ruby
173
+ # share on Slack
174
+ post_to_slack(
175
+ message: "App successfully released!",
176
+ username: "Release Bot", # Overrides the bot's username
177
+ icon_url: "https://fastlane.tools/assets/img/fastlane_icon.png" # Overrides the bot's icon
178
+ )
179
+ ```
180
+
171
181
  ## Examples (Upload a file)
172
182
 
173
183
  Let’s Upload a file to a slack channel that main branch unit tests has been failed, see scan logs.
@@ -1,5 +1,7 @@
1
1
  require 'fastlane/action'
2
2
  require_relative '../helper/slack_bot_helper'
3
+ require_relative '../helper/slack_bot_attachments_helper'
4
+ require_relative '../helper/slack_bot_link_formatter_helper'
3
5
 
4
6
  module Fastlane
5
7
  module Actions
@@ -9,10 +11,8 @@ module Fastlane
9
11
 
10
12
  class PostToSlackAction < Action
11
13
  def self.run(options)
12
- require 'slack-notifier'
13
-
14
14
  options[:message] = (options[:message].to_s || '').gsub('\n', "\n")
15
- options[:message] = Slack::Notifier::Util::LinkFormatter.format(options[:message])
15
+ options[:message] = Helper::SlackBotLinkFormatterHelper.format(options[:message])
16
16
  options[:pretext] = options[:pretext].gsub('\n', "\n") unless options[:pretext].nil?
17
17
 
18
18
  if options[:channel].to_s.length > 0
@@ -20,7 +20,7 @@ module Fastlane
20
20
  slack_channel = ('#' + options[:channel]) unless ['#', 'C', '@'].include?(slack_channel[0]) # Add prefix(#) by default, if needed
21
21
  end
22
22
 
23
- slack_attachment = SlackAction.generate_slack_attachments(options)
23
+ slack_attachment = Helper::SlackBotAttachmentsHelper.generate_slack_attachments(options)
24
24
  bot_username = options[:username]
25
25
  bot_icon_url = options[:icon_url]
26
26
 
@@ -38,10 +38,11 @@ module Fastlane
38
38
  icon_url: bot_icon_url,
39
39
  attachments: [slack_attachment]
40
40
  }
41
+ payload[:as_user] = options[:as_user] if options[:as_user] # default is false
41
42
  payload[:thread_ts] = options[:thread_ts] unless options[:thread_ts].nil?
42
43
  payload = payload.to_json
43
44
 
44
- response = Excon.post(api_url, headers: headers, body: payload)
45
+ response = Excon.post(api_url, headers: headers, body: payload, omit_default_port: true)
45
46
  result = self.formatted_result(response)
46
47
  rescue => exception
47
48
  UI.error("Exception: #{exception}")
@@ -104,6 +105,12 @@ module Fastlane
104
105
  default_value: "https://fastlane.tools/assets/img/fastlane_icon.png",
105
106
  is_string: true,
106
107
  optional: true),
108
+ FastlaneCore::ConfigItem.new(key: :as_user,
109
+ env_name: "FL_POST_TO_SLACK_AS_USER",
110
+ description: "Pass true to post the message as the authed user. Defaults to false",
111
+ optional: true,
112
+ default_value: false,
113
+ is_string: false),
107
114
  FastlaneCore::ConfigItem.new(key: :pretext,
108
115
  env_name: "FL_POST_TO_SLACK_PRETEXT",
109
116
  description: "This is optional text that appears above the message attachment block. This supports the standard Slack markup language",
@@ -1,5 +1,7 @@
1
1
  require 'fastlane/action'
2
2
  require_relative '../helper/slack_bot_helper'
3
+ require_relative '../helper/slack_bot_attachments_helper'
4
+ require_relative '../helper/slack_bot_link_formatter_helper'
3
5
 
4
6
  module Fastlane
5
7
  module Actions
@@ -8,12 +10,10 @@ module Fastlane
8
10
  end
9
11
  class UpdateSlackMessageAction < Action
10
12
  def self.run(options)
11
- require 'slack-notifier'
12
-
13
13
  options[:message] = (options[:message].to_s || '').gsub('\n', "\n")
14
- options[:message] = Slack::Notifier::Util::LinkFormatter.format(options[:message])
14
+ options[:message] = Helper::SlackBotLinkFormatterHelper.format(options[:message])
15
15
  options[:pretext] = options[:pretext].gsub('\n', "\n") unless options[:pretext].nil?
16
- slack_attachment = SlackAction.generate_slack_attachments(options)
16
+ slack_attachment = Helper::SlackBotAttachmentsHelper.generate_slack_attachments(options)
17
17
 
18
18
  begin
19
19
  require 'excon'
@@ -0,0 +1,113 @@
1
+ require 'fastlane_core/ui/ui'
2
+ require 'fastlane_core/env'
3
+ require_relative 'slack_bot_link_formatter_helper'
4
+
5
+ module Fastlane
6
+ UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
7
+
8
+ module Helper
9
+ class SlackBotAttachmentsHelper
10
+ # class methods that you define here become available in your action
11
+ # as `Helper::SlackBotAttachmentsHelper.your_method`
12
+ #
13
+ def self.show_message
14
+ UI.message("Hello from the slack_bot plugin attachments helper!")
15
+ end
16
+
17
+ def self.generate_slack_attachments(options)
18
+ color = (options[:success] ? 'good' : 'danger')
19
+ should_add_payload = ->(payload_name) { options[:default_payloads].map(&:to_sym).include?(payload_name.to_sym) }
20
+
21
+ slack_attachment = {
22
+ fallback: options[:message],
23
+ text: options[:message],
24
+ pretext: options[:pretext],
25
+ color: color,
26
+ mrkdwn_in: ["pretext", "text", "fields", "message"],
27
+ fields: []
28
+ }
29
+
30
+ # custom user payloads
31
+ slack_attachment[:fields] += options[:payload].map do |k, v|
32
+ {
33
+ title: k.to_s,
34
+ value: SlackBotLinkFormatterHelper.format(v.to_s),
35
+ short: false
36
+ }
37
+ end
38
+
39
+ # Add the lane to the Slack message
40
+ # This might be nil, if slack is called as "one-off" action
41
+ if should_add_payload[:lane] && Actions.lane_context[Actions::SharedValues::LANE_NAME]
42
+ slack_attachment[:fields] << {
43
+ title: 'Lane',
44
+ value: Actions.lane_context[Actions::SharedValues::LANE_NAME],
45
+ short: true
46
+ }
47
+ end
48
+
49
+ # test_result
50
+ if should_add_payload[:test_result]
51
+ slack_attachment[:fields] << {
52
+ title: 'Result',
53
+ value: (options[:success] ? 'Success' : 'Error'),
54
+ short: true
55
+ }
56
+ end
57
+
58
+ # git branch
59
+ if Actions.git_branch && should_add_payload[:git_branch]
60
+ slack_attachment[:fields] << {
61
+ title: 'Git Branch',
62
+ value: Actions.git_branch,
63
+ short: true
64
+ }
65
+ end
66
+
67
+ # git_author
68
+ if Actions.git_author_email && should_add_payload[:git_author]
69
+ if FastlaneCore::Env.truthy?('FASTLANE_SLACK_HIDE_AUTHOR_ON_SUCCESS') && options[:success]
70
+ # We only show the git author if the build failed
71
+ else
72
+ slack_attachment[:fields] << {
73
+ title: 'Git Author',
74
+ value: Actions.git_author_email,
75
+ short: true
76
+ }
77
+ end
78
+ end
79
+
80
+ # last_git_commit
81
+ if Actions.last_git_commit_message && should_add_payload[:last_git_commit]
82
+ slack_attachment[:fields] << {
83
+ title: 'Git Commit',
84
+ value: Actions.last_git_commit_message,
85
+ short: false
86
+ }
87
+ end
88
+
89
+ # last_git_commit_hash
90
+ if Actions.last_git_commit_hash(true) && should_add_payload[:last_git_commit_hash]
91
+ slack_attachment[:fields] << {
92
+ title: 'Git Commit Hash',
93
+ value: Actions.last_git_commit_hash(short: true),
94
+ short: false
95
+ }
96
+ end
97
+
98
+ # merge additional properties
99
+ deep_merge(slack_attachment, options[:attachment_properties])
100
+ end
101
+
102
+ # Adapted from https://stackoverflow.com/a/30225093/158525
103
+ def self.deep_merge(a, b)
104
+ merger = proc do |key, v1, v2|
105
+ Hash === v1 && Hash === v2 ?
106
+ v1.merge(v2, &merger) : Array === v1 && Array === v2 ?
107
+ v1 | v2 : [:undefined, nil, :nil].include?(v2) ? v1 : v2
108
+ end
109
+ a.merge(b, &merger)
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,82 @@
1
+ require 'fastlane_core/ui/ui'
2
+
3
+ module Fastlane
4
+ UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
5
+
6
+ module Helper
7
+ # Fastlane is moving away 'slack-notifier' gem, https://github.com/fastlane/fastlane/pull/18512
8
+ # Duplicate of: https://github.com/stevenosloan/slack-notifier/blob/master/lib/slack-notifier/util/link_formatter.rb
9
+ class SlackBotLinkFormatterHelper
10
+ # http://rubular.com/r/19cNXW5qbH
11
+ HTML_PATTERN = %r{
12
+ <a
13
+ (?:.*?)
14
+ href=['"](.+?)['"]
15
+ (?:.*?)>
16
+ (.+?)
17
+ </a>
18
+ }x
19
+
20
+ # the path portion of a url can contain these characters
21
+ VALID_PATH_CHARS = '\w\-\.\~\/\?\#\='
22
+
23
+ # Attempt at only matching pairs of parens per
24
+ # the markdown spec http://spec.commonmark.org/0.27/#links
25
+ #
26
+ # http://rubular.com/r/y107aevxqT
27
+ MARKDOWN_PATTERN = %r{
28
+ \[ ([^\[\]]*?) \]
29
+ \( ((https?://.*?) | (mailto:.*?)) \)
30
+ (?! [#{VALID_PATH_CHARS}]* \) )
31
+ }x
32
+
33
+ class << self
34
+ def format string, opts={}
35
+ SlackBotLinkFormatterHelper.new(string, **opts).formatted
36
+ end
37
+ end
38
+
39
+ attr_reader :formats
40
+
41
+ def initialize string, opts = {}
42
+ @formats = opts[:formats] || %i[html markdown]
43
+ @orig = string.respond_to?(:scrub) ? string.scrub : string
44
+ end
45
+
46
+ # rubocop:disable Lint/RescueWithoutErrorClass
47
+ def formatted
48
+ return @orig unless @orig.respond_to?(:gsub)
49
+
50
+ sub_markdown_links(sub_html_links(@orig))
51
+ rescue => e
52
+ raise e unless RUBY_VERSION < "2.1" && e.message.include?("invalid byte sequence")
53
+ raise e, "#{e.message}. Consider including the 'string-scrub' gem to strip invalid characters"
54
+ end
55
+ # rubocop:enable Lint/RescueWithoutErrorClass
56
+
57
+ private
58
+
59
+ def sub_html_links string
60
+ return string unless formats.include?(:html)
61
+
62
+ string.gsub(HTML_PATTERN) do
63
+ slack_link Regexp.last_match[1], Regexp.last_match[2]
64
+ end
65
+ end
66
+
67
+ def sub_markdown_links string
68
+ return string unless formats.include?(:markdown)
69
+
70
+ string.gsub(MARKDOWN_PATTERN) do
71
+ slack_link Regexp.last_match[2], Regexp.last_match[1]
72
+ end
73
+ end
74
+
75
+ def slack_link link, text=nil
76
+ "<#{link}" \
77
+ "#{text && !text.empty? ? "|#{text}" : ''}" \
78
+ ">"
79
+ end
80
+ end
81
+ end
82
+ end
@@ -1,5 +1,5 @@
1
1
  module Fastlane
2
2
  module SlackBot
3
- VERSION = "0.4.0"
3
+ VERSION = "1.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.4.0
4
+ version: 1.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: 2021-02-20 00:00:00.000000000 Z
11
+ date: 2021-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -149,7 +149,9 @@ files:
149
149
  - lib/fastlane/plugin/slack_bot/actions/file_upload_to_slack.rb
150
150
  - lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb
151
151
  - lib/fastlane/plugin/slack_bot/actions/update_slack_message.rb
152
+ - lib/fastlane/plugin/slack_bot/helper/slack_bot_attachments_helper.rb
152
153
  - lib/fastlane/plugin/slack_bot/helper/slack_bot_helper.rb
154
+ - lib/fastlane/plugin/slack_bot/helper/slack_bot_link_formatter_helper.rb
153
155
  - lib/fastlane/plugin/slack_bot/version.rb
154
156
  homepage: https://github.com/crazymanish/fastlane-plugin-slack_bot
155
157
  licenses: