class PactBroker::Client::CanIDeploy

Attributes

matrix_options[R]
options[R]
pact_broker_client_options[R]
version_selectors[R]

Public Class Methods

call(version_selectors, matrix_options, options, pact_broker_client_options={}) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 22
def self.call(version_selectors, matrix_options, options, pact_broker_client_options={})
  new(version_selectors, matrix_options, options, pact_broker_client_options).call
end
new(version_selectors, matrix_options, options, pact_broker_client_options) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 26
def initialize(version_selectors, matrix_options, options, pact_broker_client_options)
  @version_selectors = version_selectors
  @matrix_options = matrix_options
  @options = options
  @pact_broker_client_options = pact_broker_client_options
end

Public Instance Methods

call() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 33
def call
  create_result(fetch_matrix_with_retries)
rescue StandardError => e
  Result.new(dry_run_or_false, for_dry_run(Term::ANSIColor.red("Error retrieving matrix. #{e.class} - #{e.message}") + "\n#{e.backtrace.join("\n")}"))
end

Private Instance Methods

check_if_retry_while_unknown_supported(matrix) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 167
def check_if_retry_while_unknown_supported(matrix)
  if !matrix.supports_unknown_count?
    raise PactBroker::Client::Error.new("This version of the Pact Broker does not provide a count of the unknown verification results. Please upgrade your Broker to >= v2.23.4")
  end
end
computer_says(success) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 69
def computer_says(success)
  if success
    if dry_run?
      "Computer says yes \\o/ (and maybe you don't need to enable dry run)"
    else
      Term::ANSIColor.green('Computer says yes \o/ ')
    end
  else
    if dry_run?
      "Computer says no ¯\\_(ツ)_/¯ (but you're ignoring this by enabling dry run)"
    else
      Term::ANSIColor.red("Computer says no ¯\\_(ツ)_/¯")
    end
  end
end
create_result(matrix) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 43
def create_result(matrix)
  if matrix.deployable?
    Result.new(true, success_message(matrix))
  else
    Result.new(dry_run_or_false, failure_message(matrix))
  end
end
dry_run?() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 129
def dry_run?
  options[:dry_run]
end
dry_run_or_false() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 133
def dry_run_or_false
  dry_run? || false
end
failure_message(matrix) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 60
def failure_message(matrix)
  message = format_matrix(matrix)
  if format != 'json'
    message = warning(matrix) + computer_says(false) + message + "\n\n" + notice_or_reason(matrix, :red)
    message = for_dry_run(message)
  end
  message
end
fetch_matrix() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 106
def fetch_matrix
  Retry.while_error { PactBroker::Client::Matrix::Query.call({ selectors: version_selectors, matrix_options: matrix_options }, options, pact_broker_client_options) }
end
fetch_matrix_with_retries() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 110
def fetch_matrix_with_retries
  matrix = fetch_matrix
  if retry_while_unknown? && !dry_run_or_false
    check_if_retry_while_unknown_supported(matrix)
    if matrix.any_unknown?
      results = matrix.unknown_count == 1 ? "result" : "results"
      $stderr.puts "Waiting for #{matrix.unknown_count} verification #{results} to be published (maximum of #{wait_time} seconds)"
      matrix = Retry.until_truthy_or_max_times(retry_options) do
        fetch_matrix
      end
    end
  end
  matrix
end
for_dry_run(lines) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 137
def for_dry_run(lines)
  if dry_run?
    prefix = Term::ANSIColor.yellow("[dry-run] ")
    lines.split("\n").collect { |line| prefix + Term::ANSIColor.uncolor(line) }.join("\n") + "\n" + prefix + "\n" + prefix + Term::ANSIColor.green("Dry run enabled - ignoring any failures")
  else
    lines
  end
end
format() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 102
def format
  options[:output]
end
format_matrix(matrix) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 93
def format_matrix(matrix)
  formatted_matrix = Matrix::Formatter.call(matrix, format)
  if format != 'json' && formatted_matrix.size > 0
    "\n\n" + formatted_matrix
  else
    formatted_matrix
  end
end
notice_or_reason(matrix, reason_color) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 85
def notice_or_reason(matrix, reason_color)
  if matrix.notices
    PactBroker::Client::ColorizeNotices.call(matrix.notices).join("\n")
  else
    Term::ANSIColor.send(reason_color, matrix.reason)
  end
end
retry_interval() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 155
def retry_interval
  options[:retry_interval]
end
retry_options() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 146
def retry_options
  {
    condition: lambda { |matrix| !matrix.any_unknown?  },
    times: retry_tries,
    sleep: retry_interval,
    sleep_first: true
  }
end
retry_tries() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 159
def retry_tries
  options[:retry_while_unknown]
end
retry_while_unknown?() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 125
def retry_while_unknown?
  options[:retry_while_unknown] > 0
end
success_message(matrix) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 51
def success_message(matrix)
  message = format_matrix(matrix)
  if format != 'json'
    message = warning(matrix) + computer_says(true) + message + "\n\n" + notice_or_reason(matrix, :green)
    message = for_dry_run(message)
  end
  message
end
wait_time() click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 163
def wait_time
  retry_interval * retry_tries
end
warning(matrix) click to toggle source
# File lib/pact_broker/client/can_i_deploy.rb, line 173
def warning(matrix)
  if matrix_options[:ignore_selectors] && matrix_options[:ignore_selectors].any? && !matrix.supports_ignore?
    Term::ANSIColor.yellow("WARN: This version of the Pact Broker does not support ignoring pacticipants. Please upgrade your Broker to >= 2.80.0") + "\n\n"
  else
    ""
  end
end