class Fastlane::Actions::ReportAction

Public Class Methods

authors() click to toggle source
# File lib/fastlane/plugin/report/actions/report_action.rb, line 105
def self.authors
  ["Bruno Miguè‚ns"]
end
available_options() click to toggle source
# File lib/fastlane/plugin/report/actions/report_action.rb, line 109
def self.available_options
  [FastlaneCore::ConfigItem.new(key: :slack_api_token,
                            env_name: "REPORT_SLACK_API_TOKEN",
                         description: "Token of slack rest api. Need to be generated at 'https://api.slack.com'",
                            optional: false,
                                type: String,
                        verify_block: proc do |value|
                                   UI.user_error!("You need to provide a 'slack_api_token' parameter") if value.to_s.length == 0
                                 end),
  FastlaneCore::ConfigItem.new(key: :path,
                            env_name: "REPORT_PATH",
                         description: "Relative path to report file (ex. .../report/20170603140211.html)",
                            optional: false,
                                type: String,
                        verify_block: proc do |value|
                                   UI.user_error!("You need to provide a 'path' parameter") if value.to_s.length == 0
                                 end),
  FastlaneCore::ConfigItem.new(key: :path_type,
                            env_name: "REPORT_PATH_TYPE",
                         description: "Multipart type of your report file (ex. text/html)",
                            optional: false,
                                type: String,
                        verify_block: proc do |value|
                                   UI.user_error!("You need to provide a 'path_type' parameter") if value.to_s.length == 0
                                 end),
  FastlaneCore::ConfigItem.new(key: :file_name,
                            env_name: "REPORT_FILE_NAME",
                         description: "Name of your file (ex. 20170603140211.html)",
                            optional: false,
                                type: String,
                        verify_block: proc do |value|
                                   UI.user_error!("You need to provide a 'file_name' parameter") if value.to_s.length == 0
                                 end),
  FastlaneCore::ConfigItem.new(key: :channels,
                            env_name: "REPORT_SLACK_CHANNELS",
                         description: "Selected channels to upload and post your report (default: '#channel')",
                            optional: true,
                                type: String),
  FastlaneCore::ConfigItem.new(key: :title,
                            env_name: "REPORT_TITLE",
                         description: "Title that will be shown on Slack (default: 'Report!')",
                            optional: true,
                                type: String),
  FastlaneCore::ConfigItem.new(key: :message,
                            env_name: "REPORT_MESSAGE",
                         description: "Message that will be shown on Slack (default: 'This is your report.')",
                            optional: true,
                                type: String),
  FastlaneCore::ConfigItem.new(key: :scan_as_pdf,
                            env_name: "REPORT_SCAN_AS_PDF",
                         description: "Message that will be shown on Slack (default: 'true' if was a HTML file)",
                            optional: true,
                                type: TrueClass)
  ]
end
category() click to toggle source
# File lib/fastlane/plugin/report/actions/report_action.rb, line 165
def self.category
  :notifications
end
description() click to toggle source
# File lib/fastlane/plugin/report/actions/report_action.rb, line 90
def self.description
  [
    "After an action, like scan for instance, you're able to generate a report file.",
    "This fastlane plugin gives you a way to upload and share your report using Slack.",
    "",
    "report(
      slack_api_token: 'xxxx-0000000000-11111111111-222222222222-xxxxxxxxx33333333xxxxxxxx3333333',
      path: './some/path/report.html',
      path_type: 'text/html',
      file_name: 'report.html'
    )",
    ""
  ].join('\n')
end
example_code() click to toggle source
# File lib/fastlane/plugin/report/actions/report_action.rb, line 173
def self.example_code
  [
    "report(
      slack_api_token: 'xxxx-0000000000-11111111111-222222222222-xxxxxxxxx33333333xxxxxxxx3333333',
      path: './some/path/report.html',
      path_type: 'text/html',
      file_name: 'report.html'
    )"
  ]
end
is_supported?(platform) click to toggle source
# File lib/fastlane/plugin/report/actions/report_action.rb, line 169
def self.is_supported?(platform)
  true
end
pdf(options) click to toggle source
# File lib/fastlane/plugin/report/actions/report_action.rb, line 51
def self.pdf(options)
  Actions.verify_gem!('pdfkit')
  Actions.verify_gem!('nokogiri')

  require 'pdfkit'
  require 'nokogiri'

  PDFKit.configure do |config|

    config.default_options = {
      page_size: "Letter",
      encoding: "UTF-8",
      disable_smart_shrinking: false,
      footer_spacing: 2,
      footer_center: "Page [page] of [toPage]"
    }

  end

  path = options[:path] ||= ''
  html = File.open(path, "r")

  # Will be removed the segmented bar in case of you are uploading a Fastlane Scan report
  html_doc = Nokogiri::HTML(html)
  html_doc.css('section#segment-bar').remove
  html = html_doc.to_html

  kit = PDFKit.new(html, :page_size => 'Letter')

  if !kit.nil?
    path = path.gsub('.html', '.pdf')
    file = kit.to_file(path)
    return file
  else
    return path
  end

end
run(options) click to toggle source
# File lib/fastlane/plugin/report/actions/report_action.rb, line 4
def self.run(options)
  Actions.verify_gem!('slack-ruby-client')

  require 'slack-ruby-client'

  if options.nil?
    UI.error "Is mandatory to provide a 'Slack API Token'."
  else

    token = options[:slack_api_token] ||= ''

    Slack.configure do |config|
      config.token = token
    end

    channel = options[:channels] ||= '#channel'
    title = options[:title] ||= 'Report!'
    message = options[:message] ||= 'This is your report.'

    path = options[:path]
    path_type = options[:path_type]
    file_name = options[:file_name]
    scan_as_pdf = options[:scan_as_pdf] ||= path_type == 'text/html'

    if scan_as_pdf
      path = pdf(path: path)
    end

    if !token.empty?
      client = Slack::Web::Client.new
      client.files_upload(
        channels: channel,
        as_user: false,
        file: Faraday::UploadIO.new(path, path_type),
        title: title,
        filename: file_name,
        initial_comment: message
      )
    end

    UI.success("Report successfully uploaded and sent to Slack channel '" + channel + "'.")
    UI.success("Title: " + title + " and messsage: " + options[:message])

  end

end