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

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

Methods included from Services

badge_service, certificate_service, get, group_service, index_service, integration_service, label_service, matrix_service, metrics_service, pact_service, pacticipant_service, register_default_services, register_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



91
92
93
94
95
96
97
98
99
# File 'lib/pact_broker/index/service.rb', line 91

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



136
137
138
# File 'lib/pact_broker/index/service.rb', line 136

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

.find_all_index_itemsObject



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



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pact_broker/index/service.rb', line 33

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)
    .eager(integration: [{latest_verification: :provider_version}, :latest_triggered_webhooks])
    .eager(consumer_version: [:latest_version_for_branch, { tags: :head_tag }])
    .eager(latest_verification: { provider_version: [:latest_version_for_branch, { 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])
    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]).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, **ignored) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pact_broker/index/service.rb', line 101

def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, **ignored)
  latest_pp_ids = latest_pact_publication_ids
  pact_publications = head_pact_publications(consumer_name: consumer_name, provider_name: provider_name, tags: true)
    .eager(:consumer)
    .eager(:provider)
    .eager(:pact_version)
    .eager(consumer_version: [:latest_version_for_branch, { tags: :head_tag }])
    .eager(latest_verification: { provider_version: [:latest_version_for_branch, { 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.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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/pact_broker/index/service.rb', line 140

def self.head_pact_publications(options = {})
  base = PactBroker::Pacts::PactPublication.select(Sequel[:pact_publications][:id])

  if options[:consumer_name]
    consumer = pacticipant_repository.find_by_name!(options[:consumer_name])
    base = base.for_consumer(consumer)
  end

  if options[:provider_name]
    provider = pacticipant_repository.find_by_name!(options[:provider_name])
    base = base.for_provider(provider)
  end

  latest = base.overall_latest
  ids_query = if options[:tags].is_a?(Array)
    latest.union(base.latest_for_consumer_tag(options[:tags]))
  elsif options[:tags]
    latest.union(base.latest_by_consumer_tag)
  else
    latest
  end

  query = PactBroker::Pacts::PactPublication.select_all_qualified.where(Sequel[:pact_publications][:id] => ids_query)
    .join_consumers(:consumers)
    .join_providers(:providers)
    .join(:versions, { Sequel[:pact_publications][:consumer_version_id] => Sequel[:cv][:id] }, { table_alias: :cv } )

  order_columns = [
    Sequel.asc(Sequel.function(:lower, Sequel[:consumers][:name])),
    Sequel.desc(Sequel[:cv][:order]),
    Sequel.asc(Sequel.function(:lower, Sequel[:providers][:name]))
  ]

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

.latest_pact_publication_idsObject



132
133
134
# File 'lib/pact_broker/index/service.rb', line 132

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

.latest_pact_publicationsObject



128
129
130
# File 'lib/pact_broker/index/service.rb', line 128

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) ⇒ Object

Worst. Code. Ever.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pact_broker/index/service.rb', line 77

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

eager loading the tag stuff doesn’t seem to make it quicker



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/pact_broker/index/service.rb', line 178

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



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

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