Class: PactBroker::Matrix::SelectorResolver
- Inherits:
-
Object
- Object
- PactBroker::Matrix::SelectorResolver
- 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
- .build_selectors(pacticipants_hash, unresolved_selector, selector_type, selector_ignorer) ⇒ Object
-
.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.
-
.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 as specified in the selector (not necessarily the pacticipant's stored name, when use_case_sensitive_resource_names is false), and the value is the pacticipant.
-
.find_versions_for_selector(unresolved_selector) ⇒ Object
Find the pacticipant versions for the unresolved selector.
- .pacticipant_name_matches?(stored_name, requested_name) ⇒ Boolean
- .pacticipants_matching_names(names) ⇒ Array<PactBroker::Domain::Pacticipant>
-
.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
--tooptions (eg.--to-environment ENVor--to TAG) we need to create the inferred selectors for the pacticipant versions in that environment/with that tag/branch. -
.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.
-
.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.
-
.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.
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
113 114 115 116 117 118 119 120 121 |
# File 'lib/pact_broker/matrix/selector_resolver.rb', line 113 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
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/pact_broker/matrix/selector_resolver.rb', line 142 def build_unresolved_selectors_for_inferred_pacticipants(inferred_pacticipant_names, ) inferred_pacticipant_names.collect do | pacticipant_name | selector = UnresolvedSelector.new(pacticipant_name: pacticipant_name) selector.tag = [:tag] if [:tag] selector.branch = [:branch] if [:branch] selector.main_branch = [:main_branch] if [:main_branch] selector.latest = [:latest] if [:latest] selector.environment_name = [:environment_name] if [: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 as specified in the selector (not necessarily the pacticipant's stored name, when use_case_sensitive_resource_names is false), and the value is the pacticipant.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/pact_broker/matrix/selector_resolver.rb', line 80 def find_pacticipants_for_selectors(unresolved_selectors) names = unresolved_selectors.collect(&:pacticipant_name) pacticipants = pacticipants_matching_names(names) names.uniq.each_with_object({}) do | name, hash | matching = pacticipants.select { | candidate | pacticipant_name_matches?(candidate.name, name) } pacticipant_repository.handle_multiple_pacticipants_found(name, matching) if matching.size > 1 hash[name] = matching.first if matching.any? end end |
.find_versions_for_selector(unresolved_selector) ⇒ Object
Find the pacticipant versions for the unresolved selector.
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/pact_broker/matrix/selector_resolver.rb', line 125 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 |
.pacticipant_name_matches?(stored_name, requested_name) ⇒ Boolean
103 104 105 106 107 108 109 |
# File 'lib/pact_broker/matrix/selector_resolver.rb', line 103 def pacticipant_name_matches?(stored_name, requested_name) if PactBroker.configuration.use_case_sensitive_resource_names stored_name == requested_name else stored_name.casecmp?(requested_name) end end |
.pacticipants_matching_names(names) ⇒ Array<PactBroker::Domain::Pacticipant>
95 96 97 98 99 |
# File 'lib/pact_broker/matrix/selector_resolver.rb', line 95 def pacticipants_matching_names(names) return [] if names.empty? PactBroker::Domain::Pacticipant.where(Sequel.|(*names.collect { | name | Sequel.name_like(:name, name) })).all 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.
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, ) 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, ) 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.
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.
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
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 |