fastlane-plugin-slack_bot 0.4.1 → 1.0.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: 9e0d95cdc34c99e1608ec3d1e30ff2f0b538516bd370fd166bc09c97d61e5781
4
- data.tar.gz: cc8eb032bfc0b3e3022a2a390981120cdf9245812ef46c7fbe9ff0dd2808a0f5
3
+ metadata.gz: 289a56616a3791c91d4aa9033fe725da23f086ba112f63f383617de611824aa9
4
+ data.tar.gz: 406ed9f6c5809ed39b6ea2024f9c998409bcb11711616006714fdfef28063441
5
5
  SHA512:
6
- metadata.gz: 9d76ae4594ce67f876db6d0fe4673503f05088e64cad0bf6faf5bd5f0fcaaa5d972f2c146454aa425e6a1de980436297533e5881eb8438547e78878783dc6706
7
- data.tar.gz: d0abe8adad2c616a9feb9521bf29a2cda9f49cbab0f4895379bbb97fd2ed2791d354f9b1bdafdb59430e0773ecf0baa9888d0aa0ecefd6e448974f866dda0556
6
+ metadata.gz: 821fbff8025635a8c8728e40ceccc52f54db653fc2aaa4ddeed6d7bb5aaec10d78798b19a88282c44ba8168fb0342da8804030bbbf715299827e0b38aca60f9e
7
+ data.tar.gz: d9bd6cc8c9aa8d0dd1704ddd583735f3400a38d2ac440b0a61ff63aa586b079d3b93b75d5f5770f28e523ef097abeaf4142b56ceaefaf77e1f08d0e99e959b2c
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
 
@@ -175,7 +175,7 @@ Let’s post a message with custom slack bot username and icon
175
175
  # share on Slack
176
176
  post_to_slack(
177
177
  message: "App successfully released!",
178
- username: "Release Bot", # Overrides the bot's image
178
+ username: "Release Bot", # Overrides the bot's username
179
179
  icon_url: "https://fastlane.tools/assets/img/fastlane_icon.png" # Overrides the bot's icon
180
180
  )
181
181
  ```
@@ -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
 
@@ -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, formats: %i[html markdown]
42
+ @formats = formats
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.1"
3
+ VERSION = "1.0.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.1
4
+ version: 1.0.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-03-04 00:00:00.000000000 Z
11
+ date: 2021-04-18 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: