Class: Fastlane::Actions::MailgunAction

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

Constant Summary

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, authors, deprecated_notes, details, lane_context, method_missing, other_action, output, return_type, return_value, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorObject



106
107
108
# File 'fastlane/lib/fastlane/actions/mailgun.rb', line 106

def self.author
  "thiagolioy"
end

.available_optionsObject



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

def self.available_options
  [
    # This is here just for while due to the transition, not needed anymore
    FastlaneCore::ConfigItem.new(key: :mailgun_sandbox_domain,
                                 env_name: "MAILGUN_SANDBOX_POSTMASTER",
                                 optional: true,
                                 description: "Mailgun sandbox domain postmaster for your mail. Please use postmaster instead"),
    # This is here just for while due to the transition, should use postmaster instead
    FastlaneCore::ConfigItem.new(key: :mailgun_sandbox_postmaster,
                                 env_name: "MAILGUN_SANDBOX_POSTMASTER",
                                 optional: true,
                                 description: "Mailgun sandbox domain postmaster for your mail. Please use postmaster instead"),
    # This is here just for while due to the transition, should use apikey instead
    FastlaneCore::ConfigItem.new(key: :mailgun_apikey,
                                 env_name: "MAILGUN_APIKEY",
                                 sensitive: true,
                                 optional: true,
                                 description: "Mailgun apikey for your mail. Please use postmaster instead"),

    FastlaneCore::ConfigItem.new(key: :postmaster,
                                 env_name: "MAILGUN_SANDBOX_POSTMASTER",
                                 description: "Mailgun sandbox domain postmaster for your mail"),
    FastlaneCore::ConfigItem.new(key: :apikey,
                                 env_name: "MAILGUN_APIKEY",
                                 sensitive: true,
                                 description: "Mailgun apikey for your mail"),
    FastlaneCore::ConfigItem.new(key: :to,
                                 env_name: "MAILGUN_TO",
                                 description: "Destination of your mail"),
    FastlaneCore::ConfigItem.new(key: :from,
                                 env_name: "MAILGUN_FROM",
                                 optional: true,
                                 description: "Mailgun sender name",
                                 default_value: "Mailgun Sandbox"),
    FastlaneCore::ConfigItem.new(key: :message,
                                 env_name: "MAILGUN_MESSAGE",
                                 description: "Message of your mail"),
    FastlaneCore::ConfigItem.new(key: :subject,
                                 env_name: "MAILGUN_SUBJECT",
                                 description: "Subject of your mail",
                                 optional: true,
                                 default_value: "fastlane build"),
    FastlaneCore::ConfigItem.new(key: :success,
                                 env_name: "MAILGUN_SUCCESS",
                                 description: "Was this build successful? (true/false)",
                                 optional: true,
                                 default_value: true,
                                 type: Boolean),
    FastlaneCore::ConfigItem.new(key: :app_link,
                                 env_name: "MAILGUN_APP_LINK",
                                 description: "App Release link",
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :ci_build_link,
                                 env_name: "MAILGUN_CI_BUILD_LINK",
                                 description: "CI Build Link",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :template_path,
                                 env_name: "MAILGUN_TEMPLATE_PATH",
                                 description: "Mail HTML template",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :reply_to,
                                 env_name: "MAILGUN_REPLY_TO",
                                 description: "Mail Reply to",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :attachment,
                                 env_name: "MAILGUN_ATTACHMENT",
                                 description: "Mail Attachment filenames, either an array or just one string",
                                 optional: true,
                                 type: Array),
    FastlaneCore::ConfigItem.new(key: :custom_placeholders,
                                 short_option: "-p",
                                 env_name: "MAILGUN_CUSTOM_PLACEHOLDERS",
                                 description: "Placeholders for template given as a hash",
                                 default_value: {},
                                 type: Hash)
  ]
end

.categoryObject



208
209
210
# File 'fastlane/lib/fastlane/actions/mailgun.rb', line 208

def self.category
  :notifications
end

.descriptionObject



24
25
26
# File 'fastlane/lib/fastlane/actions/mailgun.rb', line 24

def self.description
  "Send a success/error message to an email group"
end

.example_codeObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'fastlane/lib/fastlane/actions/mailgun.rb', line 181

