fastlane-plugin-slack_message 0.1.0 → 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: cb44a150bc4c84c5b7806cbebb429dc578dfdbc70175d7d962e468e6b7e461de
4
- data.tar.gz: dedc9f9e50e383a13a55e5c7332d3829e4f0244b4ee8747bb74da65c5dd5b4b8
3
+ metadata.gz: cae74cb422fd9b26a624afbb6a7cf2de4a3286612fbf830f4d6988cf447f504b
4
+ data.tar.gz: a97feba09615c3d6c1cd696ef3b7a99001a7e79428824a880d24a444d2af62d9
5
5
  SHA512:
6
- metadata.gz: 921b8ce98b6e7ff51497e587116407cb6ae30f4d8d5a88028305a8c9baf1b0360fca06724af08cc623f747d18eb67ddb08bf594ff6c68f56255934c95f97a3df
7
- data.tar.gz: 5885e91ccd9eae87fd9dc208204834ae5e75017f420a7a5633473fd4b8a1db60c4fba67963f2a2b8a531de0d9568486ea33acfca7d78ea340a3b01890f02dee4
6
+ metadata.gz: 1439e4313cc2c44c071405ae06ee688b200032bba2cd6d83c4ecd229a3b3cda1553e821bc6aa529014815775336bf474106827d142429e80c5f5a26152e28e9b
7
+ data.tar.gz: 87ea224361d5353b71ab0afb6727de5e0d40bbb2aff9b2f46d1bc8623943d671f99b6c35619be4415985b2e642d306bfd0587c25afdc4c6ff1496afbc167cf70
@@ -1,148 +1,104 @@
1
1
  require 'fastlane/action'
2
- require 'fastlane/actions/slack'
2
+ require 'fastlane_core/configuration/config_item'
3
3
  require_relative '../helper/slack_message_helper'
4
4
 
5
5
  module Fastlane
6
6
  module Actions
7
7
  class SlackMessageAction < Action
8
- class Runner
9
- def initialize(slack_url)
10
- @webhook_url = slack_url
11
-
12
- @client = Faraday.new do |conn|
13
- conn.use(Faraday::Response::RaiseError)
14
- end
15
- end
16
-
17
- def run(options)
18
-
19
- options[:message] = Fastlane::Actions::SlackAction.trim_message(options[:message].to_s || '')
20
- options[:message] = Fastlane::Notification::Slack::LinkConverter.convert(options[:message])
21
-
22
- options[:pretext] = options[:pretext].gsub('\n', "\n") unless options[:pretext].nil?
23
-
24
- channel = nil
25
- if options[:channel].to_s.length > 0
26
- channel = options[:channel]
27
- channel = ('#' + options[:channel]) unless ['#', '@'].include?(channel[0]) # send message to channel by default
28
- end
29
-
30
- username = options[:use_webhook_configured_username_and_icon] ? nil : options[:username]
31
-
32
- slack_attachment = Fastlane::Actions::SlackAction.generate_slack_attachments(options)
33
- link_names = options[:link_names]
34
- icon_url = options[:use_webhook_configured_username_and_icon] ? nil : options[:icon_url]
35
-
36
- post_message(
37
- channel: channel,
38
- thread_timestamp: options[:thread_timestamp],
39
- username: username,
40
- attachments: [slack_attachment],
41
- link_names: link_names,
42
- icon_url: icon_url,
43
- fail_on_error: options[:fail_on_error]
44
- )
45
- end
46
-
47
- def post_message(channel:, thread_timestamp:, username:, attachments:, link_names:, icon_url:, fail_on_error:)
48
- @client.post(@webhook_url) do |request|
49
- request.headers['Content-Type'] = 'application/json'
50
- request.body = {
51
- channel: channel,
52
- username: username,
53
- thread_ts: thread_timestamp,
54
- icon_url: icon_url,
55
- attachments: attachments,
56
- link_names: link_names
57
- }.to_json
58
- end
59
- UI.success('Successfully sent Slack notification')
60
- rescue => error
61
- UI.error("Exception: #{error}")
62
- message = "Error pushing Slack message, maybe the integration has no permission to post on this channel? Try removing the channel parameter in your Fastfile, this is usually caused by a misspelled or changed group/channel name or an expired SLACK_URL"
63
- if fail_on_error
64
- UI.user_error!(message)
65
- else
66
- UI.error(message)
67
- end
68
- end
69
- end
70
-
71
8
  def self.run(options)
