Class: PactBroker::Pacts::Repository

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

Overview

rubocop: disable Metrics/ClassLength

Constant Summary

Constants included from Repositories

Repositories::REPOSITORY_FACTORIES

Instance Method Summary collapse

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 Logging

included, #log_error, #log_with_tag, #measure_info

Instance Method Details

#create(params) ⇒ PactBroker::Domain::Pact



38
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 38

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,
    consumer_version_order: params.fetch(:version).order,
  ).upsert
  update_latest_pact_publication_ids(pact_publication)
  pact_publication.with_version_branches_and_tags.to_domain
end

#delete(params) ⇒ Object



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

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



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

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]
  query = query.for_branch_name(options[:branch_name]) if options[:branch_name]

  ids = query.select_for_subquery(Sequel.qualify(:pact_publications, :id).as(: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



135
136
137
138
139
# File 'lib/pact_broker/pacts/repository.rb', line 135

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



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

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



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

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

#find_all_revisions(consumer_name, consumer_version_number, provider_name) ⇒ Object

rubocop: enable Metrics/CyclomaticComplexity, Metrics/MethodLength



308
309
310
311
312
313
314
315
316
# File 'lib/pact_broker/pacts/repository.rb', line 308

def find_all_revisions consumer_name, consumer_version_number, provider_name
  scope_for(PactPublication)
    .eager_for_domain_with_content
    .for_provider_name(provider_name)
    .for_consumer_name_and_maybe_version_number(consumer_name, consumer_version_number)
    .order_by_consumer_version_order
    .all
    .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



203
204
205
206
207
208
209
210
# File 'lib/pact_broker/pacts/repository.rb', line 203

def find_by_consumer_version consumer_name, consumer_version_number
  scope_for(PactPublication)
    .eager_for_domain_with_content
    .for_consumer_name_and_maybe_version_number(consumer_name, consumer_version_number)
    .remove_overridden_revisions_from_complete_query
    .all
    .collect(&:to_domain_with_content)
end

#find_by_version_and_provider(version_id, provider_id) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/pact_broker/pacts/repository.rb', line 212

def find_by_version_and_provider version_id, provider_id
  scope_for(PactPublication)
    .eager_for_domain_with_content
    .where(consumer_version_id: version_id, provider_id: provider_id)
    .remove_overridden_revisions_from_complete_query
    .limit(1)
    .all
    .collect(&:to_domain_with_content)[0]
end

#find_for_verification(provider_name, consumer_version_selectors) ⇒ Object



185
186
187
# File 'lib/pact_broker/pacts/repository.rb', line 185

def find_for_verification(provider_name, consumer_version_selectors)
  PactsForVerificationRepository.new.find(provider_name, consumer_version_selectors)
end

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



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

def find_latest_pact(consumer_name, provider_name, tag = nil, branch_name = nil)
  query = scope_for(PactPublication)
    .eager_for_domain_with_content
    .select_all_qualified
    .for_consumer_name(consumer_name)
    .for_provider_name(provider_name)
    .remove_overridden_revisions
  if tag == :untagged
    query = query.untagged
  elsif tag
    query = query.for_consumer_version_tag_all_revisions(tag)
  elsif branch_name
    query = query.old_latest_for_consumer_branch(branch_name)
  end
  query.latest_by_consumer_version_order.all.collect(&:to_domain_with_content)[0]
end

#find_latest_pactsObject



222
223
224
225
226
227
228
229
230
# File 'lib/pact_broker/pacts/repository.rb', line 222

def find_latest_pacts
  scope_for(PactPublication)
    .eager_for_domain_with_content
    .overall_latest
    .eager(:provider)
    .all
    .collect(&:to_domain)
    .sort
end

#find_latest_pacts_for_provider(provider_name, tag = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/pact_broker/pacts/repository.rb', line 141

def find_latest_pacts_for_provider provider_name, tag = nil
  query = scope_for(PactPublication)
            .for_provider_name(provider_name)
            .eager_for_domain_with_content

  if tag
    query = query.latest_for_consumer_tag(tag)
  else
    query = query.overall_latest
  end

  query.all.sort_by{ | p| p.consumer_name.downcase }.collect(&:to_head_pact)
end

#find_next_pact(pact) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
# File 'lib/pact_broker/pacts/repository.rb', line 338

def find_next_pact pact
  scope_for(PactPublication)
    .eager_for_domain_with_content
    .for_consumer_name(pact.consumer.name)
    .for_provider_name(pact.provider.name)
    .remove_overridden_revisions
    .consumer_version_order_after(pact.consumer_version.order)
    .earliest
    .all_allowing_lazy_load
    .collect(&:to_domain_with_content)[0]
end

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

rubocop: disable Metrics/CyclomaticComplexity, Metrics/MethodLength



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/pact_broker/pacts/repository.rb', line 268

def find_pact consumer_name, consumer_version_number, provider_name, pact_version_sha = nil
  pact_publication_by_consumer_version = scope_for(PactPublication)
      .eager_for_domain_with_content
      .select_all_qualified
      .for_consumer_name_and_maybe_version_number(consumer_name, consumer_version_number)
      .for_provider_name(provider_name)
      .remove_overridden_revisions
      .limit(1)

  latest_pact_publication_by_sha = scope_for(PactPublication)
      .eager_for_domain_with_content
      .select_all_qualified
      .for_consumer_name(consumer_name)
      .for_provider_name(provider_name)
      .for_pact_version_sha(pact_version_sha)
      .reverse_order(:consumer_version_order, :revision_number)
      .limit(1)

  # Must use .all to trigger the eager loading
  if consumer_version_number && !pact_version_sha
    pact_publication_by_consumer_version
      .all.first&.to_domain_with_content
  elsif pact_version_sha && !consumer_version_number
    latest_pact_publication_by_sha
      .all.first&.to_domain_with_content
  elsif consumer_version_number && pact_version_sha
    pact_publication = pact_publication_by_consumer_version.all.first
    if pact_publication && pact_publication.pact_version.sha == pact_version_sha
      pact_publication.to_domain_with_content
    else
      latest_pact_publication_by_sha.all.first&.to_domain_with_content
    end
  else
    pact_publication_by_consumer_version
      .reverse_order(:consumer_version_order, :revision_number)
      .all.first&.to_domain_with_content
  end
end

#find_pact_version(consumer, provider, pact_version_sha) ⇒ Object



369
370
371
# File 'lib/pact_broker/pacts/repository.rb', line 369

def find_pact_version(consumer, provider, pact_version_sha)
  PactBroker::Pacts::PactVersion.where(consumer_id: consumer.id, provider_id: provider.id, sha: pact_version_sha).single_record
end

#find_pact_versions_for_provider(provider_name, tag = nil) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/pact_broker/pacts/repository.rb', line 193

def find_pact_versions_for_provider provider_name, tag = nil
  query = scope_for(PactPublication)
      .select_all_qualified
      .eager_for_domain_with_content
      .for_provider_name(provider_name)
  query = query.for_consumer_version_tag(tag) if tag
  query.all.collect(&:to_domain).sort
end

#find_pacts_by_consumer_branch(provider_name, options = {}) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/pact_broker/pacts/repository.rb', line 155

def find_pacts_by_consumer_branch(provider_name, options = {})
  consumer_name = options[:consumer]
  latest = options.fetch(:latest, false)
  branch = options[:branch_name]
  main_branch = options.fetch(:main_branch, false)
  
  query = scope_for(PactPublication)
            .eager_for_domain_with_content
            .for_provider_name(provider_name)
  
  if consumer_name
    query = query.for_consumer_name(consumer_name)
  end

  if main_branch
    if latest
      query = query.latest_for_main_branches
    else
      query = query.for_main_branches
    end
  else 
    if latest
      query = query.latest_for_consumer_branch(branch)
    else
      query = query.for_branch_name(branch)
    end
  end
  query.all.sort_by{ | p| p.consumer_name.downcase }.collect(&:to_head_pact)
end

#find_previous_distinct_pact(pact) ⇒ Object



350
351
352
353
354
355
356
357
# File 'lib/pact_broker/pacts/repository.rb', line 350

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



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

def find_previous_pact pact, tag = nil
  query = scope_for(PactPublication)
      .eager_for_domain_with_content
      .remove_overridden_revisions
      .for_consumer_name(pact.consumer.name)
      .for_provider_name(pact.provider.name)

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

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

#find_previous_pacts(pact) ⇒ Object



359
360
361
362
363
364
365
366
367
# File 'lib/pact_broker/pacts/repository.rb', line 359

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_version_branch, provider_tags_names, explicitly_specified_verifiable_pacts, options = {}) ⇒ Object



189
190
191
# File 'lib/pact_broker/pacts/repository.rb', line 189

def find_wip_pact_versions_for_provider provider_name, provider_version_branch, provider_tags_names, explicitly_specified_verifiable_pacts, options = {}
  PactsForVerificationRepository.new.find_wip(provider_name, provider_version_branch, provider_tags_names, explicitly_specified_verifiable_pacts, options)
end

#next_revision_number(existing_model) ⇒ Object

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



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

def next_revision_number(existing_model)
  existing_model.revision_number + 1
end

#scope_for(scope) ⇒ Object



27
28
29
# File 'lib/pact_broker/pacts/repository.rb', line 27

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



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/pact_broker/pacts/repository.rb', line 250

def search_for_latest_pact(consumer_name, provider_name, tag = nil)
  query = scope_for(PactPublication).select_all_qualified.eager_for_domain_with_content
  query = query.for_consumer_name(consumer_name) if consumer_name
  query = query.for_provider_name(provider_name) if provider_name

  if tag == :untagged
    query = query.untagged
  elsif tag
    query = query.for_consumer_version_tag_all_revisions(tag)
  end
  query
    .remove_overridden_revisions_from_complete_query
    .latest_by_consumer_version_order
    .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.



33
34
35
# File 'lib/pact_broker/pacts/repository.rb', line 33

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
80
# 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,
      consumer_version_order: existing_model.consumer_version_order,
      pact_version_id: pact_version.id,
      created_at: Sequel.datetime_class.now
    ).upsert
    update_latest_pact_publication_ids(pact_publication)
    pact_publication.with_version_branches_and_tags.to_domain
  else
    existing_model.with_version_branches_and_tags.to_domain
  end
end

#update_latest_pact_publication_ids(pact_publication) ⇒ Object



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

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