Class: PactBroker::Matrix::SelectorResolver

Inherits:
Object
  • Object
show all
Extended by:
Repositories
Defined in:
lib/pact_broker/matrix/selector_resolver.rb

Constant Summary

Constants included from Repositories

Repositories::REPOSITORY_FACTORIES

Class Method Summary collapse

Methods included from Repositories

branch_repository, branch_version_repository, get_repository, integration_repository, label_repository, matrix_repository, pact_repository, pacticipant_repository, register_default_repositories, register_repository, tag_repository, verification_repository, version_repository, webhook_repository

Class Method Details

.build_selectors(pacticipants_hash, unresolved_selector, selector_type, selector_ignorer) ⇒ Object



85
86
87
88
89
90
91
92
93
# 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

.build_unresolved_selectors_for_inferred_pacticipants(inferred_pacticipant_names, options) ⇒ Object

Build an unresolved selector for the integrations that we have inferred for the target environment/branch/tag

Parameters:

  • inferred_pacticipant_names (Array<String>)

    the names of the pacticipants that we have determined to be integrated with the versions for the specified selectors



114
115
116
117
118
119
120
121
122
123
124
# 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) ⇒ Hash<String, PactBroker::Domain::Pacticipant>

Return a Hash of the pacticipant names used in the selectors, where the key is the name and the value is the pacticipant

Returns:



78
79
80
81
# 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) ⇒ Object

Find the pacticipant versions for the unresolved selector.

Parameters:



97
98
99
100
101
102
103
104
105
106
107
108
# 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_inferred_selectors(resolved_specified_selectors, resolved_ignore_selectors, integrations, options) ⇒ Array<PactBroker::Matrix::ResolvedSelector>

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.

Parameters:

Returns:



47
48
49
50
51
52
53
# 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) ⇒ Array<PactBroker::Matrix::ResolvedSelector>

Resolve the selectors that were specified in the can-i-deploy command eg. ‘–pacticipant Foo –version 43434` There may be one or multiple.

Parameters:

Returns:



34
35
36
# 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

.resolve_versions_and_add_ids(unresolved_selectors, selector_type, selector_ignorer) ⇒ Array<PactBroker::Matrix::ResolvedSelector>

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.

Parameters:

Returns:



67
68
69
70
71
72
# 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

.resolved_ignore_selectors(unresolved_ignore_selectors) ⇒ Array<PactBroker::Matrix::ResolvedSelector>

Resolve any ignore selectors used in the can-i-deploy command e.g ‘–ignore SomeProviderThatIsNotReadyYet`

Parameters:

Returns:



23
24
25
26
27
# 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