Class: Fastlane::Actions::PostToSlackAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb

Class Method Summary collapse

Class Method Details

.authorsObject



158
159
160
# File 'lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb', line 158

def self.authors
  ["crazymanish"]
end

.available_optionsObject



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
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb', line 82

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: "FL_POST_TO_SLACK_BOT_TOKEN",
                                 description: "Slack bot Token",
                                 sensitive: true,
                                 code_gen_sensitive: true,
                                 is_string: true,
                                 default_value: ENV["SLACK_API_TOKEN"],
                                 default_value_dynamic: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :channel,
                                 env_name: "FL_POST_TO_SLACK_CHANNEL",
                                 description: "#channel or @username",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :username,
                                 env_name: "FL_SLACK_USERNAME",
                                 description: "Overrides the bot's username (chat:write.customize scope required)",
                                 default_value: "fastlane",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :icon_url,
                                 env_name: "FL_SLACK_ICON_URL",
                                 description: "Overrides the bot's image (chat:write.customize scope required)",
                                 default_value: "https://fastlane.tools/assets/img/fastlane_icon.png",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :as_user,
                                 env_name: "FL_POST_TO_SLACK_AS_USER",
                                 description: "Pass true to post the message as the authed user. Defaults to false",
                                 optional: true,
                                 default_value: false,
                                 is_string: false),
    FastlaneCore::ConfigItem.new(key: :pretext,
                                 env_name: "FL_POST_TO_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: :message,
                                 env_name: "FL_POST_TO_SLACK_MESSAGE",
                                 description: "The message that should be displayed on Slack",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :payload,
                                 env_name: "FL_POST_TO_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_POST_TO_SLACK_DEFAULT_PAYLOADS",
                                 description: "Remove some of the default payloads. More information about the available payloads on GitHub",
                                 optional: true,
                                 default_value: ['lane', 'test_result', 'git_branch', 'git_author', 'last_git_commit', 'last_git_commit_hash'],
                                 type: Array),
    FastlaneCore::ConfigItem.new(key: :attachment_properties,
                                 env_name: "FL_POST_TO_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_POST_TO_SLACK_SUCCESS",
                                 description: "Was this successful? (true/false)",
                                 optional: true,
                                 default_value: true,
                                 is_string: false),
    FastlaneCore::ConfigItem.new(key: :thread_ts,
                                 env_name: "FL_POST_TO_SLACK_THREAD_TS",
                                 description: "Provide another message's ts value to make this message a reply",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :reply_broadcast,
                                 env_name: "FL_POST_TO_SLACK_REPLY_BROADCAST",
                                 description: "Used in conjunction with thread_ts and indicates whether reply should be made visible to everyone in the channel or conversation. Defaults to false",
                                 optional: true,
                                 default_value: false,
                                 is_string: false)
  ]
end

.descriptionObject



74
75
76
# File 'lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb', line 74

def self.description
  "Post a slack message"
end

.detailsObject



78
79
80
# File 'lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb', line 78

def self.details
  "Post a slack message to any #channel/@user using Slack bot chat postMessage api."
end

.example_codeObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb', line 162

def self.example_code
  [
    'post_to_slack(message: "App successfully released!")',
    'post_to_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

.formatted_result(response) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb', line 58

def self.formatted_result(response)
  result = {
    status: response[:status],
    body: response.body || "",
    json: self.parse_json(response.body) || {}
  }
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb', line 188

def self.is_supported?(platform)
  true
end

.parse_json(value) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb', line 66

def self.parse_json(value)
  require 'json'

  JSON.parse(value)
rescue JSON::ParserError
  nil
end

.run(options) ⇒ Object



13
14
15
16
17
18
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
54
55
56
# File 'lib/fastlane/plugin/slack_bot/actions/post_to_slack.rb', line 13

def self.run(options)
  options[:message] = (options[:message].to_s || '').gsub('\n', "\n")
  options[:message] = Helper::SlackBotLinkFormatterHelper.format(options[:message])
  options[:pretext] = options[:pretext].gsub('\n', "\n") unless options[:pretext].nil?

  if options[:channel].to_s.length > 0
    slack_channel = options[:channel]
    slack_channel = ('#' + options[:channel]) unless ['#', 'C', '@'].include?(slack_channel[0]) # Add prefix(#) by default, if needed
  end

  slack_attachment = Helper::SlackBotAttachmentsHelper.generate_slack_attachments(options)
  bot_username = options[:username]
  bot_icon_url = options[:icon_url]

  begin
    require 'excon'

    api_url = "https://slack.com/api/chat.postMessage"
    headers = {
      "Content-Type": "application/json; charset=utf-8",
      "Authorization": "Bearer #{options[:api_token]}"
    }
    payload = {
      channel: slack_channel,
      username: bot_username,
      icon_url: bot_icon_url,
      attachments: [slack_attachment]
    }
    payload[:as_user] = options[:as_user] if options[:as_user] # default is false
    payload[:reply_broadcast] = options[:reply_broadcast] if options[:reply_broadcast] # default is false
    payload[:thread_ts] = options[:thread_ts] unless options[:thread_ts].nil?
    payload = payload.to_json

    response = Excon.post(api_url, headers: headers, body: payload, omit_default_port: true)
    result = self.formatted_result(response)
  rescue => exception
    UI.error("Exception: #{exception}")
    return nil
  else
    UI.success("Successfully sent Slack notification")
    Actions.lane_context[SharedValues::POST_TO_SLACK_RESULT] = result
    return result
  end
end