# rubocop:disable Metrics/MethodLength require 'fastlane/erb_template_helper' module Fastlane module Actions class MailgunAction < Action def self.is_supported?(platform) true end def self.run(options) Actions.verify_gem!('rest-client') Actions.verify_gem!('premailer') require 'rest-client' require 'premailer' inline_images(options) mailgunit(options) end def self.description "Send a success/error message to an email group" end def self.available_options [ 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", 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: "Polidea Store"), FastlaneCore::ConfigItem.new(key: :success, env_name: "MAILGUN_SUCCESS", description: "Was this build successful? (true/false)", optional: true, default_value: true, is_string: false), FastlaneCore::ConfigItem.new(key: :ci_build_link, env_name: "MAILGUN_CI_BUILD_LINK", description: "CI Build Link", optional: true, is_string: true), FastlaneCore::ConfigItem.new(key: :template_path, env_name: "MAILGUN_TEMPLATE_PATH", description: "Mail HTML template", optional: true, is_string: true), FastlaneCore::ConfigItem.new(key: :app_icon, env_name: "MAILGUN_APP_ICON", description: "Path to app icon file", is_string: true, optional: true, default_value: Actions.lane_context[SharedValues::ICON_OUTPUT_PATH]), FastlaneCore::ConfigItem.new(key: :app_name, env_name: "MAILGUN_APP_NAME", description: "Application name", is_string: true, default_value: Actions.lane_context[SharedValues::APP_NAME]), FastlaneCore::ConfigItem.new(key: :app_version, env_name: "MAILGUN_APP_VERSION", description: "Application version", is_string: true, default_value: Actions.lane_context[SharedValues::APP_VERSION]), FastlaneCore::ConfigItem.new(key: :build_number, env_name: "MAILGUN_BUILD_NUMBER", description: "Build number", default_value: Actions.lane_context[SharedValues::BUILD_NUMBER]), FastlaneCore::ConfigItem.new(key: :installation_link, env_name: "MAILGUN_INSTALLATION_LINK", description: "Link to installation page", is_string: true, default_value: Actions.lane_context[SharedValues::S3_HTML_OUTPUT_PATH]), FastlaneCore::ConfigItem.new(key: :release_notes, env_name: "MAILGUN_RELEASE_NOTES", description: "Release notes", type: String, optional: true, default_value: Actions.lane_context[SharedValues::RELEASE_NOTES]), FastlaneCore::ConfigItem.new(key: :binary_size, env_name: "MAILGUN_BINARY_SIZE", description: "Binary size", type: Fixnum, default_value: Actions.lane_context[SharedValues::BINARY_SIZE]), FastlaneCore::ConfigItem.new(key: :inline, type: Array, optional: true, default_value: []) ] end def self.author "thiagolioy" end def self.inline_images(options) base_path = "#{__dir__}/../templates/images" options[:inline] = [ qr_code(options[:installation_link]) ] options[:inline] << File.new(options[:app_icon]) if options[:app_icon] images = [ "polidea-facebook-icon.png", "polidea-github-icon.png", "polidea-logo.png", "polidea-twitter-icon.png" ] images << "icon-placeholder.png" unless options[:app_icon] options[:inline].concat(images.map { |image| File.new("#{base_path}/#{image}") }) end def self.qr_code(installation_link) qr_code_path = "/tmp/qr_code.png" QRGenerator.new(installation_link, false, ::ChunkyPNG::Color.rgb(103, 103, 103)).generate(qr_code_path) File.new(qr_code_path) end def self.mailgunit(options) sandbox_domain = options[:postmaster].split("@").last body = mail_template(options) RestClient.post "https://api:#{options[:apikey]}@api.mailgun.net/v3/#{sandbox_domain}/messages", from: "#{options[:from]}<#{options[:postmaster]}>", to: (options[:to]).to_s, subject: "#{options[:app_name]} #{options[:app_version]} (#{options[:build_number]}) for #{Actions.lane_context[Actions::SharedValues::PLATFORM_NAME]} is ready to install", html: body, inline: options[:inline] return body end def self.mail_template(options) hash = { author: Actions.git_author_email, last_commit: Actions.last_git_commit_message, is_android: Actions.lane_context[Actions::SharedValues::PLATFORM_NAME] == :android, app_icon: options[:app_icon] ? File.basename(options[:app_icon]) : nil, app_name: options[:app_name], app_version: options[:app_version], build_number: options[:build_number], installation_link: options[:installation_link], release_notes: (options[:release_notes] || "").split("\n"), platform: Actions.lane_context[Actions::SharedValues::PLATFORM_NAME], release_date: DateTime.now.strftime('%b %d, %Y'), binary_size: (options[:binary_size] / 1024.0 / 1024.0).round(2).to_s } hash[:success] = options[:success] hash[:ci_build_link] = options[:ci_build_link] # 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_from_path("#{__dir__}/../templates/mailgun_template.erb") end premailer = Premailer.new( eth.render(html_template, hash), { warn_level: Premailer::Warnings::SAFE, with_html_string: true } ) premailer.to_inline_css end end end end # rubocop:enable Metrics/MethodLength