Class: Fastlane::Actions::UpdateSlackMessageAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



109
110
111
# File 'lib/fastlane/plugin/slack_bot/actions/update_slack_message.rb', line 109

def self.authors
  ["crazymanish"]
end

.available_optionsObject



57
58
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
98
99
100
101
102
103
104
105
106
107
# File 'lib/fastlane/plugin/slack_bot/actions/update_slack_message.rb', line 57

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: "FL_UPDATE_SLACK_MESSAGE_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: :ts,
                                 env_name: "FL_UPDATE_SLACK_MESSAGE_TS",
                                 description: "Timestamp of the message to be updated",
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :channel,
                                 env_name: "FL_UPDATE_SLACK_MESSAGE_CHANNEL",
                                 description: "Slack channel i.e C1234567890",
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :pretext,
                                 env_name: "FL_UPDATE_SLACK_MESSAGE_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_UPDATE_SLACK_MESSAGE_MESSAGE",
                                 description: "The message that should be displayed on Slack",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :payload,
                                 env_name: "FL_UPDATE_SLACK_MESSAGE_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_UPDATE_SLACK_MESSAGE_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_UPDATE_SLACK_MESSAGE_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_UPDATE_SLACK_MESSAGE_SUCCESS",
                                 description: "Was this successful? (true/false)",
                                 optional: true,
                                 default_value: true,
                                 is_string: false)
  ]
end

.descriptionObject



53
54
55
# File 'lib/fastlane/plugin/slack_bot/actions/update_slack_message.rb', line 53

def self.description
  "Update a slack message using time-stamp(ts) value"
end

.example_codeObject



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
# File 'lib/fastlane/plugin/slack_bot/actions/update_slack_message.rb', line 113

def self.example_code
  [
    'update_slack_message(
      ts: "1609711037.000100",
      channel: "C1234567890",
      message: "Update: App successfully released!"
    )',
    'update_slack_message(
      ts: "1609711037.000100",
      channel: "C1234567890",
      message: "Update: App successfully released!",
      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



37
38
39
40
41
42
43
# File 'lib/fastlane/plugin/slack_bot/actions/update_slack_message.rb', line 37

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)


144
145
146
# File 'lib/fastlane/plugin/slack_bot/actions/update_slack_message.rb', line 144

def self.is_supported?(platform)
  true
end

.parse_json(value) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/fastlane/plugin/slack_bot/actions/update_slack_message.rb', line 45

def self.parse_json(value)
  require 'json'

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

.run(options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fastlane/plugin/slack_bot/actions/update_slack_message.rb', line 12

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?
  slack_attachment = Helper::SlackBotAttachmentsHelper.generate_slack_attachments(options)

  begin
    require 'excon'

    api_url = "https://slack.com/api/chat.update"
    headers = { "Content-Type": "application/json", "Authorization": "Bearer #{options[:api_token]}" }
    payload = { channel: options[:channel], attachments: [slack_attachment], ts: options[:ts] }.to_json

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