Class: PactBroker::Index::Service

Inherits:
Object
  • Object
show all
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 Services

Services::FACTORIES

Class Method Summary collapse

Methods included from Repositories

branch_version_repository, integration_repository, label_repository, matrix_repository, pact_repository, pacticipant_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, 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

Class Method Details

.consumer_version_tags(pact_publication, tags_option) ⇒ Object

rubocop: enable Metrics/CyclomaticComplexity



125
126
127
128
129
130
131
132
133
# File 'lib/pact_broker/index/service.rb', line 125

def self.consumer_version_tags(pact_publication, tags_option)
  if tags_option == true
    pact_publication.head_pact_tags
  elsif tags_option.is_a?(Array)
    pact_publication.head_pact_tags.select{ |tag| tags_option.include?(tag.name)}
  else
    []
  end
end

.dbObject



170
171
172
# File 'lib/pact_broker/index/service.rb', line 170

def self.db
  PactBroker::Pacts::PactPublication.db
end

.find_all_index_itemsObject



26
27
28
29
30
# File 'lib/pact_broker/index/service.rb', line 26

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



60
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
# File 'lib/pact_broker/index/service.rb', line 60

def self.find_index_items options = {}
  latest_verifications_for_cv_tags = latest_verifications_for_consumer_version_tags(options)
  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(options)
  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_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, latest_verifications_for_cv_tags, options[:tags], options)
    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,
      consumer_version_tags(pact_publication, options[:tags]).sort_by(&:created_at).collect(&:name),
      options[:tags] && latest_verification ? latest_verification.provider_version.tags.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



135
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
# File 'lib/pact_broker/index/service.rb', line 135

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.head_pact_tags.sort_by(&:created_at).collect(&:name),
      pact_publication.latest_verification ? pact_publication.latest_verification.provider_version.tags.select(&:latest_for_pacticipant?).sort_by(&:created_at) : []
    )
  end.sort
end

.head_pact_publications(options = {}) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/pact_broker/index/service.rb', line 174

def self.head_pact_publications(options = {})
  base = base_query(options)

  if options[:search]
    pacticipant_ids = pact_pacticipant_ids_by_name(options[: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(options[:page_number] || DEFAULT_PAGE_NUMBER, options[:page_size] || DEFAULT_PAGE_SIZE) if pacticipant_ids.blank?
  end

  ids_query = if options[:view]
                pact_publications_by_view(base, options)
              else
                query_pact_publication_ids_by_tags(base, options[:tags])
              end

  select_columns_and_order(ids_query, options)
end

.latest_pact_publication_idsObject



166
167
168
# File 'lib/pact_broker/index/service.rb', line 166

def self.latest_pact_publication_ids
  PactBroker::Pacts::PactPublication.select(Sequel[:pact_publications][:id]).overall_latest.collect(&:id)
end

.latest_pact_publicationsObject



162
163
164
# File 'lib/pact_broker/index/service.rb', line 162

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pact_broker/index/service.rb', line 108

def self.latest_verification_for_pseudo_branch(pact_publication, is_overall_latest, latest_verifications_for_cv_tags, tags_option, options)
  if options[:view] == "branch" || (options[:view] == "all" && pact_publication.consumer_version.branch_heads.any?)
    pact_publication.latest_verification || pact_publication.latest_verification_for_consumer_branches
  elsif tags_option == true
    latest_verifications_for_cv_tags
      .select{ | v | v.consumer_id == pact_publication.consumer_id && v.provider_id == pact_publication.provider_id && pact_publication.head_pact_tags.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 tags_option.is_a?(Array)
    latest_verifications_for_cv_tags
    .select{ | v | v.consumer_id == pact_publication.consumer_id && v.provider_id == pact_publication.provider_id && pact_publication.head_pact_tags.collect(&:name).include?(v.consumer_version_tag_name) && tags_option.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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/pact_broker/index/service.rb', line 212

def self.latest_verifications_for_consumer_version_tags(options)
  # server side rendered index page with tags[]=a&tags=[]b
  if options[:tags].is_a?(Array)
    PactBroker::Verifications::LatestVerificationForConsumerVersionTag
      .eager(:provider_version)
      .where(consumer_version_tag_name: options[:tags])
      .all
  elsif options[: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_scopeObject

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



22
23
24
# File 'lib/pact_broker/index/service.rb', line 22

def self.pact_publication_scope
  PactBroker.policy_scope!(PactBroker::Pacts::PactPublication)
end

.pact_publications_by_view(query, options) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/pact_broker/index/service.rb', line 197

def self.pact_publications_by_view(query, options)
  case options[: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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pact_broker/index/service.rb', line 32

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

    pact_publication_with_tags =   PactBroker::Pacts::PactPublication
      .for_consumer_name(consumer_name)
      .for_provider_name(provider_name)
      .join_consumer_version_tags

    if pact_publications_with_branches.any?
      "branch"
    elsif pact_publication_with_tags.any?
      "tag"
    else
      "all"
    end
  else
    nil
  end
end