Class: PactBroker::Pacts::Repository

Inherits:
Object
  • Object
show all
Includes:
Logging, Repositories, Repositories::Helpers
Defined in:
lib/pact_broker/pacts/repository.rb

Instance Method Summary collapse

Methods included from Repositories::Helpers

#mysql?, #name_like, #order_append_ignore_case, #order_ignore_case, #pacticipant_id_for_name, #postgres?, #select_all_qualified, #select_for_subquery

Methods included from Repositories

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

Methods included from Logging

included, #log_error

Instance Method Details

#create(params) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pact_broker/pacts/repository.rb', line 39

def create params
  pact_version = find_or_create_pact_version(
    params.fetch(:consumer_id),
    params.fetch(:provider_id),
    params.fetch(:pact_version_sha),
    params.fetch(:json_content)
  )
  pact_publication = PactPublication.new(
    consumer_version_id: params[:version_id],
    provider_id: params[:provider_id],
    consumer_id: params[:consumer_id],
    pact_version: pact_version,
    revision_number: 1
  ).upsert
  update_latest_pact_publication_ids(pact_publication)
  pact_publication.to_domain
end

#delete(params) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pact_broker/pacts/repository.rb', line 99

def delete params
  id = scope_for(PactPublication)
    .join_consumers
    .join_providers
    .join_consumer_versions
    .consumer_name_like(params.consumer_name)
    .provider_name_like(params.provider_name)
    .consumer_version_number_like(params.consumer_version_number)
    .select_for_subquery(Sequel[:pact_publications][:id].as(:id))
  unscoped(PactPublication).where(id: id).delete
end

#delete_all_pact_publications_between(consumer_name, options) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/pact_broker/pacts/repository.rb', line 122

def delete_all_pact_publications_between consumer_name, options
  consumer = pacticipant_repository.find_by_name!(consumer_name)
  provider = pacticipant_repository.find_by_name!(options.fetch(:and))
  query = scope_for(PactPublication).where(consumer: consumer, provider: provider)
  query = query.tag(options[:tag]) if options[:tag]

  ids = query.select_for_subquery(:id)
  webhook_repository.delete_triggered_webhooks_by_pact_publication_ids(ids)
  unscoped(PactPublication).where(id: ids).delete
end

#delete_all_pact_versions_between(consumer_name, options) ⇒ Object



133
134
135
136
137
# File 'lib/pact_broker/pacts/repository.rb', line 133

def delete_all_pact_versions_between consumer_name, options
  consumer = pacticipant_repository.find_by_name(consumer_name)
  provider = pacticipant_repository.find_by_name(options.fetch(:and))
  scope_for(PactVersion).where(consumer: consumer, provider: provider).delete
end

#delete_by_version_id(version_id) ⇒ Object



111
112
113
# File 'lib/pact_broker/pacts/repository.rb', line 111

def delete_by_version_id version_id
  scope_for(PactPublication).where(consumer_version_id: version_id).delete
end

#find_all_pact_versions_between(consumer_name, options) ⇒ Object



115
116
117
118
119
120
# File 'lib/pact_broker/pacts/repository.rb', line 115

def find_all_pact_versions_between consumer_name, options
  find_all_database_versions_between(consumer_name, options)
    .eager(:tags)
    .reverse_order(:consumer_version_order)
    .collect(&:to_domain)
end

#find_all_pact_versions_for_provider_with_consumer_version_tags(provider_name, selector) ⇒ Object



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
# File 'lib/pact_broker/pacts/repository.rb', line 147

def find_all_pact_versions_for_provider_with_consumer_version_tags provider_name, selector
  provider = pacticipant_repository.find_by_name(provider_name)
  consumer = selector.consumer ? pacticipant_repository.find_by_name(selector.consumer) : nil

  scope_for(PactPublication)
    .select_all_qualified
    .select_append(Sequel[:cv][:order].as(:consumer_version_order))
    .select_append(Sequel[:ct][:name].as(:consumer_version_tag_name))
    .remove_overridden_revisions
    .join_consumer_versions(:cv)
    .join_consumer_version_tags_with_names(selector.tag)
    .where(provider: provider)
    .where_consumer_if_set(consumer)
    .eager(:consumer)
    .eager(:consumer_version)
    .eager(:provider)
    .eager(:pact_version)
    .all
    .group_by(&:pact_version_id)
    .values
    .collect do | pact_publications |
      latest_pact_publication = pact_publications.sort_by{ |p| p.values.fetch(:consumer_version_order) }.last
      SelectedPact.new(latest_pact_publication.to_domain, Selectors.new(selector))
    end
