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



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

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



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
132
# 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]
  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



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

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



277
278
279
280
281
282
283
284
285
# File 'lib/pact_broker/pacts/repository.rb', line 277

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



172
173
174
175
176
177
178
179
# File 'lib/pact_broker/pacts/repository.rb', line 172

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



181
182
183
184
185
186
187
188
189
# File 'lib/pact_broker/pacts/repository.rb', line 181

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



154
155
156
# File 'lib/pact_broker/pacts/repository.rb', line 154

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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/pact_broker/pacts/repository.rb', line 201

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



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

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



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

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



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

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



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/pact_broker/pacts/repository.rb', line 237

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



338
339
340
# File 'lib/pact_broker/pacts/repository.rb', line 338

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



162
163
164
165
166
167
168
169
# File 'lib/pact_broker/pacts/repository.rb', line 162

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_previous_distinct_pact(pact) ⇒ Object



319
320
321
322
323
324
325
326
# File 'lib/pact_broker/pacts/repository.rb', line 319

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



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 287

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



328
329
330
331
332
333
334
335
336
# File 'lib/pact_broker/pacts/repository.rb', line 328

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



158
159
160
# File 'lib/pact_broker/pacts/repository.rb', line 158

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



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



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

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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/pact_broker/pacts/repository.rb', line 219

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.



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

def unscoped(scope)
  scope
end

#update(id, params) ⇒ Object



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

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



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