class PactBroker::Matrix::SelectorResolver

Public Class Methods

build_selectors(pacticipants_hash, unresolved_selector, selector_type, selector_ignorer) click to toggle source
# File lib/pact_broker/matrix/selector_resolver.rb, line 85
def build_selectors(pacticipants_hash, unresolved_selector, selector_type, selector_ignorer)
  selector_builder = ResolvedSelectorBuilder.new(unresolved_selector, selector_type, selector_ignorer)
  selector_builder.pacticipant = pacticipants_hash[unresolved_selector.pacticipant_name]
  if selector_builder.pacticipant
    versions = find_versions_for_selector(unresolved_selector)
    selector_builder.versions = versions
  end
  selector_builder.build
end
resolve_inferred_selectors(resolved_specified_selectors, resolved_ignore_selectors, integrations, options) click to toggle source

When the can-i-deploy command uses any of the ‘–to` options (eg. `–to-environment ENV` or `–to TAG`) we need to create the inferred selectors for the pacticipant versions in that environment/with that tag/branch. eg. if A -> B, and the CLI command is `can-i-deploy –pacticipant A –version 3434 –to-environment prod`, then we need to make the inferred selector for pacticipant B with the version that is in prod. @param [Array<PactBroker::Matrix::ResolvedSelector>] resolved_specified_selectors @param [Array<PactBroker::Matrix::ResolvedSelector>] resolved_ignore_selectors @param [Array<PactBroker::Matrix::Integration>] integrations @param [Hash] options @return [Array<PactBroker::Matrix::ResolvedSelector>]

# File lib/pact_broker/matrix/selector_resolver.rb, line 47
def resolve_inferred_selectors(resolved_specified_selectors, resolved_ignore_selectors, integrations, options)
  all_pacticipant_names = integrations.collect(&:pacticipant_names).flatten.uniq
  specified_names = resolved_specified_selectors.collect{ |s| s[:pacticipant_name] }
  inferred_pacticipant_names = all_pacticipant_names - specified_names
  unresolved_selectors = build_unresolved_selectors_for_inferred_pacticipants(inferred_pacticipant_names, options)
  resolve_versions_and_add_ids(unresolved_selectors, :inferred, SelectorIgnorer.new(resolved_ignore_selectors))
end
resolve_specified_selectors(unresolved_specified_selectors, resolved_ignore_selectors) click to toggle source

Resolve the selectors that were specified in the can-i-deploy command eg. ‘–pacticipant Foo –version 43434` There may be one or multiple. @param [Array<PactBroker::Matrix::UnresolvedSelector>] unresolved_specified_selectors @param [Array<PactBroker::Matrix::ResolvedSelector>] resolved_ignore_selectors previously resolved selectors for the versions to ignore @return [Array<PactBroker::Matrix::ResolvedSelector>]

# File lib/pact_broker/matrix/selector_resolver.rb, line 34
def resolve_specified_selectors(unresolved_specified_selectors, resolved_ignore_selectors)
  resolve_versions_and_add_ids(unresolved_specified_selectors, :specified, SelectorIgnorer.new(resolved_ignore_selectors))
end
resolved_ignore_selectors(unresolved_ignore_selectors) click to toggle source

Resolve any ignore selectors used in the can-i-deploy command e.g ‘–ignore SomeProviderThatIsNotReadyYet` @param [Array<PactBroker::Matrix::UnresolvedSelector>] unresolved_ignore_selectors @return [Array<PactBroker::Matrix::ResolvedSelector>]

# File lib/pact_broker/matrix/selector_resolver.rb, line 23
def resolved_ignore_selectors(unresolved_ignore_selectors)
  # When resolving the ignore_selectors, use the NilSelectorIgnorer because it doesn't make sense to ignore
  # the ignore selectors.
  resolve_versions_and_add_ids(unresolved_ignore_selectors, :ignored, NilSelectorIgnorer.new)
end

Private Class Methods

build_unresolved_selectors_for_inferred_pacticipants(inferred_pacticipant_names, options) click to toggle source

Build an unresolved selector for the integrations that we have inferred for the target environment/branch/tag @param [Array<String>] inferred_pacticipant_names the names of the pacticipants that we have determined to be integrated with the versions for the specified selectors

# File lib/pact_broker/matrix/selector_resolver.rb, line 114
def build_unresolved_selectors_for_inferred_pacticipants(inferred_pacticipant_names, options)
  inferred_pacticipant_names.collect do | pacticipant_name |
    selector = UnresolvedSelector.new(pacticipant_name: pacticipant_name)
    selector.tag = options[:tag] if options[:tag]
    selector.branch = options[:branch] if options[:branch]
    selector.main_branch = options[:main_branch] if options[:main_branch]
    selector.latest = options[:latest] if options[:latest]
    selector.environment_name = options[:environment_name] if options[:environment_name]
    selector
  end
end
find_pacticipants_for_selectors(unresolved_selectors) click to toggle source

Return a Hash of the pacticipant names used in the selectors, where the key is the name and the value is the pacticipant @return [Hash<String, PactBroker::Domain::Pacticipant>]

# File lib/pact_broker/matrix/selector_resolver.rb, line 78
def find_pacticipants_for_selectors(unresolved_selectors)
  names = unresolved_selectors.collect(&:pacticipant_name)
  PactBroker::Domain::Pacticipant.where(name: names).all.group_by(&:name).transform_values(&:first)
end
find_versions_for_selector(unresolved_selector) click to toggle source

Find the pacticipant versions for the unresolved selector. @param [PactBroker::Matrix::UnresolvedSelector] unresolved_selector

# File lib/pact_broker/matrix/selector_resolver.rb, line 97
def find_versions_for_selector(unresolved_selector)
  # For selectors that just set the pacticipant name, there's no need to resolve the version -
  # only the pacticipant ID will be used in the query
  return nil if unresolved_selector.all_for_pacticipant?
  versions = version_repository.find_versions_for_selector(unresolved_selector)

  if unresolved_selector.latest
    [versions.first]
  else
    versions.empty? ? [nil] : versions
  end
end
resolve_versions_and_add_ids(unresolved_selectors, selector_type, selector_ignorer) click to toggle source

Find the IDs of every pacticipant and version in the UnresolvedSelectors, and return them as ResolvedSelectors, expanding selectors for multiple versions. This gets called first for the ignore selectors, then the specified selectors, and then the inferred selectors. When it gets called for the first time for the ignore selectors, they will be passed in as the unresolved_selectors, and the resolved_ignore_selectors will be empty. The next times it is called with the specified selectors and the inferred selectors, the previously resolved ignore selectors will be passed in as resolved_ignore_selectors so we can work out which of those selectors needs to be ignored.

@param [Array<PactBroker::Matrix::UnresolvedSelector>] unresolved_selectors @param [Symbol] selector_type which may be :specified or :inferred @param [SelectorIgnorer] selector_ignorer @return [Array<PactBroker::Matrix::ResolvedSelector>]

# File lib/pact_broker/matrix/selector_resolver.rb, line 67
def resolve_versions_and_add_ids(unresolved_selectors, selector_type, selector_ignorer)
  pacticipants_hash = find_pacticipants_for_selectors(unresolved_selectors)
  unresolved_selectors.collect do | unresolved_selector |
    build_selectors(pacticipants_hash, unresolved_selector, selector_type, selector_ignorer)
  end.flatten
end