def self.example_code
  [
    'mailgun(
      to: "fastlane@krausefx.com",
      success: true,
      message: "This is the mail\'s content"
    )',
    'mailgun(
      postmaster: "MY_POSTMASTER",
      apikey: "MY_API_KEY",
      to: "DESTINATION_EMAIL",
      from: "EMAIL_FROM_NAME",
      reply_to: "EMAIL_REPLY_TO",
      success: true,
      message: "Mail Body",
      app_link: "http://www.myapplink.com",
      ci_build_link: "http://www.mycibuildlink.com",
      template_path: "HTML_TEMPLATE_PATH",
      custom_placeholders: {
        :var1 => 123,
        :var2 => "string"
      },
      attachment: "dirname/filename.ext"
    )'
  ]
end

.handle_params_transition(options) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'fastlane/lib/fastlane/actions/mailgun.rb', line 110

def self.handle_params_transition(options)
  if options[:mailgun_sandbox_postmaster] && !options[:postmaster]
    options[:postmaster] = options[:mailgun_sandbox_postmaster]
    puts("\nUsing :mailgun_sandbox_postmaster is deprecated, please change to :postmaster".yellow)
  end

  if options[:mailgun_apikey] && !options[:apikey]
    options[:apikey] = options[:mailgun_apikey]
    puts("\nUsing :mailgun_apikey is deprecated, please change to :apikey".yellow)
  end
end

.is_supported?(platform) ⇒ Boolean

Returns:



6
7
8
# File 'fastlane/lib/fastlane/actions/mailgun.rb', line 6

def self.is_supported?(platform)
  true
end

.mail_template(options) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'fastlane/lib/fastlane/actions/mailgun.rb', line 155

def self.mail_template(options)
  hash = {
    author: Actions.git_author_email,
    last_commit: Actions.last_git_commit_message,
    message: options[:message],
    app_link: options[:app_link]
  }
  hash[:success] = options[:success]
  hash[:ci_build_link] = options[:ci_build_link]

  # concatenate with custom placeholders passed by user
  hash = hash.merge(options[:custom_placeholders])

  # grabs module
  eth = Fastlane::ErbTemplateHelper

  # create html from template
  html_template_path = options[:template_path]
  if html_template_path && File.exist?(html_template_path)
    html_template = eth.load_from_path(html_template_path)
  else
    html_template = eth.load("mailgun_html_template")
  end
  eth.render(html_template, hash)
end

.mailgunit(options) ⇒ Object



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

def self.mailgunit(options)
  sandbox_domain = options[:postmaster].split("@").last
  params = {
    from: "#{options[:from]} <#{options[:postmaster]}>",
    to: (options[:to]).to_s,
    subject: options[:subject],
    html: mail_template(options)
  }
  unless options[:reply_to].nil?
    params.store(:"h:Reply-To", options[:reply_to])
  end

  unless options[:attachment].nil?
    attachment_filenames = [*options[:attachment]]
    attachments = attachment_filenames.map { |filename| Faraday::UploadIO.new(filename, mime_for(filename), filename) }
    params.store(:attachment, attachments)
  end

  conn = Faraday.new(url: "https://api:#{options[:apikey]}@api.mailgun.net") do |f|
    f.request(:multipart)
    f.request(:url_encoded)
    f.adapter(:net_http)
  end
  response = conn.post("/v3/#{sandbox_domain}/messages", params)
  UI.user_error!("Failed to send message via Mailgun, response: #{response.status}: #{response.body}.") if response.status != 200
  mail_template(options)
end

.mime_for(path) ⇒ Object



150
151
152
153
# File 'fastlane/lib/fastlane/actions/mailgun.rb', line 150

def self.mime_for(path)
  mime = MIME::Types.type_for(path)
  mime.empty? ? 'text/plain' : mime[0].content_type
end

.run(options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'fastlane/lib/fastlane/actions/mailgun.rb', line 10

def self.run(options)
  Actions.verify_gem!('faraday')
  Actions.verify_gem!('mime-types')
  require 'faraday'
  begin
    # Use mime/types/columnar if available, for reduced memory usage
    require 'mime/types/columnar'
  rescue LoadError
    require 'mime/types'
  end
  handle_params_transition(options)
  mailgunit(options)
end