end

#find_all_revisions(consumer_name, consumer_version, provider_name) ⇒ Object



312
313
314
315
316
317
318
# File 'lib/pact_broker/pacts/repository.rb', line 312

def find_all_revisions consumer_name, consumer_version, provider_name
  scope_for(AllPactPublications)
    .consumer(consumer_name)
    .provider(provider_name)
    .consumer_version_number(consumer_version)
    .order(:consumer_version_order, :revision_number).collect(&:to_domain_with_content)
end

#find_by_consumer_version(consumer_name, consumer_version_number) ⇒ Object

Returns latest pact version for the consumer_version_number



250
251
252
253
254
255
# File 'lib/pact_broker/pacts/repository.rb', line 250

def find_by_consumer_version consumer_name, consumer_version_number
  scope_for(LatestPactPublicationsByConsumerVersion)
    .consumer(consumer_name)
    .consumer_version_number(consumer_version_number)
    .collect(&:to_domain_with_content)
end

#find_by_version_and_provider(version_id, provider_id) ⇒ Object



257
258
259
260
261
262
# File 'lib/pact_broker/pacts/repository.rb', line 257

def find_by_version_and_provider version_id, provider_id
  scope_for(LatestPactPublicationsByConsumerVersion)
    .eager(:tags)
    .where(consumer_version_id: version_id, provider_id: provider_id)
    .limit(1).collect(&:to_domain_with_content)[0]
end

#find_for_verification(provider_name, consumer_version_selectors) ⇒ Object

Returns a list of Domain::Pact objects the represent pact publications



365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/pact_broker/pacts/repository.rb', line 365

def find_for_verification(provider_name, consumer_version_selectors)
  selected_pacts = find_pacts_for_which_the_latest_version_is_required(provider_name, consumer_version_selectors) +
  find_pacts_for_which_the_latest_version_for_the_tag_is_required(provider_name, consumer_version_selectors) +
  find_pacts_for_which_all_versions_for_the_tag_are_required(provider_name, consumer_version_selectors)

  selected_pacts = selected_pacts + find_pacts_for_fallback_tags(selected_pacts, provider_name, consumer_version_selectors)

  selected_pacts
    .group_by{ |p| [p.consumer_name, p.pact_version_sha] }
    .values
    .collect do | selected_pacts_for_pact_version_id |
      SelectedPact.merge(selected_pacts_for_pact_version_id)
    end
end

#find_latest_pact(consumer_name, provider_name, tag = nil) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/pact_broker/pacts/repository.rb', line 268

def find_latest_pact(consumer_name, provider_name, tag = nil)
  query = scope_for(LatestPactPublicationsByConsumerVersion)
    .select_all_qualified
    .consumer(consumer_name)
    .provider(provider_name)
  if tag == :untagged
    query = query.untagged
  elsif tag
    query = query.tag(tag)
  end
  query.latest.all.collect(&:to_domain_with_content)[0]
end

#find_latest_pact_versions_for_provider(provider_name, tag = nil) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/pact_broker/pacts/repository.rb', line 139

def find_latest_pact_versions_for_provider provider_name, tag = nil
  if tag
    scope_for(LatestTaggedPactPublications).provider(provider_name).order_ignore_case(:consumer_name).where(tag_name: tag).collect(&:to_domain)
  else
    scope_for(LatestPactPublications).provider(provider_name).order_ignore_case(:consumer_name).collect(&:to_domain)
  end
end

#find_latest_pactsObject



264
265
266
# File 'lib/pact_broker/pacts/repository.rb', line 264

def find_latest_pacts
  scope_for(LatestPactPublications).order(:consumer_name, :provider_name).collect(&:to_domain)
end

#find_next_pact(pact) ⇒ Object



336
337
338
339
340
341
342
343
# File 'lib/pact_broker/pacts/repository.rb', line 336

def find_next_pact pact
  scope_for(LatestPactPublicationsByConsumerVersion)
    .eager(:tags)
    .consumer(pact.consumer.name)
    .provider(pact.provider.name)
    .consumer_version_order_after(pact.consumer_version.order)
    .earliest.collect(&:to_domain_with_content)[0]
end

#find_pact(consumer_name, consumer_version, provider_name, pact_version_sha = nil) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/pact_broker/pacts/repository.rb', line 295

