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, details, output, sh

Class Method Details

.authorObject



108
109
110
# File 'lib/fastlane/actions/slack.rb', line 108

def self.author
  "KrauseFx"
end

.available_optionsObject



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

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: :success,
                                 env_name: "FL_SLACK_SUCCESS",
                                 description: "Was this build successful? (true/false)",
                                 optional: true,
                                 default_value: true,
                                 is_string: false),
  ]
end

.descriptionObject



69
70
71
# File 'lib/fastlane/actions/slack.rb', line 69

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

.git_authorObject



4
5
6
7
8
9
10
11
# File 'lib/fastlane/actions/slack.rb', line 4

def self.git_author
  s = `git log --name-status HEAD^..HEAD`
  s = s.match(/Author:.*<(.*)>/)[1]
  return s if s.to_s.length > 0
  return nil
rescue
  return nil
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/fastlane/actions/slack.rb', line 19

def self.is_supported?(platform)
  true
end

.last_git_commitObject



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

def self.last_git_commit
  s = `git log -1 --pretty=%B`.strip
  return s if s.to_s.length > 0
  nil
end

.run(options) ⇒ Object



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
67
# File 'lib/fastlane/actions/slack.rb', line 33

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



26
27
28
29
30
31
# File 'lib/fastlane/actions/slack.rb', line 26

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