Class: PactBroker::Index::Service
- Inherits:
-
Object
- Object
- PactBroker::Index::Service
- Extended by:
- Logging, Repositories, Services
- Defined in:
- lib/pact_broker/index/service.rb
Constant Summary collapse
- DEFAULT_PAGE_SIZE =
30
- DEFAULT_PAGE_NUMBER =
1
Constants included from Repositories
Repositories::REPOSITORY_FACTORIES
Constants included from Services
Class Method Summary collapse
-
.consumer_version_tags(pact_publication, tags_option) ⇒ Object
rubocop: enable Metrics/CyclomaticComplexity.
- .db ⇒ Object
- .find_all_index_items ⇒ Object
-
.find_index_items(options = {}) ⇒ Object
rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength.
- .find_index_items_for_api(consumer_name: nil, provider_name: nil, page_number: nil, page_size: nil, **_ignored) ⇒ Object
- .head_pact_publications(options = {}) ⇒ Object
- .latest_pact_publication_ids ⇒ Object
- .latest_pact_publications ⇒ Object
-
.latest_verification_for_pseudo_branch(pact_publication, is_overall_latest, latest_verifications_for_cv_tags, tags_option, options) ⇒ Object
Worst.
-
.latest_verifications_for_consumer_version_tags(options) ⇒ Object
eager loading the tag stuff doesn’t seem to make it quicker.
-
.pact_publication_scope ⇒ Object
This method provides data for both the OSS server side rendered index (with and without tags) and the PactFlow UI.
- .pact_publications_by_view(query, options) ⇒ Object
- .smart_default_view(consumer_name, provider_name) ⇒ Object
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
Methods included from Services
badge_service, branch_service, certificate_service, contract_service, deployed_version_service, environment_service, get_service, group_service, index_service, integration_service, label_service, matrix_service, metrics_service, pact_service, pacticipant_service, register_default_services, register_service, released_version_service, tag_service, verification_service, version_service, webhook_service, webhook_trigger_service
Methods included from Logging
included, log_error, log_with_tag, measure_info
Class Method Details
.consumer_version_tags(pact_publication, tags_option) ⇒ Object
rubocop: enable Metrics/CyclomaticComplexity
126 127 128 129 130 131 132 133 134 |
# File 'lib/pact_broker/index/service.rb', line 126 def self.(pact_publication, ) if == true pact_publication. elsif .is_a?(Array) pact_publication..select{ |tag| .include?(tag.name)} else [] end end |
.db ⇒ Object
171 172 173 |
# File 'lib/pact_broker/index/service.rb', line 171 def self.db PactBroker::Pacts::PactPublication.db end |
.find_all_index_items ⇒ Object
27 28 29 30 31 |
# File 'lib/pact_broker/index/service.rb', line 27 def self.find_all_index_items # Is there a better way to do this? Setting a page_size of nil or -1 doesn't work # If we get to 100000000000 index items, we're probably going to have bigger issues... find_index_items(page_number: 1, page_size: 100000000000) end |
.find_index_items(options = {}) ⇒ Object
rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/pact_broker/index/service.rb', line 61 def self.find_index_items = {} = () latest_pp_ids = latest_pact_publication_ids # We only need to know if a webhook exists for an integration, not what its properties are webhooks = PactBroker::Webhooks::Webhook.select(:consumer_id, :provider_id).distinct.all pact_publication_query = head_pact_publications() pagination_record_count = pact_publication_query.pagination_record_count pact_publications = pact_publication_query .eager(:consumer) .eager(:provider) .eager(pact_version: { latest_verification: { provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :branch_heads, { tags: :head_tag } ] } }) .eager(integration: [{latest_verification: :provider_version}, :latest_triggered_webhooks]) .eager(consumer_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :branch_versions, :branch_heads, { tags: :head_tag }]) .eager(:head_pact_publications_for_tags) index_items = pact_publications.all.collect do | pact_publication | is_overall_latest_for_integration = latest_pp_ids.include?(pact_publication.id) latest_verification = latest_verification_for_pseudo_branch(pact_publication, is_overall_latest_for_integration, , [:tags], ) webhook = webhooks.find{ |wh| wh.is_for?(pact_publication.integration) } PactBroker::Domain::IndexItem.create( pact_publication.consumer, pact_publication.provider, pact_publication.consumer_version, pact_publication.to_domain_lightweight, is_overall_latest_for_integration, latest_verification, webhook ? [webhook]: [], pact_publication.integration.latest_triggered_webhooks, (pact_publication, [:tags]).sort_by(&:created_at).collect(&:name), [:tags] && latest_verification ? latest_verification.provider_version..select(&:latest_for_pacticipant?).sort_by(&:created_at) : [], pact_publication.latest_for_branch? ) end.sort Page.new(index_items, pagination_record_count) end |
.find_index_items_for_api(consumer_name: nil, provider_name: nil, page_number: nil, page_size: nil, **_ignored) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/pact_broker/index/service.rb', line 136 def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, page_number: nil, page_size: nil, **_ignored) latest_pp_ids = latest_pact_publication_ids pact_publications = head_pact_publications(consumer_name: consumer_name, provider_name: provider_name, tags: true, page_number: page_number, page_size: page_size) .eager(:consumer) .eager(:provider) .eager(pact_version: { latest_verification: { provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, { branch_heads: :branch_version }, { tags: :head_tag }]} }) .eager(consumer_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, { branch_heads: :branch_version }, { tags: :head_tag }]) .eager(:head_pact_publications_for_tags) pact_publications.all.collect do | pact_publication | is_overall_latest_for_integration = latest_pp_ids.include?(pact_publication.id) PactBroker::Domain::IndexItem.create( pact_publication.consumer, pact_publication.provider, pact_publication.consumer_version, pact_publication.to_domain_lightweight, is_overall_latest_for_integration, pact_publication.latest_verification, [], [], pact_publication..sort_by(&:created_at).collect(&:name), pact_publication.latest_verification ? pact_publication.latest_verification.provider_version..select(&:latest_for_pacticipant?).sort_by(&:created_at) : [] ) end.sort end |
.head_pact_publications(options = {}) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/pact_broker/index/service.rb', line 175 def self.head_pact_publications( = {}) base = base_query() if [:search] pacticipant_ids = pact_pacticipant_ids_by_name([:search]) base = base.where(Sequel.|( { Sequel[:pact_publications][:consumer_id] => pacticipant_ids }, { Sequel[:pact_publications][:provider_id] => pacticipant_ids } )) # Return early if there is no pacticipant matches the input name return base.paginate([:page_number] || DEFAULT_PAGE_NUMBER, [:page_size] || DEFAULT_PAGE_SIZE) if pacticipant_ids.empty? end ids_query = if [:view] pact_publications_by_view(base, ) else (base, [:tags]) end select_columns_and_order(ids_query, ) end |
.latest_pact_publication_ids ⇒ Object
167 168 169 |
# File 'lib/pact_broker/index/service.rb', line 167 def self.latest_pact_publication_ids PactBroker::Pacts::PactPublication.select(Sequel[:pact_publications][:id]).overall_latest.collect(&:id) end |
.latest_pact_publications ⇒ Object
163 164 165 |
# File 'lib/pact_broker/index/service.rb', line 163 def self.latest_pact_publications PactBroker::Pacts::PactPublication.overall_latest end |
.latest_verification_for_pseudo_branch(pact_publication, is_overall_latest, latest_verifications_for_cv_tags, tags_option, options) ⇒ Object
Worst. Code. Ever. This needs a big rethink. TODO environments view rubocop: disable Metrics/CyclomaticComplexity
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/pact_broker/index/service.rb', line 109 def self.latest_verification_for_pseudo_branch(pact_publication, is_overall_latest, , , ) if [:view] == "branch" || ([:view] == "all" && pact_publication.consumer_version.branch_heads.any?) pact_publication.latest_verification || pact_publication.latest_verification_for_consumer_branches(forbid_lazy_load: false) elsif == true .select{ | v | v.consumer_id == pact_publication.consumer_id && v.provider_id == pact_publication.provider_id && pact_publication..collect(&:name).include?(v.consumer_version_tag_name) } .sort{ |v1, v2| v1.id <=> v2.id }.last || (is_overall_latest ? pact_publication.integration.latest_verification : nil) elsif .is_a?(Array) .select{ | v | v.consumer_id == pact_publication.consumer_id && v.provider_id == pact_publication.provider_id && pact_publication..collect(&:name).include?(v.consumer_version_tag_name) && .include?(v.consumer_version_tag_name) } .sort{ |v1, v2| v1.id <=> v2.id }.last || (is_overall_latest ? pact_publication.integration.latest_verification : nil) else pact_publication.integration.latest_verification end end |
.latest_verifications_for_consumer_version_tags(options) ⇒ Object
eager loading the tag stuff doesn’t seem to make it quicker
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/pact_broker/index/service.rb', line 213 def self.() # server side rendered index page with tags[]=a&tags=[]b if [:tags].is_a?(Array) PactBroker::Verifications::LatestVerificationForConsumerVersionTag .eager(:provider_version) .where(consumer_version_tag_name: [:tags]) .all elsif [:tags] # server side rendered index page with tags=true PactBroker::Verifications::LatestVerificationForConsumerVersionTag .eager(provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }]) .all else nil # should not be used end end |
.pact_publication_scope ⇒ Object
This method provides data for both the OSS server side rendered index (with and without tags) and the PactFlow UI. It really needs to be broken into to separate methods, as it’s getting too messy supporting both
23 24 25 |
# File 'lib/pact_broker/index/service.rb', line 23 def self.pact_publication_scope PactBroker.policy_scope!(PactBroker::Pacts::PactPublication) end |
.pact_publications_by_view(query, options) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/pact_broker/index/service.rb', line 198 def self.pact_publications_by_view(query, ) case [:view] when "branch" then query.latest_by_consumer_branch when "tag" then query.latest_by_consumer_tag when "environment" then query.in_environments else query .overall_latest .union(query.latest_by_consumer_branch) .union(query.latest_by_consumer_tag) .union(query.in_environments) end end |
.smart_default_view(consumer_name, provider_name) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pact_broker/index/service.rb', line 33 def self.smart_default_view(consumer_name, provider_name) consumer = pacticipant_repository.find_by_name(consumer_name) provider = pacticipant_repository.find_by_name(provider_name) if consumer && provider pact_publications_with_branches = PactBroker::Pacts::PactPublication .for_consumer_name(consumer_name) .for_provider_name(provider_name) .join_consumer_branch_versions = PactBroker::Pacts::PactPublication .for_consumer_name(consumer_name) .for_provider_name(provider_name) . if pact_publications_with_branches.any? "branch" elsif .any? "tag" else "all" end else nil end end |