def find_pact consumer_name, consumer_version, provider_name, pact_version_sha = nil
  query = if pact_version_sha
    scope_for(AllPactPublications)
      .pact_version_sha(pact_version_sha)
      .reverse_order(:consumer_version_order)
      .limit(1)
  else
    scope_for(LatestPactPublicationsByConsumerVersion)
  end
  query = query
    .eager(:tags)
    .consumer(consumer_name)
    .provider(provider_name)
  query = query.consumer_version_number(consumer_version) if consumer_version
  query.collect(&:to_domain_with_content)[0]
end

#find_pact_versions_for_provider(provider_name, tag = nil) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/pact_broker/pacts/repository.rb', line 231

def find_pact_versions_for_provider provider_name, tag = nil
  if tag
    scope_for(LatestPactPublicationsByConsumerVersion)
      .join(:tags, {version_id: :consumer_version_id})
      .provider(provider_name)
      .order_ignore_case(:consumer_name)
      .order_append(:consumer_version_order)
      .where(Sequel[:tags][:name] => tag)
      .collect(&:to_domain)
  else
    scope_for(LatestPactPublicationsByConsumerVersion)
      .provider(provider_name)
      .order_ignore_case(:consumer_name)
      .order_append(:consumer_version_order)
      .collect(&:to_domain)
  end
end

#find_previous_distinct_pact(pact) ⇒ Object



345
346
347
348
349
350
351
352
# File 'lib/pact_broker/pacts/repository.rb', line 345

def find_previous_distinct_pact pact
  previous, current = nil, pact
  loop do
    previous = find_previous_distinct_pact_by_sha current
    return previous if previous.nil? || different?(current, previous)
    current = previous
  end
end

#find_previous_pact(pact, tag = nil) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/pact_broker/pacts/repository.rb', line 320

def find_previous_pact pact, tag = nil
  query = scope_for(LatestPactPublicationsByConsumerVersion)
      .eager(:tags)
      .consumer(pact.consumer.name)
      .provider(pact.provider.name)

  if tag == :untagged
    query = query.untagged
  elsif tag
    query = query.tag(tag)
  end

  query.consumer_version_order_before(pact.consumer_version.order)
      .latest.collect(&:to_domain_with_content)[0]
end

#find_previous_pacts(pact) ⇒ Object



354
355
356
357
358
359
360
361
362
# File 'lib/pact_broker/pacts/repository.rb', line 354

def find_previous_pacts pact
  if pact.consumer_version_tag_names.any?
    pact.consumer_version_tag_names.each_with_object({}) do |tag, tags_to_pacts|
      tags_to_pacts[tag] = find_previous_pact(pact, tag)
    end
  else
    { :untagged => find_previous_pact(pact, :untagged) }
  end
end

#find_wip_pact_versions_for_provider(provider_name, provider_tags_names = [], options = {}) ⇒ Object

To find the work in progress pacts for this verification execution: For each provider tag that will be applied to this verification result (usually there will just be one, but we have to allow for multiple tags), find the head pacts (the pacts that are the latest for their tag) that have been successfully verified against the provider tag. Then, find all the head pacts, and remove the ones that have been successfully verified by ALL of the provider tags supplied, and the ones that were published before the include_wip_pacts_since date. Then, for all of the head pacts that are remaining (these are the WIP ones) work out which provider tags they are pending for. Don’t include pact publications that were created before the provider tag was first used (that is, before the provider’s git branch was created).



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/pact_broker/pacts/repository.rb', line 184

