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

COLS =
[:id, :consumer_name, :provider_name, :consumer_version_order]
LATEST_PPS =
Sequel::Model.db[:latest_pact_publications].select(*COLS)
LATEST_TAGGED_PPS =
Sequel::Model.db[:latest_tagged_pact_publications].select(*COLS)
HEAD_PP_ORDER_COLUMNS =
[
  Sequel.asc(Sequel.function(:lower, :consumer_name)),
  Sequel.desc(:consumer_version_order),
  Sequel.asc(Sequel.function(:lower, :provider_name))
].freeze
DEFAULT_PAGE_SIZE =
30
DEFAULT_PAGE_NUMBER =
1

Class Method Summary collapse

Methods included from Repositories

label_repository, matrix_repository, pact_repository, pacticipant_repository, tag_repository, verification_repository, version_repository, webhook_repository

Methods included from Services

badge_service, certificate_service, group_service, index_service, integration_service, label_service, matrix_service, metrics_service, pact_service, pacticipant_service, tag_service, verification_service, version_service, webhook_service, webhook_trigger_service

Methods included from Logging

included, log_error

Class Method Details

.consumer_version_tags(pact_publication, tags_option) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/pact_broker/index/service.rb', line 99

def self.consumer_version_tags(pact_publication, tags_option)
  if tags_option == true
    pact_publication.head_pact_tags.collect(&:name)
  elsif tags_option.is_a?(Array)
    pact_publication.head_pact_tags.collect(&:name) & tags_option
  else
    []
  end
end

.dbObject



146
147
148
# File 'lib/pact_broker/index/service.rb', line 146

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

.find_all_index_itemsObject



35
36
37
38
39
# File 'lib/pact_broker/index/service.rb', line 35

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pact_broker/index/service.rb', line 41

def self.find_index_items options = {}
  latest_verifications_for_cv_tags = latest_verifications_for_consumer_version_tags(options)
  latest_pact_publication_ids = latest_pact_publications.select(:id).all.collect{ |h| h[:id] }

  # 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_ids = head_pact_publication_ids(options)
  pagination_record_count = pact_publication_ids.pagination_record_count

  pact_publications = pact_publication_scope
    .where(id: pact_publication_ids)
    .select_all_qualified
    .eager(:consumer)
    .eager(:provider)
    .eager(:pact_version)
    .eager(integration: [{latest_verification: :provider_version}, :latest_triggered_webhooks])
    .eager(:consumer_version)
    .eager(latest_verification: { provider_version: :tags_with_latest_flag })
    .eager(:head_pact_tags)

  index_items = pact_publications.all.collect do | pact_publication |
    is_overall_latest_for_integration = latest_pact_publication_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])
    webhook = webhooks.find{ |webhook| webhook.is_for?(pact_publication.integration) }

    PactBroker::Domain::IndexItem.create(
      pact_publication.consumer,
      pact_publication.provider,
      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]),
      options[:tags] && latest_verification ? latest_verification.provider_version.tags_with_latest_flag.select(&:latest?) : []
    )
  end.sort

  Page.new(index_items, pagination_record_count)
end

.find_index_items_for_api(consumer_name: nil, provider_name: nil, **ignored) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pact_broker/index/service.rb', line 109

def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, **ignored)
  latest_pact_publication_ids = latest_pact_publications.select(:id).all.collect{ |h| h[:id] }
  pact_publication_ids = head_pact_publication_ids(consumer_name: consumer_name, provider_name: provider_name, tags: true)

  pact_publications = pact_publication_scope
    .where(id: pact_publication_ids)
    .select_all_qualified
    .eager(:consumer)
    .eager(:provider)
    .eager(:pact_version)
    .eager(:consumer_version)
    .eager(latest_verification: { provider_version: :tags_with_latest_flag })
    .eager(:head_pact_tags)


  pact_publications.all.collect do | pact_publication |

    is_overall_latest_for_integration = latest_pact_publication_ids.include?(pact_publication.id)

    PactBroker::Domain::IndexItem.create(
      pact_publication.consumer,
      pact_publication.provider,
      pact_publication.to_domain_lightweight,
      is_overall_latest_for_integration,
      pact_publication.latest_verification,
      [],
      [],
      pact_publication.head_pact_tags.collect(&:name),
      pact_publication.latest_verification ? pact_publication.latest_verification.provider_version.tags_with_latest_flag.select(&:latest?) : []
    )
  end.sort
end

.head_pact_publication_ids(options = {}) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/pact_broker/index/service.rb', line 150

def self.head_pact_publication_ids(options = {})
  query = if options[:tags].is_a?(Array)
    LATEST_PPS.union(LATEST_TAGGED_PPS.where(tag_name: options[:tags]))
  elsif options[:tags]
    LATEST_PPS.union(LATEST_TAGGED_PPS)
  else
    LATEST_PPS
  end

  if options[:consumer_name]
    query = query.where(PactBroker::Repositories::Helpers.name_like(:consumer_name, options[:consumer_name]))
  end

  if options[:provider_name]
    query = query.where(PactBroker::Repositories::Helpers.name_like(:provider_name, options[:provider_name]))
  end

  query.order(*HEAD_PP_ORDER_COLUMNS)
    .paginate(options[:page_number] || DEFAULT_PAGE_NUMBER, options[:page_size] || DEFAULT_PAGE_SIZE)
    .select(:id)
end

.latest_pact_publicationsObject



142
143
144
# File 'lib/pact_broker/index/service.rb', line 142

def self.latest_pact_publications
  db[:latest_pact_publications]
end

.latest_verification_for_pseudo_branch(pact_publication, is_overall_latest, latest_verifications_for_cv_tags, tags_option) ⇒ Object

Worst. Code. Ever.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pact_broker/index/service.rb', line 85

def self.latest_verification_for_pseudo_branch(pact_publication, is_overall_latest, latest_verifications_for_cv_tags, tags_option)
  if 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)
  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)
  else
    pact_publication.integration.latest_verification
  end
end

.latest_verifications_for_consumer_version_tags(options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/pact_broker/index/service.rb', line 172

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)
      .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



31
32
33
# File 'lib/pact_broker/index/service.rb', line 31

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