Class: Fastlane::Actions::FileUploadToSlackAction

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

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



140
141
142
# File 'lib/fastlane/plugin/slack_bot/actions/file_upload_to_slack.rb', line 140

def self.authors
  ["crazymanish"]
end

.available_optionsObject



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

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: "FL_FILE_UPLOAD_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: :channels,
                                 env_name: "FL_FETCH_FILES_SLACK_CHANNELS",
                                 description: "Comma-separated list of slack #channel names where the file will be shared",
                                 is_string: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :file_path,
                                 env_name: "FL_FILE_UPLOAD_TO_SLACK_FILE_PATH",
                                 description: "relative file path which will upload to slack",
                                 is_string: true,
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :file_name,
                                 env_name: "FL_FILE_UPLOAD_TO_SLACK_FILE_NAME",
                                 description: "This is optional filename of the file",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :file_type,
                                 env_name: "FL_FILE_UPLOAD_TO_SLACK_FILE_TYPE",
                                 description: "This is optional filetype of the file",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :title,
                                 env_name: "FL_FILE_UPLOAD_TO_SLACK_TITLE",
                                 description: "This is optional Title of file",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :initial_comment,
                                 env_name: "FL_FILE_UPLOAD_TO_SLACK_INITIAL_COMMENT",
                                 description: "This is optional message text introducing the file",
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :thread_ts,
                                 env_name: "FL_FILE_UPLOAD_TO_SLACK_THREAD_TS",
                                 description: "Provide another message's ts value to make this message a reply",
                                 is_string: true,
                                 optional: true)
  ]
end

.descriptionObject



83
84
85
# File 'lib/fastlane/plugin/slack_bot/actions/file_upload_to_slack.rb', line 83

def self.description
  "Upload a file to slack channel"
end

.detailsObject



87
88
89
# File 'lib/fastlane/plugin/slack_bot/actions/file_upload_to_slack.rb', line 87

def self.details
  "Upload a file to slack channel or DM to a slack user"
end

.example_codeObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fastlane/plugin/slack_bot/actions/file_upload_to_slack.rb', line 144

def self.example_code
  [
    'file_upload_to_slack(
      channels: "slack_channel_name",
      file_path: "fastlane/test.png"
    )',
    'file_upload_to_slack(
      title: "This is test title",
      channels: "slack_channel_name1, slack_channel_name2",
      file_path: "fastlane/report.xml"
    )',
    'file_upload_to_slack(
      title: "This is test title",
      initial_comment: "This is test initial comment",
      channels: "slack_channel_name",
      file_path: "fastlane/screenshots.zip"
    )',
    'file_upload_to_slack(
      title: "This is test title", # Optional, uploading file title
      initial_comment: "This is test initial comment",  # Optional, uploading file initial comment
      channels: "slack_channel_name",
      file_path: "fastlane/screenshots.zip",
      thread_ts: thread_ts # Optional, Provide parent slack message `ts` value to upload this file as a reply.
    )'
  ]
end

.formatted_result(response) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/fastlane/plugin/slack_bot/actions/file_upload_to_slack.rb', line 63

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)


171
172
173
# File 'lib/fastlane/plugin/slack_bot/actions/file_upload_to_slack.rb', line 171

def self.is_supported?(platform)
  true
end

.parse_json(value) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/fastlane/plugin/slack_bot/actions/file_upload_to_slack.rb', line 71

def self.parse_json(value)
  require 'json'

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

.run(params) ⇒ Object



11
12
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
57
58
59
60
61
# File 'lib/fastlane/plugin/slack_bot/actions/file_upload_to_slack.rb', line 11

def self.run(params)
  file_path = params[:file_path]

  if params[:file_name].to_s.empty?
    file_name = File.basename(file_path, ".*") # if file_path = "/path/file_name.jpeg" then will return "file_name"
  else
    file_name = params[:file_name]
  end

  if params[:file_type].to_s.empty?
    file_type = File.extname(file_path)[1..-1] # if file_path = "/path/file_name.jpeg" then will return "jpeg"
  else
    file_type = params[:file_type]
  end

  begin
    require 'faraday'

    api_url = "https://slack.com/api/files.upload"
    conn = Faraday.new(url: api_url) do |faraday|
      faraday.request :multipart
      faraday.request :url_encoded
      faraday.adapter :net_http
    end

    payload = {
      channels: params[:channels],
      file: Faraday::FilePart.new(file_path, file_type),
      filename: file_name,
      filetype: file_type
    }

    payload[:title] = params[:title] unless params[:title].nil?
    payload[:initial_comment] = params[:initial_comment] unless params[:initial_comment].nil?
    payload[:thread_ts] = params[:thread_ts] unless params[:thread_ts].nil?

    response = conn.post do |req|
      req.headers['Authorization'] = "Bearer #{params[:api_token]}"
      req.body = payload
     end

    result = self.formatted_result(response)
  rescue => exception
    UI.error("Exception: #{exception}")
    return nil
  else
    UI.success("Successfully uploaded file to Slack! 🚀")
    Actions.lane_context[SharedValues::FILE_UPLOAD_TO_SLACK_RESULT] = result
    return result
  end
end