Class: Fastlane::Actions::SlackAction
- Inherits:
-
Fastlane::Action
- Object
- Fastlane::Action
- Fastlane::Actions::SlackAction
- Defined in:
- fastlane/lib/fastlane/actions/slack.rb
Constant Summary
Constants inherited from Fastlane::Action
Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES
Helper collapse
-
.deep_merge(a, b) ⇒ Object
Adapted from stackoverflow.com/a/30225093/158525.
- .generate_slack_attachments(options) ⇒ Object
Class Method Summary collapse
- .author ⇒ Object
- .available_options ⇒ Object
- .category ⇒ Object
- .description ⇒ Object
- .details ⇒ Object
- .example_code ⇒ Object
- .is_supported?(platform) ⇒ Boolean
- .run(options) ⇒ Object
-
.trim_message(message) ⇒ Object
As there is a text limit in the notifications, we are usually interested in the last part of the message e.g.
Methods inherited from Fastlane::Action
action_name, authors, deprecated_notes, lane_context, method_missing, other_action, output, return_type, return_value, sample_return_value, shell_out_should_use_bundle_exec?, step_text
Class Method Details
.author ⇒ Object
147 148 149 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 147 def self. "KrauseFx" end |
.available_options ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 72 def self. [ FastlaneCore::ConfigItem.new(key: :message, env_name: "FL_SLACK_MESSAGE", description: "The message that should be displayed on Slack. This supports the standard Slack markup language", optional: true), FastlaneCore::ConfigItem.new(key: :pretext, env_name: "FL_SLACK_PRETEXT", description: "This is optional text that appears above the message attachment block. This supports the standard Slack markup language", optional: true), FastlaneCore::ConfigItem.new(key: :channel, env_name: "FL_SLACK_CHANNEL", description: "#channel or @username", optional: true), FastlaneCore::ConfigItem.new(key: :use_webhook_configured_username_and_icon, env_name: "FL_SLACK_USE_WEBHOOK_CONFIGURED_USERNAME_AND_ICON", description: "Use webhook's default username and icon settings? (true/false)", default_value: false, is_string: false, optional: true), FastlaneCore::ConfigItem.new(key: :slack_url, env_name: "SLACK_URL", sensitive: true, description: "Create an Incoming WebHook for your Slack group", verify_block: proc do |value| UI.user_error!("Invalid URL, must start with https://") unless value.start_with?("https://") end), FastlaneCore::ConfigItem.new(key: :username, env_name: "FL_SLACK_USERNAME", description: "Overrides the webhook's username property if use_webhook_configured_username_and_icon is false", default_value: "fastlane", is_string: true, optional: true), FastlaneCore::ConfigItem.new(key: :icon_url, env_name: "FL_SLACK_ICON_URL", description: "Overrides the webhook's image property if use_webhook_configured_username_and_icon is false", default_value: "https://s3-eu-west-1.amazonaws.com/fastlane.tools/fastlane.png", is_string: true, optional: true), FastlaneCore::ConfigItem.new(key: :payload, env_name: "FL_SLACK_PAYLOAD", description: "Add additional information to this post. payload must be a hash containing any key with any value", default_value: {}, is_string: false), FastlaneCore::ConfigItem.new(key: :default_payloads, env_name: "FL_SLACK_DEFAULT_PAYLOADS", description: "Remove some of the default payloads. More information about the available payloads on GitHub", optional: true, type: Array), FastlaneCore::ConfigItem.new(key: :attachment_properties, env_name: "FL_SLACK_ATTACHMENT_PROPERTIES", description: "Merge additional properties in the slack attachment, see https://api.slack.com/docs/attachments", default_value: {}, is_string: false), FastlaneCore::ConfigItem.new(key: :success, env_name: "FL_SLACK_SUCCESS", description: "Was this build successful? (true/false)", optional: true, default_value: true, is_string: false), FastlaneCore::ConfigItem.new(key: :fail_on_error, env_name: "FL_SLACK_FAIL_ON_ERROR", description: "Should an error sending the slack notification cause a failure? (true/false)", optional: true, default_value: true, is_string: false), FastlaneCore::ConfigItem.new(key: :link_names, env_name: "FL_SLACK_LINK_NAMES", description: "Find and link channel names and usernames (true/false)", optional: true, default_value: false, is_string: false) ] end |
.category ⇒ Object
177 178 179 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 177 def self.category :notifications end |
.deep_merge(a, b) ⇒ Object
Adapted from stackoverflow.com/a/30225093/158525
275 276 277 278 279 280 281 282 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 275 def self.deep_merge(a, b) merger = proc do |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : Array === v1 && Array === v2 ? v1 | v2 : [:undefined, nil, :nil].include?(v2) ? v1 : v2 end a.merge(b, &merger) end |
.description ⇒ Object
68 69 70 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 68 def self.description "Send a success/error message to your [Slack](https://slack.com) group" end |
.details ⇒ Object
181 182 183 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 181 def self.details "Create an Incoming WebHook and export this as `SLACK_URL`. Can send a message to **#channel** (by default), a direct message to **@username** or a message to a private group **group** with success (green) or failure (red) status." end |
.example_code ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 151 def self.example_code [ 'slack(message: "App successfully released!")', 'slack( message: "App successfully released!", channel: "#channel", # Optional, by default will post to the default channel configured for the POST URL. success: true, # Optional, defaults to true. payload: { # Optional, lets you specify any number of your own Slack attachments. "Build Date" => Time.new.to_s, "Built by" => "Jenkins", }, 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. # 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`. attachment_properties: { # Optional, lets you specify any other properties available for attachments in the slack API (see https://api.slack.com/docs/attachments). # 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. thumb_url: "http://example.com/path/to/thumb.png", fields: [{ title: "My Field", value: "My Value", short: true }] } )' ] end |
.generate_slack_attachments(options) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 189 def self.() color = ([:success] ? 'good' : 'danger') should_add_payload = ->(payload_name) { [:default_payloads].nil? || [:default_payloads].map(&:to_sym).include?(payload_name.to_sym) } = { fallback: [:message], text: [:message], pretext: [:pretext], color: color, mrkdwn_in: ["pretext", "text", "fields", "message"], fields: [] } # custom user payloads [:fields] += [:payload].map do |k, v| { title: k.to_s, value: Slack::Notifier::Util::LinkFormatter.format(v.to_s), short: false } end # Add the lane to the Slack message # This might be nil, if slack is called as "one-off" action if should_add_payload[:lane] && Actions.lane_context[Actions::SharedValues::LANE_NAME] [:fields] << { title: 'Lane', value: Actions.lane_context[Actions::SharedValues::LANE_NAME], short: true } end # test_result if should_add_payload[:test_result] [:fields] << { title: 'Result', value: ([:success] ? 'Success' : 'Error'), short: true } end # git branch if Actions.git_branch && should_add_payload[:git_branch] [:fields] << { title: 'Git Branch', value: Actions.git_branch, short: true } end # git_author if Actions. && should_add_payload[:git_author] if FastlaneCore::Env.truthy?('FASTLANE_SLACK_HIDE_AUTHOR_ON_SUCCESS') && [:success] # We only show the git author if the build failed else [:fields] << { title: 'Git Author', value: Actions., short: true } end end # last_git_commit if Actions. && should_add_payload[:last_git_commit] [:fields] << { title: 'Git Commit', value: Actions., short: false } end # last_git_commit_hash if Actions.last_git_commit_hash(true) && should_add_payload[:last_git_commit_hash] [:fields] << { title: 'Git Commit Hash', value: Actions.last_git_commit_hash(short: true), short: false } end # merge additional properties deep_merge(, [:attachment_properties]) end |
.is_supported?(platform) ⇒ Boolean
7 8 9 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 7 def self.is_supported?(platform) true end |
.run(options) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 23 def self.run() require 'slack-notifier' [:message] = self.([:message].to_s || '') [:message] = Slack::Notifier::Util::LinkFormatter.format([:message]) [:pretext] = [:pretext].gsub('\n', "\n") unless [:pretext].nil? if [:channel].to_s.length > 0 channel = [:channel] channel = ('#' + [:channel]) unless ['#', '@'].include?(channel[0]) # send message to channel by default end username = [:use_webhook_configured_username_and_icon] ? nil : [:username] notifier = Slack::Notifier.new([:slack_url], channel: channel, username: username) link_names = [:link_names] icon_url = [:use_webhook_configured_username_and_icon] ? nil : [:icon_url] = () return [notifier, ] if Helper.test? # tests will verify the slack attachments and other properties begin results = notifier.ping('', link_names: link_names, icon_url: icon_url, attachments: []) rescue => exception UI.error("Exception: #{exception}") ensure result = results.first if results if !result.nil? && result.code.to_i == 200 UI.success('Successfully sent Slack notification') else UI.verbose(result) unless result.nil? = "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" if [:fail_on_error] UI.user_error!() else UI.error() end end end end |
.trim_message(message) ⇒ Object
As there is a text limit in the notifications, we are usually interested in the last part of the message e.g. for tests
14 15 16 17 18 19 20 21 |
# File 'fastlane/lib/fastlane/actions/slack.rb', line 14 def self.() # We want the last 7000 characters, instead of the first 7000, as the error is at the bottom start_index = [.length - 7000, 0].max = [start_index..-1] # We want line breaks to be shown on slack output so we replace # input non-interpreted line break with interpreted line break .gsub('\n', "\n") end |