# rubocop:disable Metrics/MethodLength require 'redcarpet' require 'fastlane/erb_template_helper' module Fastlane module Actions class FotaMailAction < Action def self.is_supported?(platform) true end def self.run(options) Fastlane::Polidea.session.action_launched("fota_mail", options) Actions.verify_gem!('premailer') require 'premailer' inline_images(options) mailgunit(options) Fastlane::Polidea.session.action_completed("fota_mail") UI.success "Mail sent!" 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"), 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: :platform, env_name: "MAILGUN_PLATFORM", description: "Platform used in mail template", optional: true, default_value: Actions.lane_context[Actions::SharedValues::PLATFORM_NAME]), 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: Integer, 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) options[:inline] = [ qr_code(options[:installation_link]) ] options[:inline] << File.new(options[:app_icon]) if options[:app_icon] end def self.qr_code(installation_link) qr_code_path = "/tmp/qr_code.png" QRGenerator.new(installation_link, ::ChunkyPNG::Color.rgb(21, 18, 126)).generate(qr_code_path) File.new(qr_code_path) end def self.mailgunit(options) sandbox_domain = options[:postmaster].split("@").last body = mail_template(options) sh """curl -s --user 'api:#{options[:apikey]}' \ https://api.mailgun.net/v3/#{sandbox_domain}/messages \ -F from='#{options[:from]} <#{options[:postmaster]}>' \ -F to='#{options[:to]}' \ -F subject='#{options[:app_name]} #{options[:app_version]} (#{options[:build_number]}) for #{options[:platform]} is ready to install' \ --form-string html=#{Shellwords.escape(body)} \ """ + options[:inline].map { |file| "-F inline=@#{file.path}" }.join(" "), print_command: false, print_command_output: false return body end def self.mail_template(options) hash = { author: Actions.git_author_email, last_commit: Actions.last_git_commit_message, is_android: options[:platform] == :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], platform: options[:platform], release_date: DateTime.now.strftime('%b %d, %Y'), binary_size: (options[:binary_size] / 1024.0 / 1024.0).round(2).to_s, qr_code: "qr_code.png" } hash[:success] = options[:success] hash[:ci_build_link] = options[:ci_build_link] # create html from template html_template_path = options[:template_path] if html_template_path && File.exist?(html_template_path) eth = Fastlane::ErbTemplateHelper html_template = eth.load_from_path(html_template_path) mail_html = eth.render(html_template, hash) else mail_html = PageGenerator.mail(hash) end premailer = Premailer.new( mail_html, { warn_level: Premailer::Warnings::SAFE, with_html_string: true } ) premailer.to_inline_css end end end end # rubocop:enable Metrics/MethodLength