Class: Fastlane::Actions::SlackAction

Inherits:
Fastlane::Action show all
Defined in:
lib/fastlane/actions/slack.rb

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, authors, details, output, sh, step_text

Class Method Details

.authorObject



99
100
101
# File 'lib/fastlane/actions/slack.rb', line 99

def self.author
  "KrauseFx"
end

.available_optionsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
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
# File 'lib/fastlane/actions/slack.rb', line 59

def self.available_options
  [
    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: :channel,
                                 env_name: "FL_SLACK_CHANNEL",
                                 description: "#channel or @username",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :slack_url,
                                 env_name: "SLACK_URL",
                                 description: "Create an Incoming WebHook for your Slack group",
                                 verify_block: Proc.new do |value|
                                  raise "Invalid URL, must start with https://" unless value.start_with?"https://"
                                 end),
    FastlaneCore::ConfigItem.new(key: :payload,
                                 env_name: "FL_SLACK_PAYLOAD",
                                 description: "Add additional information to this post. payload must be a hash containg 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,
                                 is_string: false),
    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),
  ]
end

.descriptionObject



55
56
57
# File 'lib/fastlane/actions/slack.rb', line 55

def self.description
  "Send a success/error message to your Slack group"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/fastlane/actions/slack.rb', line 5

def self.is_supported?(platform)
  true
end

.run(options) ⇒ Object



19
20
21
22
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
# File 'lib/fastlane/actions/slack.rb', line 19

def self.run(options)
  require 'slack-notifier'

  options[:message] = self.trim_message(options[:message].to_s || '')
  options[:message] = Slack::Notifier::LinkFormatter.format(options[:message])

  url = ENV['SLACK_URL']
  unless url
    Helper.log.fatal "Please add 'ENV[\"SLACK_URL\"] = \"https://hooks.slack.com/services/...\"' to your Fastfile's `before_all` section.".red
    raise 'No SLACK_URL given.'.red
  end

  notifier = Slack::Notifier.new url

  notifier.username = 'fastlane'
  if options[:channel].to_s.length > 0
    notifier.channel = options[:channel]
    notifier.channel = ('#' + notifier.channel) unless ['#', '@'].include?(notifier.channel[0]) # send message to channel by default
  end

  slack_attachment = generate_slack_attachments(options)

  return [notifier, slack_attachment] if Helper.is_test? # tests will verify the slack attachments and other properties

  result = notifier.ping '',
                         icon_url: 'https://s3-eu-west-1.amazonaws.com/fastlane.tools/fastlane.png',
                         attachments: [slack_attachment]

  unless result.code.to_i == 200
    Helper.log.debug result
    raise 'Error pushing Slack message, maybe the integration has no permission to post on this channel? Try removing the channel parameter in your Fastfile.'.red
  else
    Helper.log.info 'Successfully sent Slack notification'.green
  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



12
13
14
15
16
17
# File 'lib/fastlane/actions/slack.rb', line 12

def self.trim_message(message)
  # We want the last 7000 characters, instead of the first 7000, as the error is at the bottom
  start_index = [message.length - 7000, 0].max
  message = message[start_index..-1]
  message
end