72
- Runner.new(options[:slack_url]).run(options)
9
+ channel = options[:channel]
10
+ channel = Helper::SlackMessageHelper.format_channel(channel)
11
+
12
+ username = options[:use_webhook_configured_username_and_icon] ? nil : options[:username]
13
+
14
+ message = Helper::SlackMessageHelper.prepare_message(options)
15
+
16
+ link_names = options[:link_names]
17
+ icon_url = options[:use_webhook_configured_username_and_icon] ? nil : options[:icon_url]
18
+
19
+ Helper::SlackMessageHelper.post_message(
20
+ slack_url: options[:slack_url],
21
+ channel: channel,
22
+ thread_timestamp: options[:thread_timestamp],
23
+ username: username,
24
+ attachments: [message],
25
+ link_names: link_names,
26
+ icon_url: icon_url,
27
+ fail_on_error: options[:fail_on_error]
28
+ )
73
29
  end
74
30
 
75
31
  def self.available_options
76
32
  [
77
33
  FastlaneCore::ConfigItem.new(key: :message,
78
- env_name: "FL_SLACK_MESSAGE",
79
- description: "The message that should be displayed on Slack. This supports the standard Slack markup language",
34
+ env_name: 'FL_SLACK_MESSAGE',
35
+ description: 'The message that should be displayed on Slack. This supports the standard Slack markup language',
80
36
  optional: true),
81
37
  FastlaneCore::ConfigItem.new(key: :pretext,
82
- env_name: "FL_SLACK_PRETEXT",
83
- description: "This is optional text that appears above the message attachment block. This supports the standard Slack markup language",
38
+ env_name: 'FL_SLACK_PRETEXT',
39
+ description: 'This is optional text that appears above the message attachment block. This supports the standard Slack markup language',
84
40
  optional: true),
85
41
  FastlaneCore::ConfigItem.new(key: :channel,
86
- env_name: "FL_SLACK_CHANNEL",
87
- description: "#channel or @username",
42
+ env_name: 'FL_SLACK_CHANNEL',
43
+ description: '#channel or @username',
88
44
  optional: true),
89
45
  FastlaneCore::ConfigItem.new(key: :thread_timestamp,
90
- env_name: "FL_SLACK_THREAD_TIMESTAMP",
91
- description: "Timestamp of the message to reply",
46
+ env_name: 'FL_SLACK_THREAD_TIMESTAMP',
47
+ description: 'Timestamp of the message to reply',
92
48
  optional: true),
93
49
  FastlaneCore::ConfigItem.new(key: :slack_url,
94
- env_name: "SLACK_URL",
50
+ env_name: 'SLACK_URL',
95
51
  sensitive: true,
96
- description: "Create an Incoming WebHook for your Slack group",
52
+ description: 'Create an Incoming WebHook for your Slack group',
97
53
  verify_block: proc do |value|
98
- UI.user_error!("Invalid URL, must start with https://") unless value.start_with?("https://")
54
+ UI.user_error!('Invalid URL, must start with https://') unless value.start_with?('https://')
99
55
  end),
100
56
  FastlaneCore::ConfigItem.new(key: :username,
101
- env_name: "FL_SLACK_USERNAME",
102
- description: "Overrides the webhook's username property if use_webhook_configured_username_and_icon is false",
103
- default_value: "fastlane",
57
+ env_name: 'FL_SLACK_USERNAME',
58
+ description: 'Overrides the webhook\'s username property if use_webhook_configured_username_and_icon is false',
59
+ default_value: 'fastlane',
104
60
  optional: true),
105
61
  FastlaneCore::ConfigItem.new(key: :use_webhook_configured_username_and_icon,
106
- env_name: "FL_SLACK_USE_WEBHOOK_CONFIGURED_USERNAME_AND_ICON",
107
- description: "Use webhook's default username and icon settings? (true/false)",
62
+ env_name: 'FL_SLACK_USE_WEBHOOK_CONFIGURED_USERNAME_AND_ICON',
63
+ description: 'Use webhook\'s default username and icon settings? (true/false)',
108
64
  default_value: false,
109
65
  type: Boolean,
110
66
  optional: true),
111
67
  FastlaneCore::ConfigItem.new(key: :icon_url,
112
- env_name: "FL_SLACK_ICON_URL",
113
- description: "Overrides the webhook's image property if use_webhook_configured_username_and_icon is false",
114
- default_value: "https://fastlane.tools/assets/img/fastlane_icon.png",
68
+ env_name: 'FL_SLACK_ICON_URL',
69
+ description: 'Overrides the webhook\'s image property if use_webhook_configured_username_and_icon is false',
70
+ default_value: 'https://fastlane.tools/assets/img/fastlane_icon.png',
115
71
  optional: true),
116
72
  FastlaneCore::ConfigItem.new(key: :payload,
117
- env_name: "FL_SLACK_PAYLOAD",
118
- description: "Add additional information to this post. payload must be a hash containing any key with any value",
73
+ env_name: 'FL_SLACK_PAYLOAD',
74
+ description: 'Add additional information to this post. payload must be a hash containing any key with any value',
119
75
  default_value: {},
120
76
  type: Hash),
121
77
  FastlaneCore::ConfigItem.new(key: :default_payloads,
122
- env_name: "FL_SLACK_DEFAULT_PAYLOADS",
123
- description: "Specifies default payloads to include. Pass an empty array to suppress all the default payloads",
124
- default_value: ['lane', 'test_result', 'git_branch', 'git_author', 'last_git_commit', 'last_git_commit_hash'],
78
+ env_name: 'FL_SLACK_DEFAULT_PAYLOADS',
79
+ description: 'Specifies default payloads to include. Pass an empty array to suppress all the default payloads',
80
+ default_value: %w[lane test_result git_branch git_author last_git_commit last_git_commit_hash],
125
81
  type: Array),
126
82
  FastlaneCore::ConfigItem.new(key: :attachment_properties,
127
- env_name: "FL_SLACK_ATTACHMENT_PROPERTIES",
128
- description: "Merge additional properties in the slack attachment, see https://api.slack.com/docs/attachments",
83
+ env_name: 'FL_SLACK_ATTACHMENT_PROPERTIES',
84
+ description: 'Merge additional properties in the slack attachment, see https://api.slack.com/docs/attachments',
129
85
  default_value: {},
130
86
  type: Hash),
131
87
  FastlaneCore::ConfigItem.new(key: :success,
132
- env_name: "FL_SLACK_SUCCESS",
133
- description: "Was this build successful? (true/false)",
88
+ env_name: 'FL_SLACK_SUCCESS',
89
+ description: 'Was this build successful? (true/false)',
134
90
  optional: true,
135
91
  default_value: true,
136
92
  type: Boolean),
137
93
  FastlaneCore::ConfigItem.new(key: :fail_on_error,
138
- env_name: "FL_SLACK_FAIL_ON_ERROR",
139
- description: "Should an error sending the slack notification cause a failure? (true/false)",
94
+ env_name: 'FL_SLACK_FAIL_ON_ERROR',
95
+ description: 'Should an error sending the slack notification cause a failure? (true/false)',
140
96
  optional: true,
141
97
  default_value: true,
142
98
  type: Boolean),
143
99
  FastlaneCore::ConfigItem.new(key: :link_names,
144
- env_name: "FL_SLACK_LINK_NAMES",
145
- description: "Find and link channel names and usernames (true/false)",
100
+ env_name: 'FL_SLACK_LINK_NAMES',
101
+ description: 'Find and link channel names and usernames (true/false)',
146
102
  optional: true,
147
103
  default_value: false,
148
104
  type: Boolean)
@@ -158,15 +114,15 @@ module Fastlane
158
114
  end
159
115
 
160
116
  def self.description
161
- "Send a message to your [Slack](https://slack.com) group"
117
+ 'Send a message to your [Slack](https://slack.com) group'
162
118
  end
163
119
 
164
120
  def self.details
165
- "This plugin is forked from the Fastlane Slack action. Additionally, it takes thread_timestamp parameter to send the message under a thread."
121
+ 'This plugin is forked from the Fastlane Slack action. Additionally, it takes thread_timestamp parameter to send the message under a thread.'
166
122
  end
167
123
 
168
124
  def self.authors
169
- ["Doruk Kangal"]
125
+ ['Doruk Kangal']
170
126
  end
171
127
 
172
128
  def self.example_code
@@ -1,15 +1,68 @@
1
1
  require 'fastlane_core/ui/ui'
2
+ require 'fastlane/action'
3
+ require 'fastlane/actions/slack'
4
+ require 'faraday'
2
5
 
3
6
  module Fastlane
4
- UI = FastlaneCore::UI unless Fastlane.const_defined?("UI")
7
+ UI = FastlaneCore::UI unless Fastlane.const_defined?(:UI)
5
8
 
6
9
  module Helper
7
10
  class SlackMessageHelper
8
- # class methods that you define here become available in your action
9
- # as `Helper::SlackMessageHelper.your_method`
10
- #
11
- def self.show_message
12
- UI.message("Hello from the slack_message plugin helper!")
11
+ @client = Faraday.new do |builder|
12
+ builder.use(Faraday::Response::RaiseError)
13
+ end
14
+
15
+ def self.post_message(slack_url:, channel:, thread_timestamp:, username:, attachments:, link_names:, icon_url:, fail_on_error:)
16
+ @client.post(slack_url) do |request|
17
+ request.headers['Content-Type'] = 'application/json'
18
+ request.body = {
19
+ channel: channel,
20
+ username: username,
21
+ thread_ts: thread_timestamp,
22
+ icon_url: icon_url,
23
+ attachments: attachments,
24
+ link_names: link_names
25
+ }.to_json
26
+ end
27
+ UI.success('Successfully sent Slack notification')
28
+ rescue StandardError => e
29
+ UI.error("Error while pushing Slack message: #{e}")
30
+ message = "Maybe the integration has no permission to post on this channel? Try removing the channel parameter in your Fastfile, this is usually caused by a misspelled or changed group/channel name or an expired SLACK_URL"
31
+ if fail_on_error
32
+ UI.user_error!(message)
33
+ else
34
+ UI.error(message)
35
+ end
36
+ end
37
+
38
+ def self.prepare_message(options)
39
+ options[:message] = trim(options[:message])
40
+ options[:message] = convert_links(options[:message])
41
+ options[:pretext] = newline_interpretation(options[:pretext])
42
+
43
+ Fastlane::Actions::SlackAction.generate_slack_attachments(options)
44
+ end
45
+
46
+ def self.trim(text)
47
+ Fastlane::Actions::SlackAction.trim_message(text || '')
48
+ end
49
+
50
+ def self.convert_links(text)
51
+ Fastlane::Notification::Slack::LinkConverter.convert(text)
52
+ end
53
+
54
+ def self.newline_interpretation(text)
55
+ (text || '').gsub('\n', "\n")
56
+ end
57
+
58
+ def self.format_channel(channel_name)
59
+ if channel_name.nil? || channel_name.empty?
60
+ nil
61
+ elsif %w[# @].include?(channel_name[0])
62
+ channel_name
63
+ else
64
+ '#' + channel_name
65
+ end
13
66
  end
14
67
  end
15
68
  end
@@ -1,5 +1,5 @@
1
1
  module Fastlane
2
2
  module SlackMessage
3
- VERSION = "0.1.0"
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_message
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doruk Kangal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-24 00:00:00.000000000 Z
11
+ date: 2022-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler