module Diffend::Execute

Executes a check for a given command

Public Class Methods

build_allow_message(command, response) click to toggle source

@param command [String] command executed via bundler @param response [Hash] response from diffend API

@return [String]

# File lib/diffend/execute.rb, line 94
      def build_allow_message(command, response)
        <<~MSG
          #{build_message_header('an allow', command)}
          #{build_message_info(response)}\n
          #{response['review_url']}\n
        MSG
      end
build_definition(command) click to toggle source

Build bundler definition

@return [::Bundler::Definition]

# File lib/diffend/execute.rb, line 21
def build_definition(command)
  Diffend::BuildBundlerDefinition.call(
    command,
    ::Bundler.default_gemfile,
    ::Bundler.default_lockfile
  )
end
build_deny_message(command, response) click to toggle source

@param command [String] command executed via bundler @param response [Hash] response from diffend API

@return [String]

# File lib/diffend/execute.rb, line 118
      def build_deny_message(command, response)
        <<~MSG
          #{build_message_header('a deny', command)}
          #{build_message_info(response)} Please go to the url below and review the issues.\n
          #{response['review_url']}\n
        MSG
      end
build_error(config, response) click to toggle source

@param config [Diffend::Config] @param response [Hash] response from diffend API

# File lib/diffend/execute.rb, line 48
def build_error(config, response)
  build_error_message(response)
    .tap(&config.logger.method(:error))

  raise Diffend::Errors::HandledException
end
build_error_message(response) click to toggle source

@param response [Hash] response from diffend API

@return [String]

# File lib/diffend/execute.rb, line 83
      def build_error_message(response)
        <<~MSG
          \nDiffend returned an error for your request.\n
          #{response['error']}\n
        MSG
      end
build_message(config, response) click to toggle source

@param config [Diffend::Config] @param response [Hash] response from diffend API

# File lib/diffend/execute.rb, line 31
def build_message(config, response)
  if response.key?('error')
    build_error(config, response)
  elsif response.key?('action')
    build_verdict(config, response)
  else
    Diffend::HandleErrors::Report.call(
      config: config,
      message: :unsupported_response,
      payload: response,
      report: true
    )
  end
end
build_message_header(type, command) click to toggle source

@param type [String] verdict type @param command [String] command executed via bundler

@return [String]

# File lib/diffend/execute.rb, line 130
def build_message_header(type, command)
  "\nDiffend reported #{type} verdict for #{command} command for this project."
end
build_message_info(response) click to toggle source

@param response [Hash] response from diffend API

@return [String]

# File lib/diffend/execute.rb, line 137
def build_message_info(response)
  "\nQuality score: #{response['quality_score']}, allows: #{response['allows_count']}, warnings: #{response['warns_count']}, denies: #{response['denies_count']}."
end
build_verdict(config, response) click to toggle source

@param config [Diffend::Config] @param response [Hash] response from diffend API

# File lib/diffend/execute.rb, line 57
def build_verdict(config, response)
  case response['action']
  when 'allow'
    build_allow_message(config.command, response)
      .tap(&config.logger.method(:info))
  when 'warn'
    build_warn_message(config.command, response)
      .tap(&config.logger.method(:warn))
  when 'deny'
    build_deny_message(config.command, response)
      .tap(&config.logger.method(:error))

    exit 1 unless ENV.key?('DIFFEND_SKIP_DENY')
  else
    Diffend::HandleErrors::Report.call(
      config: config,
      message: :unsupported_verdict,
      payload: response,
      report: true
    )
  end
end
build_warn_message(command, response) click to toggle source

@param command [String] command executed via bundler @param response [Hash] response from diffend API

@return [String]

# File lib/diffend/execute.rb, line 106
      def build_warn_message(command, response)
        <<~MSG
          #{build_message_header('a warn', command)}
          #{build_message_info(response)} Please go to the url below and review the issues.\n
          #{response['review_url']}\n
        MSG
      end
call(config) click to toggle source

Build verdict

@param config [Diffend::Config]

# File lib/diffend/execute.rb, line 10
def call(config)
  Diffend::RequestVerdict
    .call(config, build_definition(config.command))
    .tap { |response| build_message(config, response) }
rescue Diffend::Errors::DependenciesResolveException
  # We are unable to resolve dependencies, no message will be printed
end