def find_wip_pact_versions_for_provider provider_name, provider_tags_names = [], options = {}
  # TODO not sure about this
  return [] if provider_tags_names.empty?

  provider = pacticipant_repository.find_by_name(provider_name)

  # Hash of provider tag name => list of head pacts that have been successfully verified by that tag
  successfully_verified_head_pacts_for_provider_tags = find_successfully_verified_head_pacts_by_provider_tag(provider.id, provider_tags_names, options)
  # Create hash of provider tag name => list of pact publication ids
  successfully_verified_head_pact_publication_ids_for_each_provider_tag = successfully_verified_head_pacts_for_provider_tags.each_with_object({}) do | (provider_tag_name, head_pacts), hash |
    hash[provider_tag_name] = head_pacts.collect(&:id).uniq
  end

  # list of head pact_publication_ids that are NOT work in progress because they've been verified by all of the provider version tags supplied
  non_wip_pact_publication_ids = successfully_verified_head_pacts_for_provider_tags.values.collect{ |head_pacts| head_pacts.collect(&:id) }.reduce(:&)

  wip_pact_publication_ids = find_head_pacts_that_have_not_been_successfully_verified_by_all_provider_tags(
    provider.id,
    non_wip_pact_publication_ids,
    options)

  wip_pacts = scope_for(PactPublication).where(id: wip_pact_publication_ids)

  # The first instance (by date) of each provider tag with that name
  provider_tag_collection = PactBroker::Domain::Tag
    .select_group(Sequel[:tags][:name], Sequel[:pacticipant_id])
    .select_append(Sequel.function(:min, Sequel[:tags][:created_at]).as(:created_at))
    .distinct
    .join(:versions, { Sequel[:tags][:version_id] => Sequel[:versions][:id] } )
    .where(pacticipant_id: provider.id)
    .where(name: provider_tags_names)
    .all

  wip_pacts.collect do | pact|

    pending_tag_names = find_provider_tags_for_which_pact_publication_id_is_pending(pact, successfully_verified_head_pact_publication_ids_for_each_provider_tag)
    pre_existing_tag_names = find_provider_tag_names_that_were_first_used_before_pact_published(pact, provider_tag_collection)

    pre_existing_pending_tags = pending_tag_names & pre_existing_tag_names

    if pre_existing_pending_tags.any?
      selectors = Selectors.create_for_latest_of_each_tag(pact.head_tag_names)
      VerifiablePact.new(pact.to_domain, selectors, true, pre_existing_pending_tags, [], true)
    end
  end.compact.sort
end

#next_revision_number(existing_model) ⇒ Object

This logic is a separate method so we can stub it to create a “conflict” scenario



82
83
84
# File 'lib/pact_broker/pacts/repository.rb', line 82

def next_revision_number(existing_model)
  existing_model.revision_number + 1
end

#scope_for(scope) ⇒ Object



29
30
31
# File 'lib/pact_broker/pacts/repository.rb', line 29

def scope_for(scope)
  PactBroker.policy_scope!(scope)
end

#search_for_latest_pact(consumer_name, provider_name, tag = nil) ⇒ Object

Allows optional consumer_name and provider_name



282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/pact_broker/pacts/repository.rb', line 282

def search_for_latest_pact(consumer_name, provider_name, tag = nil)
  query = scope_for(LatestPactPublicationsByConsumerVersion).select_all_qualified
  query = query.consumer(consumer_name) if consumer_name
  query = query.provider(provider_name) if provider_name

  if tag == :untagged
    query = query.untagged
  elsif tag
    query = query.tag(tag)
  end
  query.latest.all.collect(&:to_domain_with_content)[0]
end

#unscoped(scope) ⇒ Object

For the times when it doesn’t make sense to use the scoped class, this is a way to indicate that it is an intentional use of the PactVersion class directly.



35
36
37
# File 'lib/pact_broker/pacts/repository.rb', line 35

def unscoped(scope)
  scope
end

#update(id, params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pact_broker/pacts/repository.rb', line 57

def update id, params
  existing_model = PactPublication.find(id: id)
  pact_version = find_or_create_pact_version(
    existing_model.consumer_version.pacticipant_id,
    existing_model.provider_id,
    params.fetch(:pact_version_sha),
    params.fetch(:json_content)
  )
  if existing_model.pact_version_id != pact_version.id
    pact_publication = PactPublication.new(
      consumer_version_id: existing_model.consumer_version_id,
      provider_id: existing_model.provider_id,
      revision_number: next_revision_number(existing_model),
      consumer_id: existing_model.consumer_id,
      pact_version_id: pact_version.id,
      created_at: Sequel.datetime_class.now
    ).upsert
    update_latest_pact_publication_ids(pact_publication)
    pact_publication.to_domain
  else
    existing_model.to_domain
  end
end

#update_latest_pact_publication_ids(pact_publication) ⇒ Object



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

def update_latest_pact_publication_ids(pact_publication)
  params = {
    consumer_version_id: pact_publication.consumer_version_id,
    provider_id: pact_publication.provider_id,
    pact_publication_id: pact_publication.id,
    consumer_id: pact_publication.consumer_id,
    pact_version_id: pact_publication.pact_version_id,
    created_at: pact_publication.consumer_version.created_at
  }

  LatestPactPublicationIdForConsumerVersion.new(params).upsert
end